diff --git a/src/main/java/org/gcube/vremanagement/executor/client/plugins/ExecutorPlugin.java b/src/main/java/org/gcube/vremanagement/executor/client/plugins/ExecutorPlugin.java index 607ae00..4e7be87 100644 --- a/src/main/java/org/gcube/vremanagement/executor/client/plugins/ExecutorPlugin.java +++ b/src/main/java/org/gcube/vremanagement/executor/client/plugins/ExecutorPlugin.java @@ -25,13 +25,12 @@ import org.gcube.vremanagement.executor.client.util.Tuple; */ public class ExecutorPlugin extends AbstractPlugin { - private static final ExecutorPlugin executorPlugin = new ExecutorPlugin(); - public static ProxyBuilder getExecutorProxy( String pluginName, Tuple[] tuples, ServiceEndpointQueryFilter serviceEndpointQueryFilter, EndpointDiscoveryFilter endpointDiscoveryFilter) { + ExecutorPlugin executorPlugin = new ExecutorPlugin(); SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin); query.addConditions(pluginName, tuples); query.setServiceEndpointQueryFilter(serviceEndpointQueryFilter); @@ -41,12 +40,14 @@ public class ExecutorPlugin extends AbstractPlugin getExecutorProxy(String pluginName, Tuple ... tuples) { + ExecutorPlugin executorPlugin = new ExecutorPlugin(); SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin); query.addConditions(pluginName, tuples); return new ProxyBuilderImpl(executorPlugin, query); } public static ProxyBuilder getExecutorProxy() { + ExecutorPlugin executorPlugin = new ExecutorPlugin(); return new ProxyBuilderImpl(executorPlugin); } diff --git a/src/main/java/org/gcube/vremanagement/executor/client/plugins/query/SmartExecutorPluginQuery.java b/src/main/java/org/gcube/vremanagement/executor/client/plugins/query/SmartExecutorPluginQuery.java index a7fa711..19fd3b0 100644 --- a/src/main/java/org/gcube/vremanagement/executor/client/plugins/query/SmartExecutorPluginQuery.java +++ b/src/main/java/org/gcube/vremanagement/executor/client/plugins/query/SmartExecutorPluginQuery.java @@ -14,7 +14,6 @@ import org.gcube.common.clients.exceptions.DiscoveryException; import org.gcube.common.clients.queries.Query; import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint; -import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.icclient.ICFactory; import org.gcube.vremanagement.executor.client.plugins.query.filter.EndpointDiscoveryFilter; @@ -33,30 +32,30 @@ public class SmartExecutorPluginQuery implements Query { public static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'"; public static String containsFormat = "contains($entry/string(),'%1s')"; - private DiscoveryClient smartExecutorDiscoveryClient = ICFactory.clientFor(ServiceEndpoint.class); - private DiscoveryClient gCoreEndpointDiscoveryClient = ICFactory.client(); + private final Plugin plugin; private final SimpleQuery smartExecutorDiscoveryQuery; + private ServiceEndpointQueryFilter serviceEndpointQueryFilter; - - private final SimpleQuery gCoreEndpointDiscoveryQuery; private EndpointDiscoveryFilter endpointDiscoveryFilter = new RandomEndpointDiscoveryFilter();; - - public SmartExecutorPluginQuery(Plugin plugin){ - smartExecutorDiscoveryQuery = ICFactory.queryFor(ServiceEndpoint.class) - .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", plugin.serviceClass())) - .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", plugin.serviceName())) - .setResult("$resource"); - - gCoreEndpointDiscoveryQuery = ICFactory.queryFor(GCoreEndpoint.class) + private static SimpleQuery makeEndpointDiscoveryQuery(Plugin plugin){ + return ICFactory.queryFor(GCoreEndpoint.class) .addCondition(String.format(classFormat, plugin.serviceClass())) .addCondition(String.format(nameFormat, plugin.serviceName())) .addCondition(String.format(statusFormat)) .addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint") .addCondition(String.format(containsFormat,plugin.name())) .setResult("$entry/text()"); + } + + public SmartExecutorPluginQuery(Plugin plugin){ + this.plugin = plugin; + smartExecutorDiscoveryQuery = ICFactory.queryFor(ServiceEndpoint.class) + .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", plugin.serviceClass())) + .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", plugin.serviceName())) + .setResult("$resource"); } @@ -87,29 +86,50 @@ public class SmartExecutorPluginQuery implements Query { } } + public List discoverEndpoints(EndpointDiscoveryFilter endpointDiscoveryFilter) throws DiscoveryException { + if(serviceEndpointQueryFilter!=null){ + serviceEndpointQueryFilter.filter(smartExecutorDiscoveryQuery); + } + + List serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class).submit(smartExecutorDiscoveryQuery); + + if(serviceEndpoints.size() == 0){ + throw new DiscoveryException("No running SmartExecutor wich match the requested conditions"); + } + + SimpleQuery preRunQuery = makeEndpointDiscoveryQuery(plugin); + endpointDiscoveryFilter.filter(preRunQuery, serviceEndpoints); + + return ICFactory.client().submit(preRunQuery); + + } + + @Override public List fire() throws DiscoveryException { if(serviceEndpointQueryFilter!=null){ serviceEndpointQueryFilter.filter(smartExecutorDiscoveryQuery); } - List serviceEndpoints = smartExecutorDiscoveryClient.submit(smartExecutorDiscoveryQuery); + List serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class).submit(smartExecutorDiscoveryQuery); if(serviceEndpoints.size() == 0){ throw new DiscoveryException("No running SmartExecutor wich match the requested conditions"); } + + SimpleQuery gCoreEndpointDiscoveryQuery = makeEndpointDiscoveryQuery(plugin); endpointDiscoveryFilter.filter(gCoreEndpointDiscoveryQuery, serviceEndpoints); List refs = new ArrayList(); try { - List addresses = gCoreEndpointDiscoveryClient.submit(gCoreEndpointDiscoveryQuery); + List addresses = ICFactory.client().submit(gCoreEndpointDiscoveryQuery); for(String address : addresses) refs.add(new W3CEndpointReferenceBuilder().address(address).build()); - } - catch(org.gcube.resources.discovery.client.api.DiscoveryException ex) { + } catch(org.gcube.resources.discovery.client.api.DiscoveryException ex) { throw new DiscoveryException(ex); } + return refs; } diff --git a/src/main/java/org/gcube/vremanagement/executor/client/plugins/query/filter/ListEndpointDiscoveryFilter.java b/src/main/java/org/gcube/vremanagement/executor/client/plugins/query/filter/ListEndpointDiscoveryFilter.java new file mode 100644 index 0000000..bcb44e4 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/executor/client/plugins/query/filter/ListEndpointDiscoveryFilter.java @@ -0,0 +1,36 @@ +/** + * + */ +package org.gcube.vremanagement.executor.client.plugins.query.filter; + +import java.util.List; + +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery; + +/** + * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ + * + */ +public class ListEndpointDiscoveryFilter implements EndpointDiscoveryFilter { + + @Override + public void filter(SimpleQuery simpleQuery, List serviceEndpoints) { + + String expression = ""; + int size = serviceEndpoints.size(); + for(int i=0; i serviceEndpoints) { + simpleQuery.addCondition(format(SmartExecutorPluginQuery.containsFormat, endpointURI)); + } + +} diff --git a/src/test/java/org/gcube/vremanagement/executor/client/QueriedClientTest.java b/src/test/java/org/gcube/vremanagement/executor/client/QueriedClientTest.java index c466ffb..d08f64e 100644 --- a/src/test/java/org/gcube/vremanagement/executor/client/QueriedClientTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/client/QueriedClientTest.java @@ -4,6 +4,7 @@ package org.gcube.vremanagement.executor.client; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.acme.HelloWorldPluginDeclaration; @@ -14,7 +15,9 @@ import org.gcube.vremanagement.executor.api.SmartExecutor; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin; import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery; +import org.gcube.vremanagement.executor.client.plugins.query.filter.ListEndpointDiscoveryFilter; import org.gcube.vremanagement.executor.client.plugins.query.filter.RandomEndpointDiscoveryFilter; +import org.gcube.vremanagement.executor.client.plugins.query.filter.SpecificEndpointDiscoveryFilter; import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy; import org.gcube.vremanagement.executor.client.util.Tuple; import org.junit.Assert; @@ -145,7 +148,7 @@ public class QueriedClientTest { HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration(); Map map = helloWorldPluginDeclaration.getSupportedCapabilities(); @SuppressWarnings("unchecked") - Tuple[] tuples = new Tuple[map.size()+1]; + Tuple[] tuples = new Tuple[map.size()]; int i = 0; for(String key : map.keySet()){ tuples[i] = new Tuple(key, map.get(key)); @@ -162,4 +165,72 @@ public class QueriedClientTest { } } + @SuppressWarnings("unchecked") + @Test + public void testWithListfilters() { + ScopeProvider.instance.set("/gcube"); + + ExecutorPlugin executorPlugin = new ExecutorPlugin(); + SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin); + HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration(); + Map map = helloWorldPluginDeclaration.getSupportedCapabilities(); + Tuple tuple = new Tuple(); + for(String key : map.keySet()){ + tuple = new Tuple(key, map.get(key)); + break; // Get only the first + } + + query.addConditions(HelloWorldPluginDeclaration.NAME, tuple); + query.setServiceEndpointQueryFilter(null); + query.setEndpointDiscoveryFilter(new ListEndpointDiscoveryFilter()); + + SmartExecutorProxy proxy = new ProxyBuilderImpl(executorPlugin, query).build(); + + try { + lauchTest(proxy); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + + @SuppressWarnings("unchecked") + @Test + public void testWithSpecificSelection() { + ScopeProvider.instance.set("/gcube"); + + ExecutorPlugin executorPlugin = new ExecutorPlugin(); + SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin); + HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration(); + Map map = helloWorldPluginDeclaration.getSupportedCapabilities(); + Tuple tuple = new Tuple(); + for(String key : map.keySet()){ + tuple = new Tuple(key, map.get(key)); + break; // Get only the first + } + + query.addConditions(HelloWorldPluginDeclaration.NAME, tuple); + query.setServiceEndpointQueryFilter(null); + List endpoints = query.discoverEndpoints(new ListEndpointDiscoveryFilter()); + + for(String endpoint : endpoints){ + + ExecutorPlugin runExecutorPlugin = new ExecutorPlugin(); + SmartExecutorPluginQuery runQuery = new SmartExecutorPluginQuery(runExecutorPlugin); + runQuery.addConditions(HelloWorldPluginDeclaration.NAME, tuple); + + SpecificEndpointDiscoveryFilter sedf = new SpecificEndpointDiscoveryFilter(endpoint); + runQuery.setEndpointDiscoveryFilter(sedf); + SmartExecutorProxy proxy = new ProxyBuilderImpl(runExecutorPlugin, runQuery).build(); + + try { + lauchTest(proxy); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }