From 8e35e18b5854077cc4a52f8936f316b3aafecc0b Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 26 Oct 2020 11:48:26 +0100 Subject: [PATCH] EService is no more removed instead is set to down --- .../resourceregistry/EServiceHandler.java | 2 +- .../resourceregistry/HostingNodeHandler.java | 4 +- .../resourcemanager/EServiceManager.java | 51 +++++++++++-------- .../resourcemanager/HostingNodeManager.java | 24 +++------ 4 files changed, 40 insertions(+), 41 deletions(-) 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 36f70a1..439b5c0 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java @@ -142,7 +142,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler { } try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); - eServiceManager.removeEService(); + eServiceManager.updateServiceStateFacet(); } catch (Exception e) { logger.error("Failed to update {} State", EService.NAME, e); } finally { 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 909639b..833f33a 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java @@ -134,7 +134,7 @@ public class HostingNodeHandler extends ContainerHandler { break; default: - //hostingNodeManager.updateStatus(); + hostingNodeManager.updateStatus(); break; } } catch (Exception e) { @@ -155,7 +155,7 @@ public class HostingNodeHandler extends ContainerHandler { } try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); - hostingNodeManager.updateFacets(); + hostingNodeManager.updateStatus(); } catch (Exception e) { logger.error("cannot complete periodic update of {}", HostingNode.NAME, 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 3b88f77..1251a03 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 @@ -13,7 +13,6 @@ import org.gcube.informationsystem.model.reference.properties.Header; 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.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; @@ -46,6 +45,14 @@ import org.gcube.smartgears.handler.resourceregistry.ContextUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * @author lucafrosini + * + */ +/** + * @author lucafrosini + * + */ public class EServiceManager { private static Logger logger = LoggerFactory.getLogger(HostingNodeManager.class); @@ -54,7 +61,7 @@ public class EServiceManager { private ResourceRegistryPublisher resourceRegistryPublisher; - private Activates activates; + // private Activates activates; private EService eService; private ServiceStateFacet serviceStateFacet; @@ -169,29 +176,29 @@ public class EServiceManager { } public EService createEService() throws ResourceRegistryException { + ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); + UUID eServiceUUID = UUID.fromString(applicationContext.id()); try { - ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); - UUID uuid = UUID.fromString(applicationContext.id()); - try { - eService = resourceRegistryClient.getInstance(EService.class, uuid); - // It is not convenient to update the node because the contexts could be changed - // so we should remove from all contexts and then it will be added again. - // It is more efficient to delete it. - resourceRegistryPublisher.delete(eService); - } catch (NotFoundException e) { - // Fine we are going to create it again - } + resourceRegistryClient.exists(EService.class, eServiceUUID); + eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + } catch (NotFoundException e) { eService = instantiateEService(); - // eService = resourceRegistryPublisher.createResource(eService); - activates = createActivatesRelation(eService); - } catch (AvailableInAnotherContextException | AlreadyPresentException e) { - // resourceRegistryPublisher.delete(eService); - // eService = resourceRegistryPublisher.createResource(eService); - resourceRegistryPublisher.delete(activates); - activates = createActivatesRelation(eService); + createActivatesRelation(eService); + } catch (AvailableInAnotherContextException e) { + addToContext(); + try { + eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + } catch (AvailableInAnotherContextException ex) { + resourceRegistryPublisher.addResourceToCurrentContext(eService); + // 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); + } + } catch (ResourceRegistryException e) { + throw e; } - eService = activates.getTarget(); - serviceStateFacet = eService.getFacets(ServiceStateFacet.class).get(0); return eService; } 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 a097668..7966347 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 @@ -32,7 +32,6 @@ import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; -import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; @@ -323,24 +322,17 @@ public class HostingNodeManager { } public HostingNode createHostingNode() throws ResourceRegistryException { + ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); + UUID uuid = UUID.fromString(containerContext.id()); try { - ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); - UUID uuid = UUID.fromString(containerContext.id()); - try { - hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid); - - // It is not convenient to update the node because the contexts could be changed - // so we should remove from all contexts and then it will be added again. - // It is more efficient to delete it. - resourceRegistryPublisher.delete(hostingNode); - } catch (NotFoundException e) { - // Fine we are going to create it again - } + hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid); + hostingNode = updateFacets(); + } catch (NotFoundException e) { hostingNode = instantiateHostingNode(); hostingNode = resourceRegistryPublisher.createResource(hostingNode); - } catch (AvailableInAnotherContextException | AlreadyPresentException e) { - resourceRegistryPublisher.delete(hostingNode); - hostingNode = resourceRegistryPublisher.createResource(hostingNode); + } catch (AvailableInAnotherContextException e) { + addToContext(); + hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid); } return hostingNode; }