b1062962a60d64aeb76e2e2ed597bf981a5ef656
[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  * 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.common.workflow.service;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.UUID;
29
30 import org.camunda.bpm.engine.RuntimeService;
31 import org.camunda.bpm.engine.runtime.ProcessInstance;
32 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
33 import org.onap.so.bpmn.common.workflow.context.WorkflowResponse;
34 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowProcessorException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.scheduling.annotation.Async;
38 import org.springframework.stereotype.Service;
39
40 @Service
41 public class WorkflowProcessor extends ProcessEngineAwareService {
42         
43         private static final Logger logger = LoggerFactory.getLogger(WorkflowProcessor.class);
44         protected static final String logMarker = "[WRKFLOW-RESOURCE]";
45         
46         @Async
47         public void startProcess( String processKey, VariableMapImpl variableMap)
48         {
49                 
50                 long startTime = System.currentTimeMillis();
51                 Map<String, Object> inputVariables = null;
52                 String processInstanceId = null;
53                 try {
54                         inputVariables = getInputVariables(variableMap);
55                         // This variable indicates that the flow was invoked asynchronously
56                         inputVariables.put("isAsyncProcess", "true");
57
58                         // Note: this creates a random businessKey if it wasn't specified.
59                         String businessKey = getBusinessKey(inputVariables);
60
61                         logger.debug("***Received MSO startProcessInstanceByKey with processKey: {} and variables: {}", processKey,
62                                 inputVariables);
63
64                         RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
65                         ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, businessKey,
66                                         inputVariables);
67                         processInstanceId = processInstance.getId();
68
69                         logger.debug(logMarker + "Process " + processKey + ":" + processInstanceId + " "
70                                         + (processInstance.isEnded() ? "ENDED" : "RUNNING"));
71                 } catch (Exception e) {
72                         WorkflowResponse workflowResponse = new WorkflowResponse();
73                         workflowResponse.setResponse("Error occurred while executing the process: " + e);
74                         workflowResponse.setProcessInstanceID(processInstanceId);
75                         workflowResponse.setMessageCode(500);
76                         workflowResponse.setMessage("Fail");
77                         throw new WorkflowProcessorException(workflowResponse);
78                 }
79         }
80
81         // Note: the business key is used to identify the process in unit tests
82         protected static String getBusinessKey(Map<String, Object> inputVariables) {
83         return getOrCreate(inputVariables, "mso-business-key");
84         }
85
86         
87         protected static Map<String, Object> getInputVariables(VariableMapImpl variableMap) {
88                 Map<String, Object> inputVariables = new HashMap<>();
89                 @SuppressWarnings("unchecked")
90                 Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables");
91                 for (Map.Entry<String, Object> entry : vMap.entrySet()) {
92                         String vName = entry.getKey();
93                         Object value = entry.getValue();
94                         @SuppressWarnings("unchecked")
95                         Map<String, Object> valueMap = (Map<String,Object>)value; // value, type
96                         inputVariables.put(vName, valueMap.get("value"));
97                 }
98                 return inputVariables;
99         }
100         
101     protected static String getOrCreate(Map<String, Object> inputVariables, String key) {
102         String value = Objects.toString(inputVariables.get(key), null);
103         if (value == null) {
104             value = UUID.randomUUID().toString();
105             inputVariables.put(key, value);
106         }
107         return value;
108     }
109
110 }