diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/ContextManager.java b/src/main/java/org/gcube/vremanagement/contextmanager/ContextManager.java index 85a7027..38c964a 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/ContextManager.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/ContextManager.java @@ -7,11 +7,16 @@ import javax.enterprise.inject.Instance; import javax.inject.Inject; import javax.inject.Singleton; +import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException; import org.gcube.vremanagement.contextmanager.handlers.ContextContainer; import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException; import org.gcube.vremanagement.contextmanager.model.operators.context.CustomContextOperator; import org.gcube.vremanagement.contextmanager.model.operators.context.MandatoryContextOperator; +import org.gcube.vremanagement.contextmanager.model.report.OperationResult; +import org.gcube.vremanagement.contextmanager.model.report.Report; +import org.gcube.vremanagement.contextmanager.model.report.ReportEntry; +import org.gcube.vremanagement.contextmanager.model.report.ReportGroup; import org.gcube.vremanagement.contextmanager.model.types.Context; import org.gcube.vremanagement.contextmanager.model.types.Context.Type; import org.slf4j.Logger; @@ -33,27 +38,43 @@ public class ContextManager { @Inject private ContextContainer contextContainer; - public void createContext(String parentContextId, String vreName, List resourcesIds) throws InvalidContextException { + @Inject + private ResourceManager resourceManager; + + public Report createContext(String parentContextId, String vreName, List resourcesIds) throws InvalidContextException { + Context parentContext = contextContainer.getContextById(parentContextId); Context newContext = new Context(parentContext, vreName, vreName, Type.VRE); + + Report report = new Report(newContext.getId(), vreName, newContext.getType(), "createContext", AuthorizationProvider.instance.get().getClient().getId()); + try { contextContainer.addContext(newContext ); } catch (ContextAlreadyExistsException e) { - throw new InvalidContextException("the context with id "+vreName+" already exists"); - } - for (String resourceId: resourcesIds) { - contextContainer.addResource(newContext.getId(), resourceId); - log.debug("resource {} added", resourceId); + String errorMsg = String.format("the context with id %s already exists", vreName); + report.setResult(OperationResult.failure(errorMsg)); + return report; } - mandatoryContextOperators.forEach(co -> co.onCreate(newContext)); + ReportGroup reportResGroup = new ReportGroup("addResources"); + for (String resourceId: resourcesIds) { + resourceManager.addResourceToContext(newContext, resourceId); + reportResGroup.addEntry(new ReportEntry(resourceId, "add resource with id "+resourceId, OperationResult.success())); + } + + + + report.setResult(OperationResult.success()); + return report; + } - public void disposeContext(String contextId) throws InvalidContextException { + public Report disposeContext(String contextId) throws InvalidContextException { Context context = contextContainer.getContextById(contextId); contextContainer.removeContext(contextId); mandatoryContextOperators.forEach(co -> co.onDispose(context)); + return new Report(contextId, context.getName(), context.getType(), "disposeContext", AuthorizationProvider.instance.get().getClient().getId()); } public List getAvailableContexts(){ diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java b/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java index 77d51c6..a2484c3 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/ResourceManager.java @@ -41,6 +41,7 @@ public class ResourceManager { handler.addResource(context, res); contextContainer.addResource(context.getId(), id); return getScopedResource(res); + } public ScopedResource removeResourceFromContext(Context context, String id ) throws InvalidContextException { diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java index cfb39e6..ae8efd4 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ContextContainer.java @@ -11,15 +11,13 @@ public interface ContextContainer { List getAvailableContexts(); - Context getContextById(String id) throws InvalidContextException; - void addContext(Context toAdd) throws InvalidContextException, ContextAlreadyExistsException; void removeContext(String contextId) throws InvalidContextException; - void addResource(String contextId, String resourceId) throws InvalidContextException; + boolean addResource(String contextId, String resourceId) throws InvalidContextException; - void removeResource(String contextId, String resourceId) throws InvalidContextException; + boolean removeResource(String contextId, String resourceId) throws InvalidContextException; List getResources(String contextId) throws InvalidContextException; } diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java index 57bc38f..817a442 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/ResourceHandler.java @@ -9,16 +9,16 @@ import org.gcube.vremanagement.contextmanager.model.types.Context; public interface ResourceHandler { - public List getManagedResources(); + List getManagedResources(); - public void addResource(Context context, Resource resource); + boolean addResource(Context context, Resource resource); - public void removeResource(Context context, Resource resource); + boolean removeResource(Context context, Resource resource); //in case of resource created directly by the publisher - public void createResourceNotified(Context context, Resource resource); + void createResourceNotified(Context context, Resource resource); //in case of resource deleted directly by the publisher - public void removeResourceNotified(Context context, Resource resource); + void removeResourceNotified(Context context, Resource resource); } diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java index 7896306..80e8516 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/handlers/impl/ContextContainerImpl.java @@ -14,10 +14,11 @@ import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsExc import org.gcube.vremanagement.contextmanager.handlers.ContextContainer; import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException; import org.gcube.vremanagement.contextmanager.model.types.Context; +import org.gcube.vremanagement.contextmanager.model.types.ContextRetriever; @Singleton @Default -public class ContextContainerImpl implements ContextContainer { +public class ContextContainerImpl implements ContextContainer, ContextRetriever { private Map> resourceMap = new HashMap<>(); diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/operators/AddResourceOperation.java b/src/main/java/org/gcube/vremanagement/contextmanager/operators/AddResourceOperation.java new file mode 100644 index 0000000..7afa851 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/contextmanager/operators/AddResourceOperation.java @@ -0,0 +1,87 @@ +package org.gcube.vremanagement.contextmanager.operators; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import javax.inject.Inject; + +import org.gcube.vremanagement.contextmanager.ResourceManager; +import org.gcube.vremanagement.contextmanager.ScopedResource; +import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException; +import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidParameterException; +import org.gcube.vremanagement.contextmanager.model.exceptions.OperationException; +import org.gcube.vremanagement.contextmanager.model.operators.AddResourceParameter; +import org.gcube.vremanagement.contextmanager.model.operators.OperatorParameters; +import org.gcube.vremanagement.contextmanager.model.operators.context.CustomContextOperator; +import org.gcube.vremanagement.contextmanager.model.types.Context; +import org.gcube.vremanagement.contextmanager.model.types.Context.Type; +import org.gcube.vremanagement.contextmanager.model.types.ContextRetriever; +import org.slf4j.Logger; + +public class AddResourceOperation extends CustomContextOperator { + + @Inject + private Logger log; + + @Inject + private ResourceManager resourceManager; + + @Inject + private ContextRetriever contextRetriever; + + + + @Override + public Set getAllowedContextType() { + return new HashSet<>(Arrays.asList(Type.values())); + } + + @Override + public String getOperationId() { + return "add-resource"; + } + + @Override + public String getDescription() { + return "Adds resource to context"; + } + + @Override + public ScopedResource execute(Context context, AddResourceParameter params) throws OperationException { + log.debug("executing operation %s",this.getOperationId()); + try { + Context toAddTo = contextRetriever.getContextById(params.getContextId()); + ScopedResource res = resourceManager.addResourceToContext(toAddTo, params.getResourceId()); + return res; + }catch (InvalidContextException e) { + //it should never happen + throw new InvalidParameterException("unknown exception"); + } + } + + @Override + public ScopedResource undo(Context context, OperatorParameters params) { + // TODO Auto-generated method stub + return null; + } + + @Override + protected AddResourceParameter checkAndTrasformParameters(Context context, OperatorParameters params) + throws InvalidParameterException { + AddResourceParameter addParams = AddResourceParameter.class.cast(params); + if (addParams.getContextId()==null) + throw new InvalidParameterException("contextId is null"); + try { + contextRetriever.getContextById(addParams.getContextId()); + }catch (InvalidContextException e) { + throw new InvalidParameterException("contextId "+addParams.getContextId()+" doesn't exist"); + } + if (addParams.getResourceId()==null) + throw new InvalidParameterException("resourceId is null"); + return addParams; + } + + +} diff --git a/src/main/java/org/gcube/vremanagement/contextmanager/operators/StorageHubOperation.java b/src/main/java/org/gcube/vremanagement/contextmanager/operators/StorageHubOperation.java index 40db443..1faa6a7 100644 --- a/src/main/java/org/gcube/vremanagement/contextmanager/operators/StorageHubOperation.java +++ b/src/main/java/org/gcube/vremanagement/contextmanager/operators/StorageHubOperation.java @@ -1,6 +1,7 @@ package org.gcube.vremanagement.contextmanager.operators; -import java.util.UUID; +import java.util.Collections; +import java.util.Set; import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.storagehub.client.dsl.StorageHubClient; @@ -12,8 +13,6 @@ import org.gcube.vremanagement.contextmanager.model.types.Context; import org.gcube.vremanagement.contextmanager.model.types.Context.Type; public class StorageHubOperation implements MandatoryContextOperator{ - - private String id = UUID.randomUUID().toString(); StorageHubClient client = new StorageHubClient(); @@ -45,11 +44,6 @@ public class StorageHubOperation implements MandatoryContextOperator{ } - @Override - public String getName() { - return "StorageHub"; - } - public String getDescription() { return "creates/removes all the needed stuff in Storagehub"; @@ -57,11 +51,11 @@ public class StorageHubOperation implements MandatoryContextOperator{ @Override public String getOperationId() { - return id; + return "storagehub-operations"; } @Override - public Type getApplicationContextType() { - return Context.Type.VRE; + public Set getAllowedContextType() { + return Collections.singleton(Context.Type.VRE); } }