Implementing new IS publisher

This commit is contained in:
Luca Frosini 2020-09-30 16:18:46 +02:00
parent 90512acf7b
commit 36a4e6684d
3 changed files with 88 additions and 45 deletions

View File

@ -33,6 +33,9 @@ public class SmartExecutorInitializator implements ApplicationManager {
public static final long JOIN_TIMEOUT = 1000; public static final long JOIN_TIMEOUT = 1000;
protected static GCoreISPublisher gCoreISPublisher;
protected static RestISPublisher restISPublisher;
/** /**
* {@inheritDoc} * {@inheritDoc}
* The method discover the plugins available on classpath and their own * The method discover the plugins available on classpath and their own

View File

@ -3,11 +3,14 @@ package org.gcube.vremanagement.executor.ispublisher;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
@ -37,63 +40,88 @@ public class RestISPublisher extends ISPublisher {
private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class); private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class);
protected final UUID eServiceUUID; protected final UUID eServiceUUID;
protected ResourceRegistryClient resourceRegistryClient;
protected ResourceRegistryPublisher resourceRegistryPublisher;
public RestISPublisher(ApplicationContext applicationContext) { public RestISPublisher(ApplicationContext applicationContext) {
super(applicationContext); super(applicationContext);
this.eServiceUUID = UUID.fromString(applicationContext.id()); this.eServiceUUID = UUID.fromString(applicationContext.id());
this.resourceRegistryClient = ResourceRegistryClientFactory.create();
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
} }
@Override @SuppressWarnings("unused")
public void publishPlugins(Map<String, Class<? extends Plugin>> availablePlugins) throws Exception { protected RunningPlugin publishRunningPluginWithRelations(String pluginName) throws Exception {
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); Plugin plugin = PluginManager.getPlugin(pluginName);
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
for(String pluginName : availablePlugins.keySet()) { RunningPlugin runningPlugin = new RunningPluginImpl();
Plugin plugin = PluginManager.getPlugin(pluginName);
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
RunningPlugin runningPlugin = new RunningPluginImpl(); softwareFacet.setGroup(plugin.getGroup());
softwareFacet.setName(pluginName);
SoftwareFacet softwareFacet = new SoftwareFacetImpl(); softwareFacet.setVersion(plugin.getVersion());
softwareFacet.setGroup(plugin.getGroup()); softwareFacet.setDescription(plugin.getDescription());
softwareFacet.setName(pluginName); runningPlugin.addFacet(softwareFacet);
softwareFacet.setVersion(plugin.getVersion());
softwareFacet.setDescription(plugin.getDescription()); Map<String,String> pluginCapabilities = plugin.getSupportedCapabilities();
runningPlugin.addFacet(softwareFacet); for(String capabilityName : pluginCapabilities.keySet()) {
SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl();
Map<String,String> pluginCapabilities = plugin.getSupportedCapabilities(); simplePropertyFacet.setName(capabilityName);
for(String capabilityName : pluginCapabilities.keySet()) { simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName));
SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); runningPlugin.addFacet(simplePropertyFacet);
simplePropertyFacet.setName(capabilityName); }
simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName));
runningPlugin.addFacet(simplePropertyFacet); EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl();
usesPropagationConstraint.setAddConstraint(AddConstraint.propagate);
usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl(); Uses<EService, EService> uses = new UsesImpl<EService, EService>(smartExecutorEService, runningPlugin, usesPropagationConstraint);
usesPropagationConstraint.setAddConstraint(AddConstraint.propagate); try {
usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); resourceRegistryPublisher.createIsRelatedTo(uses);
} catch (ResourceRegistryException e) {
Uses<EService, EService> uses = new UsesImpl<EService, EService>(smartExecutorEService, runningPlugin, usesPropagationConstraint); logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName);
throw e;
}
org.gcube.resourcemanagement.model.reference.entities.resources.Plugin pluginResource = null;
if(pluginResource!=null) { // The if allows not commenting the following code in the meanwhile the pluginResource retrieving is properly coded
PropagationConstraint enablesPropagationConstraint = new PropagationConstraintImpl();
enablesPropagationConstraint.setAddConstraint(AddConstraint.propagate);
enablesPropagationConstraint.setRemoveConstraint(RemoveConstraint.keep);
Enables<Service, Software> enables = new EnablesImpl<Service, Software>(runningPlugin, pluginResource, enablesPropagationConstraint);
try { try {
resourceRegistryPublisher.createIsRelatedTo(uses); resourceRegistryPublisher.createIsRelatedTo(enables);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName);
throw e; throw e;
} }
}
return runningPlugin;
}
@Override
public void publishPlugins(Map<String, Class<? extends Plugin>> availablePlugins) throws Exception {
UUID contextUUID = resourceRegistryClient.getCurrentContext().getHeader().getUUID();
for(String pluginName : availablePlugins.keySet()) {
org.gcube.resourcemanagement.model.reference.entities.resources.Plugin pluginResource = null; UUID uuid = PluginManager.getPluginUUID(pluginName);
if(pluginResource!=null) { // The if allows not commenting the following code in the meanwhile the pluginResource retrieving is properly coded
PropagationConstraint enablesPropagationConstraint = new PropagationConstraintImpl(); RunningPlugin runningPlugin;
enablesPropagationConstraint.setAddConstraint(AddConstraint.propagate);
enablesPropagationConstraint.setRemoveConstraint(RemoveConstraint.keep); try {
Enables<Service, Software> enables = new EnablesImpl<Service, Software>(runningPlugin, pluginResource, enablesPropagationConstraint); runningPlugin = resourceRegistryClient.getInstance(RunningPlugin.class, uuid);
try { } catch (NotFoundException e) {
resourceRegistryPublisher.createIsRelatedTo(enables); runningPlugin = publishRunningPluginWithRelations(pluginName);
} catch (ResourceRegistryException e) { } catch (AvailableInAnotherContextException e) {
logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); runningPlugin = new RunningPluginImpl();
throw e; runningPlugin.setHeader(new HeaderImpl(uuid));
} resourceRegistryPublisher.addToContext(contextUUID, runningPlugin);
} catch (ResourceRegistryException e) {
throw e;
} }
} }

View File

@ -3,6 +3,7 @@ package org.gcube.vremanagement.executor.pluginmanager;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.UUID;
import org.gcube.vremanagement.executor.exception.ExecutorException; import org.gcube.vremanagement.executor.exception.ExecutorException;
import org.gcube.vremanagement.executor.exception.PluginNotFoundException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException;
@ -34,6 +35,8 @@ public class PluginManager {
*/ */
private Map<String, Class<? extends Plugin>> availablePlugins; private Map<String, Class<? extends Plugin>> availablePlugins;
private Map<String, UUID> uuids;
/** /**
* Get the singleton instance of {@link #PluginManager}. * Get the singleton instance of {@link #PluginManager}.
* The first time this function is invoked the instance is null * The first time this function is invoked the instance is null
@ -53,13 +56,15 @@ public class PluginManager {
*/ */
protected PluginManager(){ protected PluginManager(){
logger.debug("Loading plugins available on classpath"); logger.debug("Loading plugins available on classpath");
this.availablePlugins = new HashMap<String, Class<? extends Plugin>>(); this.availablePlugins = new HashMap<>();
this.uuids = new HashMap<>();
ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class); ServiceLoader<Plugin> serviceLoader = ServiceLoader.load(Plugin.class);
for (Plugin plugin : serviceLoader) { for (Plugin plugin : serviceLoader) {
try { try {
logger.debug(String.format("%s plugin found", plugin.getName())); logger.debug(String.format("%s plugin found", plugin.getName()));
String name = plugin.getName(); String name = plugin.getName();
this.availablePlugins.put(name, plugin.getClass()); this.availablePlugins.put(name, plugin.getClass());
this.uuids.put(name, UUID.randomUUID());
} catch (Exception e) { } catch (Exception e) {
logger.debug(String.format("%s not initialized correctly. It will not be used", plugin.getName())); logger.debug(String.format("%s not initialized correctly. It will not be used", plugin.getName()));
} }
@ -84,10 +89,17 @@ public class PluginManager {
} }
} }
public static UUID getPluginUUID(String pluginName) {
return PluginManager.getInstance().uuids.get(pluginName);
}
/** /**
* @return the availablePlugins * @return the availablePlugins
*/ */
public static Map<String, Class<? extends Plugin>> getAvailablePlugins() { public static Map<String, Class<? extends Plugin>> getAvailablePlugins() {
return PluginManager.getInstance().availablePlugins; return PluginManager.getInstance().availablePlugins;
} }
} }