Merge "Skip cloud regions and fix macro"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBFailure.java
1 package org.onap.so.bpmn.infrastructure.workflow.tasks;
2
3 import java.util.Optional;
4
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;
14
15 @Component
16 public class WorkflowActionBBFailure {
17
18         private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
19         @Autowired
20         private RequestsDbClient requestDbclient;
21         @Autowired
22         private WorkflowAction workflowAction;
23         
24         protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
25                 try {
26                         String requestId = (String) execution.getVariable("mso-request-id");
27                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
28                         String errorMsg = "";
29                         Optional<String> errorMsgOp = retrieveErrorMessage(execution);
30                         if(errorMsgOp.isPresent()){
31                                 errorMsg = errorMsgOp.get();
32                         }else{
33                                 errorMsg = "Failed to determine error message";
34                         }
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);
39                 }
40         }
41         
42         public void updateRequestStatusToFailed(DelegateExecution execution) {
43                 try {
44                         String requestId = (String) execution.getVariable("mso-request-id");
45                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
46                         String rollbackErrorMsg = "";
47                         String errorMsg = "";
48                         Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
49                         Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
50                         
51                         if(rollbackCompletedSuccessfully==null)
52                                 rollbackCompletedSuccessfully = false;
53                         
54                         if(isRollbackFailure==null)
55                                 isRollbackFailure = false;
56                         
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();
65                                 }else{
66                                         rollbackErrorMsg = "Failed to determine rollback error message.";
67                                 }
68                                 request.setRollbackStatusMessage(rollbackErrorMsg);
69                                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
70                         }else{
71                                 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
72                                 if(errorMsgOp.isPresent()){
73                                         errorMsg = errorMsgOp.get();
74                                 }else{
75                                         errorMsg = "Failed to determine error message";
76                                 }
77                                 request.setStatusMessage(errorMsg);
78                                 execution.setVariable("ErrorMessage", errorMsg);
79                         }
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);
86                 }
87         }
88         
89         private Optional<String> retrieveErrorMessage (DelegateExecution execution){
90                 String errorMsg = "";
91                 try {
92                         WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
93                         if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){
94                                 errorMsg = exception.getErrorMessage();
95                         }
96                         if(errorMsg == null || errorMsg.equals("")){
97                                 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
98                         }
99                         return Optional.of(errorMsg);
100                 } catch (Exception ex) {
101                         logger.error("Failed to extract workflow exception from execution.",ex);
102                 }
103                 return Optional.of(errorMsg);
104         }
105         
106         public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
107                 execution.setVariable("isRollbackComplete", true);
108                 updateRequestStatusToFailed(execution);
109         }
110
111         public void abortCallErrorHandling(DelegateExecution execution) {
112                 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
113                 logger.error(msg);
114                 throw new BpmnError(msg);
115         }
116 }