Fixed handlers
This commit is contained in:
parent
d704290b17
commit
0fc5d9886e
|
@ -56,10 +56,9 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
private ScheduledFuture<?> periodicUpdates;
|
||||
|
||||
protected EServiceManager eServiceManager;
|
||||
|
||||
|
||||
public EServiceHandler() {
|
||||
super();
|
||||
eServiceManager = new EServiceManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -87,8 +86,10 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
ContextUtility.setContextFromToken(token);
|
||||
try {
|
||||
if (create) {
|
||||
eServiceManager = new EServiceManager();
|
||||
eServiceManager.createEService(applicationContext);
|
||||
applicationContext.properties().add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager));
|
||||
applicationContext.properties()
|
||||
.add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager));
|
||||
create = false;
|
||||
} else {
|
||||
eServiceManager.addToContext(applicationContext);
|
||||
|
@ -116,9 +117,9 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
void onChanged(ApplicationLifecycle lc) {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String currentToken = SecurityTokenProvider.instance.get();
|
||||
if (currentToken == null)
|
||||
if (currentToken == null) {
|
||||
currentToken = applicationContext.configuration().startTokens().iterator().next();
|
||||
|
||||
}
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
|
||||
try {
|
||||
|
@ -136,9 +137,9 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
void onStop(ApplicationLifecycle lc) {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String currentToken = SecurityTokenProvider.instance.get();
|
||||
if (currentToken == null)
|
||||
if (currentToken == null) {
|
||||
currentToken = applicationContext.configuration().startTokens().iterator().next();
|
||||
|
||||
}
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
|
||||
try {
|
||||
|
@ -189,7 +190,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void schedulePeriodicUpdates() {
|
||||
|
||||
|
@ -221,7 +221,8 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
try {
|
||||
String currentToken = SecurityTokenProvider.instance.get();
|
||||
if (currentToken == null)
|
||||
currentToken = applicationContext.configuration().startTokens().iterator().next();
|
||||
currentToken = applicationContext.configuration().startTokens().iterator()
|
||||
.next();
|
||||
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
eServiceManager.updateServiceStateFacet(applicationContext);
|
||||
|
|
|
@ -60,10 +60,9 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
private ScheduledFuture<?> periodicUpdates;
|
||||
|
||||
protected HostingNodeManager hostingNodeManager;
|
||||
|
||||
|
||||
public HostingNodeHandler() {
|
||||
super();
|
||||
hostingNodeManager = new HostingNodeManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -79,7 +78,7 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
logger.error("onStart failed", re);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void init() {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String previousToken = SecurityTokenProvider.instance.get();
|
||||
|
@ -91,8 +90,10 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
ContextUtility.setContextFromToken(token);
|
||||
try {
|
||||
if (create) {
|
||||
hostingNodeManager.createHostingNode(containerContext);
|
||||
containerContext.properties().add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
|
||||
hostingNodeManager = new HostingNodeManager(containerContext);
|
||||
hostingNodeManager.createHostingNode();
|
||||
containerContext.properties()
|
||||
.add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
|
||||
create = false;
|
||||
} else {
|
||||
hostingNodeManager.addToContext();
|
||||
|
@ -110,39 +111,57 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
}
|
||||
logger.info("{} init() terminated", this.getClass().getSimpleName());
|
||||
}
|
||||
|
||||
|
||||
private void registerObservers() {
|
||||
|
||||
containerContext.events().subscribe(new Object() {
|
||||
|
||||
@Observes({ activation, part_activation, shutdown, stop, failure })
|
||||
// @Observes({ activation, part_activation, shutdown, stop, failure })
|
||||
@Observes({ activation, part_activation })
|
||||
void onChanged(ContainerLifecycle lc) {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String currentToken = SecurityTokenProvider.instance.get();
|
||||
if (currentToken == null)
|
||||
if (currentToken == null) {
|
||||
currentToken = containerContext.configuration().startTokens().iterator().next();
|
||||
|
||||
}
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
|
||||
try {
|
||||
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
|
||||
hostingNodeManager.updateFacets(containerContext);
|
||||
hostingNodeManager.updateFacets();
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to update {} State", HostingNode.NAME, e);
|
||||
} finally {
|
||||
Thread.currentThread().setContextClassLoader(contextCL);
|
||||
}
|
||||
}
|
||||
|
||||
@Observes({ shutdown, stop, failure })
|
||||
void onStop(ContainerLifecycle lc) {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String currentToken = SecurityTokenProvider.instance.get();
|
||||
if (currentToken == null) {
|
||||
currentToken = containerContext.configuration().startTokens().iterator().next();
|
||||
}
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
|
||||
try {
|
||||
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
|
||||
hostingNodeManager.setDown();
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to update {} State", HostingNode.NAME, e);
|
||||
} finally {
|
||||
Thread.currentThread().setContextClassLoader(contextCL);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Observes(value = addToContext)
|
||||
void addTo(String token) {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String currentToken = SecurityTokenProvider.instance.get();
|
||||
if (currentToken == null)
|
||||
if (currentToken == null) {
|
||||
currentToken = containerContext.configuration().startTokens().iterator().next();
|
||||
|
||||
}
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
|
||||
try {
|
||||
|
@ -153,16 +172,15 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
} finally {
|
||||
Thread.currentThread().setContextClassLoader(contextCL);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Observes(value = removeFromContext)
|
||||
void removeFrom(String token) {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String currentToken = SecurityTokenProvider.instance.get();
|
||||
if (currentToken == null)
|
||||
if (currentToken == null) {
|
||||
currentToken = containerContext.configuration().startTokens().iterator().next();
|
||||
|
||||
}
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
|
||||
try {
|
||||
|
@ -210,7 +228,7 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
|
||||
ContextUtility.setContextFromToken(currentToken);
|
||||
try {
|
||||
hostingNodeManager.updateFacets(containerContext);
|
||||
hostingNodeManager.updateFacets();
|
||||
} catch (Exception e) {
|
||||
logger.error("cannot complete periodic update of {}", HostingNode.NAME, e);
|
||||
}
|
||||
|
|
|
@ -3,25 +3,24 @@ package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
|
|||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.gcube.informationsystem.model.impl.properties.HeaderImpl;
|
||||
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
|
||||
import org.gcube.informationsystem.model.impl.relations.ConsistsOfImpl;
|
||||
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.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;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
|
||||
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
|
||||
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
|
||||
import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl;
|
||||
|
@ -44,43 +43,44 @@ 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.gcube.smartgears.handler.resourceregistry.EServiceHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EServiceManager {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(HostingNodeManager.class);
|
||||
|
||||
|
||||
private static List<String> servletExcludes = Arrays.asList("default", "jsp");
|
||||
|
||||
|
||||
private ResourceRegistryPublisher resourceRegistryPublisher;
|
||||
|
||||
|
||||
private Activates<HostingNode, EService> activates;
|
||||
private EService eService;
|
||||
|
||||
private ServiceStateFacet serviceStateFacet;
|
||||
|
||||
public EServiceManager() {
|
||||
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
|
||||
}
|
||||
|
||||
|
||||
public EService geteService() {
|
||||
return eService;
|
||||
}
|
||||
|
||||
public void addToContext(ApplicationContext applicationContext) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
HostingNode hostingNode = applicationContext.container().properties().lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY)
|
||||
.value(HostingNodeManager.class).getHostingNode();
|
||||
|
||||
public void addToContext(ApplicationContext applicationContext)
|
||||
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
HostingNode hostingNode = applicationContext.container().properties()
|
||||
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode();
|
||||
boolean added = resourceRegistryPublisher.addResourceToCurrentContext(hostingNode);
|
||||
if (added) {
|
||||
logger.info("{} successfully added to current context ({})", eService,
|
||||
ContextUtility.getCurrentContextName());
|
||||
} else {
|
||||
logger.error("Unable to add {} to current context ({})", eService,
|
||||
ContextUtility.getCurrentContextName());
|
||||
logger.error("Unable to add {} to current context ({})", eService, ContextUtility.getCurrentContextName());
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFromContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
|
||||
public void removeFromContext()
|
||||
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
boolean removed = false;
|
||||
removed = resourceRegistryPublisher.removeResourceFromCurrentContext(eService);
|
||||
if (removed) {
|
||||
|
@ -115,13 +115,13 @@ public class EServiceManager {
|
|||
public String getState(ApplicationContext applicationContext) {
|
||||
return applicationContext.lifecycle().state().remoteForm().toLowerCase();
|
||||
}
|
||||
|
||||
|
||||
private EService instantiateEService(ApplicationContext applicationContext) {
|
||||
logger.info("Creating {} for {}", EService.NAME, applicationContext.name());
|
||||
|
||||
ApplicationConfiguration applicationConfiguration = applicationContext.configuration();
|
||||
String id = applicationContext.id();
|
||||
|
||||
|
||||
UUID uuid = UUID.fromString(id);
|
||||
EService eService = new EServiceImpl();
|
||||
Header header = new HeaderImpl(uuid);
|
||||
|
@ -158,86 +158,53 @@ public class EServiceManager {
|
|||
}
|
||||
}
|
||||
|
||||
ServiceStateFacet serviceStateFacet = new ServiceStateFacetImpl();
|
||||
serviceStateFacet.setValue(getState(applicationContext));
|
||||
serviceStateFacet = new ServiceStateFacetImpl();
|
||||
String state = getState(applicationContext);
|
||||
serviceStateFacet.setValue(state);
|
||||
eService.addFacet(serviceStateFacet);
|
||||
|
||||
return eService;
|
||||
}
|
||||
|
||||
|
||||
public EService createEService(ApplicationContext applicationContext) throws ResourceRegistryException {
|
||||
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
|
||||
}
|
||||
eService = instantiateEService(applicationContext);
|
||||
activates = createActivatesRelation(applicationContext, eService);
|
||||
//eService = resourceRegistryPublisher.createResource(eService);
|
||||
// eService = resourceRegistryPublisher.createResource(eService);
|
||||
activates = createActivatesRelation(applicationContext, eService);
|
||||
} catch (AvailableInAnotherContextException | AlreadyPresentException e) {
|
||||
// resourceRegistryPublisher.delete(eService);
|
||||
// eService = resourceRegistryPublisher.createResource(eService);
|
||||
resourceRegistryPublisher.delete(activates);
|
||||
activates = createActivatesRelation(applicationContext, eService);
|
||||
activates = createActivatesRelation(applicationContext, eService);
|
||||
}
|
||||
eService = activates.getTarget();
|
||||
serviceStateFacet = eService.getFacets(ServiceStateFacet.class).get(0);
|
||||
return eService;
|
||||
}
|
||||
|
||||
|
||||
public void updateServiceStateFacet(ApplicationContext applicationContext) throws ResourceRegistryException {
|
||||
String state = getState(applicationContext);
|
||||
ServiceStateFacet serviceStateFacet = null;
|
||||
|
||||
List<ServiceStateFacet> serviceStateFacets = eService.getFacets(ServiceStateFacet.class);
|
||||
if (serviceStateFacets != null && serviceStateFacets.size() >= 1) {
|
||||
serviceStateFacet = serviceStateFacets.get(0);
|
||||
serviceStateFacet.setValue(state);
|
||||
serviceStateFacet = resourceRegistryPublisher.updateFacet(serviceStateFacet);
|
||||
|
||||
for (int i = 1; i < serviceStateFacets.size(); i++) {
|
||||
try {
|
||||
logger.warn("You should not be here. There are more than one {}. Anyway deleting it : {}",
|
||||
ServiceStateFacet.class.getSimpleName(), serviceStateFacets.get(i));
|
||||
resourceRegistryPublisher.deleteFacet(serviceStateFacets.get(i));
|
||||
} catch (Exception e) {
|
||||
logger.warn("Unable to delete {} which should not exists : {}",
|
||||
ServiceStateFacet.class.getSimpleName(), serviceStateFacets.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
serviceStateFacet = new ServiceStateFacetImpl();
|
||||
serviceStateFacet.setValue(state);
|
||||
serviceStateFacet = resourceRegistryPublisher.createFacet(serviceStateFacet);
|
||||
|
||||
ConsistsOf<EService, ServiceStateFacet> consistsOf = new ConsistsOfImpl<EService, ServiceStateFacet>(
|
||||
eService, serviceStateFacet, null);
|
||||
consistsOf = resourceRegistryPublisher.createConsistsOf(consistsOf);
|
||||
|
||||
}
|
||||
|
||||
// Newly created ServiceStateFacet must be added to all context
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
try {
|
||||
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
|
||||
|
||||
Set<String> startTokens = applicationContext.configuration().startTokens();
|
||||
for (String token : startTokens) {
|
||||
ContextUtility.setContextFromToken(token);
|
||||
addToContext(applicationContext);
|
||||
}
|
||||
|
||||
} catch (ResourceRegistryException e) {
|
||||
throw e;
|
||||
} finally {
|
||||
ContextUtility.resetContex();
|
||||
Thread.currentThread().setContextClassLoader(contextCL);
|
||||
}
|
||||
|
||||
serviceStateFacet.setValue(state);
|
||||
serviceStateFacet = resourceRegistryPublisher.updateFacet(serviceStateFacet);
|
||||
}
|
||||
|
||||
private Activates<HostingNode, EService> createActivatesRelation(ApplicationContext applicationContext, EService eService)
|
||||
throws ResourceRegistryException {
|
||||
|
||||
HostingNode hostingNode = applicationContext.container().properties().lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY)
|
||||
.value(HostingNodeManager.class).getHostingNode();
|
||||
|
||||
private Activates<HostingNode, EService> createActivatesRelation(ApplicationContext applicationContext,
|
||||
EService eService) throws ResourceRegistryException {
|
||||
|
||||
HostingNode hostingNode = applicationContext.container().properties()
|
||||
.lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode();
|
||||
|
||||
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
|
||||
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
|
||||
propagationConstraint.setAddConstraint(AddConstraint.propagate);
|
||||
|
@ -258,12 +225,13 @@ public class EServiceManager {
|
|||
return activates;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void removeEService(ApplicationContext applicationContext) throws ResourceRegistryException {
|
||||
try {
|
||||
resourceRegistryPublisher.delete(eService);
|
||||
} catch (ResourceRegistryException e) {
|
||||
logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(), getState(applicationContext));
|
||||
logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(),
|
||||
getState(applicationContext));
|
||||
updateServiceStateFacet(applicationContext);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,10 +38,13 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundExcep
|
|||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
|
||||
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.ContainerStateFacetImpl;
|
||||
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;
|
||||
import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl;
|
||||
|
@ -52,6 +55,7 @@ import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasVolatileM
|
|||
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.ContainerStateFacet;
|
||||
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;
|
||||
import org.gcube.resourcemanagement.model.reference.entities.facets.NetworkingFacet;
|
||||
|
@ -62,6 +66,7 @@ import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasPers
|
|||
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasVolatileMemory;
|
||||
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy;
|
||||
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;
|
||||
|
@ -70,27 +75,29 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class HostingNodeManager {
|
||||
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(HostingNodeManager.class);
|
||||
|
||||
|
||||
public static final String MEMORY_TYPE = "memoryType";
|
||||
public static final String MEMORY_TYPE_RAM = "RAM";
|
||||
public static final String MEMORY_TYPE_JVM = "JVM";
|
||||
public static final String JVM_MAX_MEMORY = "jvmMaxMemory";
|
||||
|
||||
public static final String MESSAGE = "message";
|
||||
|
||||
|
||||
private ContainerContext containerContext;
|
||||
private ResourceRegistryPublisher resourceRegistryPublisher;
|
||||
private HostingNode hostingNode;
|
||||
|
||||
public HostingNodeManager() {
|
||||
|
||||
public HostingNodeManager(ContainerContext containerContext) {
|
||||
this.containerContext = containerContext;
|
||||
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
|
||||
}
|
||||
|
||||
|
||||
public HostingNode getHostingNode() {
|
||||
return hostingNode;
|
||||
}
|
||||
|
||||
|
||||
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
boolean added = resourceRegistryPublisher.addResourceToCurrentContext(hostingNode);
|
||||
if (added) {
|
||||
|
@ -101,8 +108,9 @@ public class HostingNodeManager {
|
|||
ContextUtility.getCurrentContextName());
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFromContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
|
||||
public void removeFromContext()
|
||||
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
boolean removed = false;
|
||||
removed = resourceRegistryPublisher.removeResourceFromCurrentContext(hostingNode);
|
||||
if (removed) {
|
||||
|
@ -113,13 +121,10 @@ public class HostingNodeManager {
|
|||
ContextUtility.getCurrentContextName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public HostingNode updateFacets(ContainerContext containerContext) throws ResourceRegistryException {
|
||||
logger.debug("Updating HostingNode");
|
||||
|
||||
ContainerConfiguration containerConfiguration = containerContext.configuration();
|
||||
|
||||
public HostingNode updateFacets() throws ResourceRegistryException {
|
||||
logger.debug("Updating {}", HostingNode.NAME);
|
||||
|
||||
ContainerStateFacet containerStateFacet = null;
|
||||
MemoryFacet ramFacet = null;
|
||||
MemoryFacet jvmMemoryFacet = null;
|
||||
|
@ -131,7 +136,7 @@ public class HostingNodeManager {
|
|||
for (ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfList) {
|
||||
if (c.getTarget() instanceof ContainerStateFacet) {
|
||||
containerStateFacet = (ContainerStateFacet) c.getTarget();
|
||||
containerStateFacet = getContainerStateFacet(containerStateFacet, containerContext);
|
||||
containerStateFacet = getContainerStateFacet(containerStateFacet);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -153,7 +158,7 @@ public class HostingNodeManager {
|
|||
|
||||
if (c instanceof HasPersistentMemory) {
|
||||
disk = (MemoryFacet) c.getTarget();
|
||||
disk = getDiskSpace(disk, containerConfiguration);
|
||||
disk = getDiskSpace(disk);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -182,16 +187,53 @@ public class HostingNodeManager {
|
|||
return hostingNode;
|
||||
}
|
||||
|
||||
private String getState(ContainerContext containerContext) {
|
||||
public HostingNode setDown() throws ResourceRegistryException {
|
||||
logger.debug("Setting {} down", HostingNode.NAME);
|
||||
|
||||
ContainerStateFacet containerStateFacet = 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 ContainerStateFacet) {
|
||||
containerStateFacet = (ContainerStateFacet) c.getTarget();
|
||||
containerStateFacet = getContainerStateFacet(containerStateFacet);
|
||||
continue;
|
||||
}
|
||||
consistsOfToRemove.add(c);
|
||||
}
|
||||
|
||||
// Resource Update has effect only on specified facet.
|
||||
// Removing the one that have not to be changed
|
||||
consistsOfList.removeAll(consistsOfToRemove);
|
||||
|
||||
try {
|
||||
hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
|
||||
} catch (NotFoundException e) {
|
||||
/* Update failed trying to recreate it */
|
||||
// ReAdding the removed relations to recreate all
|
||||
consistsOfList.addAll(consistsOfToRemove);
|
||||
hostingNode = resourceRegistryPublisher.createResource(hostingNode);
|
||||
} catch (AvailableInAnotherContextException e) {
|
||||
addToContext();
|
||||
hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
|
||||
} catch (ResourceRegistryException e) {
|
||||
logger.error("error trying to publish hosting node", e);
|
||||
}
|
||||
return hostingNode;
|
||||
}
|
||||
|
||||
private String getState() {
|
||||
return containerContext.lifecycle().state().remoteForm().toLowerCase();
|
||||
}
|
||||
|
||||
private HostingNode instantiateHostingNode(ContainerContext containerContext) {
|
||||
|
||||
private HostingNode instantiateHostingNode() {
|
||||
logger.info("Creating {}", HostingNode.NAME);
|
||||
|
||||
ContainerConfiguration containerConfiguration = containerContext.configuration();
|
||||
String id = containerContext.id();
|
||||
|
||||
|
||||
UUID uuid = UUID.fromString(id);
|
||||
HostingNode hostingNode = new HostingNodeImpl();
|
||||
Header header = new HeaderImpl(uuid);
|
||||
|
@ -217,17 +259,42 @@ public class HostingNodeManager {
|
|||
hostingNode.addFacet(cpuFacet);
|
||||
}
|
||||
|
||||
SoftwareFacet softwareFacet = new SoftwareFacetImpl();
|
||||
SoftwareFacet osSoftwareFacet = new SoftwareFacetImpl();
|
||||
OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
|
||||
softwareFacet.setGroup(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name"));
|
||||
softwareFacet.setName(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch"));
|
||||
softwareFacet.setVersion(mxbean.getVersion()); // softwareFacet.setName(System.getProperty("os.version"));
|
||||
hostingNode.addFacet(softwareFacet);
|
||||
osSoftwareFacet.setGroup(mxbean.getName()); // softwareFacet.setGroup(System.getProperty("os.name"));
|
||||
osSoftwareFacet.setName(mxbean.getArch()); // softwareFacet.setName(System.getProperty("os.arch"));
|
||||
osSoftwareFacet.setVersion(mxbean.getVersion()); // softwareFacet.setName(System.getProperty("os.version"));
|
||||
hostingNode.addFacet(osSoftwareFacet);
|
||||
|
||||
SmartGearsConfiguration config = ProviderFactory.provider().smartgearsConfiguration();
|
||||
SoftwareFacet smartgearsSoftwareFacet = new SoftwareFacetImpl();
|
||||
smartgearsSoftwareFacet.setGroup("gCube");
|
||||
smartgearsSoftwareFacet.setName("SmartGears");
|
||||
smartgearsSoftwareFacet.setVersion(config.version());
|
||||
hostingNode.addFacet(smartgearsSoftwareFacet);
|
||||
|
||||
SoftwareFacet smartgearsDistributionSoftwareFacet = new SoftwareFacetImpl();
|
||||
smartgearsDistributionSoftwareFacet.setGroup("gCube");
|
||||
smartgearsDistributionSoftwareFacet.setName("SmartGearsDistribution");
|
||||
String smartgearsDistributionVersion = containerConfiguration.properties().get("SmartGearsDistribution");
|
||||
smartgearsDistributionSoftwareFacet.setVersion(smartgearsDistributionVersion);
|
||||
smartgearsDistributionSoftwareFacet.setAdditionalProperty("publication-frequency",
|
||||
String.valueOf(containerConfiguration.publicationFrequency()));
|
||||
hostingNode.addFacet(smartgearsDistributionSoftwareFacet);
|
||||
|
||||
|
||||
SoftwareFacet javaSoftwareFacet = new SoftwareFacetImpl();
|
||||
javaSoftwareFacet.setGroup(System.getProperty("java.vendor"));
|
||||
javaSoftwareFacet.setName("Java");
|
||||
javaSoftwareFacet.setVersion(System.getProperty("java.version"));
|
||||
javaSoftwareFacet.setAdditionalProperty("java.vendor.url", System.getProperty("java.vendor.url"));
|
||||
javaSoftwareFacet.setAdditionalProperty("java.specification.version", System.getProperty("java.specification.version"));
|
||||
hostingNode.addFacet(javaSoftwareFacet);
|
||||
|
||||
SimplePropertyFacet simplePropertyFacet = addEnvironmentVariables(containerConfiguration);
|
||||
hostingNode.addFacet(simplePropertyFacet);
|
||||
|
||||
ContainerStateFacet containerStateFacet = getContainerStateFacet(null, containerContext);
|
||||
ContainerStateFacet containerStateFacet = getContainerStateFacet(null);
|
||||
hostingNode.addFacet(containerStateFacet);
|
||||
|
||||
MemoryFacet ramFacet = getRamInfo(null);
|
||||
|
@ -242,25 +309,38 @@ public class HostingNodeManager {
|
|||
hasVolatileJVMMemory.setAdditionalProperty(MEMORY_TYPE, MEMORY_TYPE_JVM);
|
||||
hostingNode.addFacet(hasVolatileJVMMemory);
|
||||
|
||||
MemoryFacet diskFacet = getDiskSpace(null, containerConfiguration);
|
||||
MemoryFacet diskFacet = getDiskSpace(null);
|
||||
HasPersistentMemory<HostingNode, MemoryFacet> hasPersistentMemory = new HasPersistentMemoryImpl<HostingNode, MemoryFacet>(
|
||||
hostingNode, diskFacet, null);
|
||||
hostingNode.addFacet(hasPersistentMemory);
|
||||
|
||||
// TODO Add a Reference to Site
|
||||
/*
|
||||
* node.profile().newSite().country(cfg.site().country ()).location(cfg.site
|
||||
* ().location()) .latitude(cfg .site().latitude()).longitude(cfg.site
|
||||
* ().longitude ()).domain(domainIn(cfg.hostname()));
|
||||
*/
|
||||
|
||||
|
||||
LocationFacet locationFacet = new LocationFacetImpl();
|
||||
Site site = containerConfiguration.site();
|
||||
locationFacet.setCountry(site.country());
|
||||
locationFacet.setLocation(site.location());
|
||||
locationFacet.setLatitude(site.latitude());
|
||||
locationFacet.setLongitude(site.longitude());
|
||||
hostingNode.addFacet(locationFacet);
|
||||
|
||||
logger.info("hostingNode instanciated");
|
||||
return hostingNode;
|
||||
}
|
||||
|
||||
public HostingNode createHostingNode(ContainerContext containerContext) throws ResourceRegistryException {
|
||||
|
||||
public HostingNode createHostingNode() throws ResourceRegistryException {
|
||||
try {
|
||||
hostingNode = instantiateHostingNode(containerContext);
|
||||
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 = instantiateHostingNode();
|
||||
hostingNode = resourceRegistryPublisher.createResource(hostingNode);
|
||||
} catch (AvailableInAnotherContextException | AlreadyPresentException e) {
|
||||
resourceRegistryPublisher.delete(hostingNode);
|
||||
|
@ -269,15 +349,16 @@ public class HostingNodeManager {
|
|||
return hostingNode;
|
||||
}
|
||||
|
||||
private ContainerStateFacet getContainerStateFacet(ContainerStateFacet containerStateFacet, ContainerContext containerContext) {
|
||||
if(containerStateFacet==null) {
|
||||
private ContainerStateFacet getContainerStateFacet(ContainerStateFacet containerStateFacet) {
|
||||
if (containerStateFacet == null) {
|
||||
containerStateFacet = new ContainerStateFacetImpl();
|
||||
}
|
||||
containerStateFacet.setValue(getState(containerContext));
|
||||
String state = getState();
|
||||
containerStateFacet.setValue(state);
|
||||
return containerStateFacet;
|
||||
}
|
||||
|
||||
private MemoryFacet getDiskSpace(MemoryFacet memoryFacet, ContainerConfiguration containerConfiguration) {
|
||||
private MemoryFacet getDiskSpace(MemoryFacet memoryFacet) {
|
||||
if (memoryFacet == null) {
|
||||
memoryFacet = new MemoryFacetImpl();
|
||||
}
|
||||
|
@ -286,7 +367,7 @@ public class HostingNodeManager {
|
|||
long total = 0;
|
||||
try {
|
||||
FileStore fileStore = Files
|
||||
.getFileStore(Paths.get(containerConfiguration.persistence().location()));
|
||||
.getFileStore(Paths.get(containerContext.configuration().persistence().location()));
|
||||
free = fileStore.getUsableSpace() / 1048576; // 1048576 = 1024*1024
|
||||
// user to convert
|
||||
// bytes in MByte
|
||||
|
@ -306,7 +387,7 @@ public class HostingNodeManager {
|
|||
}
|
||||
|
||||
private static final long BYTE_TO_MB = 1024 * 1024;
|
||||
|
||||
|
||||
private MemoryFacet getRamInfo(MemoryFacet memoryFacet) {
|
||||
if (memoryFacet == null) {
|
||||
memoryFacet = new MemoryFacetImpl();
|
||||
|
@ -372,9 +453,8 @@ public class HostingNodeManager {
|
|||
private static String sanitizeKey(String key) {
|
||||
return key.trim().replace(" ", "_");
|
||||
}
|
||||
|
||||
private SimplePropertyFacet addEnvironmentVariables(ContainerConfiguration containerConfiguration) {
|
||||
|
||||
private SimplePropertyFacet addEnvironmentVariables(ContainerConfiguration containerConfiguration) {
|
||||
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.putAll(containerConfiguration.properties());
|
||||
|
@ -391,18 +471,9 @@ public class HostingNodeManager {
|
|||
|| (varname.compareToIgnoreCase("LS_COLORS") == 0)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
simplePropertyFacet.setAdditionalProperty(sanitizeKey(entry.getKey()), entry.getValue());
|
||||
|
||||
}
|
||||
|
||||
simplePropertyFacet.setAdditionalProperty("Java", System.getProperty("java.version"));
|
||||
SmartGearsConfiguration config = ProviderFactory.provider().smartgearsConfiguration();
|
||||
|
||||
simplePropertyFacet.setAdditionalProperty("SmartGears", config.version());
|
||||
simplePropertyFacet.setAdditionalProperty("ghn-update-interval-in-secs",
|
||||
String.valueOf(containerConfiguration.publicationFrequency()));
|
||||
|
||||
return simplePropertyFacet;
|
||||
}
|
||||
|
||||
|
@ -419,7 +490,7 @@ public class HostingNodeManager {
|
|||
return hostname;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final String CPU_PROCESSOR = "processor";
|
||||
public static final String CPU_VENDOR_ID = "vendor_id";
|
||||
public static final String CPU_MODEL_NAME = "model name";
|
||||
|
|
Loading…
Reference in New Issue