changes for AddresourceOperation

This commit is contained in:
lucio.lelii 2020-12-29 20:33:20 +01:00
parent 0f3975bd79
commit f8b8b0d124
7 changed files with 131 additions and 29 deletions

View File

@ -7,11 +7,16 @@ import javax.enterprise.inject.Instance;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException; import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer; import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException; 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.CustomContextOperator;
import org.gcube.vremanagement.contextmanager.model.operators.context.MandatoryContextOperator; 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;
import org.gcube.vremanagement.contextmanager.model.types.Context.Type; import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -33,27 +38,43 @@ public class ContextManager {
@Inject @Inject
private ContextContainer contextContainer; private ContextContainer contextContainer;
public void createContext(String parentContextId, String vreName, List<String> resourcesIds) throws InvalidContextException { @Inject
private ResourceManager resourceManager;
public Report createContext(String parentContextId, String vreName, List<String> resourcesIds) throws InvalidContextException {
Context parentContext = contextContainer.getContextById(parentContextId); Context parentContext = contextContainer.getContextById(parentContextId);
Context newContext = new Context(parentContext, vreName, vreName, Type.VRE); 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 { try {
contextContainer.addContext(newContext ); contextContainer.addContext(newContext );
} catch (ContextAlreadyExistsException e) { } catch (ContextAlreadyExistsException e) {
throw new InvalidContextException("the context with id "+vreName+" already exists"); String errorMsg = String.format("the context with id %s already exists", vreName);
} report.setResult(OperationResult.failure(errorMsg));
for (String resourceId: resourcesIds) { return report;
contextContainer.addResource(newContext.getId(), resourceId);
log.debug("resource {} added", resourceId);
} }
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); Context context = contextContainer.getContextById(contextId);
contextContainer.removeContext(contextId); contextContainer.removeContext(contextId);
mandatoryContextOperators.forEach(co -> co.onDispose(context)); mandatoryContextOperators.forEach(co -> co.onDispose(context));
return new Report(contextId, context.getName(), context.getType(), "disposeContext", AuthorizationProvider.instance.get().getClient().getId());
} }
public List<String> getAvailableContexts(){ public List<String> getAvailableContexts(){

View File

@ -41,6 +41,7 @@ public class ResourceManager {
handler.addResource(context, res); handler.addResource(context, res);
contextContainer.addResource(context.getId(), id); contextContainer.addResource(context.getId(), id);
return getScopedResource(res); return getScopedResource(res);
} }
public ScopedResource removeResourceFromContext(Context context, String id ) throws InvalidContextException { public ScopedResource removeResourceFromContext(Context context, String id ) throws InvalidContextException {

View File

@ -11,15 +11,13 @@ public interface ContextContainer {
List<String> getAvailableContexts(); List<String> getAvailableContexts();
Context getContextById(String id) throws InvalidContextException;
void addContext(Context toAdd) throws InvalidContextException, ContextAlreadyExistsException; void addContext(Context toAdd) throws InvalidContextException, ContextAlreadyExistsException;
void removeContext(String contextId) throws InvalidContextException; 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<String> getResources(String contextId) throws InvalidContextException; List<String> getResources(String contextId) throws InvalidContextException;
} }

View File

@ -9,16 +9,16 @@ import org.gcube.vremanagement.contextmanager.model.types.Context;
public interface ResourceHandler { public interface ResourceHandler {
public List<Type> getManagedResources(); List<Type> 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 //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 //in case of resource deleted directly by the publisher
public void removeResourceNotified(Context context, Resource resource); void removeResourceNotified(Context context, Resource resource);
} }

View File

@ -14,10 +14,11 @@ import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsExc
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer; import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException; import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
import org.gcube.vremanagement.contextmanager.model.types.Context; import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.gcube.vremanagement.contextmanager.model.types.ContextRetriever;
@Singleton @Singleton
@Default @Default
public class ContextContainerImpl implements ContextContainer { public class ContextContainerImpl implements ContextContainer, ContextRetriever {
private Map<String, List<String>> resourceMap = new HashMap<>(); private Map<String, List<String>> resourceMap = new HashMap<>();

View File

@ -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<ScopedResource, AddResourceParameter> {
@Inject
private Logger log;
@Inject
private ResourceManager resourceManager;
@Inject
private ContextRetriever contextRetriever;
@Override
public Set<Type> 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;
}
}

View File

@ -1,6 +1,7 @@
package org.gcube.vremanagement.contextmanager.operators; 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.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.storagehub.client.dsl.StorageHubClient; 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; import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
public class StorageHubOperation implements MandatoryContextOperator{ public class StorageHubOperation implements MandatoryContextOperator{
private String id = UUID.randomUUID().toString();
StorageHubClient client = new StorageHubClient(); StorageHubClient client = new StorageHubClient();
@ -45,11 +44,6 @@ public class StorageHubOperation implements MandatoryContextOperator{
} }
@Override
public String getName() {
return "StorageHub";
}
public String getDescription() { public String getDescription() {
return "creates/removes all the needed stuff in Storagehub"; return "creates/removes all the needed stuff in Storagehub";
@ -57,11 +51,11 @@ public class StorageHubOperation implements MandatoryContextOperator{
@Override @Override
public String getOperationId() { public String getOperationId() {
return id; return "storagehub-operations";
} }
@Override @Override
public Type getApplicationContextType() { public Set<Type> getAllowedContextType() {
return Context.Type.VRE; return Collections.singleton(Context.Type.VRE);
} }
} }