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.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;
38 public class WorkflowProcessor extends ProcessEngineAwareService {
40 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, WorkflowProcessor.class);
42 protected static final String logMarker = "[WRKFLOW-RESOURCE]";
43 protected static final long DEFAULT_WAIT_TIME = 30000; //default wait time
46 public void startProcess( String processKey, VariableMapImpl variableMap) throws InterruptedException
49 long startTime = System.currentTimeMillis();
50 Map<String, Object> inputVariables = null;
51 String processInstanceId = null;
53 inputVariables = getInputVariables(variableMap);
54 // This variable indicates that the flow was invoked asynchronously
55 inputVariables.put("isAsyncProcess", "true");
57 // Note: this creates a random businessKey if it wasn't specified.
58 String businessKey = getBusinessKey(inputVariables);
60 msoLogger.debug("***Received MSO startProcessInstanceByKey with processKey: " + processKey
61 + " and variables: " + inputVariables);
63 RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
64 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, businessKey,
66 processInstanceId = processInstance.getId();
68 msoLogger.debug(logMarker + "Process " + processKey + ":" + processInstanceId + " "
69 + (processInstance.isEnded() ? "ENDED" : "RUNNING"));
70 } catch (Exception e) {
72 msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError,
73 logMarker + "Error in starting the process: " + e.getMessage());
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);
84 protected static String getKeyValueFromInputVariables(Map<String,Object> inputVariables, String key) {
85 if (inputVariables == null) {
89 return Objects.toString(inputVariables.get(key), "N/A");
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");
97 protected static String getRequestId(Map<String, Object> inputVariables) {
98 return getOrCreate(inputVariables, "mso-request-id");
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"));
112 return inputVariables;
115 protected static String getOrCreate(Map<String, Object> inputVariables, String key) {
116 String value = Objects.toString(inputVariables.get(key), null);
118 value = UUID.randomUUID().toString();
119 inputVariables.put(key, value);
124 protected long getWaitTime(Map<String, Object> inputVariables)
127 String timeout = Objects.toString(inputVariables.get("mso-service-request-timeout"), null);
129 if (timeout != null) {
131 return Long.parseLong(timeout)*1000;
132 } catch (NumberFormatException nex) {
133 msoLogger.debug("Invalid input for mso-service-request-timeout");
137 return DEFAULT_WAIT_TIME;