context-manager/src/main/java/org/gcube/vremanagement/contextmanager/ContextManager.java

67 lines
2.3 KiB
Java
Raw Normal View History

2020-12-23 10:42:07 +01:00
package org.gcube.vremanagement.contextmanager;
2020-12-03 14:24:34 +01:00
import java.util.List;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.inject.Singleton;
2020-12-14 20:32:46 +01:00
import org.gcube.vremanagement.contextmanager.exceptions.ContextAlreadyExistsException;
2020-12-23 10:42:07 +01:00
import org.gcube.vremanagement.contextmanager.handlers.ContextContainer;
2020-12-04 19:57:56 +01:00
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
2020-12-03 14:24:34 +01:00
import org.gcube.vremanagement.contextmanager.model.operators.context.CustomContextOperator;
import org.gcube.vremanagement.contextmanager.model.operators.context.MandatoryContextOperator;
2020-12-04 19:57:56 +01:00
import org.gcube.vremanagement.contextmanager.model.types.Context;
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
import org.slf4j.Logger;
2020-12-03 14:24:34 +01:00
@Singleton
2020-12-23 10:42:07 +01:00
public class ContextManager {
2020-12-03 14:24:34 +01:00
2020-12-23 10:42:07 +01:00
@Inject Logger log;
2020-12-04 19:57:56 +01:00
2020-12-03 14:24:34 +01:00
@Inject
@Any
2020-12-23 10:42:07 +01:00
private Instance<MandatoryContextOperator> mandatoryContextOperators;
2020-12-03 14:24:34 +01:00
@Inject
@Any
2020-12-23 10:42:07 +01:00
private Instance<CustomContextOperator> customContextOperators;
2020-12-03 14:24:34 +01:00
2020-12-04 19:57:56 +01:00
@Inject
2020-12-23 10:42:07 +01:00
private ContextContainer contextContainer;
2020-12-04 19:57:56 +01:00
2020-12-23 10:42:07 +01:00
public void createContext(String parentContextId, String vreName, List<String> resourcesIds) throws InvalidContextException {
2020-12-04 19:57:56 +01:00
Context parentContext = contextContainer.getContextById(parentContextId);
2020-12-14 20:32:46 +01:00
Context newContext = new Context(parentContext, vreName, vreName, Type.VRE);
try {
contextContainer.addContext(newContext );
} catch (ContextAlreadyExistsException e) {
throw new InvalidContextException("the context with id "+vreName+" already exists");
}
2020-12-04 19:57:56 +01:00
for (String resourceId: resourcesIds) {
contextContainer.addResource(newContext.getId(), resourceId);
log.debug("resource {} added", resourceId);
}
2020-12-23 10:42:07 +01:00
mandatoryContextOperators.forEach(co -> co.onCreate(newContext));
2020-12-04 19:57:56 +01:00
}
2020-12-23 10:42:07 +01:00
public void disposeContext(String contextId) throws InvalidContextException {
Context context = contextContainer.getContextById(contextId);
2020-12-04 19:57:56 +01:00
contextContainer.removeContext(contextId);
2020-12-23 10:42:07 +01:00
mandatoryContextOperators.forEach(co -> co.onDispose(context));
2020-12-04 19:57:56 +01:00
}
public List<String> getAvailableContexts(){
return contextContainer.getAvailableContexts();
2020-12-03 14:24:34 +01:00
}
2020-12-04 19:57:56 +01:00
public List<String> getAssociatedResources(String contextId) throws InvalidContextException{
return contextContainer.getResources(contextId);
2020-12-03 14:24:34 +01:00
}
}