From a74fed303630d718a6fadacf9f9d927a1fdb5331 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 4 Aug 2021 15:12:58 +0200 Subject: [PATCH] Fixed handler according to the fix on gcube model --- .../resourcemanager/HostingNodeManager.java | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) 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 f04af94..7d2d461 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 @@ -12,6 +12,7 @@ import java.nio.file.FileStore; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -44,6 +45,7 @@ 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.CPUFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.LocationFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.MemoryFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.NetworkingFacetImpl; @@ -55,6 +57,7 @@ import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasPersisten import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasVolatileMemoryImpl; import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; import org.gcube.resourcemanagement.model.reference.entities.facets.CPUFacet; +import org.gcube.resourcemanagement.model.reference.entities.facets.EventFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.LocationFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.MemoryFacet.MemoryUnit; @@ -71,6 +74,7 @@ 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; import org.slf4j.LoggerFactory; @@ -196,12 +200,18 @@ public class HostingNodeManager { } public HostingNode updateStatus() throws ResourceRegistryException { - logger.debug("Setting {} down", HostingNode.NAME); - + ContainerState containerState = containerContext.lifecycle().state(); + + logger.debug("Setting {} {}", HostingNode.NAME, containerState.remoteForm().toLowerCase()); + + EventFacet eventFacet = null; + StateFacet stateFacet = null; List> consistsOfToRemove = new ArrayList<>(); + + List> consistsOfList = hostingNode.getConsistsOf(); for (ConsistsOf c : consistsOfList) { if (c.getTarget() instanceof StateFacet) { @@ -209,6 +219,21 @@ public class HostingNodeManager { stateFacet = getStateFacet(stateFacet); continue; } + + if(c.getTarget() instanceof EventFacet) { + eventFacet = (EventFacet) c.getTarget(); + String value = eventFacet.getEvent(); + if(value.compareTo(containerState.remoteForm().toLowerCase())==0) { + // This facet must be updated (the date must be updated) so it must not be removed from udpate + eventFacet.setDate(Calendar.getInstance().getTime()); + continue; + }else { + // This is not the event facet to be updated + // Setting the variable to null so it will be created if the event does not already exists. + eventFacet = null; + } + } + consistsOfToRemove.add(c); } @@ -216,6 +241,11 @@ public class HostingNodeManager { // Removing the one that have not to be changed consistsOfList.removeAll(consistsOfToRemove); + if(eventFacet == null) { + eventFacet = getEventFacet(); + hostingNode.addFacet(eventFacet); + } + try { hostingNode = resourceRegistryPublisher.updateResource(hostingNode); } catch (NotFoundException e) { @@ -300,7 +330,10 @@ public class HostingNodeManager { StateFacet stateFacet = getStateFacet(null); hostingNode.addFacet(stateFacet); - + + EventFacet eventFacet = getEventFacet(); + hostingNode.addFacet(eventFacet); + MemoryFacet ramFacet = getRamInfo(null); HasVolatileMemory hasVolatileRAMMemory = new HasVolatileMemoryImpl( hostingNode, ramFacet, null); @@ -354,7 +387,15 @@ public class HostingNodeManager { stateFacet.setValue(state); return stateFacet; } - + + private EventFacet getEventFacet() { + EventFacet eventFacet = new EventFacetImpl(); + String state = containerContext.lifecycle().state().remoteForm().toLowerCase(); + eventFacet.setDate(Calendar.getInstance().getTime()); + eventFacet.setEvent(state); + return eventFacet; + } + private MemoryFacet getDiskSpace(MemoryFacet memoryFacet) { if (memoryFacet == null) { memoryFacet = new MemoryFacetImpl();