8bc005534333ea1e5e95e5a079f7cd77e40d2049
[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
29 import org.camunda.bpm.engine.delegate.BpmnError;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.engine.delegate.ExecutionListener;
32 import org.camunda.bpm.engine.delegate.JavaDelegate;
33 import org.camunda.bpm.engine.impl.bpmn.behavior.ClassDelegateActivityBehavior;
34 import org.camunda.bpm.engine.impl.bpmn.parser.AbstractBpmnParseListener;
35 import org.camunda.bpm.engine.impl.bpmn.parser.BpmnParseListener;
36 import org.camunda.bpm.engine.impl.bpmn.parser.FieldDeclaration;
37 import org.camunda.bpm.engine.impl.cfg.AbstractProcessEnginePlugin;
38 import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
39 import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
40 import org.camunda.bpm.engine.impl.pvm.PvmTransition;
41 import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
42 import org.camunda.bpm.engine.impl.pvm.process.TransitionImpl;
43 import org.camunda.bpm.engine.impl.util.xml.Element;
44
45 import org.onap.so.bpmn.core.WorkflowException;
46 import org.springframework.stereotype.Component;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * This plugin does the following:
52  * <ol>
53  * <li>
54  * Adds logic at the start of every Call Activity to remove any existing
55  * WorkflowException object from the execution (saving a copy of it in a
56  * different variable).
57  * </li>
58  * <li>
59  * Adds logic at the end of every Call Activity to generate a MSOWorkflowException
60  * event if there is a WorkflowException object in the execution.
61  * </li>
62  * </ol>
63  */
64 @Component
65 public class WorkflowExceptionPlugin extends AbstractProcessEnginePlugin {
66         private static final Logger logger = LoggerFactory.getLogger(WorkflowExceptionPlugin.class);
67
68         @Override
69         public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
70                 List<BpmnParseListener> preParseListeners =
71                         processEngineConfiguration.getCustomPreBPMNParseListeners();
72
73                 if (preParseListeners == null) {
74                         preParseListeners = new ArrayList<>();
75                         processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
76                 }
77
78                 preParseListeners.add(new WorkflowExceptionParseListener());
79         }
80
81         public static class WorkflowExceptionParseListener extends AbstractBpmnParseListener {
82                 @Override
83                 public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {
84                         AtomicInteger triggerTaskIndex = new AtomicInteger(1);
85                         List<ActivityImpl> activities = new ArrayList<>(processDefinition.getActivities());
86                         recurse(activities, triggerTaskIndex);
87                 }
88
89                 /**
90                  * Helper method that recurses (into subprocesses) over all the listed activities.
91                  * @param activities a list of workflow activities
92                  * @param triggerTaskIndex the index of the next trigger task (mutable)
93                  */
94                 private void recurse(List<ActivityImpl> activities, AtomicInteger triggerTaskIndex) {
95                         for (ActivityImpl activity : activities) {
96                                 String type = (String) activity.getProperty("type");
97
98                                 if ("callActivity".equals(type)) {
99                                         // Add a WorkflowExceptionResetListener to clear the WorkflowException
100                                         // variable when each Call Activity starts.
101
102                                         activity.addListener(
103                                                 ExecutionListener.EVENTNAME_START,
104                                                 new WorkflowExceptionResetListener());
105
106                                         // Add a WorkflowExceptionTriggerTask after the call activity.
107                                         // It must be a task because a listener cannot be used to generate
108                                         // an event.  Throwing BpmnError from an execution listener will
109                                         // cause the process to die.
110
111                                         List<PvmTransition> outTransitions =
112                         new ArrayList<>(activity.getOutgoingTransitions());
113
114                                         for (PvmTransition transition : outTransitions) {
115                                                 String triggerTaskId = "WorkflowExceptionTriggerTask_" + triggerTaskIndex;
116
117                                                 ActivityImpl triggerTask = activity.getFlowScope().createActivity(triggerTaskId);
118
119                                                 ClassDelegateActivityBehavior behavior = new  ClassDelegateActivityBehavior(
120                                                                 WorkflowExceptionTriggerTask.class.getName(),
121                             new ArrayList<>(0));
122
123                                                 triggerTask.setActivityBehavior(behavior);
124                                                 triggerTask.setName("Workflow Exception Trigger Task " + triggerTaskIndex);
125                                                 triggerTaskIndex.getAndIncrement();
126
127                                                 TransitionImpl transitionImpl = (TransitionImpl) transition;
128                                                 TransitionImpl triggerTaskOutTransition = triggerTask.createOutgoingTransition();
129                                                 triggerTaskOutTransition.setDestination((ActivityImpl)transitionImpl.getDestination());
130                                                 transitionImpl.setDestination(triggerTask);
131                                         }
132                                 } else if ("subProcess".equals(type)) {
133                                         recurse(new ArrayList<>(activity.getActivities()), triggerTaskIndex);
134                                 }
135                         }
136                 }
137         }
138
139     /**
140      * If there is a WorkflowException object in the execution, this method
141      * removes it (saving a copy of it in a different variable).
142      */
143         public static class WorkflowExceptionResetListener implements ExecutionListener {
144                 public void notify(DelegateExecution execution) throws Exception {
145                         Object workflowException = execution.getVariable("WorkflowException");
146
147                         if (workflowException instanceof WorkflowException) {
148                                 int index = 1;
149                                 String saveName = "SavedWorkflowException" + index;
150                                 while (execution.getVariable(saveName) != null) {
151                                         saveName = "SavedWorkflowException" + (++index);
152                                 }
153
154                                 logger.debug("WorkflowExceptionResetTask is moving WorkflowException to " + saveName);
155
156                                 execution.setVariable(saveName, workflowException);
157                                 execution.setVariable("WorkflowException", null);
158                         }
159                 }
160         }
161
162     /**
163      * Generates an MSOWorkflowException event if there is a WorkflowException
164      * object in the execution.
165      */
166         public static class WorkflowExceptionTriggerTask implements JavaDelegate {
167                 public void execute(DelegateExecution execution) throws Exception {
168                         if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
169                                 logger.debug("WorkflowExceptionTriggerTask is generating a MSOWorkflowException event");
170                                 throw new BpmnError("MSOWorkflowException");
171                         }
172                 }
173         }
174 }