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