diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/Constants.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/Constants.java index d55ce98..b0d9109 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/Constants.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/Constants.java @@ -1,8 +1,5 @@ package org.gcube.smartgears.handler.resourceregistry; -import org.gcube.smartgears.handler.resourceregistry.resourcemanager.EServiceManager; -import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNodeManager; - /** * Library-wide constants. * @author Luca Frosini @@ -10,12 +7,12 @@ import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNode */ public class Constants { - public static final String HOSTING_NODE_MANAGER_PROPERTY = HostingNodeManager.class.getSimpleName(); - - /** - * The name of the context property that contains the EService Resource. - */ - public static final String ESERVICE_MANAGER_PROPERTY = EServiceManager.class.getSimpleName(); +// public static final String HOSTING_NODE_MANAGER_PROPERT = HostingNodeManager.class.getSimpleName(); +// +// /** +// * The name of the context property that contains the EService Resource. +// */ +// public static final String ESERVICE_MANAGER_PROPERTY = EServiceManager.class.getSimpleName(); /** * The configuration name of {@link EServiceHandler} and {@link HostingNodeHandler}. diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java index 598ca9c..500ea0d 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java @@ -48,6 +48,13 @@ public class ContextUtility { return getContextName(token); } + public static UUID getContextUUID(String token) throws ResourceRegistryException { + ContextCache contextCache = ContextCache.getInstance(); + String contextFullName = getContextName(token); + UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); + return contextUUID; + } + public static String getContextName(String token) { try { return authorizationProxy.get(token).getContext(); diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java index 5e31f34..78d509a 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java @@ -23,7 +23,6 @@ import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet; import org.gcube.resourcemanagement.model.reference.entities.resources.EService; -import org.gcube.smartgears.context.Property; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.handler.resourceregistry.resourcemanager.EServiceManager; import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent.Start; @@ -100,27 +99,31 @@ public class EServiceHandler extends ApplicationLifecycleHandler { try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); boolean create = true; + Set startTokens = applicationContext.configuration().startTokens(); Set startContextsUUID = new HashSet<>(); ContextCache contextCache = ContextCache.getInstance(); for (String token : startTokens) { - try { - if (create) { - ContextUtility.setContextFromToken(token); - eServiceManager = new EServiceManager(applicationContext); - eServiceManager.createEService(); - applicationContext.properties() - .add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager)); - create = false; - } else { - eServiceManager.addToContext(); + + if (create) { + ContextUtility.setContextFromToken(token); + eServiceManager = new EServiceManager(applicationContext); + eServiceManager.createEService(); +// applicationContext.properties() +// .add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager)); + create = false; + } else { + try { + UUID contextUUID = ContextUtility.getContextUUID(token); + eServiceManager.addToContext(contextUUID); + } catch (Exception e) { + UUID uuid = UUID.fromString(applicationContext.id()); + logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, + ContextUtility.getContextName(token), e); } - } catch (Exception e) { - UUID uuid = UUID.fromString(applicationContext.id()); - logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, - ContextUtility.getContextName(token), e); } + String contextFullName = ContextUtility.getContextName(token); UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); startContextsUUID.add(contextUUID); @@ -171,7 +174,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler { try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); ContextUtility.setContextFromToken(token); - eServiceManager.addToContext(); + eServiceManager.addToCurrentContext(); } catch (Exception e) { logger.error("Failed to add {} to current context ({})", EService.NAME, ContextUtility.getCurrentContextName(), e); diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java index 9300040..3bc19ec 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java @@ -31,7 +31,6 @@ import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet; import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode; -import org.gcube.smartgears.context.Property; import org.gcube.smartgears.context.container.ContainerContext; import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNodeManager; import org.gcube.smartgears.handlers.container.ContainerHandler; @@ -106,24 +105,24 @@ public class HostingNodeHandler extends ContainerHandler { Set startContextsUUID = new HashSet<>(); ContextCache contextCache = ContextCache.getInstance(); for (String token : startTokens) { - try { - if (create) { - ContextUtility.setContextFromToken(token); - hostingNodeManager = new HostingNodeManager(containerContext); - hostingNodeManager.createHostingNode(); - containerContext.properties() - .add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager)); - create = false; - - - } else { - hostingNodeManager.addToContext(); + if (create) { + ContextUtility.setContextFromToken(token); + hostingNodeManager = new HostingNodeManager(containerContext); + hostingNodeManager.createHostingNode(); +// containerContext.properties() +// .add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager)); + create = false; + } else { + try { + UUID contextUUID = ContextUtility.getContextUUID(token); + hostingNodeManager.addToContext(contextUUID); + } catch (Exception e) { + UUID uuid = UUID.fromString(containerContext.id()); + logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, + ContextUtility.getContextName(token), e); } - } catch (Exception e) { - UUID uuid = UUID.fromString(containerContext.id()); - logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, - ContextUtility.getContextName(token), e); } + String contextFullName = ContextUtility.getContextName(token); UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); startContextsUUID.add(contextUUID); @@ -182,7 +181,7 @@ public class HostingNodeHandler extends ContainerHandler { } try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); - hostingNodeManager.addToContext(); + hostingNodeManager.addToCurrentContext(); } catch (Exception e) { logger.error("Failed to update Service State", e); } finally { diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java index e8de3bf..607dc19 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java @@ -87,7 +87,7 @@ public class EServiceManager { } } - public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { UUID uuid = UUID.fromString(applicationContext.container().id()); try { resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); @@ -96,6 +96,16 @@ public class EServiceManager { logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); } } + + public void addToContext(UUID contextUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + UUID uuid = UUID.fromString(applicationContext.container().id()); + try { + resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false); + logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName()); + } catch (Exception e) { + logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); + } + } public void removeFromContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { @@ -216,20 +226,20 @@ public class EServiceManager { createActivatesRelation(eService); stateFacet = eService.getFacets(StateFacet.class).get(0); } catch (AvailableInAnotherContextException e) { - addToContext(); - try { - eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); - } catch (AvailableInAnotherContextException ex) { - addEServiceToCurrentContext(); - eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); - // addToContext() is executed on HostingNode. - // If the EService is still not available we need to create activates - // relation because does not exists otherwise the EService should - // already be in the context due to propagation constraint. - createActivatesRelation(eService); - } - stateFacet = eService.getFacets(StateFacet.class).get(0); - updateServiceStateFacet(); +// addToContext(); +// try { +// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); +// } catch (AvailableInAnotherContextException ex) { +// addEServiceToCurrentContext(); +// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); +// // addToContext() is executed on HostingNode. +// // If the EService is still not available we need to create activates +// // relation because does not exists otherwise the EService should +// // already be in the context due to propagation constraint. +// createActivatesRelation(eService); +// } +// stateFacet = eService.getFacets(StateFacet.class).get(0); +// updateServiceStateFacet(); } catch (ResourceRegistryException e) { throw e; } diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java index a19f237..890004d 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java @@ -110,7 +110,7 @@ public class HostingNodeManager { return hostingNode; } - public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { UUID uuid = UUID.fromString(containerContext.id()); try { resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); @@ -119,6 +119,16 @@ public class HostingNodeManager { logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); } } + + public void addToContext(UUID contextUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + UUID uuid = UUID.fromString(containerContext.id()); + try { + resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false); + logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName()); + } catch (Exception e) { + logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); + } + } public void removeFromContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { @@ -200,7 +210,7 @@ public class HostingNodeManager { consistsOfList.addAll(consistsOfToRemove); hostingNode = resourceRegistryPublisher.createResource(hostingNode); } catch (AvailableInAnotherContextException e) { - addToContext(); + addToCurrentContext(); hostingNode = resourceRegistryPublisher.updateResource(hostingNode); } catch (ResourceRegistryException e) { logger.error("error trying to publish hosting node", e); @@ -264,7 +274,7 @@ public class HostingNodeManager { consistsOfList.addAll(consistsOfToRemove); hostingNode = resourceRegistryPublisher.createResource(hostingNode); } catch (AvailableInAnotherContextException e) { - addToContext(); + addToCurrentContext(); hostingNode = resourceRegistryPublisher.updateResource(hostingNode); } catch (ResourceRegistryException e) { logger.error("error trying to publish hosting node", e); @@ -410,8 +420,10 @@ public class HostingNodeManager { hostingNode = instantiateHostingNode(); hostingNode = resourceRegistryPublisher.createResource(hostingNode); } catch (AvailableInAnotherContextException e) { - addToContext(); + addToCurrentContext(); hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid); + } catch (ResourceRegistryException e) { + logger.error("", e); } return hostingNode; }