From 9492350a61a5ca298ec81a6391207f5d8bb94d8d Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 18 Mar 2022 12:19:20 +0100 Subject: [PATCH] Migrated to SecretManagerProvider --- .../resourceregistry/ContextUtility.java | 23 ---- .../resourceregistry/EServiceHandler.java | 117 ++++++++---------- .../resourceregistry/HostingNodeHandler.java | 94 +++++++------- .../resourcemanager/EServiceManager.java | 78 +++++++++--- .../resourcemanager/HostingNodeManager.java | 30 +++-- 5 files changed, 170 insertions(+), 172 deletions(-) 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..3c8a6f2 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java @@ -6,8 +6,6 @@ import java.util.TreeSet; import java.util.UUID; import org.gcube.common.authorization.client.proxy.AuthorizationProxy; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.smartgears.provider.ProviderFactory; @@ -27,27 +25,6 @@ public class ContextUtility { authorizationProxy = ProviderFactory.provider().authorizationProxy(); } - public static void resetContex() { - SecurityTokenProvider.instance.reset(); - ScopeProvider.instance.reset(); - } - - public static void setContextFromToken(String token) { - if (token == null || token.compareTo("") == 0) { - resetContex(); - } else { - SecurityTokenProvider.instance.set(token); - String scope = getContextName(token); - ScopeProvider.instance.set(scope); - } - - } - - public static String getCurrentContextName() { - String token = SecurityTokenProvider.instance.get(); - return getContextName(token); - } - 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 15489bd..0c07054 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java @@ -10,13 +10,17 @@ import static org.gcube.smartgears.utils.Utils.rethrowUnchecked; import java.util.HashSet; import java.util.Set; +import java.util.SortedSet; import java.util.UUID; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import javax.xml.bind.annotation.XmlRootElement; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.common.authorization.utils.secret.SecretUtility; import org.gcube.common.events.Observes; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.relations.ConsistsOf; @@ -96,7 +100,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler { private void init() { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); boolean create = true; @@ -105,11 +109,12 @@ public class EServiceHandler extends ApplicationLifecycleHandler { Set startContextsUUID = new HashSet<>(); ContextCache contextCache = ContextCache.getInstance(); for (String token : startTokens) { - ContextUtility.setContextFromToken(token); - String contextFullName = ContextUtility.getContextName(token); - UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); - startContextsUUID.add(contextUUID); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); try { + String contextFullName = ContextUtility.getContextName(token); + UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); + startContextsUUID.add(contextUUID); if (create) { eServiceManager = new EServiceManager(applicationContext); eServiceManager.createEService(); @@ -120,8 +125,11 @@ public class EServiceHandler extends ApplicationLifecycleHandler { eServiceManager.addToContext(); } } catch (Exception e) { - logger.error("Unable to add {} to current context ({})", eServiceManager.getEService(), + UUID uuid = UUID.fromString(applicationContext.id()); + logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, ContextUtility.getContextName(token), e); + }finally { + secretManager.endSession(); } } @@ -131,7 +139,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler { } catch (Throwable e) { rethrowUnchecked(e); } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } logger.info("{} init() terminated", this.getClass().getSimpleName()); @@ -144,61 +152,38 @@ public class EServiceHandler extends ApplicationLifecycleHandler { @Observes({ activation, stop, failure }) void onChanged(ApplicationLifecycle lc) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility - .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); - } + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); + SortedSet secrets = secretManager.getCurrentSecretHolder().getSecrets(); + if (secrets==null || secrets.size()==0) { + String token = applicationContext.configuration().startTokens().iterator().next(); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); + } eServiceManager.updateServiceStateFacet(); } catch (Exception e) { logger.error("Failed to update {} State", EService.NAME, e); } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } } - /* - @Observes({ stop, failure }) - void onStop(ApplicationLifecycle lc) { - ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility - .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); - } - try { - Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); - eServiceManager.updateServiceStateFacet(); - } catch (Exception e) { - logger.error("Failed to update {} State", EService.NAME, e); - } finally { - ContextUtility.setContextFromToken(previousToken); - Thread.currentThread().setContextClassLoader(contextCL); - } - - } - */ - @Observes(value = addToContext) void addTo(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility - .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); - } + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); - ContextUtility.setContextFromToken(token); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); eServiceManager.addToContext(); } catch (Exception e) { logger.error("Failed to add {} to current context ({})", EService.NAME, - ContextUtility.getCurrentContextName(), e); + secretManager.getContext(), e); } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } } @@ -206,19 +191,17 @@ public class EServiceHandler extends ApplicationLifecycleHandler { @Observes(value = removeFromContext) void removeFrom(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility - .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); - } + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); eServiceManager.removeFromContext(); } catch (Exception e) { logger.error("Failed to remove {} from current context ({})", - EService.NAME, ContextUtility.getCurrentContextName(), e); + EService.NAME, secretManager.getContext(), e); } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } @@ -245,26 +228,28 @@ public class EServiceHandler extends ApplicationLifecycleHandler { } if (lc.state() == ApplicationState.active) { - logger.info("scheduling periodic updates of application {} EService", - applicationContext.name()); + logger.info("Scheduling periodic updates of {} from application {}", + EService.NAME, applicationContext.name()); } else { - logger.info("resuming periodic updates of application {} EService", - applicationContext.name()); + logger.info("Resuming periodic updates of {} for application {}", + EService.NAME, applicationContext.name()); } final Runnable updateTask = new Runnable() { public void run() { - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility.setContextFromToken( - applicationContext.configuration().startTokens().iterator().next()); - } + SecretManager secretManager = SecretManagerProvider.instance.get(); try { + SortedSet secrets = secretManager.getCurrentSecretHolder().getSecrets(); + if (secrets==null || secrets.size()==0) { + String token = applicationContext.configuration().startTokens().iterator().next(); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); + } eServiceManager.updateServiceStateFacet(); } catch (Exception e) { - logger.error("Cannot complete periodic update of EService", e); + logger.error("Cannot complete periodic update of {}", EService.NAME, e); } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); } } }; @@ -279,15 +264,15 @@ public class EServiceHandler extends ApplicationLifecycleHandler { synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) { if (periodicUpdates != null) { - logger.trace("stopping periodic updates of EService for application {} ", - applicationContext.name()); + logger.trace("Stopping periodic updates of {} for application {} ", + EService.NAME, applicationContext.name()); try { periodicUpdates.cancel(true); periodicUpdates = null; } catch (Exception e) { - logger.warn("could not stop periodic updates of EService for application {}", - applicationContext.name(), e); + logger.warn("Could not stop periodic updates of {} for application {}", + EService.NAME, applicationContext.name(), 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 3b34200..cf632b6 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java @@ -15,6 +15,7 @@ import static org.gcube.smartgears.utils.Utils.rethrowUnchecked; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.SortedSet; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -22,7 +23,10 @@ import java.util.concurrent.ScheduledFuture; import javax.xml.bind.annotation.XmlRootElement; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.common.authorization.utils.secret.SecretUtility; import org.gcube.common.events.Observes; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; @@ -95,7 +99,7 @@ public class HostingNodeHandler extends ContainerHandler { private void init() { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); boolean create = true; @@ -105,7 +109,8 @@ public class HostingNodeHandler extends ContainerHandler { Set startContextsUUID = new HashSet<>(); ContextCache contextCache = ContextCache.getInstance(); for (String token : startTokens) { - ContextUtility.setContextFromToken(token); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); String contextFullName = ContextUtility.getContextName(token); UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); startContextsUUID.add(contextUUID); @@ -122,7 +127,8 @@ public class HostingNodeHandler extends ContainerHandler { hostingNodeManager.addToContext(); } } catch (Exception e) { - logger.error("Unable to add {} to current context ({})", hostingNodeManager.getHostingNode(), + UUID uuid = UUID.fromString(containerContext.id()); + logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getContextName(token), e); } } @@ -133,7 +139,7 @@ public class HostingNodeHandler extends ContainerHandler { } catch (Throwable e) { rethrowUnchecked(e); } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } logger.info("{} init() terminated", this.getClass().getSimpleName()); @@ -143,16 +149,18 @@ public class HostingNodeHandler extends ContainerHandler { containerContext.events().subscribe(new Object() { - //@Observes({ activation, part_activation, shutdown, stop, failure }) - @Observes({ activation, part_activation}) + @Observes({ activation, part_activation, shutdown, stop, failure}) void onChanged(ContainerLifecycle cl) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility.setContextFromToken(containerContext.configuration().startTokens().iterator().next()); - } + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); + SortedSet secrets = secretManager.getCurrentSecretHolder().getSecrets(); + if (secrets==null || secrets.size()==0) { + String token = containerContext.configuration().startTokens().iterator().next(); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); + } ContainerState containerState = cl.state(); switch (containerState) { case active: @@ -166,26 +174,7 @@ public class HostingNodeHandler extends ContainerHandler { } catch (Exception e) { logger.error("Failed to update {} State", HostingNode.NAME, e); } finally { - ContextUtility.setContextFromToken(previousToken); - Thread.currentThread().setContextClassLoader(contextCL); - } - } - - @Observes({ shutdown, stop, failure }) - void onShutdown(ContainerLifecycle cl) { - ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility - .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); - } - try { - Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); - hostingNodeManager.updateStatus(); - } catch (Exception e) { - logger.error("cannot complete periodic update of {}", HostingNode.NAME, e); - } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } } @@ -193,18 +182,16 @@ public class HostingNodeHandler extends ContainerHandler { @Observes(value = addToContext) void addTo(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility - .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); - } + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); hostingNodeManager.addToContext(); } catch (Exception e) { logger.error("Failed to update Service State", e); } finally { - ContextUtility.setContextFromToken(previousToken); + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } } @@ -212,17 +199,16 @@ public class HostingNodeHandler extends ContainerHandler { @Observes(value = removeFromContext) void removeFrom(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String previousToken = SecurityTokenProvider.instance.get(); - if (previousToken == null) { - ContextUtility - .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); - } + SecretManager secretManager = SecretManagerProvider.instance.get(); try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); hostingNodeManager.removeFromContext(); } catch (Exception e) { logger.error("Failed to update Service State", e); } finally { + secretManager.endSession(); Thread.currentThread().setContextClassLoader(contextCL); } } @@ -249,22 +235,26 @@ public class HostingNodeHandler extends ContainerHandler { } if (cl.state() == active) { - logger.info("scheduling periodic updates of {}", HostingNode.NAME); + logger.info("Scheduling periodic updates of {}", HostingNode.NAME); } else { - logger.info("resuming periodic updates of {}", HostingNode.NAME); + logger.info("Resuming periodic updates of {}", HostingNode.NAME); } final Runnable updateTask = new Runnable() { public void run() { - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) - currentToken = containerContext.configuration().startTokens().iterator().next(); - - ContextUtility.setContextFromToken(currentToken); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { + SortedSet secrets = secretManager.getCurrentSecretHolder().getSecrets(); + if (secrets==null || secrets.size()==0) { + String token = containerContext.configuration().startTokens().iterator().next(); + Secret secret = SecretUtility.getSecretByTokenString(token); + secretManager.startSession(secret); + } hostingNodeManager.updateFacets(); } catch (Exception e) { - logger.error("cannot complete periodic update of {}", HostingNode.NAME, e); + logger.error("Cannot complete periodic update of {}", HostingNode.NAME, e); + }finally { + secretManager.endSession(); } } }; @@ -278,13 +268,13 @@ public class HostingNodeHandler extends ContainerHandler { synchronized void cancelPeriodicUpdates(ContainerLifecycle cl) { if (periodicUpdates != null) { - logger.trace("stopping periodic updates of {}", HostingNode.NAME); + logger.trace("Stopping periodic updates of {}", HostingNode.NAME); try { periodicUpdates.cancel(true); service.shutdownNow(); periodicUpdates = null; } catch (Exception e) { - logger.warn("could not stop periodic updates of {}", HostingNode.NAME, e); + logger.warn("Could not stop periodic updates of {}", HostingNode.NAME, e); } } } 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 b487688..cbefee8 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 @@ -2,12 +2,16 @@ package org.gcube.smartgears.handler.resourceregistry.resourcemanager; import java.net.URI; import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.ServletRegistration; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.properties.Header; @@ -25,13 +29,16 @@ import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClien import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory; import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.StateFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.resources.EServiceImpl; +import org.gcube.resourcemanagement.model.impl.entities.resources.HostingNodeImpl; import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl; import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl; import org.gcube.resourcemanagement.model.reference.entities.facets.AccessPointFacet; +import org.gcube.resourcemanagement.model.reference.entities.facets.EventFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet; import org.gcube.resourcemanagement.model.reference.entities.resources.EService; @@ -42,8 +49,6 @@ import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activa 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.handler.resourceregistry.Constants; -import org.gcube.smartgears.handler.resourceregistry.ContextUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,36 +79,38 @@ public class EServiceManager { } public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { - HostingNode hostingNode = applicationContext.container().properties() - .lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode(); + UUID uuid = UUID.fromString(applicationContext.container().id()); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { - resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false); - logger.info("{} successfully added to current context ({})", eService, ContextUtility.getCurrentContextName()); + resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); + logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, secretManager.getContext()); } catch (Exception e) { - logger.error("Unable to add {} to current context ({})", eService, ContextUtility.getCurrentContextName(), e); + logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e); } } public void removeFromContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + UUID uuid = UUID.fromString(applicationContext.container().id()); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { - resourceRegistryPublisher.removeResourceFromCurrentContext(eService, false); - logger.info("{} successfully removed from current context ({})", eService, - ContextUtility.getCurrentContextName()); + resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false); + logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid, + secretManager.getContext()); } catch (Exception e) { - logger.error("Unable to remove {} from current context ({})", eService, - ContextUtility.getCurrentContextName(), e); + logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e); } } public void removeFromContext(UUID contextUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID); + UUID uuid = UUID.fromString(applicationContext.container().id()); try { - resourceRegistryPublisher.removeResourceFromContext(eService, contextUUID, false); - logger.info("{} successfully removed from context ({})", eService, contextFullName); + resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false); + logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName); } catch (Exception e) { - logger.error("Unable to remove {} from current context ({})", eService, contextFullName, e); + logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e); } } @@ -173,11 +180,18 @@ public class EServiceManager { } } + Date date = Calendar.getInstance().getTime(); + stateFacet = new StateFacetImpl(); String state = getState(); stateFacet.setValue(state); eService.addFacet(stateFacet); - + stateFacet.setAdditionalProperty("date", date); + + EventFacet eventFacet = new EventFacetImpl(); + eventFacet.setEvent(state); + eventFacet.setDate(date); + return eService; } @@ -192,6 +206,8 @@ public class EServiceManager { stateFacet = new StateFacetImpl(); String state = getState(); stateFacet.setValue(state); + Date date = Calendar.getInstance().getTime(); + stateFacet.setAdditionalProperty("date", date); eService.addFacet(stateFacet); resourceRegistryPublisher.update(eService); } else { @@ -218,6 +234,8 @@ public class EServiceManager { stateFacet = new StateFacetImpl(); String state = getState(); stateFacet.setValue(state); + Date date = Calendar.getInstance().getTime(); + stateFacet.setAdditionalProperty("date", date); eService.addFacet(stateFacet); resourceRegistryPublisher.update(eService); } else { @@ -230,16 +248,38 @@ public class EServiceManager { } public void updateServiceStateFacet() throws ResourceRegistryException { + Date date = Calendar.getInstance().getTime(); + String state = getState(); stateFacet.setValue(state); - stateFacet = resourceRegistryPublisher.updateFacet(stateFacet); + + boolean found = false; + List eventFacets = eService.getFacets(EventFacet.class); + for(EventFacet eventFacet : eventFacets) { + if(eventFacet.getEvent().compareTo(state)==0) { + found = true; + eventFacet.setDate(date); + break; + } + } + + if(!found) { + EventFacet eventFacet = new EventFacetImpl(); + eventFacet.setDate(date); + eventFacet.setEvent(state); + eService.addFacet(eventFacet); + } + + eService = resourceRegistryPublisher.updateResource(eService); } private Activates createActivatesRelation(EService eService) throws ResourceRegistryException { - HostingNode hostingNode = applicationContext.container().properties() - .lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode(); + UUID hostingNodeUUID = UUID.fromString(applicationContext.container().id()); + HostingNode hostingNode = new HostingNodeImpl(); + Header header = new HeaderImpl(hostingNodeUUID); + hostingNode.setHeader(header); PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); 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 df846a3..5836eaf 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,6 +32,8 @@ import javax.management.ObjectName; import javax.management.ReflectionException; import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; @@ -77,7 +79,6 @@ import org.gcube.smartgears.configuration.container.ContainerConfiguration; import org.gcube.smartgears.configuration.container.Site; import org.gcube.smartgears.configuration.library.SmartGearsConfiguration; import org.gcube.smartgears.context.container.ContainerContext; -import org.gcube.smartgears.handler.resourceregistry.ContextUtility; import org.gcube.smartgears.lifecycle.container.ContainerState; import org.gcube.smartgears.provider.ProviderFactory; import org.slf4j.Logger; @@ -111,33 +112,38 @@ public class HostingNodeManager { } public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + UUID uuid = UUID.fromString(containerContext.id()); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { - resourceRegistryPublisher.addResourceToCurrentContext(hostingNode, false); - logger.info("{} successfully added to current context ({})", hostingNode, ContextUtility.getCurrentContextName()); + resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); + logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, secretManager.getContext()); } catch (Exception e) { - logger.error("Unable to add {} to current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e); + logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e); } } public void removeFromContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + UUID uuid = UUID.fromString(containerContext.id()); + SecretManager secretManager = SecretManagerProvider.instance.get(); try { - resourceRegistryPublisher.removeResourceFromCurrentContext(hostingNode, false); - logger.info("{} successfully removed from current context ({})", hostingNode, - ContextUtility.getCurrentContextName()); + resourceRegistryPublisher.removeResourceFromCurrentContext(HostingNode.NAME, uuid, false); + logger.info("{} with UUID {} successfully removed from current context ({})", HostingNode.NAME, uuid, + secretManager.getContext()); } catch (Exception e) { - logger.error("Unable to remove {} from current context ({})", hostingNode, ContextUtility.getCurrentContextName(), e); + logger.error("Unable to remove {} with UUID {} from current context ({})", HostingNode.NAME, uuid, secretManager.getContext(), e); } } public void removeFromContext(UUID contextUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID); + UUID uuid = UUID.fromString(containerContext.id()); try { - resourceRegistryPublisher.removeResourceFromContext(hostingNode, contextUUID, false); - logger.info("{} successfully removed from context ({})", hostingNode, contextFullName); + resourceRegistryPublisher.removeResourceFromContext(HostingNode.NAME, uuid, contextUUID, false); + logger.info("{} with UUID {} successfully removed from context ({})", HostingNode.NAME, uuid, contextFullName); } catch (Exception e) { - logger.error("Unable to remove {} from current context ({})", hostingNode, contextFullName, e); + logger.error("Unable to remove {} from current context ({})", HostingNode.NAME, uuid, contextFullName, e); } } @@ -393,7 +399,7 @@ public class HostingNodeManager { locationFacet.setLongitude(site.longitude()); hostingNode.addFacet(locationFacet); - logger.info("hostingNode instanciated"); + logger.info("{} instantiated", HostingNode.NAME); return hostingNode; }