5afd7e00e82d81eb80fc94d1bb1017ed1cc0bbdf
[so.git] /
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         protected static final String logMarker = "[WRKFLOW-RESOURCE]";
42         
43         @Async
44         public void startProcess( String processKey, VariableMapImpl variableMap)
45         {
46                 
47                 long startTime = System.currentTimeMillis();
48                 Map<String, Object> inputVariables = null;
49                 String processInstanceId = null;
50                 try {
51                         inputVariables = getInputVariables(variableMap);
52                         // This variable indicates that the flow was invoked asynchronously
53                         inputVariables.put("isAsyncProcess", "true");
54
55                         // Note: this creates a random businessKey if it wasn't specified.
56                         String businessKey = getBusinessKey(inputVariables);
57
58                         msoLogger.debug("***Received MSO startProcessInstanceByKey with processKey: " + processKey
59                                         + " and variables: " + inputVariables);
60
61                         RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
62                         ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, businessKey,
63                                         inputVariables);
64                         processInstanceId = processInstance.getId();
65
66                         msoLogger.debug(logMarker + "Process " + processKey + ":" + processInstanceId + " "
67                                         + (processInstance.isEnded() ? "ENDED" : "RUNNING"));
68                 } catch (Exception e) {
69
70                         msoLogger.recordAuditEvent(startTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.InternalError,
71                                         logMarker + "Error in starting the process: " + e.getMessage());
72
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);
79                 }
80         }
81
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");
85         }
86
87         
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"));
98                 }
99                 return inputVariables;
100         }
101         
102     protected static String getOrCreate(Map<String, Object> inputVariables, String key) {
103         String value = Objects.toString(inputVariables.get(key), null);
104         if (value == null) {
105             value = UUID.randomUUID().toString();
106             inputVariables.put(key, value);
107         }
108         return value;
109     }
110
111 }