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

67 lines
2.3 KiB
Java

package org.gcube.vremanagement.contextmanager;
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.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;
@Singleton
public class ContextManager {
@Inject Logger log;
@Inject
@Any
private Instance<MandatoryContextOperator> mandatoryContextOperators;
@Inject
@Any
private Instance<CustomContextOperator> customContextOperators;
@Inject
private ContextContainer contextContainer;
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);
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);
}
mandatoryContextOperators.forEach(co -> co.onCreate(newContext));
}
public void disposeContext(String contextId) throws InvalidContextException {
Context context = contextContainer.getContextById(contextId);
contextContainer.removeContext(contextId);
mandatoryContextOperators.forEach(co -> co.onDispose(context));
}
public List<String> getAvailableContexts(){
return contextContainer.getAvailableContexts();
}
public List<String> getAssociatedResources(String contextId) throws InvalidContextException{
return contextContainer.getResources(contextId);
}
}