92f1b7a81621cecf0637f7923fe93ad6c8ccdee6
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
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=========================================================
22  */
23
24 package org.onap.so.bpmn.common.workflow.service;
25
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import java.util.concurrent.atomic.AtomicLong;
31
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;
40
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;
57 import org.slf4j.MDC;
58 import org.springframework.stereotype.Component;
59
60 import io.swagger.annotations.Api;
61 import io.swagger.annotations.ApiOperation;
62
63 @Path("/workflow")
64 @Api(value = "/workflow", description = "Root of workflow services")
65 @Component
66 public class WorkflowResource extends ProcessEngineAwareService {
67         
68         private static final Logger logger = LoggerFactory.getLogger(WorkflowResource.class);
69         private static final String LOGMARKER = "[WRKFLOW-RESOURCE]";
70
71         private static final int DEFAULT_WAIT_TIME = 30000;
72
73         @Context
74         private UriInfo uriInfo = null;
75
76         /**
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
82          * @param processKey
83          * @param variableMap
84          * @return
85          */
86         @POST
87         @Path("/services/{processKey}")
88         @ApiOperation(
89                 value = "Starts a new process with the appropriate process synchronously",
90                 notes = "d"
91             )
92         @Produces("application/json")
93         @Consumes("application/json")
94         public Response startProcessInstanceByKey(@PathParam("processKey") String processKey,
95                         VariableMapImpl variableMap) {
96
97                 Map<String, Object> inputVariables = getInputVariables(variableMap);    
98                 setLogContext(processKey, inputVariables);
99
100                 WorkflowResponse workflowResponse = new WorkflowResponse();
101                 long startTime = System.currentTimeMillis();
102                 ProcessInstance processInstance = null;
103
104                 try {
105                         //Kickoff the process
106                         ProcessThread thread = new ProcessThread(inputVariables,processKey);
107                         thread.start();
108
109                         Map<String, Object> responseMap = null;
110
111                         //wait for process to be completed
112                         long waitTime = getWaitTime(inputVariables);
113                         long now = System.currentTimeMillis();
114                         long start = now;
115                         long endTime = start + waitTime;
116                         long pollingInterval = 500;
117
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;
125
126                         while (now <= endTime) {
127                                 Thread.sleep(pollingInterval);
128
129                                 now = System.currentTimeMillis();
130
131                                 // Increase the polling interval over time
132
133                                 long elapsed = now - start;
134
135                                 if (elapsed > 60000) {
136                                         pollingInterval = 5000;
137                                 } else if (elapsed > 10000) {
138                                         pollingInterval = 1000;
139                                 }
140                                 Exception exception = thread.getException();
141                                 if (exception != null) {
142                                         throw new Exception(exception);
143                                 }
144
145                                 processInstance = thread.getProcessInstance();
146
147                                 if (processInstance == null) {
148                                         logger.debug("{} process has not been created yet", LOGMARKER + processKey );
149                                         continue;
150                                 }
151
152                                 String processInstanceId = processInstance.getId();
153                                 workflowResponse.setProcessInstanceID(processInstanceId);
154
155                                 responseMap = getResponseMap(processInstance, processKey, timeProcessEnded);
156
157                                 if (responseMap == null) {
158                                         logger.debug("{} has not produced a response yet", LOGMARKER + processKey);
159
160                                         if (timeProcessEnded.longValue() != 0) {
161                                                 long elapsedSinceEnded = System.currentTimeMillis() - timeProcessEnded.longValue();
162
163                                                 if (elapsedSinceEnded > timeToWaitAfterProcessEnded) {
164                                                         endedWithNoResponse = true;
165                                                         break;
166                                                 }
167                                         }
168                                 } else {
169                                         processResponseMap(workflowResponse, responseMap);
170                                         recordEvents(processKey, workflowResponse, startTime);
171                                         return Response.status(workflowResponse.getMessageCode()).entity(workflowResponse).build();
172                                 }
173                         }
174
175                         //if we dont get response after waiting then send timeout response
176
177                         String state;
178                         String processInstanceId;
179
180                         if (processInstance == null) {
181                                 processInstanceId = "N/A";
182                                 state = "NOT STARTED";
183                         } else {
184                                 processInstanceId = processInstance.getProcessInstanceId();
185                                 state = isProcessEnded(processInstanceId) ? "ENDED" : "NOT ENDED";
186                         }
187
188                         workflowResponse.setMessage("Fail");
189                         if (endedWithNoResponse) {
190                                 workflowResponse.setResponse("Process ended without producing a response");
191                         } else {
192                                 workflowResponse.setResponse("Request timed out, process state: " + state);
193                         }
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());
203
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());
208                         
209                         workflowResponse.setMessageCode(500);
210                         recordEvents(processKey, workflowResponse, startTime);
211                         return Response.status(500).entity(workflowResponse).build();
212                 }
213         }
214
215         /**
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
219          * @return
220          */
221         private int getWaitTime(Map<String, Object> inputVariables)
222         {
223                 String timeout = inputVariables.get("mso-service-request-timeout") == null
224                         ? null : inputVariables.get("mso-service-request-timeout").toString();
225
226                 if (timeout != null) {
227                         try {
228                                 return Integer.parseInt(timeout)*1000;
229                         } catch (NumberFormatException nex) {
230                                 logger.debug("Invalid input for mso-service-request-timeout");
231                         }
232                 }
233                 return DEFAULT_WAIT_TIME;
234         }
235         
236         private void recordEvents(String processKey, WorkflowResponse response, long startTime) {
237         }
238
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"));
244                 }
245         }
246
247         private String getValueFromInputVariables(Map<String,Object> inputVariables, String key) {
248                 Object value = inputVariables.get(key);
249                 if (value == null) {
250                         return "N/A";
251                 } else {
252                         return value.toString();
253                 }
254         }
255         
256         /**
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
260          */
261         private boolean isProcessEnded(String processInstanceId) {
262                 ProcessEngineServices pes = getProcessEngineServices();
263                 try {
264                         return pes.getRuntimeService().createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult() == null ? true : false ;
265                 } catch (Exception e) {
266                         logger.debug("Exception :",e);
267                         return true;
268                 }        
269         }
270
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);
277                 }
278
279                 workflowResponse.setResponse(response);
280
281                 object = responseMap.get("ResponseCode");
282                 String responseCode = object == null ? null : String.valueOf(object);
283
284                 try {
285                         workflowResponse.setMessageCode(Integer.parseInt(responseCode));
286                 } catch(NumberFormatException nex) {
287                         logger.debug(LOGMARKER + "Failed to parse ResponseCode: " + responseCode);
288                         workflowResponse.setMessageCode(-1);
289                 }
290
291                 Object status = responseMap.get("Status");
292
293                 if ("Success".equalsIgnoreCase(String.valueOf(status))) {
294                         workflowResponse.setMessage("Success");
295                 } else if ("Fail".equalsIgnoreCase(String.valueOf(status))) {
296                         workflowResponse.setMessage("Fail");
297                 } else {
298                         logger.debug(LOGMARKER + "Unrecognized Status: " + responseCode);
299                         workflowResponse.setMessage("Fail");
300                 }
301         }
302
303         /**
304          * @version 1.0
305          * Triggers the workflow in a separate thread
306          */
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;
313
314                 public ProcessThread(Map<String, Object> inputVariables, String processKey) {
315                         this.inputVariables = inputVariables;
316                         this.processKey = processKey;
317                         this.businessKey = UUID.randomUUID().toString();
318                 }
319
320                 /**
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
326                  */
327                 public Exception getException() {
328                         return exception;
329                 }
330
331                 
332                 public ProcessInstance getProcessInstance() {
333                         return this.processInstance;
334                 }
335                 
336                 /**
337                  * Sets the process instance exception.
338                  * @param exception the exception
339                  */
340                 private void setException(Exception exception) {
341                         this.exception = exception;
342                 }
343
344                 public void run() {
345                         setLogContext(processKey, inputVariables);
346
347                         long startTime = System.currentTimeMillis();
348                         
349                         try {
350                                                 
351                                 RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
352
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);
358
359                         } catch (Exception e) {
360                                 logger.debug(LOGMARKER + "ProcessThread caught an exception executing "
361                                         + processKey + ": " + e);
362                                 setException(e);
363                         }
364                 }
365
366         }
367
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
378                         .create());
379                 }
380                 return inputVariables;
381         }
382
383         /**
384          * Attempts to get a response map from the specified process instance.
385          * @return the response map, or null if it is unavailable
386          */
387         private Map<String, Object> getResponseMap(ProcessInstance processInstance,
388                         String processKey, AtomicLong timeProcessEnded) {
389
390                 String responseMapVariable = processKey + "ResponseMap";
391                 String processInstanceId = processInstance.getId();
392
393                 // Query the runtime service to see if a response map is ready.
394
395 /*              RuntimeService runtimeService = getProcessEngineServices().getRuntimeService();
396                 List<Execution> executions = runtimeService.createExecutionQuery()
397                         .processInstanceId(processInstanceId).list();
398
399                 for (Execution execution : executions) {
400                         @SuppressWarnings("unchecked")
401                         Map<String, Object> responseMap = (Map<String, Object>)
402                                 getVariableFromExecution(runtimeService, execution.getId(),
403                                         responseMapVariable);
404
405                         if (responseMap != null) {
406                                 msoLogger.debug(LOGMARKER + "Obtained " + responseMapVariable
407                                         + " from process " + processInstanceId + " execution "
408                                         + execution.getId());
409                                 return responseMap;
410                         }
411                 }
412 */
413                 //Querying history seem to return consistent results compared to querying the runtime service
414
415                 boolean alreadyEnded = timeProcessEnded.longValue() != 0;
416
417                 if (alreadyEnded || isProcessEnded(processInstance.getId())) {
418                         if (!alreadyEnded) {
419                                 timeProcessEnded.set(System.currentTimeMillis());
420                         }
421
422                         // Query the history service to see if a response map exists.
423
424                         HistoryService historyService = getProcessEngineServices().getHistoryService();
425                         @SuppressWarnings("unchecked")
426                         Map<String, Object> responseMap = (Map<String, Object>)
427                                 getVariableFromHistory(historyService, processInstance.getId(),
428                                         responseMapVariable);
429
430                         if (responseMap != null) {
431                                 logger.debug(LOGMARKER + "Obtained " + responseMapVariable
432                                         + " from process " + processInstanceId + " history");
433                                 return responseMap;
434                         }
435
436                         // Query the history service for old-style response variables.
437
438                         String prefix = (String) getVariableFromHistory(historyService, processInstanceId, "prefix");
439
440                         if (prefix != null) {
441                                 
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);
446                                 
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");
455                                         return responseMap;
456                                 }
457                                 
458                                 
459                                 // Check for 'WorkflowException' variable
460                                 WorkflowException workflowException = null;
461                                 String workflowExceptionText = null;
462
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");
472                                                 return responseMap;
473                                         }
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");
482                                                 return responseMap;
483                                         }
484                                         
485                                 }
486                                 logger.debug(LOGMARKER + "WorkflowException: " + workflowExceptionText);
487                                 
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);
492
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");
501                                         return responseMap;
502                                 }
503         
504                                 object = getVariableFromHistory(historyService, processInstanceId, prefix + "ErrorResponse");
505                                 String errorResponse = object == null ? null : String.valueOf(object);
506                                 logger.debug(LOGMARKER + prefix + "ErrorResponse: " + errorResponse);
507
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");
516                                         return responseMap;
517                                 }
518                                 // END LEGACY SUPPORT.  TODO: REMOVE THIS CODE
519                         }
520                 }
521                 return null;
522         }
523         
524         /**
525          * Gets a variable value from the specified execution.
526          * @return the variable value, or null if the variable could not be
527          * obtained
528          */
529         private Object getVariableFromExecution(RuntimeService runtimeService,
530                         String executionId, String variableName) {
531                 try {
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);
537                         return null;
538                 }
539         }
540         /**
541          * Gets a variable value from specified historical process instance.
542          * @return the variable value, or null if the variable could not be
543          * obtained
544          */
545         private Object getVariableFromHistory(HistoryService historyService,
546                         String processInstanceId, String variableName) {
547                 try {
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,
553                                 variableName, e);
554                         return null;
555                 }
556         }
557         
558         @POST
559         @Path("/services/{processKey}/{processInstanceId}")
560         @Produces("application/json")
561         @Consumes("application/json")
562         @ApiOperation(
563                 value = "Allows for retrieval of the variables for a given process",
564                 notes = ""
565             )
566         public WorkflowResponse getProcessVariables(@PathParam("processKey") String processKey, @PathParam("processInstanceId") String processInstanceId) {
567                 //TODO filter only set of variables
568                 WorkflowResponse response = new WorkflowResponse();
569
570                 long startTime = System.currentTimeMillis();
571                 try {
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());
577                         }
578
579                         logger.debug(LOGMARKER + "***Received MSO getProcessVariables with processKey:" + processKey + " and variables: " +
580                                 variablesMap.toString());
581                         
582                         response.setVariables(variablesMap);
583                         response.setMessage("Success");
584                         response.setResponse("Successfully retrieved the variables");
585                         response.setProcessInstanceID(processInstanceId);
586
587                         logger.debug(LOGMARKER + response.getMessage() + " for processKey: " + processKey + " with response: " + response
588                                 .getResponse());
589                 } catch (Exception ex) {
590                         response.setMessage("Fail");
591                         response.setResponse("Failed to retrieve the variables," + ex.getMessage());
592                         response.setProcessInstanceID(processInstanceId);
593
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
597                                         .getResponse());
598                         logger.debug("Exception :",ex);
599                 }
600                 return response;
601         }
602 }