package org.gcube.application.cms.plugins.implementations; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.gcube.application.cms.plugins.LifecycleManager; import org.gcube.application.cms.plugins.PluginManagerInterface; import org.gcube.application.cms.plugins.faults.*; import org.gcube.application.cms.plugins.model.PluginDescriptor; import org.gcube.application.cms.plugins.reports.*; import org.gcube.application.cms.plugins.requests.BaseRequest; import org.gcube.application.cms.plugins.requests.EventExecutionRequest; import org.gcube.application.cms.plugins.requests.StepExecutionRequest; import org.gcube.application.geoportal.common.model.configuration.Configuration; import org.gcube.application.geoportal.common.model.document.accounting.User; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; import org.gcube.application.geoportal.common.model.document.lifecycle.TriggeredEvents; import org.gcube.application.geoportal.common.model.rest.ConfigurationException; import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration; import org.gcube.application.geoportal.common.utils.ContextUtils; import java.util.HashMap; import java.util.Map; @Slf4j public abstract class AbstractLifeCycleManager extends AbstractPlugin implements LifecycleManager { @Setter protected PluginManagerInterface pluginManager; private Map registeredSteps=new HashMap<>(); private Map registeredEvent=new HashMap<>(); protected void setEvent(String event,GuardedEventManager m){registeredEvent.put(event,m);} protected void setStep(String step,GuardedStepExecution e){registeredSteps.put(step,e);} private GuardedEventManager defaultUpdateManager= new GuardedEventManager() { @Override protected EventExecutionReport run() throws Exception { theReport = validate(theReport); theReport = setDefault(theReport); return theReport; } }; private GuardedEventManager noOp = new GuardedEventManager() { @Override protected EventExecutionReport run() throws Exception { return theReport; } }; public AbstractLifeCycleManager() { registerEvents(); registerSteps(); } protected EventExecutionReport onDeleteDocument(EventExecutionReport report) throws ConfigurationException, InvalidPluginRequestException, MaterializationException, EventException {return report;} protected EventExecutionReport onDeleteFileSet(EventExecutionReport report) throws ConfigurationException, InvalidPluginRequestException, MaterializationException, EventException {return report;} protected EventExecutionReport onUpdateDocument(EventExecutionReport report){ report = validate(report); report = setDefault(report); return report; } protected EventExecutionReport onInitDocument(EventExecutionReport report) throws InvalidPluginRequestException { report = validate(report); report = setDefault(report); return report; } protected void registerEvents(){ setEvent(EventExecutionRequest.Events.ON_INIT_DOCUMENT, new GuardedEventManager() { @Override protected EventExecutionReport run() throws Exception { return onInitDocument(theReport); } }); setEvent(EventExecutionRequest.Events.ON_UPDATE_DOCUMENT, new GuardedEventManager() { @Override protected EventExecutionReport run() throws Exception { return onUpdateDocument(theReport); } }); setEvent(EventExecutionRequest.Events.ON_DELETE_DOCUMENT, new GuardedEventManager() { @Override protected EventExecutionReport run() throws Exception { return onDeleteDocument(theReport); } }); setEvent(EventExecutionRequest.Events.ON_DELETE_FILESET, new GuardedEventManager() { @Override protected EventExecutionReport run() throws Exception { return onDeleteFileSet(theReport); } }); } protected void registerSteps(){} protected PluginDescriptor DESCRIPTOR=new PluginDescriptor(";;;", PluginDescriptor.BaseTypes.LIFECYCLE_MANAGER); @Override public StepExecutionReport performStep(StepExecutionRequest request) throws StepException, InvalidPluginRequestException, InvalidProfileException, ConfigurationException, InsufficientPrivileges { log.info("Serving Request {}",request); StepExecutionReport report=new StepExecutionReport(request); report.setStatus(Report.Status.OK); LifecycleInformation info=report.getToSetLifecycleInformation(); info.setLastOperationStatus(LifecycleInformation.Status.OK); info.setLastInvokedStep(request.getStep()); if(!canInvokeStep(request.getStep(),request.getCaller(), getConfigurationFromProfile(request.getUseCaseDescriptor()))) throw new InsufficientPrivileges("User is not allowed to invoke "+request.getStep()); if(!registeredSteps.containsKey(request.getStep())) throw new UnrecognizedStepException(("Invalid Step " + request.getStep())); try { return registeredSteps.get(request.getStep()) .setTheReport(report).execute(); }catch (StepException | InvalidPluginRequestException e){ throw e; }catch (Throwable t) { log.error("Unable to perform step " + request.getStep(), t); String msg = "Unable to execute Step " + request.getStep() + ". Error was " + t.getMessage(); report.setStatus(Report.Status.ERROR); report.putMessage(msg); info.setLastOperationStatus(LifecycleInformation.Status.ERROR); info.addErrorMessage(msg); } return report; } protected boolean canInvokeStep(String stepID, User u, HandlerDeclaration config) throws ConfigurationException { return new RoleManager(config).canInvokeStep(stepID,u); } @Override public InitializationReport initInContext() throws InitializationException { InitializationReport report = new InitializationReport(); try{ report.setStatus(Report.Status.OK); } catch (Throwable e) { log.error("Unable to initialize plugins in {} ", ContextUtils.getCurrentScope(),e); report.setStatus(Report.Status.WARNING); report.putMessage("Unable to initialize plugins in "+ ContextUtils.getCurrentScope()+" : "+e.getMessage()); } return report; } @Override public InitializationReport init() throws InitializationException { InitializationReport report = new InitializationReport(); try{ report.setStatus(Report.Status.OK); } catch (Throwable e) { log.error("Unable to initialize plugins ",e); report.setStatus(Report.Status.WARNING); report.putMessage("Unable to initialize plugins : "+e.getMessage()); } return report; } @Override public PluginDescriptor getDescriptor() { return DESCRIPTOR; } @Override public EventExecutionReport onEvent(EventExecutionRequest request) throws EventException, InvalidPluginRequestException { EventExecutionReport report=new EventExecutionReport(request); TriggeredEvents info = report.getToSetLifecycleInformation().getLastEvent(); try { if(!registeredEvent.containsKey(request.getEvent())) throw new UnrecognizedEventException("Unexpected Event "+request.getEvent()); return registeredEvent.get(request.getEvent()).setTheReport(report).execute(); }catch (EventException e){ throw e; }catch (Throwable t){ log.error("Unable to execute on event "+request.getEvent(),t); String msg = "Unable to execute on event "+request.getEvent()+". Error was "+t.getMessage(); info.setLastOperationStatus(LifecycleInformation.Status.ERROR); info.addErrorMessage(msg); report.setStatus(Report.Status.ERROR); report.putMessage(msg); } return report; } /** * Override this method for programmatic default values management * * @param currentReport * @return */ public EventExecutionReport setDefault(EventExecutionReport currentReport){ // Default implementation is no op return currentReport; } public EventExecutionReport validate(EventExecutionReport currentReport){ // Default implementation is no op return currentReport; } @Override public void shutdown() throws ShutDownException {} @Override public Configuration getCurrentConfiguration(BaseRequest request) throws ConfigurationException { return new Configuration(); } }