Switching pubblication to the new IS

This commit is contained in:
Luca Frosini 2020-09-29 11:10:35 +02:00
parent 68a2904371
commit 376cd8e5ac
6 changed files with 458 additions and 16 deletions

24
pom.xml
View File

@ -70,17 +70,41 @@
<version>${orientdb.version}</version>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>information-system-model</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.resource-management</groupId>
<artifactId>gcube-model</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-publisher</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.vremanagement</groupId>
<artifactId>smart-executor-api</artifactId>
<version>[3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.vremanagement</groupId>
<artifactId>smart-executor-client</artifactId>
<version>[3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>javax.ws.rs</groupId>

View File

@ -0,0 +1,82 @@
package org.gcube.vremanagement.executor;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.ClientType;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ContextUtility {
private static Logger logger = LoggerFactory.getLogger(ContextUtility.class);
public static String getCurrentScope() {
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry;
try {
authorizationEntry = Constants.authorizationService().get(token);
} catch(Exception e) {
logger.trace("Context was not retrieved from token. Going to get it from {}",
ScopeProvider.class.getSimpleName());
return ScopeProvider.instance.get();
}
String context = authorizationEntry.getContext();
logger.trace("Context retrieved from token is {}. Context in {} is {}", context,
ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get());
return context;
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
logger.info("Context of token {} is {}", token, context);
return context;
}
public static void setContext(String token) throws ObjectNotFound, Exception {
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(getCurrentScope(token));
}
public static ClientInfo getClientInfo() {
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry;
try {
authorizationEntry = Constants.authorizationService().get(token);
} catch(Exception e) {
return new ClientInfo() {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = 8311873203596762883L;
@Override
public ClientType getType() {
return ClientType.USER;
}
@Override
public List<String> getRoles() {
return new ArrayList<>();
}
@Override
public String getId() {
return "UNKNOWN";
}
};
}
return authorizationEntry.getClientInfo();
}
}

View File

@ -0,0 +1,243 @@
package org.gcube.vremanagement.executor;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Profile;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
import org.gcube.common.resources.gcore.common.Platform;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.informationsystem.publisher.RegistryPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
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.smartgears.configuration.application.ApplicationConfiguration;
import org.gcube.smartgears.configuration.container.ContainerConfiguration;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GCoreISPublisher extends ISPublisher {
private static Logger logger = LoggerFactory.getLogger(GCoreISPublisher.class);
public GCoreISPublisher(ApplicationContext applicationContext) {
super(applicationContext);
}
/**
* Publish the provided resource on all Service Scopes retrieved from
* Context
* @param resource to be published
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
private void publishResource(Resource resource) throws Exception {
StringWriter stringWriter = new StringWriter();
Resources.marshal(resource, stringWriter);
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
try {
logger.debug("Trying to publish to {}:\n{}", ContextUtility.getCurrentScope(), stringWriter);
registryPublisher.create(resource);
} catch(Exception e) {
logger.error("The resource was not published", e);
throw e;
}
}
/**
* Remove the resource from IS
* @param resource to be unpublished
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
private void unPublishResource(Resource resource) throws Exception {
//StringWriter stringWriter = new StringWriter();
//Resources.marshal(resource, stringWriter);
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
String id = resource.id();
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id,
ContextUtility.getCurrentScope());
registryPublisher.remove(resource);
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
}
/**
* Return the parsed version string as array of short.
* @param version the version as String
* @param wantedLenght if the length is equals to dot (.) separated
* number in the string. Otherwise the version is padded or truncated to
* the required version
* @return the parsed version as array of short. If on slicing some of the
* version cannot be parsed as short 1 is used for the first number, 0 is
* used instead or for padding
*/
private short[] getVersionSlice(String version, int wantedLenght) {
logger.trace("Trying to parse {}", version);
short[] versionSlices = new short[wantedLenght];
for(int j = 0; j < wantedLenght; j++) {
versionSlices[j] = (short) (j == 0 ? 1 : 0);
}
try {
String[] stringSlices = version.split("[.-]");
for(int i = 0; i < stringSlices.length; i++) {
logger.trace("Parsing version slice n. {} wich is '{}'", i, stringSlices[i]);
if(i >= wantedLenght) {
break;
}
try {
short n = Short.parseShort(stringSlices[i]);
versionSlices[i] = n;
logger.trace("Version slice n. {} wich is '{}' parsed as short {}", i, stringSlices[i], n);
} catch(NumberFormatException nfe) {
logger.trace("Version slice n. {} wich is '{}' failed to parse. The default value {} will be used",
i, stringSlices[i], versionSlices[i]);
}
}
} catch(Exception e) {
logger.trace("Error parsing the supplied version the default will be used", versionSlices);
}
logger.trace("Version {} parsed as {}", version, versionSlices);
return versionSlices;
}
private static String getRunningOn(ContainerConfiguration containerConfiguration) {
return String.format("%s:%s", containerConfiguration.hostname(), containerConfiguration.port());
}
/**
* Create the Service Endpoint using information related to discovered
* available plugins and their own discovered capabilities
* @return the created {@link ServiceEndpoint}
*/
protected ServiceEndpoint createServiceEndpoint(Map<String,PluginDeclaration> availablePlugins) {
logger.debug(
"Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities");
ApplicationConfiguration applicationConfiguration = applicationContext.configuration();
ServiceEndpoint serviceEndpoint = new ServiceEndpoint();
Profile profile = serviceEndpoint.newProfile();
profile.category(applicationConfiguration.serviceClass());
profile.name(applicationConfiguration.name());
String version = applicationConfiguration.version();
profile.version(version);
profile.description(applicationConfiguration.description());
String runningOn = getRunningOn(applicationContext.container().configuration());
Platform platform = profile.newPlatform();
platform.name(runningOn);
short[] versionSlices = getVersionSlice(version, 4);
platform.version(versionSlices[0]);
platform.minorVersion(versionSlices[1]);
platform.buildVersion(versionSlices[2]);
platform.revisionVersion(versionSlices[3]);
Runtime runtime = profile.newRuntime();
runtime.hostedOn(runningOn);
runtime.status(applicationConfiguration.mode().toString());
Group<AccessPoint> accessPoints = profile.accessPoints();
for(String pluginName : availablePlugins.keySet()) {
AccessPoint accessPointElement = new AccessPoint();
accessPointElement.name(pluginName);
PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName);
accessPointElement.description(pluginDeclaration.getDescription());
Group<Property> properties = accessPointElement.properties();
Property propertyVersionElement = new Property();
propertyVersionElement.nameAndValue("Version", pluginDeclaration.getVersion());
properties.add(propertyVersionElement);
Map<String,String> pluginCapabilities = pluginDeclaration.getSupportedCapabilities();
for(String capabilityName : pluginCapabilities.keySet()) {
Property propertyElement = new Property();
propertyElement.nameAndValue(capabilityName, pluginCapabilities.get(capabilityName));
properties.add(propertyElement);
}
accessPoints.add(accessPointElement);
}
StringWriter stringWriter = new StringWriter();
Resources.marshal(serviceEndpoint, stringWriter);
logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString());
return serviceEndpoint;
}
protected void cleanServiceEndpoints() {
try {
ApplicationConfiguration applicationConfiguration = applicationContext.configuration();
ContainerContext containerContext = applicationContext.container();
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'",
applicationConfiguration.serviceClass()))
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'",
applicationConfiguration.name()))
.addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'",
getRunningOn(containerContext.configuration())))
.setResult("$resource");
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
for(ServiceEndpoint serviceEndpoint : serviceEndpoints) {
try {
logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), ContextUtility.getCurrentScope());
unPublishResource(serviceEndpoint);
} catch(Exception e) {
logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), ContextUtility.getCurrentScope(), e);
}
}
} catch(Exception e) {
logger.debug("An Exception occur while checking and/or unpublishing old ServiceEndpoint", e);
}
}
@Override
public void publishPlugins(Map<String,PluginDeclaration> availablePlugins) {
ServiceEndpoint serviceEndpoint = createServiceEndpoint(availablePlugins);
cleanServiceEndpoints();
try {
publishResource(serviceEndpoint);
} catch(Exception e) {
logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted",
ContextUtility.getCurrentScope(), e);
throw new RuntimeException(e);
}
}
@Override
public void unpublishPlugins() {
cleanServiceEndpoints();
}
}

