Changed the way to manage hosts creation

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/resource-registry-handlers@148268 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-05-03 13:11:52 +00:00
parent 7453f3239d
commit b5416a1731
1 changed files with 42 additions and 77 deletions

View File

@ -48,15 +48,12 @@ import org.gcube.informationsystem.model.entity.resource.EService;
import org.gcube.informationsystem.model.entity.resource.HostingNode;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.model.relation.isrelatedto.Hosts;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.Direction;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
@ -256,53 +253,21 @@ public class EServiceManager extends ApplicationLifecycleHandler {
HostingNode hostingNode = applicationContext.container().properties().lookup(Constants.HOSTING_NODE_PROPERTY).value(HostingNode.class);
addHostingNodeToCurrentContext();
if (hostingNode != null) {
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
Hosts<HostingNode, EService> hosts = null;
try {
List<Resource> resources = resourceRegistryClient.getInstancesFromEntity(Hosts.NAME, false, hostingNode.getHeader().getUUID(), Direction.out);
for(Resource resource : resources){
if(resource instanceof HostingNode && resource.getHeader().getUUID().compareTo(hostingNode.getHeader().getUUID())==0){
List<IsRelatedTo<? extends Resource, ? extends Resource>> isRelatedToList = resource.getIsRelatedTo();
for (IsRelatedTo<? extends Resource, ? extends Resource> isRelatedTo : isRelatedToList) {
if (isRelatedTo instanceof Hosts) {
if (isRelatedTo.getTarget().getHeader().getUUID()
.compareTo(eService.getHeader().getUUID()) == 0) {
hosts = (Hosts<HostingNode, EService>) isRelatedTo;
break;
}
}
}
}
}
} catch (Exception e) {
logger.warn("Error while trying to retrieve Hosts relation", e);
}
if (hosts == null) {
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint
.setRemoveConstraint(RemoveConstraint.cascade);
propagationConstraint
.setAddConstraint(AddConstraint.propagate);
hosts = new HostsImpl<>(hostingNode, eService,
propagationConstraint);
try {
hosts = resourceRegistryPublisher.createIsRelatedTo(
Hosts.class, hosts);
} catch (ResourceNotFoundException e) {
logger.error("THIS IS REALLY STRANGE. YOU SHOULD NE BE HERE. Error while creating {}.", hosts, e);
} catch (ResourceRegistryException e) {
logger.error("Error while creating {}", hosts, e);
}
}
Hosts<HostingNode, EService> hosts = null;
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
propagationConstraint.setAddConstraint(AddConstraint.propagate);
hosts = new HostsImpl<>(hostingNode, eService, propagationConstraint);
try {
hosts = resourceRegistryPublisher.createIsRelatedTo(Hosts.class, hosts);
} catch (ResourceNotFoundException e) {
logger.error("THIS IS REALLY STRANGE. YOU SHOULD NE BE HERE. Error while creating {}.", hosts, e);
} catch (ResourceRegistryException e) {
logger.error("Error while creating {}", hosts, e);
}
}
private EService getEService() throws ResourceRegistryException {
@ -332,35 +297,36 @@ public class EServiceManager extends ApplicationLifecycleHandler {
ResourceRegistryPublisher resourceRegistryPublisher,
EService eService) throws ResourceRegistryException {
try {
eService = resourceRegistryPublisher.createResource(EService.class,
eService);
} catch (ResourceAlreadyPresentException e) {
ResourceRegistryClient registryClient = ResourceRegistryClientFactory.create();
eService = registryClient.getInstance(EService.class, eService.getHeader().getUUID());
String state = applicationContext.lifecycle().state().remoteForm().toLowerCase();
logger.debug("Moving app {} to {}", applicationContext.name(), state);
createOrUpdateServiceStateFacet(eService, state);
} catch (ResourceAvailableInAnotherContextException e) {
// Adding the HostingNode I also add the EService thanks to propagationConstraint
String token = SecurityTokenProvider.instance.get();
// If hosts was not created we need to add eService and HostingNode, otherwise
// addHostingnode is sufficient. Because we cannot be sure of it we invoke both
addToContext(eService, token);
addHostingNodeToCurrentContext();
ResourceRegistryClient registryClient = ResourceRegistryClientFactory.create();
eService = registryClient.getInstance(EService.class, eService.getHeader().getUUID());
}
ResourceRegistryClient registryClient = ResourceRegistryClientFactory.create();
UUID eServiceUUID = eService.getHeader().getUUID();
try {
createHostsRelation(eService, resourceRegistryPublisher);
} catch (Exception ex) {
String error = String.format(
"Unable to Create %s relation from % to % (%s : %s)",
Hosts.NAME, HostingNode.NAME, EService.NAME,
applicationContext.name(), eService.getHeader().getUUID());
logger.error(error, ex);
throw new ResourceRegistryException(error, ex);
registryClient.exists(EService.class, eServiceUUID);
} catch (ResourceAvailableInAnotherContextException e) {
addHostingNodeToCurrentContext();
try {
eService = registryClient.getInstance(EService.class, eServiceUUID);
}catch (ResourceAvailableInAnotherContextException e1) {
String token = SecurityTokenProvider.instance.get();
addToContext(eService, token);
logger.warn("Trying to add hosting node to current context did not affect the eService with UUID {}. The {} Relation was not created of the propagation constaint were modified. Please check it", eServiceUUID, Hosts.NAME);
} catch (ResourceRegistryException e1) {
throw e1;
}
} catch (ResourceNotFoundException e) {
try {
createHostsRelation(eService, resourceRegistryPublisher);
} catch (Exception ex) {
String error = String.format(
"Unable to Create %s relation from % to % (%s : %s)",
Hosts.NAME, HostingNode.NAME, EService.NAME,
applicationContext.name(), eService.getHeader().getUUID());
logger.error(error, ex);
throw new ResourceRegistryException(error, ex);
}
} catch (ResourceRegistryException e) {
throw e;
}
return eService;
@ -582,7 +548,6 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
}
@SuppressWarnings("unchecked")
private void createOrUpdateServiceStateFacet(EService eService, String state) throws ResourceRegistryException {
String previousToken = SecurityTokenProvider.instance.get();