1 package org.onap.so.bpmn.infrastructure.workflow.tasks;
3 import java.util.Optional;
5 import org.camunda.bpm.engine.delegate.BpmnError;
6 import org.camunda.bpm.engine.delegate.DelegateExecution;
7 import org.onap.so.bpmn.core.WorkflowException;
8 import org.onap.so.db.request.beans.InfraActiveRequests;
9 import org.onap.so.db.request.client.RequestsDbClient;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.stereotype.Component;
16 public class WorkflowActionBBFailure {
18 private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
20 private RequestsDbClient requestDbclient;
22 private WorkflowAction workflowAction;
24 protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
26 String requestId = (String) execution.getVariable("mso-request-id");
27 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
29 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
30 if(errorMsgOp.isPresent()){
31 errorMsg = errorMsgOp.get();
33 errorMsg = "Failed to determine error message";
35 request.setStatusMessage(errorMsg);
36 requestDbclient.updateInfraActiveRequests(request);
37 } catch (Exception e) {
38 logger.error("Failed to update Request db with the status message after retry or rollback has been initialized.",e);
42 public void updateRequestStatusToFailed(DelegateExecution execution) {
44 String requestId = (String) execution.getVariable("mso-request-id");
45 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
46 String rollbackErrorMsg = "";
48 Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
49 Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
51 if(rollbackCompletedSuccessfully==null)
52 rollbackCompletedSuccessfully = false;
54 if(isRollbackFailure==null)
55 isRollbackFailure = false;
57 if(rollbackCompletedSuccessfully){
58 rollbackErrorMsg = "Rollback has been completed successfully.";
59 request.setRollbackStatusMessage(rollbackErrorMsg);
60 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
61 }else if(isRollbackFailure){
62 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
63 if(rollbackErrorMsgOp.isPresent()){
64 rollbackErrorMsg = rollbackErrorMsgOp.get();
66 rollbackErrorMsg = "Failed to determine rollback error message.";
68 request.setRollbackStatusMessage(rollbackErrorMsg);
69 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
71 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
72 if(errorMsgOp.isPresent()){
73 errorMsg = errorMsgOp.get();
75 errorMsg = "Failed to determine error message";
77 request.setStatusMessage(errorMsg);
78 execution.setVariable("ErrorMessage", errorMsg);
80 request.setProgress(Long.valueOf(100));
81 request.setRequestStatus("FAILED");
82 request.setLastModifiedBy("CamundaBPMN");
83 requestDbclient.updateInfraActiveRequests(request);
84 } catch (Exception e) {
85 workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
89 private Optional<String> retrieveErrorMessage (DelegateExecution execution){
92 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
93 if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){
94 errorMsg = exception.getErrorMessage();
96 if(errorMsg == null || errorMsg.equals("")){
97 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
99 return Optional.of(errorMsg);
100 } catch (Exception ex) {
101 logger.error("Failed to extract workflow exception from execution.",ex);
103 return Optional.of(errorMsg);
106 public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
107 execution.setVariable("isRollbackComplete", true);
108 updateRequestStatusToFailed(execution);
111 public void abortCallErrorHandling(DelegateExecution execution) {
112 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
114 throw new BpmnError(msg);