[SO] Release so 1.13.0 image
[so.git] / bpmn / mso-infrastructure-bpmn / src / main / java / org / onap / so / bpmn / core / plugins / WorkflowExceptionPlugin.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.core.plugins;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.atomic.AtomicInteger;
28 import org.camunda.bpm.engine.delegate.BpmnError;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.camunda.bpm.engine.delegate.ExecutionListener;
31 import org.camunda.bpm.engine.delegate.JavaDelegate;
32 import org.camunda.bpm.engine.impl.bpmn.behavior.ClassDelegateActivityBehavior;
33 import org.camunda.bpm.engine.impl.bpmn.parser.AbstractBpmnParseListener;
34 import org.camunda.bpm.engine.impl.bpmn.parser.BpmnParseListener;
35 import org.camunda.bpm.engine.impl.cfg.AbstractProcessEnginePlugin;
36 import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
37 import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
38 import org.camunda.bpm.engine.impl.pvm.PvmTransition;
39 import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
40 import org.camunda.bpm.engine.impl.pvm.process.TransitionImpl;
41 import org.camunda.bpm.engine.impl.util.xml.Element;
42 import org.onap.so.bpmn.core.WorkflowException;
43 import org.springframework.stereotype.Component;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * This plugin does the following:
49  * <ol>
50  * <li>Adds logic at the start of every Call Activity to remove any existing WorkflowException object from the execution
51  * (saving a copy of it in a different variable).</li>
52  * <li>Adds logic at the end of every Call Activity to generate a MSOWorkflowException event if there is a
53  * WorkflowException object in the execution.</li>
54  * </ol>
55  */
56 @Component
57 public class WorkflowExceptionPlugin extends AbstractProcessEnginePlugin {
58     private static final Logger logger = LoggerFactory.getLogger(WorkflowExceptionPlugin.class);
59
60     private static final String WORKFLOW_EXCEPTION = "WorkflowException";
61     private static final String PAUSE_FOR_MANUAL_TASK_RAINY_DAY_ID = "PauseForManualTaskRainyDay";
62
63     @Override
64     public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
65         List<BpmnParseListener> preParseListeners = processEngineConfiguration.getCustomPreBPMNParseListeners();
66
67         if (preParseListeners == null) {
68             preParseListeners = new ArrayList<>();
69             processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
70         }
71
72         preParseListeners.add(new WorkflowExceptionParseListener());
73     }
74
75     public static class WorkflowExceptionParseListener extends AbstractBpmnParseListener {
76         @Override
77         public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {
78             AtomicInteger triggerTaskIndex = new AtomicInteger(1);
79             List<ActivityImpl> activities = new ArrayList<>(processDefinition.getActivities());
80             recurse(activities, triggerTaskIndex);
81         }
82
83         /**
84          * Helper method that recurses (into subprocesses) over all the listed activities.
85          * 
86          * @param activities a list of workflow activities
87          * @param triggerTaskIndex the index of the next trigger task (mutable)
88          */
89         private void recurse(List<ActivityImpl> activities, AtomicInteger triggerTaskIndex) {
90             for (ActivityImpl activity : activities) {
91                 String type = (String) activity.getProperty("type");
92
93                 if ("callActivity".equals(type)
94                         && !PAUSE_FOR_MANUAL_TASK_RAINY_DAY_ID.equals(activity.getActivityId())) {
95                     // Add a WorkflowExceptionResetListener to clear the WorkflowException
96                     // variable when each Call Activity starts.
97
98                     activity.addListener(ExecutionListener.EVENTNAME_START, new WorkflowExceptionResetListener());
99
100                     // Add a WorkflowExceptionTriggerTask after the call activity.
101                     // It must be a task because a listener cannot be used to generate
102                     // an event. Throwing BpmnError from an execution listener will
103                     // cause the process to die.
104
105                     List<PvmTransition> outTransitions = new ArrayList<>(activity.getOutgoingTransitions());
106
107                     for (PvmTransition transition : outTransitions) {
108                         String triggerTaskId = "WorkflowExceptionTriggerTask_" + triggerTaskIndex;
109
110                         ActivityImpl triggerTask = activity.getFlowScope().createActivity(triggerTaskId);
111
112                         ClassDelegateActivityBehavior behavior = new ClassDelegateActivityBehavior(
113                                 WorkflowExceptionTriggerTask.class.getName(), new ArrayList<>(0));
114
115                         triggerTask.setActivityBehavior(behavior);
116                         triggerTask.setName("Workflow Exception Trigger Task " + triggerTaskIndex);
117                         triggerTaskIndex.getAndIncrement();
118
119                         TransitionImpl transitionImpl = (TransitionImpl) transition;
120                         TransitionImpl triggerTaskOutTransition = triggerTask.createOutgoingTransition();
121                         triggerTaskOutTransition.setDestination((ActivityImpl) transitionImpl.getDestination());
122                         transitionImpl.setDestination(triggerTask);
123                     }
124                 } else if ("subProcess".equals(type)) {
125                     recurse(new ArrayList<>(activity.getActivities()), triggerTaskIndex);
126                 }
127             }
128         }
129     }
130
131     /**
132      * If there is a WorkflowException object in the execution, this method removes it (saving a copy of it in a
133      * different variable).
134      */
135     public static class WorkflowExceptionResetListener implements ExecutionListener {
136         public void notify(DelegateExecution execution) throws Exception {
137             Object workflowException = execution.getVariable(WORKFLOW_EXCEPTION);
138
139             if (workflowException instanceof WorkflowException) {
140                 int index = 1;
141                 String saveName = "SavedWorkflowException" + index;
142                 while (execution.getVariable(saveName) != null) {
143                     saveName = "SavedWorkflowException" + (++index);
144                 }
145
146                 logger.debug("WorkflowExceptionResetTask is moving WorkflowException to {}", saveName);
147
148                 execution.setVariable(saveName, workflowException);
149                 execution.setVariable(WORKFLOW_EXCEPTION, null);
150             }
151         }
152     }
153
154     /**
155      * Generates an MSOWorkflowException event if there is a WorkflowException object in the execution.
156      */
157     public static class WorkflowExceptionTriggerTask implements JavaDelegate {
158         public void execute(DelegateExecution execution) throws Exception {
159             if (execution.getVariable(WORKFLOW_EXCEPTION) instanceof WorkflowException) {
160                 logger.debug("WorkflowExceptionTriggerTask is generating a MSOWorkflowException event");
161                 throw new BpmnError("MSOWorkflowException");
162             }
163         }
164     }
165 }