2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.common.workflow.service;
23 import java.util.HashMap;
25 import java.util.Objects;
26 import java.util.UUID;
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.WorkflowCallbackResponse;
32 import org.onap.so.bpmn.common.workflow.context.WorkflowContextHolder;
33 import org.onap.so.logger.MsoLogger;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.scheduling.annotation.Async;
37 import org.springframework.stereotype.Service;
40 public class WorkflowProcessor extends ProcessEngineAwareService {
42 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, WorkflowProcessor.class);
44 protected static final String logMarker = "[WRKFLOW-RESOURCE]";
45 protected static final long DEFAULT_WAIT_TIME = 30000; //default wait time
48 public void startProcess( String processKey, VariableMapImpl variableMap) throws InterruptedException
50 MDC.getCopyOfContextMap();
51 long startTime = System.currentTimeMillis();
52 Map<String, Object> inputVariables = null;
53 String processInstanceId = null;
55 inputVariables = getInputVariables(variableMap);
56 setLogContext(processKey, inputVariables);
58 // This variable indicates that the flow was invoked asynchronously
59 inputVariables.put("isAsyncProcess", "true");
62 setLogContext(processKey, inputVariables);
64 // Note: this creates a random businessKey if it wasn't specified.
65 String businessKey = getBusinessKey(inputVariables);
67 msoLogger.debug("***Received MSO startProcessInstanceByKey with processKey: " + processKey
68 + " and variables: " + inputVariables);
70 RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
71 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, businessKey,
73 processInstanceId = processInstance.getId();
75 msoLogger.debug(logMarker + "Process " + processKey + ":" + processInstanceId + " "
76 + (processInstance.isEnded() ? "ENDED" : "RUNNING"));
77 } catch (Exception e) {
79 msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError,
80 logMarker + "Error in starting the process: " + e.getMessage());
82 WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse();
83 callbackResponse.setStatusCode(500);
84 callbackResponse.setMessage("Fail");
85 callbackResponse.setResponse("Error occurred while executing the process: " + e);
87 WorkflowContextHolder.getInstance().processCallback(processKey, processInstanceId,
88 getRequestId(inputVariables), callbackResponse);
92 protected static void setLogContext(String processKey,
93 Map<String, Object> inputVariables) {
94 MsoLogger.setServiceName("MSO." + processKey);
95 if (inputVariables != null) {
96 MsoLogger.setLogContext(getKeyValueFromInputVariables(inputVariables,"mso-request-id"), getKeyValueFromInputVariables(inputVariables,"mso-service-instance-id"));
100 protected static String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) {
101 if (inputVariables == null) {
105 return Objects.toString(inputVariables.get(key), "N/A");
108 // Note: the business key is used to identify the process in unit tests
109 protected static String getBusinessKey(Map<String, Object> inputVariables) {
110 return getOrCreate(inputVariables, "mso-business-key");
113 protected static String getRequestId(Map<String, Object> inputVariables) {
114 return getOrCreate(inputVariables, "mso-request-id");
117 protected static Map<String, Object> getInputVariables(VariableMapImpl variableMap) {
118 Map<String, Object> inputVariables = new HashMap<>();
119 @SuppressWarnings("unchecked")
120 Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables");
121 for (Map.Entry<String, Object> entry : vMap.entrySet()) {
122 String vName = entry.getKey();
123 Object value = entry.getValue();
124 @SuppressWarnings("unchecked")
125 Map<String, Object> valueMap = (Map<String,Object>)value; // value, type
126 inputVariables.put(vName, valueMap.get("value"));
128 return inputVariables;
131 protected static String getOrCreate(Map<String, Object> inputVariables, String key) {
132 String value = Objects.toString(inputVariables.get(key), null);
134 value = UUID.randomUUID().toString();
135 inputVariables.put(key, value);
140 protected long getWaitTime(Map<String, Object> inputVariables)
143 String timeout = Objects.toString(inputVariables.get("mso-service-request-timeout"), null);
145 if (timeout != null) {
147 return Long.parseLong(timeout)*1000;
148 } catch (NumberFormatException nex) {
149 msoLogger.debug("Invalid input for mso-service-request-timeout");
153 return DEFAULT_WAIT_TIME;