diff --git a/src/main/java/org/gcube/smartgears/connector/resourceregistry/Constants.java b/src/main/java/org/gcube/smartgears/connector/resourceregistry/Constants.java index 760c465..85f0cbf 100644 --- a/src/main/java/org/gcube/smartgears/connector/resourceregistry/Constants.java +++ b/src/main/java/org/gcube/smartgears/connector/resourceregistry/Constants.java @@ -1,5 +1,6 @@ package org.gcube.smartgears.connector.resourceregistry; +import org.gcube.smartgears.connector.resourceregistry.resourcemanager.EServiceManager; import org.gcube.smartgears.connector.resourceregistry.resourcemanager.HostingNodeManager; /** @@ -11,8 +12,6 @@ public class Constants { public static final String HOSTING_NODE_MANAGER_PROPERTY = HostingNodeManager.class.getSimpleName(); - public static final long default_container_publication_frequency_in_seconds = 60; - - public static final int application_republish_frequency_in_minutes = 20; + public static final String ESERVICE_MANAGER_PROPERTY = EServiceManager.class.getSimpleName(); } diff --git a/src/main/java/org/gcube/smartgears/connector/resourceregistry/FacetBasedPublisher.java b/src/main/java/org/gcube/smartgears/connector/resourceregistry/FacetBasedPublisher.java index 6da59d7..d9103af 100644 --- a/src/main/java/org/gcube/smartgears/connector/resourceregistry/FacetBasedPublisher.java +++ b/src/main/java/org/gcube/smartgears/connector/resourceregistry/FacetBasedPublisher.java @@ -1,8 +1,15 @@ package org.gcube.smartgears.connector.resourceregistry; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.UUID; +import org.gcube.common.security.AuthorizedTasks; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.resourcemanagement.model.reference.entities.resources.EService; +import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode; +import org.gcube.smartgears.connector.resourceregistry.resourcemanager.EServiceManager; import org.gcube.smartgears.connector.resourceregistry.resourcemanager.HostingNodeManager; import org.gcube.smartgears.context.Property; import org.gcube.smartgears.context.application.ApplicationContext; @@ -21,120 +28,126 @@ public class FacetBasedPublisher implements Publisher { @Override public boolean create(ContainerContext containerContext, Set contexts) { - // Used at startup and in add to context HostingNodeManager hostingNodeManager = new HostingNodeManager(containerContext); AuthorizationProvider authorizationProvider = containerContext.configuration().authorizationProvider(); - boolean first = true; - for (String context : contexts) { - try { - UUID contextUUID = ContextUtility.getContextUUID(context); - if(first) { - hostingNodeManager.createHostingNode(); - Property property = new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager); - containerContext.properties().add(property); - first = false; - }else { - + SortedSet contextToIterate = new TreeSet<>(contexts); + final String context = contextToIterate.first(); + + try { + AuthorizedTasks.executeSafely( () -> { + for (final String c : contextToIterate) { + try { + UUID contextUUID = ContextUtility.getContextUUID(context); + if(context.compareTo(c)==0) { + hostingNodeManager.createHostingNode(); + Property property = new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager); + containerContext.properties().add(property); + }else { + hostingNodeManager.addToContext(contextUUID); + } + }catch (Throwable e) { + logger.error("Error while publishing {} (id='{}') in context '{}'", HostingNode.NAME, containerContext.id(), context, e); + } } - }catch (Exception e) { - - } + }, authorizationProvider.getSecretForContext(context)); + }catch (Throwable e) { + logger.error("Error while publishing {} (id='{}')", HostingNode.NAME, containerContext.id(), e); } return true; } @Override public boolean create(ApplicationContext applicationContext, Set contexts) { - // Used at startup and in add to context + EServiceManager eServiceManager = new EServiceManager(applicationContext); + AuthorizationProvider authorizationProvider = applicationContext.container().configuration().authorizationProvider(); + SortedSet contextToIterate = new TreeSet<>(contexts); + final String context = contextToIterate.first(); + + try { + AuthorizedTasks.executeSafely( () -> { + for (final String c : contextToIterate) { + try { + UUID contextUUID = ContextUtility.getContextUUID(context); + if(context.compareTo(c)==0) { + eServiceManager.createEService(); + Property property = new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager); + applicationContext.properties().add(property); + }else { + eServiceManager.addToContext(contextUUID); + } + }catch (Throwable e) { + logger.error("Error while publishing {} (name='{}', id='{}') in context '{}'", EService.NAME, applicationContext.name(), applicationContext.id(), context, e); + } + } + }, authorizationProvider.getSecretForContext(context)); + }catch (Throwable e) { + logger.error("Error while publishing {} (name='{}', id='{}')", EService.NAME, applicationContext.name(), applicationContext.id(), e); + } + return true; + } + + @Override + public boolean remove(ContainerContext containerContext, Set contexts) { + HostingNodeManager hostingNodeManager = (HostingNodeManager) containerContext.properties().lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(); + AuthorizationProvider authorizationProvider = containerContext.configuration().authorizationProvider(); + for(String context : contexts) { + try { + AuthorizedTasks.executeSafely( () -> { + try { + hostingNodeManager.removeFromCurrentContext(); + }catch (Exception e) { + throw new RuntimeException(e); + } + + }, authorizationProvider.getSecretForContext(context)); + }catch (Throwable e) { + logger.error("Unable to remove {} (id='{}') from context '{}'", HostingNode.NAME, containerContext.id(), context, e); + } + } return false; } @Override public boolean remove(ApplicationContext applicationContext, Set contexts) { - // Remove from contexts - return false; + EServiceManager eServiceManager = (EServiceManager) applicationContext.properties().lookup(Constants.ESERVICE_MANAGER_PROPERTY).value(); + AuthorizationProvider authorizationProvider = applicationContext.container().configuration().authorizationProvider(); + for(String context : contexts) { + try { + AuthorizedTasks.executeSafely( () -> { + try { + eServiceManager.removeFromCurrentContext(); + }catch (Exception e) { + throw new RuntimeException(e); + } + + }, authorizationProvider.getSecretForContext(context)); + }catch (Throwable e) { + logger.error("Unable to remove {} (name='{}', id='{}') from context '{}'", EService.NAME, applicationContext.name(), applicationContext.id(), context, e); + } + } + return true; } - - @Override - public boolean remove(ContainerContext containerContext, Set contexts) { - // Remove from contexts - return false; - } - - @Override - public boolean update(ApplicationContext applicationContext) { - // Periodic updates - return false; - } - + @Override public boolean update(ContainerContext containerContext) { - // Periodic updates - return false; + HostingNodeManager hostingNodeManager = (HostingNodeManager) containerContext.properties().lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(); + try { + hostingNodeManager.updateFacets(); + } catch (ResourceRegistryException e) { + logger.error("Unable to update {} (id='{}')", HostingNode.NAME, containerContext.id(), e); + } + return true; } - - - -// @Override -// public boolean publishContainer(ContainerContext containerContext, Set contexts) { -// HostingNodeManager hostingNodeManager = new HostingNodeManager(containerContext); -// -// AuthorizationProvider authorizationProvider = containerContext.configuration().authorizationProvider(); -// Set authorizedContexts = authorizationProvider.getContexts(); -// for (String context : authorizedContexts) { -// -// } -// return true; -// } -// -// @Override -// public boolean publishApplication(ApplicationContext applicationContext, Set contexts) { -// EServiceManager eServiceManager = new EServiceManager(applicationContext); -// -// AuthorizationProvider provider = applicationContext.container().configuration().authorizationProvider(); -// -// for (String context : provider.getContexts()) -// try { -// logger.info("publishing application in context {}", context); -// AuthorizedTasks.executeSafely(new Runnable() { -// -// @Override -// public void run() { -// try { -// logger.info("(inside task)publishing application in context {}", context); -// String resource = toXml(enpoint); -// registry.getStubs().create(resource, enpoint.type().toString()); -// logger.debug("publisher resource in context {} : {}",context, resource); -// }catch (Exception e) { -// logger.error("erro publishing application", e); -// throw new RuntimeException(e); -// } -// -// } -// }, provider.getSecretForContext(context)); -// }catch (Throwable e) { -// logger.error("error publisshing application profile in context {}", context, e); -// } -// -// return true; -// } -// -// @Override -// public boolean unpublishContainer(ContainerContext container, Set contexts) { -// // TODO Auto-generated method stub -// return false; -// } -// -// @Override -// public boolean unpublishApplication(ApplicationContext application, Set contexts) { -// // TODO Auto-generated method stub -// return false; -// } -// -// private String toXml(Resource resource){ -// StringWriter writer = new StringWriter(); -// Resources.marshal(resource, writer); -// return writer.toString(); -// } - + + @Override + public boolean update(ApplicationContext applicationContext) { + EServiceManager eServiceManager = (EServiceManager) applicationContext.properties().lookup(Constants.ESERVICE_MANAGER_PROPERTY).value(); + try { + eServiceManager.updateFacets(); + } catch (ResourceRegistryException e) { + logger.error("Unable to update {} (name='{}', id='{}') ", EService.NAME, applicationContext.name(), applicationContext.id(), e); + } + return true; + } + }