Fixed handler according to the fix on gcube model

This commit is contained in:
Luca Frosini 2021-08-04 15:12:58 +02:00
parent 122cdb8288
commit a74fed3036
1 changed files with 45 additions and 4 deletions

View File

@ -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<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfToRemove = new ArrayList<>();
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfList = hostingNode.getConsistsOf();
for (ConsistsOf<? extends Resource, ? extends Facet> 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<HostingNode, MemoryFacet> hasVolatileRAMMemory = new HasVolatileMemoryImpl<HostingNode, MemoryFacet>(
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();