Operation framework added

This commit is contained in:
lucio.lelii 2020-12-23 10:42:07 +01:00
parent b2bc132be2
commit 0f3975bd79
22 changed files with 446 additions and 208 deletions

View File

@ -1,44 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="context-manager">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<dependent-module archiveName="context-manager-model-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/context-manager-model/context-manager-model">
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<dependent-module archiveName="context-manager-model-1.0.0-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/context-manager-model/context-manager-model">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="storagehub-client-library-1.2.1.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/storagehub-client/storagehub-client">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="java-output-path" value="/ContextManager/target/classes"/>
<property name="context-root" value="context-manager"/>
</wb-module>

View File

@ -37,6 +37,12 @@
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-client-library</artifactId>
<version>[1.2.0-SNASPHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>

View File

@ -1,26 +0,0 @@
package org.gcube.vremanagement.contextmanager;
import javax.inject.Inject;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.smartgears.ApplicationManager;
import org.slf4j.Logger;
public class ContextAppManager implements ApplicationManager {
@Inject Logger logger;
@Inject ScopeInitializer scopeInitializer;
@Override
public void onInit() {
String currentContext = ScopeProvider.instance.get();
scopeInitializer.initScope(currentContext);
}
@Override
public void onShutdown() {
}
}

View File

@ -1,4 +1,4 @@
package org.gcube.vremanagement.contextmanager.handlers;
package org.gcube.vremanagement.contextmanager;
import java.util.List;
@ -7,35 +7,33 @@ import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.vremanagement.contextmanager.ContextAppManager;
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.types.Context;
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class ContextHandler {
public class ContextManager {
private static Logger log = LoggerFactory.getLogger(ContextHandler.class);
@Inject Logger log;
@Inject
@Any
Instance<MandatoryContextOperator> mandatoryContextOperators;
private Instance<MandatoryContextOperator> mandatoryContextOperators;
@Inject
@Any
Instance<CustomContextOperator> customContextOperators;
private Instance<CustomContextOperator> customContextOperators;
@Inject
ContextContainer contextContainer;
private ContextContainer contextContainer;
public void createContext(String parentContextId, String vreName, List<String> resourcesIds, ContextAppManager appContext) throws InvalidContextException {
public void createContext(String parentContextId, String vreName, List<String> resourcesIds) throws InvalidContextException {
Context parentContext = contextContainer.getContextById(parentContextId);
Context newContext = new Context(parentContext, vreName, vreName, Type.VRE);
@ -49,12 +47,13 @@ public class ContextHandler {
log.debug("resource {} added", resourceId);
}
mandatoryContextOperators.forEach(co -> co.onCreateContext(SecurityTokenProvider.instance.get(), newContext.getId(), null));
mandatoryContextOperators.forEach(co -> co.onCreate(newContext));
}
public void disposeContext(String contextId, ContextAppManager appContext) throws InvalidContextException {
public void disposeContext(String contextId) throws InvalidContextException {
Context context = contextContainer.getContextById(contextId);
contextContainer.removeContext(contextId);
mandatoryContextOperators.forEach(co -> co.onDisposeContext(SecurityTokenProvider.instance.get(), contextId, null));
mandatoryContextOperators.forEach(co -> co.onDispose(context));
}
public List<String> getAvailableContexts(){

View File

@ -0,0 +1,41 @@
package org.gcube.vremanagement.contextmanager;
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.smartgears.ApplicationManager;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.slf4j.Logger;
public class ContextServiceAppManager implements ApplicationManager {
@Inject Logger logger;
@Inject ScopeInitializer scopeInitializer;
private static Set<String> managedScope =new HashSet<>();
@Override
public void onInit() {
String currentContext = ScopeProvider.instance.get();
scopeInitializer.initScope(currentContext);
managedScope.add(currentContext);
}
@Override
public void onShutdown() {
}
public boolean isManaged(Context context){
if (context.getType()==Context.Type.VRE)
return managedScope.contains(context.getParent().getId());
else
return managedScope.contains(context.getId());
}
}

View File

@ -3,13 +3,17 @@ package org.gcube.vremanagement.contextmanager;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import org.gcube.smartgears.ApplicationManagerProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class LogFactory {
class Factories {
@Produces public Logger createLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass());
}
@Produces public ContextServiceAppManager createAppManager() {
return (ContextServiceAppManager)ApplicationManagerProvider.get();
}
}

View File

@ -10,9 +10,13 @@ import javax.inject.Singleton;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resource.Type;
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
import org.gcube.vremanagement.contextmanager.handlers.ResourceHandler;
import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector;
import org.gcube.vremanagement.contextmanager.model.collectors.CollectorsBackend;
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.jboss.weld.exceptions.IllegalArgumentException;
import org.slf4j.Logger;
@Singleton
@ -24,78 +28,44 @@ public class ResourceManager {
@Inject
@Default
BackendConnector defaultCollector;
public ScopedResource addResourceToContext(Context context, Resource resource) {
ResourceHandler handler = retrieveHandler(resource.type());
handler.addResource(context, resource);
return getScopedResource(resource);
CollectorsBackend defaultCollector;
@Inject
ContextContainer contextContainer;
@Inject Logger log;
public ScopedResource addResourceToContext(Context context, String id) throws InvalidContextException {
Resource res = retrieveResource(id);
ResourceHandler handler = retrieveHandler(res.type());
handler.addResource(context, res);
contextContainer.addResource(context.getId(), id);
return getScopedResource(res);
}
public ScopedResource removeResourceFromContext(Context context, String id ) {
public ScopedResource removeResourceFromContext(Context context, String id ) throws InvalidContextException {
Resource res = retrieveResource(id);
ResourceHandler handler = retrieveHandler(res.type());
handler.removeResource(context, res);
contextContainer.removeResource(context.getId(), id);
return getScopedResource(res);
}
private Resource retrieveResource(String id) {
return defaultCollector.find(id);
/*
XQuery query = ICFactory.queryFor(Resource.class);
query.addCondition(String.format("$resource/ID/text() eq '%s'",id));
query.setResult("<Ret>{$resource/Type}{$resource}</Ret>");
DiscoveryClient<? extends Resource> client = ICFactory.clientWith(new ResourceParser());
List<? extends Resource> resources = client.submit(query);
//TODO check return size
return resources.get(0);
public static class ResourceParser implements ResultParser<Resource>{
private enum ResourceType {
GenericResource(GenericResource.class),
RuntimeResource(ServiceEndpoint.class),
RunningInstance(GCoreEndpoint.class),
Service(Software.class),
GHN(HostingNode.class);
private Class<? extends Resource> managerClass;
ResourceType(Class<? extends Resource> managerClass) {
this.managerClass = managerClass;
}
public Class<? extends Resource> getManagerClass(){
return this.managerClass;
}
}
@Override
public Resource parse(String res) throws Exception {
String type = res.replaceAll("<Ret>\\s*<Type>([^<]*).*", "$1");
String resource = res.replaceAll(".*(<Resource.*>.*</Resource>).*", "$1");
Class<? extends Resource> classForUnmrshalling= ResourceType.valueOf(type).getManagerClass();
return Resources.unmarshal(classForUnmrshalling, new StringReader(resource));
}
}
*/
}
private ResourceHandler retrieveHandler(Type type) {
Iterator<ResourceHandler> it = resourcesHandlers.iterator();
while (it.hasNext()) {
ResourceHandler rh = it.next();
log.debug("handler {} found",rh.getClass().getSimpleName());
if (rh.getManagedResources().contains(type))
return rh;
}
//TODO return handler not found
return null;
throw new IllegalArgumentException("handler not found");
}
private ScopedResource getScopedResource(Resource res) {

View File

@ -31,7 +31,8 @@ public class ScopeDescriptor {
@XmlElement(name = "SecurityEnabled")
boolean secure = false;
@XmlElementWrapper(name="ScopedRescources")
@XmlElement(name="ScopedRescource")
List<ScopedResource> scopedResources = null;

View File

@ -33,8 +33,10 @@ public class ScopeInitializer {
public void initScope(String currentContext) {
logger.debug("current context is {}",currentContext);
ScopeBean currentScopeBean = new ScopeBean(currentContext);
if (currentScopeBean.is(ScopeBean.Type.VRE)) return;
try {
createEntireContext(new ScopeBean(currentContext));
createEntireContext(currentScopeBean);
} catch (InvalidContextException e) {
logger.error("invalid context {}", currentContext,e);
return;
@ -44,14 +46,14 @@ public class ScopeInitializer {
logger.info("resource initialization started in {} ",currentContext);
SimpleQuery query = queryFor(GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType/text eq 'VRE' or $resource/Profile/SecondaryType/text eq 'VO'");
query.addCondition("$resource/Profile/SecondaryType/text() eq 'VRE' or $resource/Profile/SecondaryType/text() eq 'VO'");
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
List<GenericResource> resources = client.submit(query);
for (GenericResource resource : resources) {
try {
String body = resource.profile().bodyAsString();
ScopeDescriptor scopeDescr = ResourceBinder.get().bind(body);
ScopeDescriptor scopeDescr = ResourceBinder.get().bind("<Body>"+body+"</Body>");
String scope = scopeDescr.context;
try {
@ -69,28 +71,32 @@ public class ScopeInitializer {
private Context createEntireContext(ScopeBean originalContextBean) throws InvalidContextException{
LinkedList<String> contextList = new LinkedList<>();
contextList.addFirst(originalContextBean.name());
contextList.addFirst(originalContextBean.toString());
ScopeBean enclosingScope = originalContextBean.enclosingScope();
while (enclosingScope!=null) {
logger.debug("adding scope {} to list",enclosingScope.name());
String scope = enclosingScope.name();
String scope = enclosingScope.toString();
contextList.addFirst(scope);
enclosingScope = enclosingScope.enclosingScope();
}
Context parentContext = null;
logger.debug("context list is {}",contextList);
Context actual = null;
for (String context : contextList) {
logger.debug("adding context {}",context);
int level = context.split("/").length;
String name = context.substring(context.lastIndexOf("/"));
int level = context.split("/").length-2;
String name = context.substring(context.lastIndexOf("/")+1);
logger.debug("adding context name {} and level is {}",name,level);
try {
parentContext = new Context(parentContext,context, name, Type.values()[level]);
contextContainer.addContext(parentContext);
Context parentContext = actual;
actual = new Context(parentContext,context, name, Type.values()[level]);
contextContainer.addContext(actual);
} catch (ContextAlreadyExistsException e) {
logger.warn("context {} already exists", context,e);
}
}
return parentContext;
return actual;
}
}

View File

@ -20,9 +20,9 @@ public class ScopedResource {
@XmlElement(name = "HostedOn")
String hostedOn;
protected ScopedResource() {}
public ScopedResource(String id, String type, String jointTime) {
super();
this.id = id;

View File

@ -1,39 +1,49 @@
package org.gcube.vremanagement.contextmanager.collector;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resource.Type;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.api.ServiceMap;
import org.gcube.common.scope.impl.ScopedServiceMap;
import org.gcube.vremanagement.contextmanager.Utils;
import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector;
import org.gcube.vremanagement.contextmanager.model.collectors.CollectorsBackend;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.slf4j.Logger;
@Singleton
@Default
public class LegacyISConnector implements BackendConnector {
public class LegacyISConnector implements CollectorsBackend {
@Inject Logger log;
private Client client = ClientBuilder.newClient();
private static final String RESOURCE_SERVICE = "XMLStoreService";
@Override
public Resource find(String resourceId) {
ServiceMap map = ScopedServiceMap.instance;
String service = map.endpoint(RESOURCE_SERVICE);
WebTarget webTarget = client.target(service).path(resourceId);
return webTarget.request(MediaType.APPLICATION_XML).get(Resource.class);
WebTarget webTarget = getWebTarget().path(resourceId);
String res = webTarget.request(MediaType.APPLICATION_XML).get(String.class);
return parseResource(res);
}
@Override
@ -49,32 +59,32 @@ public class LegacyISConnector implements BackendConnector {
public boolean addResourceToContext(Context context, Resource resource) {
String completeContext = Utils.getScopeFromContext(context);
resource.scopes().asCollection().add(completeContext);
ServiceMap map = ScopedServiceMap.instance;
String service = map.endpoint(RESOURCE_SERVICE);
WebTarget webTarget = client.target(service).path(resource.type().name()).path(resource.id());
WebTarget webTarget = getWebTarget().path(resource.type().toString()).path(resource.id());
StringWriter writer = new StringWriter();
Resources.marshal(resource, writer);
String ret = webTarget.request(MediaType.TEXT_XML).put(Entity.text(writer.toString()), String.class);
return ret!=null;
Response response = webTarget.request(MediaType.TEXT_XML).put(Entity.entity(writer.toString(), MediaType.TEXT_XML));
return response.getStatusInfo().getFamily()==Family.SUCCESSFUL;
}
@Override
public boolean removeResourceFromContext(Context context, Resource resource) {
String completeContext = Utils.getScopeFromContext(context);
resource.scopes().asCollection().remove(completeContext);
ServiceMap map = ScopedServiceMap.instance;
String service = map.endpoint(RESOURCE_SERVICE);
WebTarget webTarget = client.target(service).path(resource.type().name()).path(resource.id());
String ret;
WebTarget webTarget = getWebTarget().path(resource.type().toString()).path(resource.id());
Response response;
if (resource.scopes().size()>0) {
StringWriter writer = new StringWriter();
Resources.marshal(resource, writer);
ret = webTarget.request(MediaType.TEXT_XML).put(Entity.text(writer.toString()), String.class);
} else {
ret = webTarget.request().delete(String.class);
}
return ret!=null;
response = webTarget.request(MediaType.TEXT_XML).put(Entity.entity(writer.toString(), MediaType.TEXT_XML));
return response.getStatusInfo().getFamily()==Family.SUCCESSFUL;
} else
response = webTarget.request().delete();
return response.getStatusInfo().getFamily()==Family.SUCCESSFUL;
}
@Override
@ -83,4 +93,43 @@ public class LegacyISConnector implements BackendConnector {
return false;
}
private WebTarget getWebTarget() {
ServiceMap map = ScopedServiceMap.instance;
String service = map.endpoint(RESOURCE_SERVICE);
return client.target(service).queryParam("gcube-scope", ScopeProvider.instance.get());
}
private Resource parseResource(String res) {
String typeString = res.substring(res.indexOf("<Type>")+"<Type>".length(), res.indexOf("</Type>"));
Type type = null;
for (Type resType : Type.values())
if (resType.toString().equals(typeString)) {
type = resType;
break;
}
Class<? extends Resource> resClass = null;
switch (type) {
case ENDPOINT:
resClass = ServiceEndpoint.class;
break;
case GCOREENDPOINT:
resClass = GCoreEndpoint.class;
break;
case NODE:
resClass = HostingNode.class;
break;
case GENERIC:
resClass = GenericResource.class;
break;
default:
break;
}
return Resources.unmarshal(resClass, new StringReader(res));
}
}

View File

@ -2,8 +2,6 @@ package org.gcube.vremanagement.contextmanager.handlers;
import java.util.List;
import javax.inject.Singleton;
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
import org.gcube.vremanagement.contextmanager.model.types.Context;

View File

@ -7,6 +7,7 @@ import java.util.List;
import java.util.Map;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
@ -21,20 +22,22 @@ public class ContextContainerImpl implements ContextContainer {
private Map<String, List<String>> resourceMap = new HashMap<>();
private static ContextTree contextTree = new ContextTree() {
@Inject ContextTree contextTree;
/*private static ContextTree contextTree = new ContextTree() {
@Override
public void init() {
}
};
};*/
public List<String> getAvailableContexts() {
return contextTree.getContexts();
}
public void addContext(Context toAdd) throws InvalidContextException, ContextAlreadyExistsException{
contextTree.createItem(toAdd==null?null:toAdd.getId(), toAdd);
contextTree.createItem(toAdd.getParent()==null?null:toAdd.getParent().getId(), toAdd);
}
public void removeContext(String contextId) throws InvalidContextException {

View File

@ -4,28 +4,27 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
import org.slf4j.Logger;
public abstract class ContextTree {
@Singleton
public class ContextTree {
@Inject Logger log;
public ContextTree() {
init();
}
private static TreeItem root;
private Map<String, TreeItem> values = new HashMap<>();
public abstract void init();
public void init() {};
public synchronized TreeItem removeItem(String itemId) {
TreeItem item = values.get(itemId);
@ -37,7 +36,8 @@ public abstract class ContextTree {
throw new IllegalArgumentException("item is not a leaf");
}
public synchronized TreeItem createItem(String parentId, Context context) throws InvalidContextException {
public synchronized TreeItem createItem(String parentId, Context context) throws InvalidContextException, ContextAlreadyExistsException {
if (values.containsKey(context.getId())) throw new ContextAlreadyExistsException("context with id "+context.getId()+" already exist");
log.debug("creating item {} with parentId {}", context, parentId);
TreeItem item;
if (parentId==null) {
@ -63,16 +63,18 @@ public abstract class ContextTree {
}
public List<String> getContexts(){
log.debug("searching for contexts");
return new ArrayList<>(values.keySet());
/*log.debug("searching for contexts");
List<String> toReturn = new ArrayList<>();
String rootString = "/"+root.getContext().getId();
toReturn.add(rootString);
if (!root.isLeaf())
toReturn.addAll(deepSearch(root.getChildren(), rootString));
log.debug("found {} contexts", toReturn.size());
return toReturn;
return toReturn;*/
}
/*
private List<String> deepSearch(Set<TreeItem> children, String parentString) {
List<String> toReturn = new ArrayList<>();
for (TreeItem item : children) {
@ -82,6 +84,6 @@ public abstract class ContextTree {
toReturn.addAll(deepSearch(item.getChildren(), itemString));
}
return toReturn;
}
}*/
}

View File

@ -3,21 +3,16 @@ package org.gcube.vremanagement.contextmanager.handlers.impl;
import java.util.Arrays;
import java.util.List;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resource.Type;
import org.gcube.vremanagement.contextmanager.handlers.ResourceHandler;
import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector;
import org.gcube.vremanagement.contextmanager.model.types.Context;
@Singleton
public class DynamicResourceHandler implements ResourceHandler {
@Inject
@Default
BackendConnector defaultCollector;
@Override
public List<Type> getManagedResources() {

View File

@ -6,14 +6,16 @@ import java.util.List;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resource.Type;
import org.gcube.vremanagement.contextmanager.handlers.ResourceHandler;
import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector;
import org.gcube.vremanagement.contextmanager.model.collectors.CollectorsBackend;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.slf4j.Logger;
@Singleton
public class StaticResourceHandler implements ResourceHandler {
@Inject
@ -21,7 +23,7 @@ public class StaticResourceHandler implements ResourceHandler {
@Inject
@Any
Instance<BackendConnector> connectors;
Instance<CollectorsBackend> connectors;
@Override
public List<Type> getManagedResources() {
@ -30,21 +32,21 @@ public class StaticResourceHandler implements ResourceHandler {
@Override
public void addResource(Context context, Resource resource) {
for (BackendConnector connector : connectors)
for (CollectorsBackend connector : connectors)
try {
connector.addResourceToContext(context, resource);
}catch (Exception e) {
log.warn("error adding context {} in resource with id {} ",resource.id(), context.getName());
log.warn("error adding context {} in resource with id {} ",context.getName() ,resource.id() ,e);
}
}
@Override
public void removeResource(Context context, Resource resource) {
for (BackendConnector connector : connectors)
for (CollectorsBackend connector : connectors)
try {
connector.removeResourceFromContext(context, resource);
}catch (Exception e) {
log.warn("error removing context {} in resource with id {} ",resource.id(), context.getName());
log.warn("error removing context {} in resource with id {} ",context.getName() ,resource.id(), e);
}
}

View File

@ -0,0 +1,67 @@
package org.gcube.vremanagement.contextmanager.operators;
import java.util.UUID;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.common.storagehub.model.acls.AccessType;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.vremanagement.contextmanager.model.operators.context.MandatoryContextOperator;
import org.gcube.vremanagement.contextmanager.model.report.OperationResult;
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();
@Override
public OperationResult onCreate(Context context) {
try {
client.getVreFolderManager().createVRE(AccessType.WRITE_OWNER, AuthorizationProvider.instance.get().getClient().getId());
} catch (StorageHubException e) {
return OperationResult.failure(e.getErrorMessage());
}
/*try {
for (String user: users)
client.getVreFolderManager().addUser(userId);
}catch ( e) {
// TODO: handle exception
}*/
return OperationResult.success();
}
@Override
public OperationResult onDispose(Context context) {
try {
client.getVreFolderManager().removeVRE();
} catch (StorageHubException e) {
}
return null;
}
@Override
public String getName() {
return "StorageHub";
}
public String getDescription() {
return "creates/removes all the needed stuff in Storagehub";
}
@Override
public String getOperationId() {
return id;
}
@Override
public Type getApplicationContextType() {
return Context.Type.VRE;
}
}

View File

@ -7,6 +7,7 @@ import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@ -14,33 +15,31 @@ import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.smartgears.ApplicationManagerProvider;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.annotations.ManagedBy;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.vremanagement.contextmanager.ContextAppManager;
import org.gcube.vremanagement.contextmanager.ContextManager;
import org.gcube.vremanagement.contextmanager.ContextServiceAppManager;
import org.gcube.vremanagement.contextmanager.ResourceManager;
import org.gcube.vremanagement.contextmanager.Utils;
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
import org.gcube.vremanagement.contextmanager.handlers.ContextHandler;
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.gcube.vremanagement.contextmanager.model.types.StringList;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("contexts")
@ManagedBy(ContextAppManager.class)
@ManagedBy(ContextServiceAppManager.class)
public class ContextService {
private static Logger log = LoggerFactory.getLogger(ContextService.class);
@Inject Logger log;
ApplicationContext appContext = ContextProvider.get();
@RequestScoped
@Inject
ContextHandler contextHandler;
ContextServiceAppManager appManager ;
@Inject
ContextManager contextHandler;
@Inject
ResourceManager resourceHandler;
@ -68,7 +67,7 @@ public class ContextService {
}
}
@PUT
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/{contextId}")
public void createVREContext(@FormDataParam("resources") StringList resourceIds, @FormDataParam("vreName") String vreName ){
@ -76,7 +75,7 @@ public class ContextService {
try {
//validate Call
contextHandler.createContext(contextId, vreName, resourceIds.getValues(), (ContextAppManager)ApplicationManagerProvider.get());
contextHandler.createContext(contextId, vreName, resourceIds.getValues());
}catch (Exception e) {
log.error("error creating context {}",contextId,e);
throw new WebApplicationException(e);
@ -92,7 +91,7 @@ public class ContextService {
//Context context = contextContainer.getContextById(contextId);
//validateCall(fullContext);
contextHandler.disposeContext(contextId, (ContextAppManager)ApplicationManagerProvider.get());
contextHandler.disposeContext(contextId);
}catch (Exception e) {
log.error("error disposing context {}",contextId,e);
throw new WebApplicationException(e);
@ -100,7 +99,7 @@ public class ContextService {
}
@DELETE
@Path("/{contextId}/resources/{resourceId}")
@Path("/{contextId:(.*(?=/resources))}/resources/{resourceId}")
public void removeResourceFromContext(@PathParam("resourceId") String resourceId) {
log.info("remove resource {} from context {}", resourceId, contextId);
try {
@ -113,23 +112,29 @@ public class ContextService {
}
//@Path("/{context:(.*(?=/resources))}/resources")
@PUT
@Path("/{contextId}/resources")
public String addResourceToContext(Resource resource) {
/*log.info("adding resource {} to context {}", resource.id(), context);
@Path("/{contextId:(.*(?=/resources))}/resources")
public String addResourceToContext(String resourceId) {
log.info("adding resource {} to context {}", resourceId, contextId);
try {
validateCall(context);
resourceHandler.addResourceToContext(context, resource);
Context context = contextContainer.getContextById(contextId);
validateCall(Utils.getScopeFromContext(context));
if (appManager.isManaged(context))
resourceHandler.addResourceToContext(context, resourceId);
else {
log.debug("the context {} is not managed", contextId);
//TODO: else manage queue to add resource in another ContextManager
}
}catch (Exception e) {
// TODO: handle exception
} */
}
return null;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{contextId}/resources")
@Path("/{contextId:(.*(?=/resources))}/resources")
public StringList getResources() {
log.info("getResource in context {} called",contextId);
@ -144,10 +149,10 @@ public class ContextService {
}
}
private void validateCall(String fullContext) throws Exception {
private void validateCall(String fullContext) throws InvalidContextException {
String currentContext = ScopeProvider.instance.get();
if (!fullContext.startsWith(currentContext))
throw new Exception("invalid call");
throw new InvalidContextException("invalid context");
}

View File

@ -5,11 +5,11 @@ import java.util.List;
import javax.enterprise.inject.Default;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.vremanagement.contextmanager.model.collectors.BackendConnector;
import org.gcube.vremanagement.contextmanager.model.collectors.CollectorsBackend;
import org.gcube.vremanagement.contextmanager.model.types.Context;
@Default
public class BackendConnectorTester implements BackendConnector {
public class BackendCollectorTester implements CollectorsBackend {
@Override
public Resource find(String resourceId) {

View File

@ -54,17 +54,7 @@ public class ContextServiceIntegrationTest extends JerseyTest {
});
}
@Override
protected void configureClient(ClientConfig config) {
config.register(MultiPartFeature.class)
.register(LogFactory.class).register(new AbstractBinder() {
@Override
protected void configure() {
bind(log).to(Logger.class);
}
});
}
@Test
public void getContexts_whenCorrectRequest_thenResponseIsOk() {

View File

@ -1,23 +1,89 @@
package org.gcube.vremanagement.contextmanager;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import javax.inject.Inject;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.vremanagement.contextmanager.collector.LegacyISConnector;
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
import org.gcube.vremanagement.contextmanager.handlers.impl.ContextContainerImpl;
import org.gcube.vremanagement.contextmanager.handlers.impl.DynamicResourceHandler;
import org.gcube.vremanagement.contextmanager.handlers.impl.StaticResourceHandler;
import org.gcube.vremanagement.contextmanager.model.collectors.CollectorsBackend;
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.gcube.vremanagement.contextmanager.operations.MandatoryVREOperationTest;
import org.jglue.cdiunit.AdditionalClasses;
import org.jglue.cdiunit.CdiRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
@RunWith(CdiRunner.class)
@AdditionalClasses({ContextContainerImpl.class, LogFactory.class })
@AdditionalClasses({ContextContainerImpl.class, Factories.class,
LegacyISConnector.class, DynamicResourceHandler.class, StaticResourceHandler.class, MandatoryVREOperationTest.class })
public class ScopeTester {
@Inject ScopeInitializer scopeInitializer;
@Inject ContextContainer container;
@Inject ContextManager contextManager;
@Inject CollectorsBackend is;
@Inject Logger log;
@Inject ResourceManager resourceManager;
private static String resourceId = "eabfe9bb-001d-48bc-92dc-586eefdc2006";
private static String context = "/gcube/devNext";
private static String vreContext = "/gcube/devNext/NextNext";
@Before
public void init() {
ScopeProvider.instance.set(context);
scopeInitializer.initScope(context);
}
@Test
public void initTest() {
scopeInitializer.initScope("/gcube/devNext");
container.getAvailableContexts().forEach(c -> {
try {
container.getResources(c).forEach(r-> log.debug(" {} res {} ",c,r));
} catch (InvalidContextException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
@Test
public void addAndRemoveResourceScopeTest() throws InvalidContextException {
Context context = container.getContextById(vreContext);
resourceManager.addResourceToContext(context, resourceId);
assertTrue(container.getResources(vreContext).contains(resourceId));
Resource res = is.find(resourceId);
assertTrue(res.scopes().asCollection().contains(vreContext));
log.debug("scopes are {}",res.scopes().asCollection());
resourceManager.removeResourceFromContext(context, resourceId);
assertTrue(!container.getResources(vreContext).contains(resourceId));
assertTrue(!is.find(resourceId).scopes().contains(vreContext));
}
@Test
public void createContextTest() throws InvalidContextException {
contextManager.createContext(context, "newVRE", Collections.emptyList());
}
}

View File

@ -0,0 +1,51 @@
package org.gcube.vremanagement.contextmanager.operations;
import java.util.UUID;
import javax.inject.Inject;
import org.gcube.vremanagement.contextmanager.model.operators.context.MandatoryContextOperator;
import org.gcube.vremanagement.contextmanager.model.report.OperationResult;
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
import org.slf4j.Logger;
public class MandatoryVREOperationTest implements MandatoryContextOperator{
static String id = UUID.randomUUID().toString();
@Inject Logger log;
@Override
public Type getApplicationContextType() {
return Type.VRE;
}
@Override
public String getOperationId() {
return id;
}
@Override
public String getName() {
return "testOp";
}
@Override
public String getDescription() {
return "testOp";
}
@Override
public OperationResult onCreate(Context context) {
log.debug("executing testOP");
return OperationResult.success();
}
@Override
public OperationResult onDispose(Context context) {
// TODO Auto-generated method stub
return null;
}
}