View File

@ -0,0 +1,20 @@
package org.gcube.vremanagement.executor;
import java.util.Map;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
public abstract class ISPublisher {
protected ApplicationContext applicationContext;
public ISPublisher(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public abstract void publishPlugins(Map<String, PluginDeclaration> availablePlugins) throws Exception;
public abstract void unpublishPlugins();
}

View File

@ -0,0 +1,88 @@
package org.gcube.vremanagement.executor;
import java.util.Map;
import java.util.UUID;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.entities.Resource;
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.RemoveConstraint;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.RunningPluginImpl;
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.UsesImpl;
import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet;
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin;
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Uses;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RestISPublisher extends ISPublisher {
private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class);
protected final UUID eServiceUUID;
public RestISPublisher(ApplicationContext applicationContext) {
super(applicationContext);
this.eServiceUUID = UUID.fromString(applicationContext.id());
}
@Override
public void publishPlugins(Map<String,PluginDeclaration> availablePlugins) throws Exception {
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
for(String pluginName : availablePlugins.keySet()) {
PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName);
RunningPlugin runningPlugin = new RunningPluginImpl();
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
softwareFacet.setGroup("SmartExecutorPlugin");
softwareFacet.setName(pluginName);
softwareFacet.setVersion(pluginDeclaration.getVersion());
softwareFacet.setDescription(pluginDeclaration.getDescription());
runningPlugin.addFacet(softwareFacet);
Map<String,String> pluginCapabilities = pluginDeclaration.getSupportedCapabilities();
for(String capabilityName : pluginCapabilities.keySet()) {
SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl();
simplePropertyFacet.setName(capabilityName);
simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName));
runningPlugin.addFacet(simplePropertyFacet);
}
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setAddConstraint(AddConstraint.propagate);
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
Uses<EService, EService> uses = new UsesImpl<EService, EService>(smartExecutorEService, runningPlugin, propagationConstraint);
try {
resourceRegistryPublisher.createIsRelatedTo(uses);
} 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);
throw e;
}
}
}
@Override
public void unpublishPlugins() {
logger.info("The Plugin will be removed when the Eservice will be removed thanks to propagation contraints. Nothing to do");
}
}

View File

@ -7,7 +7,6 @@ import java.util.ServiceLoader;
import org.gcube.vremanagement.executor.exception.InputsNullException;
import org.gcube.vremanagement.executor.exception.PluginNotFoundException;
import org.gcube.vremanagement.executor.persistence.Persistence;
import org.gcube.vremanagement.executor.plugin.Plugin;
import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
import org.slf4j.Logger;
@ -19,7 +18,6 @@ import org.slf4j.LoggerFactory;
* The plugin implementation class can be retrieved using its name.
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("deprecation")
public class PluginManager {
/**
@ -83,20 +81,7 @@ public class PluginManager {
try {
executorPluginConstructor = plugin.getDeclaredConstructor(argTypes);
} catch (Exception e) {
/* Maintaining backward compatibility */
argTypes = new Class[2];
argTypes[0] = pluginDeclaration.getClass();
argTypes[1] = Persistence.class;
try {
executorPluginConstructor = plugin.getDeclaredConstructor(argTypes);
arguments = new Object[2];
arguments[0] = pluginDeclaration;
arguments[1] = null;
} catch (Exception e1) {
throw new PluginNotFoundException();
}
throw new PluginNotFoundException();
}
// Instancing the plugin