From d91513728838cb39bba2b3dadf20df2f0a7ed364 Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Fri, 2 Aug 2013 12:38:58 +0200 Subject: [PATCH] Added test code for r/edescriptors ... --- .../ch/psi/fda/aq/ng/AcquisitionEngineNG.java | 1 + .../aq/ng/AcquisitionEngineNGResource.java | 108 ++++++++++++++++-- .../java/ch/psi/fda/aq/ng/EDescriptor.java | 52 +++++++++ .../main/java/ch/psi/fda/aq/ng/LManager.java | 26 ++++- .../java/ch/psi/fda/aq/ng/RDescriptor.java | 58 ++++++++++ .../fda/aq/ng/resources/ShellResource.java | 14 +++ 6 files changed, 249 insertions(+), 10 deletions(-) create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/EDescriptor.java create mode 100644 ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/RDescriptor.java diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNG.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNG.java index cccda42..5541eaf 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNG.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNG.java @@ -110,6 +110,7 @@ public class AcquisitionEngineNG { } catch (ScriptException e) { + logger.info(e.getMessage()); throw new RuntimeException("Action failed while executing the Jython script",e); } } diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java index cbe503b..7befece 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/AcquisitionEngineNGResource.java @@ -20,14 +20,21 @@ package ch.psi.fda.aq.ng; import java.util.HashMap; import java.util.Map; +import java.util.Set; import javax.inject.Inject; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.NotFoundException; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import ch.psi.fda.aq.ng.rdescriptors.ShellDescriptor; +import ch.psi.jcae.ChannelDescriptor; /** * @author ebner @@ -42,23 +49,30 @@ public class AcquisitionEngineNGResource { @Inject private LManager lmanager; - @PUT +// @PUT +// @Path("logic") +// public void execute(String logic){ +// Map map = new HashMap<>(); +// engine.execute(map, logic); +// } + + @GET @Path("logic") - public void execute(String logic){ - Map map = new HashMap<>(); - engine.execute(map, logic); + @Produces(MediaType.APPLICATION_JSON) + public Set getLogics(){ + return lmanager.getLogics(); } @PUT @Path("logic/{id}") public void setLogic(@PathParam("id") String id, String logic){ - lmanager.getLogics().put(id, logic); + lmanager.addLogic(id, logic); } @GET @Path("logic/{id}") public String getLogic(@PathParam("id") String id){ - String logic = lmanager.getLogics().get(id); + String logic = lmanager.getLogic(id); if(logic==null){ throw new NotFoundException(); } @@ -67,7 +81,85 @@ public class AcquisitionEngineNGResource { @POST @Path("logic/{id}") - public void executeLogic(@PathParam("id") String id, String logic){ - lmanager.getLogics().put(id, logic); + @Consumes(MediaType.APPLICATION_JSON) + public void executeLogic(@PathParam("id") String id, EDescriptor edescriptor){ + + // Before executing the logic first merge the default descriptors and provided descriptors + // Where the provided descriptors take precedence over the default onces. + Map rdesc = new HashMap<>(); + rdesc.putAll(lmanager.getResources(id)); + if (edescriptor.getResources() != null) { + rdesc.putAll(edescriptor.getResources()); + } + + Map map =createResourceDescriptors(rdesc); + engine.execute(map, lmanager.getLogic(id)); + } + + @PUT + @Path("logic/{id}/resources") + public void setLogicResources(@PathParam("id") String id, Map rprofile){ + lmanager.addResources(id, rprofile); + } + + @GET + @Path("logic/{id}/resources") + public Map getLogicResources(@PathParam("id") String id){ + Map rprofile = lmanager.getResources(id); + if(rprofile==null){ + throw new NotFoundException(); + } + return rprofile; + } + + /** + * Create internal resource descriptors based on the rdescriptors passed to the method. + * + * Currently following rdescriptor types are supported: + * - channel + * - shell + * + * @param descriptor + * @return + */ + private Map createResourceDescriptors(Map resourceDescriptors) { + Map map = new HashMap<>(); + + for (String k : resourceDescriptors.keySet()) { + RDescriptor r = resourceDescriptors.get(k); + if (r.getType().equals("channel")) { + Class c; + switch (r.getParameter().get("type").toLowerCase()) { + case "double": + c = Double.class; + break; + case "integer": + c = Integer.class; + break; + case "boolean": + c = Boolean.class; + break; + case "String": + c = String.class; + break; + default: + c = String.class; + } + ; + String name = r.getParameter().get("name"); + boolean monitored = false; + if(r.getParameter().get("monitor")!=null){ + monitored = new Boolean(r.getParameter().get("monitor")); + } + + // Create channel descriptor + ChannelDescriptor cd = new ChannelDescriptor<>(c, name, monitored); + map.put(k, cd); + } + else if (r.getType().equals("shell")) { + map.put(k, new ShellDescriptor()); + } + } + return map; } } diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/EDescriptor.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/EDescriptor.java new file mode 100644 index 0000000..3ddec88 --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/EDescriptor.java @@ -0,0 +1,52 @@ +/** + * + * Copyright 2013 Paul Scherrer Institute. All rights reserved. + * + * This code is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This code is distributed in the hope that it will be useful, but without any + * warranty; without even the implied warranty of merchantability or fitness for + * a particular purpose. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + * + */ +package ch.psi.fda.aq.ng; + +import java.util.List; +import java.util.Map; + +import javax.xml.bind.annotation.XmlRootElement; + +import org.codehaus.jackson.map.annotate.JsonSerialize; +import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; + +/** + * Execution descriptor - Describes resources and parameters used to execute a logic + */ +@XmlRootElement +@JsonSerialize(include = Inclusion.NON_NULL) // Do not serialize NULL values +public class EDescriptor { + + private Map resources; + private List parameter; + + public Map getResources() { + return resources; + } + public void setResources(Map resources) { + this.resources = resources; + } + public List getParameter() { + return parameter; + } + public void setParameter(List parameter) { + this.parameter = parameter; + } + +} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java index f2dc445..f5e9b74 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/LManager.java @@ -20,6 +20,7 @@ package ch.psi.fda.aq.ng; import java.util.HashMap; import java.util.Map; +import java.util.Set; /** * Logic Manager @@ -30,7 +31,28 @@ import java.util.Map; public class LManager { private final Map logics = new HashMap<>(); - public Map getLogics(){ - return logics; + /** Default resourses assigned to a logic */ + private final Map> resources = new HashMap<>(); + + public void addLogic(String id, String logic){ + logics.put(id, logic); + resources.put(id, new HashMap()); // No default resources attached to logic } + + public void addResources(String id, Map lresources){ + resources.put(id, lresources); + } + + public String getLogic(String id){ + return logics.get(id); + } + + public Set getLogics(){ + return logics.keySet(); + } + + public Map getResources(String id){ + return resources.get(id); + } + } diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/RDescriptor.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/RDescriptor.java new file mode 100644 index 0000000..494b77d --- /dev/null +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/RDescriptor.java @@ -0,0 +1,58 @@ +/** + * + * Copyright 2013 Paul Scherrer Institute. All rights reserved. + * + * This code is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This code is distributed in the hope that it will be useful, but without any + * warranty; without even the implied warranty of merchantability or fitness for + * a particular purpose. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + * + */ +package ch.psi.fda.aq.ng; + +import java.util.Map; + +import javax.xml.bind.annotation.XmlRootElement; + +import org.codehaus.jackson.map.annotate.JsonSerialize; +import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; + +/** + * Resource descriptor - Used to identify and/or create resource on server + */ +@XmlRootElement +@JsonSerialize(include = Inclusion.NON_NULL) // Do not serialize NULL values +public class RDescriptor { + + private String ref; + private String type; + private Map parameter; + + public String getRef() { + return ref; + } + public void setRef(String ref) { + this.ref = ref; + } + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public Map getParameter() { + return parameter; + } + public void setParameter(Map parameter) { + this.parameter = parameter; + } + +} diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/resources/ShellResource.java b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/resources/ShellResource.java index 50349ac..a629b7e 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/resources/ShellResource.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/aq/ng/resources/ShellResource.java @@ -31,6 +31,20 @@ public class ShellResource { private static final Logger logger = Logger.getLogger(ShellResource.class.getName()); + /** + * Execute script/command in shell + * @param script + */ + public void execute(String script){ + execute(script, 0, true); + } + + /** + * Execute script/command in shell + * @param script + * @param returnValue + * @param checkReturnValue + */ public void execute(String script, int returnValue, boolean checkReturnValue){ ProcessBuilder pb = new ProcessBuilder(new String[]{"/bin/bash","-c",script}); pb.redirectErrorStream(true);