Added test code for r/edescriptors ...

This commit is contained in:
2013-08-02 12:38:58 +02:00
parent ef1b318017
commit d915137288
6 changed files with 249 additions and 10 deletions

View File

@@ -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);
}
}

View File

@@ -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<String, ?> map = new HashMap<>();
// engine.execute(map, logic);
// }
@GET
@Path("logic")
public void execute(String logic){
Map<String, ?> map = new HashMap<>();
engine.execute(map, logic);
@Produces(MediaType.APPLICATION_JSON)
public Set<String> 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<String, RDescriptor> rdesc = new HashMap<>();
rdesc.putAll(lmanager.getResources(id));
if (edescriptor.getResources() != null) {
rdesc.putAll(edescriptor.getResources());
}
Map<String, ?> map =createResourceDescriptors(rdesc);
engine.execute(map, lmanager.getLogic(id));
}
@PUT
@Path("logic/{id}/resources")
public void setLogicResources(@PathParam("id") String id, Map<String, RDescriptor> rprofile){
lmanager.addResources(id, rprofile);
}
@GET
@Path("logic/{id}/resources")
public Map<String, RDescriptor> getLogicResources(@PathParam("id") String id){
Map<String, RDescriptor> 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<String, ?> createResourceDescriptors(Map<String, RDescriptor> resourceDescriptors) {
Map<String, Object> 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;
}
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<String, RDescriptor> resources;
private List<String> parameter;
public Map<String, RDescriptor> getResources() {
return resources;
}
public void setResources(Map<String, RDescriptor> resources) {
this.resources = resources;
}
public List<String> getParameter() {
return parameter;
}
public void setParameter(List<String> parameter) {
this.parameter = parameter;
}
}

View File

@@ -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<String, String> logics = new HashMap<>();
public Map<String,String> getLogics(){
return logics;
/** Default resourses assigned to a logic */
private final Map<String, Map<String, RDescriptor>> resources = new HashMap<>();
public void addLogic(String id, String logic){
logics.put(id, logic);
resources.put(id, new HashMap<String,RDescriptor>()); // No default resources attached to logic
}
public void addResources(String id, Map<String, RDescriptor> lresources){
resources.put(id, lresources);
}
public String getLogic(String id){
return logics.get(id);
}
public Set<String> getLogics(){
return logics.keySet();
}
public Map<String, RDescriptor> getResources(String id){
return resources.get(id);
}
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<String,String> 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<String, String> getParameter() {
return parameter;
}
public void setParameter(Map<String, String> parameter) {
this.parameter = parameter;
}
}

View File

@@ -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);