From 8850af0ead16a6156816311d84e5a3f1d8d6a5df Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Tue, 26 Jun 2012 02:47:48 +0000 Subject: [PATCH] Various fixings/improvements on scope creation git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vre-management/ResourceManager@55962 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../impl/deployment/DeployerReport.java | 2 +- .../impl/state/InstanceState.java | 31 +++++-- .../impl/state/PublishedScopeResource.java | 14 ++- .../impl/state/ResourceMap.java | 88 +++++++++++++------ .../resourcemanager/porttypes/Reporting.java | 7 +- .../porttypes/ResourceBinder.java | 12 ++- .../porttypes/ScopeController.java | 25 +++++- .../resourcemanager/porttypes/ScopeUtils.java | 17 ++++ 8 files changed, 144 insertions(+), 52 deletions(-) diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/deployment/DeployerReport.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/deployment/DeployerReport.java index 34b5e9f..990a5e5 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/deployment/DeployerReport.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/deployment/DeployerReport.java @@ -155,7 +155,7 @@ public class DeployerReport { break ; case KXmlParser.END_TAG: if (parser.getName().equals("NewInstance")){ try { - ScopedRunningInstance ri = (ScopedRunningInstance) ScopedResourceFactory.newResource(this.instanceState.getResourceList(this.targetScope),instance.getRIID(), GCUBERunningInstance.TYPE); + ScopedRunningInstance ri = (ScopedRunningInstance) ScopedResourceFactory.newResource(this.instanceState.getState(this.targetScope),instance.getRIID(), GCUBERunningInstance.TYPE); ri.setHostedON(this.host); ri.setJointTime(Calendar.getInstance().getTime()); ri.setStatus(STATUS.PUBLISHED); diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java index 7e5309d..b70df72 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/InstanceState.java @@ -34,7 +34,7 @@ public final class InstanceState extends GCUBEWSResource { @SuppressWarnings("unchecked") @Override protected void initialise(Object... params) throws Exception { - resources.loadResources(); + resources.initializeScopes(); //initialize the scope states for (ScopeState state : resources.getAllStates()) { //nodes @@ -90,26 +90,32 @@ public final class InstanceState extends GCUBEWSResource { * Adds a new {@link Session} to the service's state * * @param session the session to add + * @throws NoSuchResourceException */ - public void addSession(GCUBEScope scope, Session session) { - this.getResourceList(scope).setLastSession(session); + public void addSession(GCUBEScope scope, Session session) throws NoSuchResourceException { + this.getState(scope).setLastSession(session); id2session.put(session.getId(), session); } /** - * Gets the list of {@link ScopedResource}s + * Gets the state for the given scope * @return the list of scoped resources + * @throws NoSuchResourceException */ - public ScopeState getResourceList(GCUBEScope scope) { - return resources.getState(scope); + public ScopeState getState(GCUBEScope scope) throws NoSuchResourceException { + ScopeState state = resources.getScopeState(scope); + if (state == null) + return resources.loadState(scope); + else + return state; } /** * Disposes the scope */ public void dispose(GCUBEScope scope) { - ScopeState scopeState = resources.getState(scope); + ScopeState scopeState = resources.getScopeState(scope); Set allResources = new HashSet(); for (ScopedResource resource : scopeState.getAllResources()) { allResources.add(resource); @@ -127,4 +133,15 @@ public final class InstanceState extends GCUBEWSResource { public PublishedScopeResource getPublishedScopeResource(GCUBEScope scope) throws NoSuchResourceException { return resources.getPublishedScopeResource(scope); } + + + /** + * Creates a new state for the given scope + * @param scope + * @throws NoSuchResourceException + * @throws Exception + */ + public void create(GCUBEScope scope) throws NoSuchResourceException, Exception { + resources.createState(scope); + } } diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java index 313c3fa..b6ea78a 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/PublishedScopeResource.java @@ -64,7 +64,6 @@ public class PublishedScopeResource { try { publishedResourceList = new PublishedResourceList(); this.load(); - //this.store();//to make sure we have locally the latest resource } catch (Exception e) { logger.error("Failed to reload the Scope Resource"); throw e; @@ -199,7 +198,7 @@ public class PublishedScopeResource { public synchronized void publish() throws Exception { if (this.dismissed) return; this.resource.setBody(this.prepareBody()); - logger.trace("Publishing Scope Resource: \n" + this.toString()); + //logger.trace("Publishing Scope Resource: \n" + this.toString()); //this.store(); ISPublisher publisher = GHNContext.getImplementation(ISPublisher.class); if (this.loaded) { @@ -216,7 +215,7 @@ public class PublishedScopeResource { */ public synchronized void dismiss() throws Exception { this.resource.setBody(this.prepareBody()); - logger.trace("Unpublishing Scope Resource: \n" + this.toString()); + //logger.trace("Unpublishing Scope Resource: \n" + this.toString()); //this.store(); ISPublisher publisher = GHNContext.getImplementation(ISPublisher.class); publisher.removeGCUBEResource(this.resource.getID(), this.resource.getType(), this.scope, ServiceContext.getContext()); @@ -341,9 +340,8 @@ public class PublishedScopeResource { try { ISClient client = GHNContext.getImplementation(ISClient.class); GCUBEGenericResourceQuery query = client.getQuery(GCUBEGenericResourceQuery.class); - query.addAtomicConditions(new AtomicCondition("//SecondaryType", determineSecondaryType()), - new AtomicCondition("//Body/Scope", scope.toString())); - //logger.trace(query.toString()); + query.addAtomicConditions(new AtomicCondition("//Profile/SecondaryType", determineSecondaryType()), + new AtomicCondition("//Profile/Body/Scope", scope.toString())); List results = client.execute(query, scope); if ((results != null) && (results.size() > 0)) { this.resource = results.get(0); @@ -353,7 +351,7 @@ public class PublishedScopeResource { return true; } else logger.warn("Unable to load the resource for "+ this.scope.toString() + " from the IS"); - } catch (Exception e) {logger.warn("Published resource for "+ this.scope.toString()+ " does not exist on the IS yet", e);} + } catch (Exception e) {logger.warn("Published resource for "+ this.scope.toString()+ " does not exist on the IS", e);} return false; } @@ -405,7 +403,7 @@ public class PublishedScopeResource { * @throws Exception if the load fails */ public synchronized void loadFromLocalState(ScopeState scopeState) throws Exception { - logger.debug("LoadFromLocalState: Loading the published state from the local scope state"); + logger.debug("LoadFromLocalState: Loading the published state for " + this.scope.getName() + " from the local file system"); //reloading is needed in order to reuse the resource ID and //to avoid to cancel the old Generic Resource from the IS (plus reusing any other information do not overridden) this.reload(); diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/ResourceMap.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/ResourceMap.java index 4798fa8..d2a5060 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/ResourceMap.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/impl/state/ResourceMap.java @@ -1,6 +1,6 @@ package org.gcube.vremanagement.resourcemanager.impl.state; -import java.io.IOException; + import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -30,40 +30,48 @@ class ResourceMap { ResourceMap() {} /** - * Loads the resources for all the scopes belonging the current instance - * @throws Exception + * Initializes the resources for all the scopes belonging the current instance + * @throws Exception if any of the initializations fails */ - void loadResources() throws Exception { + void initializeScopes() throws Exception { Collection scopes = ServiceContext.getContext().getInstance().getScopes().values(); for (GCUBEScope scope : scopes) { - ScopeState scopeState = new ScopeState(); try { - logger.info("initializing scope "+ scope.getName() + " from the local file system..."); - Serializer.load(scopeState,scope); - getPublishedScopeResource(scope).loadFromLocalState(scopeState); - states.put(scope.getName(), scopeState); - } catch (IOException e) { - logger.warn("the local serialized scope is not available"); - logger.info("loading the instance state from the IS..."); - if (getPublishedScopeResource(scope).load()) { - logger.info("scope "+ scope.getName() + " harvested from the IS"); - scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled()); - //synch scopeState w/ IS list - getPublishedScopeResource(scope).to(scopeState); - } else { - logger.info("empty scope "+ scope.getName() + " created"); - //we assume that if we are running on a secure ghn, we are in a secure Scope - scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled()); - getPublishedScopeResource(scope).loadFromLocalState(scopeState); - - } - states.put(scope.getName(), scopeState); + states.put(scope.getName(), this.loadState(scope)); + } catch (Exception e) { + states.put(scope.getName(), this.createState(scope)); } } } /** - * Gets the {@link PublishedScopeResource} + * Loads the scope state from the local file system or the IS + * @param scope + * @throws Exception + */ + ScopeState loadState(GCUBEScope scope) throws NoSuchResourceException { + ScopeState scopeState = new ScopeState(); + try { + logger.info("initializing scope "+ scope.getName() + " from the local file system..."); + Serializer.load(scopeState,scope); + getPublishedScopeResource(scope).loadFromLocalState(scopeState); + } catch (Exception e) { + logger.warn("local serialized scope is not available"); + logger.info("loading the instance state from the IS..."); + if (getPublishedScopeResource(scope).load()) { + logger.info("scope "+ scope.getName() + " successfully harvested from the IS"); + scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled()); + //synch scopeState w/ IS list + getPublishedScopeResource(scope).to(scopeState); + } else + throw new NoSuchResourceException(); + + } + return scopeState; + } + + /** + * Gets the {@link PublishedScopeResource} for the state * * @return the {@link PublishedScopeResource} * @throws NoSuchResourceException @@ -77,15 +85,37 @@ class ResourceMap { } /** - * All scope states actually handled + * All scope states actually handled by the instance * @return the states */ - Collection getAllStates() { + Collection getAllStates() { return states.values(); } - public ScopeState getState(GCUBEScope scope) { + /** + * The state of the given scope + * @param scope + * @return the state or null if the state does not exist + */ + ScopeState getScopeState(GCUBEScope scope) { return states.get(scope.getName()); } + + /** + * Create a new resource for the given scope + * @param scope + * @return + * @throws NoSuchResourceException + * @throws Exception + */ + ScopeState createState(GCUBEScope scope) throws NoSuchResourceException, Exception { + logger.info("empty scope for "+ scope.getName() + " is going to be created"); + ScopeState scopeState = new ScopeState(); + //we assume that if we are running on a secure ghn, we are in a secure Scope + scopeState.initialize(scope, scope.getName(), GHNContext.getContext().isSecurityEnabled()); + getPublishedScopeResource(scope).loadFromLocalState(scopeState); + states.put(scope.getName(), scopeState); + return scopeState; + } } diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/Reporting.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/Reporting.java index 9bd6188..cb9203d 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/Reporting.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/Reporting.java @@ -15,6 +15,7 @@ import org.gcube.vremanagement.resourcemanager.impl.state.PublishedScopeResource import org.globus.wsrf.NoSuchResourceException; import org.globus.wsrf.ResourceException; +import org.gcube.vremanagement.resourcemanager.stubs.common.InvalidScopeFaultType; import org.gcube.vremanagement.resourcemanager.stubs.reporting.*; /** @@ -34,6 +35,10 @@ public class Reporting extends ResourceManagerPortType { */ public void sendReport(SendReportParameters reportMessage) throws GCUBEFault { GCUBEScope targetScope = ScopeUtils.validate(reportMessage.getTargetScope()); + if (!ScopeUtils.exists(targetScope, this)) { + logger.warn("Target scope " + targetScope.toString()+ " does not exists and cannot be modified"); + throw new InvalidScopeFaultType(); + } logger.info("Received session for session " + reportMessage.getCallbackID()); logger.trace("Report content: \n" + reportMessage.getReport()); try { @@ -60,7 +65,7 @@ public class Reporting extends ResourceManagerPortType { } session.addDeployedInstances(dreport.getInstances()); //add the newly generated RIs to the Scope State - this.getInstanceState().getResourceList(targetScope).addResources(resources); + this.getInstanceState().getState(targetScope).addResources(resources); } resource.publish(); session.save(); diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ResourceBinder.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ResourceBinder.java index b426458..59f8cd3 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ResourceBinder.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ResourceBinder.java @@ -36,11 +36,15 @@ public class ResourceBinder extends ResourceManagerPortType { public synchronized String addResources(AddResourcesParameters resourceList) throws ResourcesCreationFaultType, GCUBEFault { logger.debug("AddResources operation invoked in scope " + ServiceContext.getContext().getScope().getName()); GCUBEScope targetScope = ScopeUtils.validate(resourceList.getTargetScope()); + if (!ScopeUtils.exists(targetScope, this)) { + logger.warn("Target scope " + targetScope.toString()+ " does not exists and cannot be modified"); + throw new InvalidScopeFaultType(); + } try { //checks the input scope Session report = new Session(UUIDGenFactory.getUUIDGen().nextUUID(),OPERATION.AddResources, targetScope); this.getInstanceState().addSession(targetScope,report); - new AddResourcesOperator(this.getInstanceState().getResourceList(targetScope),new OperatorConfig(report, this.getInstanceState().getResourceList(targetScope), targetScope),resourceList).run(); + new AddResourcesOperator(this.getInstanceState().getState(targetScope),new OperatorConfig(report, this.getInstanceState().getState(targetScope), targetScope),resourceList).run(); //resource.publish(); //returns the session ID, it can be used to invoke the getReport operation return report.getId(); @@ -61,10 +65,14 @@ public class ResourceBinder extends ResourceManagerPortType { */ public synchronized String removeResources(RemoveResourcesParameters resourceList) throws ResourcesRemovalFaultType, InvalidScopeFaultType { GCUBEScope targetScope = ScopeUtils.validate(resourceList.getTargetScope()); + if (!ScopeUtils.exists(targetScope, this)) { + logger.warn("Target scope " + targetScope.toString()+ " does not exists and cannot be modified"); + throw new InvalidScopeFaultType(); + } try { Session report = new Session(UUIDGenFactory.getUUIDGen().nextUUID(), OPERATION.RemoveResources, targetScope); this.getInstanceState().addSession(targetScope,report); - new RemoveResourcesOperator(this.getInstanceState().getResourceList(targetScope), new OperatorConfig(report, this.getInstanceState().getResourceList(targetScope), targetScope),resourceList).run(); + new RemoveResourcesOperator(this.getInstanceState().getState(targetScope), new OperatorConfig(report, this.getInstanceState().getState(targetScope), targetScope),resourceList).run(); return report.getId(); } catch (IllegalScopeException ise){ logger.error("The target scope (" + resourceList.getTargetScope() + ") is not valid or null or not joined to this instance", ise); diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeController.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeController.java index aa22059..48ead3d 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeController.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeController.java @@ -14,6 +14,7 @@ import org.gcube.vremanagement.resourcemanager.impl.state.PublishedScopeResource import org.gcube.vremanagement.resourcemanager.impl.state.PublishedScopeResource.UnknownScopeOptionException; import org.gcube.vremanagement.resourcemanager.impl.state.Session.OPERATION; import org.globus.wsrf.NoSuchResourceException; +import org.globus.wsrf.ResourceException; import org.gcube.vremanagement.resourcemanager.stubs.common.InvalidScopeFaultType; import org.gcube.vremanagement.resourcemanager.stubs.scontroller.*; @@ -37,6 +38,10 @@ public class ScopeController extends ResourceManagerPortType { public synchronized String disposeScope(String targetScope) throws InvalidScopeFaultType, GCUBEFault { logger.info("Dispose Scope invoked... the entire scope is going to be thrown away!!"); GCUBEScope scope = ScopeUtils.validate(targetScope.trim()); + if (!ScopeUtils.exists(scope, this)) { + logger.warn("Target scope " + scope.toString()+ " does not exists and cannot be disposed"); + throw new InvalidScopeFaultType(); + } ScopeUtils.removeFromInstance(scope); try { scope.getServiceMap(); @@ -47,7 +52,7 @@ public class ScopeController extends ResourceManagerPortType { try { Session report = new Session(UUIDGenFactory.getUUIDGen().nextUUID(), OPERATION.Dispose,scope); this.getInstanceState().addSession(scope,report); - new DisposeScopeOperator(new OperatorConfig(report, this.getInstanceState().getResourceList(scope), scope)).run(); + new DisposeScopeOperator(new OperatorConfig(report, this.getInstanceState().getState(scope), scope)).run(); this.getInstanceState().getPublishedScopeResource(scope).dismiss(); return report.getId(); } catch (NoSuchResourceException e) { @@ -63,8 +68,11 @@ public class ScopeController extends ResourceManagerPortType { public synchronized void createScope(CreateScopeParameters params) throws InvalidScopeFaultType, InvalidOptionsFaultType, GCUBEFault { - GCUBEScope scope = ScopeUtils.validate(params.getTargetScope().trim()); - logger.info("Creating the new Scope " + scope.getName()); + GCUBEScope scope = ScopeUtils.validate(params.getTargetScope().trim()); + if (ScopeUtils.exists(scope, this)) { + logger.warn("Target scope " + scope.toString()+ " already exits and cannot be re-created"); + throw new InvalidScopeFaultType(); + } try { scope.getServiceMap(); } catch (MalformedScopeExpressionException e) { @@ -74,7 +82,12 @@ public class ScopeController extends ResourceManagerPortType { params.getServiceMap(); } //String map = params.getServiceMap(); - ScopeUtils.addToInstance(scope); + try { + this.getInstanceState().create(scope); + ScopeUtils.addToInstance(scope); + } catch (Exception e) { + e.printStackTrace(); + } this.changeScopeOptions(params.getOptionsParameters()); } @@ -87,6 +100,10 @@ public class ScopeController extends ResourceManagerPortType { public void changeScopeOptions(OptionsParameters options) throws InvalidOptionsFaultType, InvalidOptionsFaultType, GCUBEFault { PublishedScopeResource scoperesource = null; GCUBEScope scope = ScopeUtils.validate(options.getTargetScope().trim()); + if (!ScopeUtils.exists(scope, this)) { + logger.warn("Target scope " + scope.toString()+ " does not exists and cannot be modified"); + throw new InvalidScopeFaultType(); + } try { scoperesource = this.getInstanceState().getPublishedScopeResource(scope); } catch (NoSuchResourceException e) { diff --git a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeUtils.java b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeUtils.java index 2de06c9..0e37297 100644 --- a/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeUtils.java +++ b/resource-manager-service/src/main/java/org/gcube/vremanagement/resourcemanager/porttypes/ScopeUtils.java @@ -8,6 +8,8 @@ import java.util.Collection; import org.gcube.common.core.scope.GCUBEScope; import org.gcube.common.core.scope.GCUBEScopeManager.IllegalScopeException; import org.gcube.vremanagement.resourcemanager.impl.contexts.ServiceContext; +import org.globus.wsrf.NoSuchResourceException; +import org.globus.wsrf.ResourceException; /** * Validates input scopes @@ -40,6 +42,21 @@ class ScopeUtils { throw new IllegalScopeException(); } + /** + * Checks if the scope exists within this instance + * @param targetScope + * @param pt + * @return true if the scope exists, false otherwise + */ + static boolean exists(GCUBEScope targetScope, ResourceManagerPortType pt) { + try { + if (pt.getInstanceState().getState(targetScope)==null) + return false; + } catch (Exception e) { + return false; + } + return true; + } /** * Adds a scope to the instance * @param scope the scope to add