Update Logging
[so.git] / bpmn / mso-infrastructure-bpmn / src / main / java / org / onap / so / bpmn / common / workflow / service / WorkflowProcessor.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.common.workflow.service;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.UUID;
27
28 import org.camunda.bpm.engine.RuntimeService;
29 import org.camunda.bpm.engine.runtime.ProcessInstance;
30 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
31 import org.onap.so.bpmn.common.workflow.context.WorkflowResponse;
32 import org.onap.so.logger.MsoLogger;
33 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowProcessorException;
34 import org.springframework.scheduling.annotation.Async;
35 import org.springframework.stereotype.Service;
36
37 @Service
38 public class WorkflowProcessor extends ProcessEngineAwareService {
39         
40         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, WorkflowProcessor.class);
41         
42         protected static final String logMarker = "[WRKFLOW-RESOURCE]";
43         protected static final long DEFAULT_WAIT_TIME = 30000;  //default wait time
44         
45         @Async
46         public void startProcess( String processKey, VariableMapImpl variableMap) throws InterruptedException
47         {
48                 
49                 long startTime = System.currentTimeMillis();
50                 Map<String, Object> inputVariables = null;
51                 String processInstanceId = null;
52                 try {
53                         inputVariables = getInputVariables(variableMap);
54                         // This variable indicates that the flow was invoked asynchronously
55                         inputVariables.put("isAsyncProcess", "true");
56
57                         // Note: this creates a random businessKey if it wasn't specified.
58                         String businessKey = getBusinessKey(inputVariables);
59
60                         msoLogger.debug("***Received MSO startProcessInstanceByKey with processKey: " + processKey
61                                         + " and variables: " + inputVariables);
62
63                         RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
64                         ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, businessKey,
65                                         inputVariables);
66                         processInstanceId = processInstance.getId();
67
68                         msoLogger.debug(logMarker + "Process " + processKey + ":" + processInstanceId + " "
69                                         + (processInstance.isEnded() ? "ENDED" : "RUNNING"));
70                 } catch (Exception e) {
71
72                         msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError,
73                                         logMarker + "Error in starting the process: " + e.getMessage());
74
75                         WorkflowResponse workflowResponse = new WorkflowResponse();
76                         workflowResponse.setResponse("Error occurred while executing the process: " + e);
77                         workflowResponse.setProcessInstanceID(processInstanceId);
78                         workflowResponse.setMessageCode(500);
79                         workflowResponse.setMessage("Fail");
80                         throw new WorkflowProcessorException(workflowResponse);
81                 }
82         }
83         
84         protected static String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) {
85                 if (inputVariables == null) {
86                         return "";
87                 }
88
89                 return Objects.toString(inputVariables.get(key), "N/A");
90         }
91         
92         // Note: the business key is used to identify the process in unit tests
93         protected static String getBusinessKey(Map<String, Object> inputVariables) {
94         return getOrCreate(inputVariables, "mso-business-key");
95         }
96
97         protected static String getRequestId(Map<String, Object> inputVariables) {
98         return getOrCreate(inputVariables, "mso-request-id");
99         }
100         
101         protected static Map<String, Object> getInputVariables(VariableMapImpl variableMap) {
102                 Map<String, Object> inputVariables = new HashMap<>();
103                 @SuppressWarnings("unchecked")
104                 Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables");
105                 for (Map.Entry<String, Object> entry : vMap.entrySet()) {
106                         String vName = entry.getKey();
107                         Object value = entry.getValue();
108                         @SuppressWarnings("unchecked")
109                         Map<String, Object> valueMap = (Map<String,Object>)value; // value, type
110                         inputVariables.put(vName, valueMap.get("value"));
111                 }
112                 return inputVariables;
113         }
114         
115     protected static String getOrCreate(Map<String, Object> inputVariables, String key) {
116         String value = Objects.toString(inputVariables.get(key), null);
117         if (value == null) {
118             value = UUID.randomUUID().toString();
119             inputVariables.put(key, value);
120         }
121         return value;
122     }
123     
124         protected long getWaitTime(Map<String, Object> inputVariables)
125         {
126             
127                 String timeout = Objects.toString(inputVariables.get("mso-service-request-timeout"), null);
128
129                 if (timeout != null) {
130                         try {
131                                 return Long.parseLong(timeout)*1000;
132                         } catch (NumberFormatException nex) {
133                                 msoLogger.debug("Invalid input for mso-service-request-timeout");
134                         }
135                 }
136
137                 return DEFAULT_WAIT_TIME;
138         }
139 }