2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.bpmn.common.workflow.service;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.UUID;
30 import java.util.concurrent.atomic.AtomicLong;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriInfo;
41 import org.camunda.bpm.engine.HistoryService;
42 import org.camunda.bpm.engine.ProcessEngineException;
43 import org.camunda.bpm.engine.ProcessEngineServices;
44 import org.camunda.bpm.engine.RuntimeService;
45 import org.camunda.bpm.engine.history.HistoricVariableInstance;
46 import org.camunda.bpm.engine.runtime.ProcessInstance;
47 import org.camunda.bpm.engine.variable.VariableMap;
48 import org.camunda.bpm.engine.variable.Variables;
49 import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats;
50 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
51 import org.onap.so.bpmn.common.workflow.context.WorkflowResponse;
52 import org.onap.so.bpmn.core.WorkflowException;
53 import org.onap.so.logger.MessageEnum;
54 import org.onap.so.logger.MsoLogger;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import org.springframework.stereotype.Component;
60 import io.swagger.annotations.Api;
61 import io.swagger.annotations.ApiOperation;
64 @Api(value = "/workflow", description = "Root of workflow services")
66 public class WorkflowResource extends ProcessEngineAwareService {
68 private static final Logger logger = LoggerFactory.getLogger(WorkflowResource.class);
69 private static final String LOGMARKER = "[WRKFLOW-RESOURCE]";
71 private static final int DEFAULT_WAIT_TIME = 30000;
74 private UriInfo uriInfo = null;
77 * Starts the process instance and responds to client synchronously
78 * If the request does not contain mso-service-request-timeout then it waits for the value specified in DEFAULT_WAIT_TIME
79 * Note: value specified in mso-service-request-timeout is in seconds
80 * During polling time, if there is an exception encountered in the process execution then polling is stopped and the error response is
81 * returned to the client
87 @Path("/services/{processKey}")
89 value = "Starts a new process with the appropriate process synchronously",
92 @Produces("application/json")
93 @Consumes("application/json")
94 public Response startProcessInstanceByKey(@PathParam("processKey") String processKey,
95 VariableMapImpl variableMap) {
97 Map<String, Object> inputVariables = getInputVariables(variableMap);
98 setLogContext(processKey, inputVariables);
100 WorkflowResponse workflowResponse = new WorkflowResponse();
101 long startTime = System.currentTimeMillis();
102 ProcessInstance processInstance = null;
105 //Kickoff the process
106 ProcessThread thread = new ProcessThread(inputVariables,processKey);
109 Map<String, Object> responseMap = null;
111 //wait for process to be completed
112 long waitTime = getWaitTime(inputVariables);
113 long now = System.currentTimeMillis();
115 long endTime = start + waitTime;
116 long pollingInterval = 500;
118 // TEMPORARY LOGIC FOR UNIT TEST REFACTORING
119 // If this is a unit test (method is invoked directly), wait a max
120 // of 5 seconds after process ended for a result. In production,
121 // wait up to 60 seconds.
122 long timeToWaitAfterProcessEnded = uriInfo == null ? 5000 : 60000;
123 AtomicLong timeProcessEnded = new AtomicLong(0);
124 boolean endedWithNoResponse = false;
126 while (now <= endTime) {
127 Thread.sleep(pollingInterval);
129 now = System.currentTimeMillis();
131 // Increase the polling interval over time
133 long elapsed = now - start;
135 if (elapsed > 60000) {
136 pollingInterval = 5000;
137 } else if (elapsed > 10000) {
138 pollingInterval = 1000;
140 Exception exception = thread.getException();
141 if (exception != null) {
142 throw new Exception(exception);
145 processInstance = thread.getProcessInstance();
147 if (processInstance == null) {
148 logger.debug("{} process has not been created yet", LOGMARKER + processKey );
152 String processInstanceId = processInstance.getId();
153 workflowResponse.setProcessInstanceID(processInstanceId);
155 responseMap = getResponseMap(processInstance, processKey, timeProcessEnded);
157 if (responseMap == null) {
158 logger.debug("{} has not produced a response yet", LOGMARKER + processKey);
160 if (timeProcessEnded.longValue() != 0) {
161 long elapsedSinceEnded = System.currentTimeMillis() - timeProcessEnded.longValue();
163 if (elapsedSinceEnded > timeToWaitAfterProcessEnded) {
164 endedWithNoResponse = true;
169 processResponseMap(workflowResponse, responseMap);
170 recordEvents(processKey, workflowResponse, startTime);
171 return Response.status(workflowResponse.getMessageCode()).entity(workflowResponse).build();
175 //if we dont get response after waiting then send timeout response
178 String processInstanceId;
180 if (processInstance == null) {
181 processInstanceId = "N/A";
182 state = "NOT STARTED";
184 processInstanceId = processInstance.getProcessInstanceId();
185 state = isProcessEnded(processInstanceId) ? "ENDED" : "NOT ENDED";
188 workflowResponse.setMessage("Fail");
189 if (endedWithNoResponse) {
190 workflowResponse.setResponse("Process ended without producing a response");
192 workflowResponse.setResponse("Request timed out, process state: " + state);
194 workflowResponse.setProcessInstanceID(processInstanceId);
195 recordEvents(processKey, workflowResponse, startTime);
196 workflowResponse.setMessageCode(500);
197 return Response.status(500).entity(workflowResponse).build();
198 } catch (Exception ex) {
199 logger.debug(LOGMARKER + "Exception in startProcessInstance by key",ex);
200 workflowResponse.setMessage("Fail" );
201 workflowResponse.setResponse("Error occurred while executing the process: " + ex.getMessage());
202 if (processInstance != null) workflowResponse.setProcessInstanceID(processInstance.getId());
204 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "BPMN", MDC.get(processKey),
205 MsoLogger.ErrorCode.UnknownError.getValue(),
206 LOGMARKER + workflowResponse.getMessage() + " for processKey: " + processKey + " with response: "
207 + workflowResponse.getResponse());
209 workflowResponse.setMessageCode(500);
210 recordEvents(processKey, workflowResponse, startTime);
211 return Response.status(500).entity(workflowResponse).build();
216 * Returns the wait time, this is used by the resource on how long it should wait to send a response
217 * If none specified DEFAULT_WAIT_TIME is used
218 * @param inputVariables
221 private int getWaitTime(Map<String, Object> inputVariables)
223 String timeout = inputVariables.get("mso-service-request-timeout") == null
224 ? null : inputVariables.get("mso-service-request-timeout").toString();
226 if (timeout != null) {
228 return Integer.parseInt(timeout)*1000;
229 } catch (NumberFormatException nex) {
230 logger.debug("Invalid input for mso-service-request-timeout");
233 return DEFAULT_WAIT_TIME;
236 private void recordEvents(String processKey, WorkflowResponse response, long startTime) {
239 private void setLogContext(String processKey, Map<String, Object> inputVariables) {
240 MsoLogger.setServiceName("MSO." + processKey);
241 if (inputVariables != null) {
242 MsoLogger.setLogContext(getValueFromInputVariables(inputVariables, "mso-request-id"),
243 getValueFromInputVariables(inputVariables, "mso-service-instance-id"));
247 private String getValueFromInputVariables(Map<String,Object> inputVariables, String key) {
248 Object value = inputVariables.get(key);
252 return value.toString();
257 * Checks to see if the specified process is ended.
258 * @param processInstanceId the process instance ID
259 * @return true if the process is ended
261 private boolean isProcessEnded(String processInstanceId) {
262 ProcessEngineServices pes = getProcessEngineServices();
264 return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null ? true : false ;
265 } catch (Exception e) {
266 logger.debug("Exception :",e);
271 private void processResponseMap(WorkflowResponse workflowResponse, Map<String, Object> responseMap) {
272 Object object = responseMap.get("Response");
273 String response = object == null ? null : String.valueOf(object);
274 if(response == null){
275 object = responseMap.get("WorkflowResponse");
276 response = object == null ? null : String.valueOf(object);
279 workflowResponse.setResponse(response);
281 object = responseMap.get("ResponseCode");
282 String responseCode = object == null ? null : String.valueOf(object);
285 workflowResponse.setMessageCode(Integer.parseInt(responseCode));
286 } catch(NumberFormatException nex) {
287 logger.debug(LOGMARKER + "Failed to parse ResponseCode: " + responseCode);
288 workflowResponse.setMessageCode(-1);
291 Object status = responseMap.get("Status");
293 if ("Success".equalsIgnoreCase(String.valueOf(status))) {
294 workflowResponse.setMessage("Success");
295 } else if ("Fail".equalsIgnoreCase(String.valueOf(status))) {
296 workflowResponse.setMessage("Fail");
298 logger.debug(LOGMARKER + "Unrecognized Status: " + responseCode);
299 workflowResponse.setMessage("Fail");
305 * Triggers the workflow in a separate thread
307 private class ProcessThread extends Thread {
308 private final Map<String,Object> inputVariables;
309 private final String processKey;
310 private final String businessKey;
311 private ProcessInstance processInstance = null;
312 private Exception exception = null;
314 public ProcessThread(Map<String, Object> inputVariables, String processKey) {
315 this.inputVariables = inputVariables;
316 this.processKey = processKey;
317 this.businessKey = UUID.randomUUID().toString();
321 * If an exception occurs when starting the process instance, it may
322 * be obtained by calling this method. Note that exceptions are only
323 * recorded while the process is executing in its original thread.
324 * Once a process is suspended, exception recording stops.
325 * @return the exception, or null if none has occurred
327 public Exception getException() {
332 public ProcessInstance getProcessInstance() {
333 return this.processInstance;
337 * Sets the process instance exception.
338 * @param exception the exception
340 private void setException(Exception exception) {
341 this.exception = exception;
345 setLogContext(processKey, inputVariables);
347 long startTime = System.currentTimeMillis();
351 RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
353 // Note that this method doesn't return until the process suspends
354 // itself or finishes. We provide a business key so we can identify
355 // the process instance immediately.
356 processInstance = runtimeService.startProcessInstanceByKey(
357 processKey, inputVariables);
359 } catch (Exception e) {
360 logger.debug(LOGMARKER + "ProcessThread caught an exception executing "
361 + processKey + ": " + e);
368 private Map<String, Object> getInputVariables(VariableMapImpl variableMap) {
369 VariableMap inputVariables = Variables.createVariables();
370 @SuppressWarnings("unchecked")
371 Map<String, Object> vMap = (Map<String, Object>) variableMap.get("variables");
372 for (String key : vMap.keySet()) { //variabe name vn
373 @SuppressWarnings("unchecked")
374 Map<String, Object> valueMap = (Map<String,Object>)vMap.get(key); //value, type
375 inputVariables.putValueTyped(key, Variables
376 .objectValue(valueMap.get("value"))
377 .serializationDataFormat(SerializationDataFormats.JAVA) // tells the engine to use java serialization for persisting the value
380 return inputVariables;
384 * Attempts to get a response map from the specified process instance.
385 * @return the response map, or null if it is unavailable
387 private Map<String, Object> getResponseMap(ProcessInstance processInstance,
388 String processKey, AtomicLong timeProcessEnded) {
390 String responseMapVariable = processKey + "ResponseMap";
391 String processInstanceId = processInstance.getId();
393 // Query the runtime service to see if a response map is ready.
395 /* RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
396 List<Execution> executions = runtimeService.createExecutionQuery()
397 .processInstanceId(processInstanceId).list();
399 for (Execution execution : executions) {
400 @SuppressWarnings("unchecked")
401 Map<String, Object> responseMap = (Map<String, Object>)
402 getVariableFromExecution(runtimeService, execution.getId(),
403 responseMapVariable);
405 if (responseMap != null) {
406 msoLogger.debug(LOGMARKER + "Obtained " + responseMapVariable
407 + " from process " + processInstanceId + " execution "
408 + execution.getId());
413 //Querying history seem to return consistent results compared to querying the runtime service
415 boolean alreadyEnded = timeProcessEnded.longValue() != 0;
417 if (alreadyEnded || isProcessEnded(processInstance.getId())) {
419 timeProcessEnded.set(System.currentTimeMillis());
422 // Query the history service to see if a response map exists.
424 HistoryService historyService = getProcessEngineServices().getHistoryService();
425 @SuppressWarnings("unchecked")
426 Map<String, Object> responseMap = (Map<String, Object>)
427 getVariableFromHistory(historyService, processInstance.getId(),
428 responseMapVariable);
430 if (responseMap != null) {
431 logger.debug(LOGMARKER + "Obtained " + responseMapVariable
432 + " from process " + processInstanceId + " history");
436 // Query the history service for old-style response variables.
438 String prefix = (String) getVariableFromHistory(historyService, processInstanceId, "prefix");
440 if (prefix != null) {
442 // Check for 'WorkflowResponse' variable
443 Object workflowResponseObject = getVariableFromHistory(historyService, processInstanceId, "WorkflowResponse");
444 String workflowResponse = workflowResponseObject == null ? null : String.valueOf(workflowResponseObject);
445 logger.debug(LOGMARKER + "WorkflowResponse: " + workflowResponse);
447 if (workflowResponse != null) {
448 Object responseCodeObject = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode");
449 String responseCode = responseCodeObject == null ? null : String.valueOf(responseCodeObject);
450 logger.debug(LOGMARKER + prefix + "ResponseCode: " + responseCode);
451 responseMap = new HashMap<>();
452 responseMap.put("WorkflowResponse", workflowResponse);
453 responseMap.put("ResponseCode", responseCode);
454 responseMap.put("Status", "Success");
459 // Check for 'WorkflowException' variable
460 WorkflowException workflowException = null;
461 String workflowExceptionText = null;
463 Object workflowExceptionObject = getVariableFromHistory(historyService, processInstanceId, "WorkflowException");
464 if(workflowExceptionObject != null) {
465 if(workflowExceptionObject instanceof WorkflowException) {
466 workflowException = (WorkflowException) workflowExceptionObject;
467 workflowExceptionText = workflowException.toString();
468 responseMap = new HashMap<>();
469 responseMap.put("WorkflowException", workflowExceptionText);
470 responseMap.put("ResponseCode", workflowException.getErrorCode());
471 responseMap.put("Status", "Fail");
474 else if (workflowExceptionObject instanceof String) {
475 Object object = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode");
476 String responseCode = object == null ? null : String.valueOf(object);
477 workflowExceptionText = (String) workflowExceptionObject;
478 responseMap = new HashMap<>();
479 responseMap.put("WorkflowException", workflowExceptionText);
480 responseMap.put("ResponseCode", responseCode);
481 responseMap.put("Status", "Fail");
486 logger.debug(LOGMARKER + "WorkflowException: " + workflowExceptionText);
488 // BEGIN LEGACY SUPPORT. TODO: REMOVE THIS CODE
489 Object object = getVariableFromHistory(historyService, processInstanceId, processKey + "Response");
490 String response = object == null ? null : String.valueOf(object);
491 logger.debug(LOGMARKER + processKey + "Response: " + response);
493 if (response != null) {
494 object = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode");
495 String responseCode = object == null ? null : String.valueOf(object);
496 logger.debug(LOGMARKER + prefix + "ResponseCode: " + responseCode);
497 responseMap = new HashMap<>();
498 responseMap.put("Response", response);
499 responseMap.put("ResponseCode", responseCode);
500 responseMap.put("Status", "Success");
504 object = getVariableFromHistory(historyService, processInstanceId, prefix + "ErrorResponse");
505 String errorResponse = object == null ? null : String.valueOf(object);
506 logger.debug(LOGMARKER + prefix + "ErrorResponse: " + errorResponse);
508 if (errorResponse != null) {
509 object = getVariableFromHistory(historyService, processInstanceId, prefix + "ResponseCode");
510 String responseCode = object == null ? null : String.valueOf(object);
511 logger.debug(LOGMARKER + prefix + "ResponseCode: " + responseCode);
512 responseMap = new HashMap<>();
513 responseMap.put("Response", errorResponse);
514 responseMap.put("ResponseCode", responseCode);
515 responseMap.put("Status", "Fail");
518 // END LEGACY SUPPORT. TODO: REMOVE THIS CODE
525 * Gets a variable value from the specified execution.
526 * @return the variable value, or null if the variable could not be
529 private Object getVariableFromExecution(RuntimeService runtimeService,
530 String executionId, String variableName) {
532 return runtimeService.getVariable(executionId, variableName);
533 } catch (ProcessEngineException e) {
534 // Most likely cause is that the execution no longer exists.
535 logger.debug("Error retrieving execution " + executionId
536 + " variable " + variableName + ": " + e);
541 * Gets a variable value from specified historical process instance.
542 * @return the variable value, or null if the variable could not be
545 private Object getVariableFromHistory(HistoryService historyService,
546 String processInstanceId, String variableName) {
548 HistoricVariableInstance v = historyService.createHistoricVariableInstanceQuery()
549 .processInstanceId(processInstanceId).variableName(variableName).singleResult();
550 return v == null ? null : v.getValue();
551 } catch (Exception e) {
552 logger.debug("Error retrieving process {} variable {} from history: ", processInstanceId,
559 @Path("/services/{processKey}/{processInstanceId}")
560 @Produces("application/json")
561 @Consumes("application/json")
563 value = "Allows for retrieval of the variables for a given process",
566 public WorkflowResponse getProcessVariables(@PathParam("processKey") String processKey, @PathParam("processInstanceId") String processInstanceId) {
567 //TODO filter only set of variables
568 WorkflowResponse response = new WorkflowResponse();
570 long startTime = System.currentTimeMillis();
572 ProcessEngineServices engine = getProcessEngineServices();
573 List<HistoricVariableInstance> variables = engine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();
574 Map<String,String> variablesMap = new HashMap<>();
575 for (HistoricVariableInstance variableInstance: variables) {
576 variablesMap.put(variableInstance.getName(), variableInstance.getValue().toString());
579 logger.debug(LOGMARKER + "***Received MSO getProcessVariables with processKey:" + processKey + " and variables: " +
580 variablesMap.toString());
582 response.setVariables(variablesMap);
583 response.setMessage("Success");
584 response.setResponse("Successfully retrieved the variables");
585 response.setProcessInstanceID(processInstanceId);
587 logger.debug(LOGMARKER + response.getMessage() + " for processKey: " + processKey + " with response: " + response
589 } catch (Exception ex) {
590 response.setMessage("Fail");
591 response.setResponse("Failed to retrieve the variables," + ex.getMessage());
592 response.setProcessInstanceID(processInstanceId);
594 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "BPMN", MDC.get(processKey),
595 MsoLogger.ErrorCode.UnknownError.getValue(),
596 LOGMARKER + response.getMessage() + " for processKey: " + processKey + " with response: " + response
598 logger.debug("Exception :",ex);