1812e503281e2f5acfb8ea615a5ba672f28b3298
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBFailure.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.infrastructure.workflow.tasks;
22
23 import java.util.Optional;
24
25 import org.camunda.bpm.engine.delegate.BpmnError;
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.onap.so.bpmn.core.WorkflowException;
28 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
29 import org.onap.so.db.request.beans.InfraActiveRequests;
30 import org.onap.so.db.request.client.RequestsDbClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35
36 @Component
37 public class WorkflowActionBBFailure {
38
39         private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
40         @Autowired
41         private RequestsDbClient requestDbclient;
42         @Autowired
43         private WorkflowAction workflowAction;
44         
45         protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
46                 try {
47                         String requestId = (String) execution.getVariable("mso-request-id");
48                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
49                         String errorMsg = "";
50                         Optional<String> errorMsgOp = retrieveErrorMessage(execution);
51                         if(errorMsgOp.isPresent()){
52                                 errorMsg = errorMsgOp.get();
53                         }else{
54                                 errorMsg = "Failed to determine error message";
55                         }
56                         request.setStatusMessage(errorMsg);
57                         requestDbclient.updateInfraActiveRequests(request);
58                 } catch (Exception e) {
59                         logger.error("Failed to update Request db with the status message after retry or rollback has been initialized.",e);
60                 }
61         }
62         
63         public void updateRequestStatusToFailed(DelegateExecution execution) {
64                 try {
65                         String requestId = (String) execution.getVariable("mso-request-id");
66                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
67                         String rollbackErrorMsg = "";
68                         String errorMsg = "";
69                         Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
70                         Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
71                         ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
72                         if(rollbackCompletedSuccessfully==null)
73                                 rollbackCompletedSuccessfully = false;
74                         
75                         if(isRollbackFailure==null)
76                                 isRollbackFailure = false;
77                         
78                         if(rollbackCompletedSuccessfully){
79                                 rollbackErrorMsg = "Rollback has been completed successfully.";
80                                 request.setRollbackStatusMessage(rollbackErrorMsg);
81                                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
82                         }else if(isRollbackFailure){
83                                 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
84                                 if(rollbackErrorMsgOp.isPresent()){
85                                         rollbackErrorMsg = rollbackErrorMsgOp.get();
86                                 }else{
87                                         rollbackErrorMsg = "Failed to determine rollback error message.";
88                                 }
89                                 request.setRollbackStatusMessage(rollbackErrorMsg);
90                                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
91                         }else{
92                                 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
93                                 if(errorMsgOp.isPresent()){
94                                         errorMsg = errorMsgOp.get();
95                                 }else{
96                                         errorMsg = "Failed to determine error message";
97                                 }
98                                 request.setStatusMessage(errorMsg);
99                                 execution.setVariable("ErrorMessage", errorMsg);
100                         }
101                         if(ebb!=null && ebb.getBuildingBlock()!=null){
102                                 String flowStatus = "";
103                                 if(rollbackCompletedSuccessfully){
104                                         flowStatus = "All Rollback flows have completed successfully";
105                                 }else{
106                                         flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
107                                 }
108                                 request.setFlowStatus(flowStatus);
109                                 execution.setVariable("flowStatus", flowStatus);
110                         }
111
112                         request.setProgress(Long.valueOf(100));
113                         request.setRequestStatus("FAILED");
114                         request.setLastModifiedBy("CamundaBPMN");
115                         requestDbclient.updateInfraActiveRequests(request);
116                 } catch (Exception e) {
117                         workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
118                 }
119         }
120         
121         private Optional<String> retrieveErrorMessage (DelegateExecution execution){
122                 String errorMsg = "";
123                 try {
124                         WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
125                         if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){
126                                 errorMsg = exception.getErrorMessage();
127                         }
128                         if(errorMsg == null || errorMsg.equals("")){
129                                 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
130                         }
131                         return Optional.of(errorMsg);
132                 } catch (Exception ex) {
133                         logger.error("Failed to extract workflow exception from execution.",ex);
134                 }
135                 return Optional.empty();
136         }
137         
138         public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
139                 execution.setVariable("isRollbackComplete", true);
140                 updateRequestStatusToFailed(execution);
141         }
142
143         public void abortCallErrorHandling(DelegateExecution execution) {
144                 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
145                 logger.error(msg);
146                 throw new BpmnError(msg);
147         }
148 }