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