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);
41 protected static final String logMarker = "[WRKFLOW-RESOURCE]";
44 public void startProcess( String processKey, VariableMapImpl variableMap)
47 long startTime = System.currentTimeMillis();
48 Map<String, Object> inputVariables = null;
49 String processInstanceId = null;
51 inputVariables = getInputVariables(variableMap);
52 // This variable indicates that the flow was invoked asynchronously
53 inputVariables.put("isAsyncProcess", "true");
55 // Note: this creates a random businessKey if it wasn't specified.
56 String businessKey = getBusinessKey(inputVariables);
58 msoLogger.debug("***Received MSO startProcessInstanceByKey with processKey: " + processKey
59 + " and variables: " + inputVariables);
61 RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
62 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, businessKey,
64 processInstanceId = processInstance.getId();
66 msoLogger.debug(logMarker + "Process " + processKey + ":" + processInstanceId + " "
67 + (processInstance.isEnded() ? "ENDED" : "RUNNING"));
68 } catch (Exception e) {
70 msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError,
71 logMarker + "Error in starting the process: " + e.getMessage());
73 WorkflowResponse workflowResponse = new WorkflowResponse();
74 workflowResponse.setResponse("Error occurred while executing the process: " + e);
75 workflowResponse.setProcessInstanceID(processInstanceId);
76 workflowResponse.setMessageCode(500);
77 workflowResponse.setMessage("Fail");
78 throw new WorkflowProcessorException(workflowResponse);
82 // Note: the business key is used to identify the process in unit tests
83 protected static String getBusinessKey(Map<String, Object> inputVariables) {
84 return getOrCreate(inputVariables, "mso-business-key");
88 protected static Map<String, Object> getInputVariables(VariableMapImpl variableMap) {
89 Map<String, Object> inputVariables = new HashMap<>();
90 @SuppressWarnings("unchecked")
91 Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables");
92 for (Map.Entry<String, Object> entry : vMap.entrySet()) {
93 String vName = entry.getKey();
94 Object value = entry.getValue();
95 @SuppressWarnings("unchecked")
96 Map<String, Object> valueMap = (Map<String,Object>)value; // value, type
97 inputVariables.put(vName, valueMap.get("value"));
99 return inputVariables;
102 protected static String getOrCreate(Map<String, Object> inputVariables, String key) {
103 String value = Objects.toString(inputVariables.get(key), null);
105 value = UUID.randomUUID().toString();
106 inputVariables.put(key, value);