251464a34d3d42f8460b5ea0b7517eb1c79e1c48
[so.git] /
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 org.camunda.bpm.engine.RepositoryService;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.camunda.bpm.engine.delegate.ExecutionListener;
30 import org.camunda.bpm.engine.impl.bpmn.parser.AbstractBpmnParseListener;
31 import org.camunda.bpm.engine.impl.bpmn.parser.BpmnParseListener;
32 import org.camunda.bpm.engine.impl.cfg.AbstractProcessEnginePlugin;
33 import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
34 import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
35 import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
36 import org.camunda.bpm.engine.impl.pvm.process.ScopeImpl;
37 import org.camunda.bpm.engine.impl.pvm.process.TransitionImpl;
38 import org.camunda.bpm.engine.impl.util.xml.Element;
39 import org.camunda.bpm.engine.impl.variable.VariableDeclaration;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45
46
47 /**
48  * Plugin for MSO logging and URN mapping.
49  */
50 @Component
51 public class LoggingAndURNMappingPlugin extends AbstractProcessEnginePlugin {
52
53     @Autowired
54     private LoggingParseListener loggingParseListener;
55
56     @Override
57     public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
58         List<BpmnParseListener> preParseListeners = processEngineConfiguration.getCustomPreBPMNParseListeners();
59         if (preParseListeners == null) {
60             preParseListeners = new ArrayList<>();
61             processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
62         }
63         preParseListeners.add(loggingParseListener);
64     }
65
66     /**
67      * Called when a process flow is parsed so we can inject listeners.
68      */
69     @Component
70     public class LoggingParseListener extends AbstractBpmnParseListener {
71
72
73         private void injectLogExecutionListener(ActivityImpl activity) {
74             activity.addListener(ExecutionListener.EVENTNAME_END, new LoggingExecutionListener("END"));
75
76             activity.addListener(ExecutionListener.EVENTNAME_START, new LoggingExecutionListener("START"));
77
78             activity.addListener(ExecutionListener.EVENTNAME_TAKE, new LoggingExecutionListener("TAKE"));
79         }
80
81         @Override
82         public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {}
83
84         @Override
85         public void parseStartEvent(Element startEventElement, ScopeImpl scope, ActivityImpl startEventActivity) {
86             // Inject these listeners only on the main start event for the flow, not on any embedded subflow start
87             // events
88
89             injectLogExecutionListener(startEventActivity);
90         }
91
92         @Override
93         public void parseServiceTask(Element serviceTaskElement, ScopeImpl scope, ActivityImpl activity) {
94             injectLogExecutionListener(activity);
95         }
96
97         @Override
98         public void parseExclusiveGateway(Element exclusiveGwElement, ScopeImpl scope, ActivityImpl activity) {
99             injectLogExecutionListener(activity);
100         }
101
102         @Override
103         public void parseInclusiveGateway(Element inclusiveGwElement, ScopeImpl scope, ActivityImpl activity) {
104             injectLogExecutionListener(activity);
105         }
106
107         @Override
108         public void parseParallelGateway(Element parallelGwElement, ScopeImpl scope, ActivityImpl activity) {
109             injectLogExecutionListener(activity);
110         }
111
112         @Override
113         public void parseScriptTask(Element scriptTaskElement, ScopeImpl scope, ActivityImpl activity) {
114             injectLogExecutionListener(activity);
115         }
116
117         @Override
118         public void parseBusinessRuleTask(Element businessRuleTaskElement, ScopeImpl scope, ActivityImpl activity) {
119             injectLogExecutionListener(activity);
120         }
121
122         @Override
123         public void parseTask(Element taskElement, ScopeImpl scope, ActivityImpl activity) {
124             injectLogExecutionListener(activity);
125         }
126
127         @Override
128         public void parseManualTask(Element manualTaskElement, ScopeImpl scope, ActivityImpl activity) {
129             injectLogExecutionListener(activity);
130         }
131
132         @Override
133         public void parseUserTask(Element userTaskElement, ScopeImpl scope, ActivityImpl activity) {
134             injectLogExecutionListener(activity);
135         }
136
137         @Override
138         public void parseEndEvent(Element endEventElement, ScopeImpl scope, ActivityImpl activity) {
139             injectLogExecutionListener(activity);
140         }
141
142         @Override
143         public void parseBoundaryTimerEventDefinition(Element timerEventDefinition, boolean interrupting,
144                 ActivityImpl timerActivity) {
145             injectLogExecutionListener(timerActivity);
146         }
147
148         @Override
149         public void parseBoundaryErrorEventDefinition(Element errorEventDefinition, boolean interrupting,
150                 ActivityImpl activity, ActivityImpl nestedErrorEventActivity) {
151             injectLogExecutionListener(activity);
152         }
153
154         @Override
155         public void parseSubProcess(Element subProcessElement, ScopeImpl scope, ActivityImpl activity) {
156             injectLogExecutionListener(activity);
157         }
158
159         @Override
160         public void parseCallActivity(Element callActivityElement, ScopeImpl scope, ActivityImpl activity) {
161             injectLogExecutionListener(activity);
162         }
163
164         @Override
165         public void parseProperty(Element propertyElement, VariableDeclaration variableDeclaration,
166                 ActivityImpl activity) {
167             injectLogExecutionListener(activity);
168         }
169
170         @Override
171         public void parseSequenceFlow(Element sequenceFlowElement, ScopeImpl scopeElement, TransitionImpl transition) {
172             // injectLogExecutionListener(activity);
173         }
174
175         @Override
176         public void parseSendTask(Element sendTaskElement, ScopeImpl scope, ActivityImpl activity) {
177             injectLogExecutionListener(activity);
178         }
179
180         @Override
181         public void parseMultiInstanceLoopCharacteristics(Element activityElement,
182                 Element multiInstanceLoopCharacteristicsElement, ActivityImpl activity) {
183             injectLogExecutionListener(activity);
184         }
185
186         @Override
187         public void parseIntermediateTimerEventDefinition(Element timerEventDefinition, ActivityImpl timerActivity) {
188             injectLogExecutionListener(timerActivity);
189         }
190
191         @Override
192         public void parseRootElement(Element rootElement, List<ProcessDefinitionEntity> processDefinitions) {
193
194         }
195
196         @Override
197         public void parseReceiveTask(Element receiveTaskElement, ScopeImpl scope, ActivityImpl activity) {
198             injectLogExecutionListener(activity);
199         }
200
201         @Override
202         public void parseIntermediateSignalCatchEventDefinition(Element signalEventDefinition,
203                 ActivityImpl signalActivity) {
204             injectLogExecutionListener(signalActivity);
205         }
206
207         @Override
208         public void parseBoundarySignalEventDefinition(Element signalEventDefinition, boolean interrupting,
209                 ActivityImpl signalActivity) {
210             injectLogExecutionListener(signalActivity);
211         }
212
213         @Override
214         public void parseEventBasedGateway(Element eventBasedGwElement, ScopeImpl scope, ActivityImpl activity) {
215             injectLogExecutionListener(activity);
216         }
217
218         @Override
219         public void parseTransaction(Element transactionElement, ScopeImpl scope, ActivityImpl activity) {
220             injectLogExecutionListener(activity);
221         }
222
223         @Override
224         public void parseCompensateEventDefinition(Element compensateEventDefinition,
225                 ActivityImpl compensationActivity) {
226             injectLogExecutionListener(compensationActivity);
227         }
228
229         @Override
230         public void parseIntermediateThrowEvent(Element intermediateEventElement, ScopeImpl scope,
231                 ActivityImpl activity) {
232             injectLogExecutionListener(activity);
233         }
234
235         @Override
236         public void parseIntermediateCatchEvent(Element intermediateEventElement, ScopeImpl scope,
237                 ActivityImpl activity) {
238             injectLogExecutionListener(activity);
239         }
240
241         @Override
242         public void parseBoundaryEvent(Element boundaryEventElement, ScopeImpl scopeElement,
243                 ActivityImpl nestedActivity) {
244             injectLogExecutionListener(nestedActivity);
245         }
246
247         @Override
248         public void parseIntermediateMessageCatchEventDefinition(Element messageEventDefinition,
249                 ActivityImpl nestedActivity) {
250             injectLogExecutionListener(nestedActivity);
251         }
252
253         @Override
254         public void parseBoundaryMessageEventDefinition(Element element, boolean interrupting,
255                 ActivityImpl messageActivity) {
256             injectLogExecutionListener(messageActivity);
257         }
258     }
259
260     /**
261      * Logs details about the current activity.
262      */
263     public class LoggingExecutionListener implements ExecutionListener {
264         private final Logger logger = LoggerFactory.getLogger(LoggingExecutionListener.class);
265
266         private String event;
267
268         public LoggingExecutionListener() {
269             this.event = "";
270         }
271
272         public LoggingExecutionListener(String event) {
273             this.event = event;
274         }
275
276         public String getEvent() {
277             return event;
278         }
279
280         @Override
281         public void notify(DelegateExecution execution) throws Exception {
282             // required for legacy groovy processing in camunda
283             execution.setVariable("isDebugLogEnabled", "true");
284             if (!isBlank(execution.getCurrentActivityName())) {
285                 try {
286
287                     String id = execution.getId();
288                     if (id != null) {
289                         RepositoryService repositoryService =
290                                 execution.getProcessEngineServices().getRepositoryService();
291                         String processName = repositoryService.createProcessDefinitionQuery()
292                                 .processDefinitionId(execution.getProcessDefinitionId()).singleResult().getName();
293
294
295                         String requestId = (String) execution.getVariable("mso-request-id");
296                         String svcid = (String) execution.getVariable("mso-service-instance-id");
297                     }
298                 } catch (Exception e) {
299                     logger.error("Exception occurred", e);
300                 }
301             }
302         }
303
304         private boolean isBlank(Object object) {
305             return object == null || "".equals(object.toString().trim());
306         }
307     }
308 }