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