5f357f5478889a36c9e1f5bfcec4f9536fa8f7ab
[so.git] /
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.sql.Timestamp;
24 import java.util.Optional;
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             request.setProgress(Long.valueOf(100));
58             request.setRequestStatus("FAILED");
59             request.setLastModifiedBy("CamundaBPMN");
60             request.setEndTime(new Timestamp(System.currentTimeMillis()));
61             requestDbclient.updateInfraActiveRequests(request);
62         } catch (Exception e) {
63             logger.error(
64                     "Failed to update Request db with the status message after retry or rollback has been initialized.",
65                     e);
66         }
67     }
68
69     public void updateRequestStatusToFailed(DelegateExecution execution) {
70         try {
71             String requestId = (String) execution.getVariable("mso-request-id");
72             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
73             String rollbackErrorMsg = "";
74             String errorMsg = "";
75             Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
76             Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
77             ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
78             if (rollbackCompletedSuccessfully == null)
79                 rollbackCompletedSuccessfully = false;
80
81             if (isRollbackFailure == null)
82                 isRollbackFailure = false;
83
84             if (rollbackCompletedSuccessfully) {
85                 rollbackErrorMsg = "Rollback has been completed successfully.";
86                 request.setRollbackStatusMessage(rollbackErrorMsg);
87                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
88             } else if (isRollbackFailure) {
89                 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
90                 if (rollbackErrorMsgOp.isPresent()) {
91                     rollbackErrorMsg = rollbackErrorMsgOp.get();
92                 } else {
93                     rollbackErrorMsg = "Failed to determine rollback error message.";
94                 }
95                 request.setRollbackStatusMessage(rollbackErrorMsg);
96                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
97             } else {
98                 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
99                 if (errorMsgOp.isPresent()) {
100                     errorMsg = errorMsgOp.get();
101                 } else {
102                     errorMsg = "Failed to determine error message";
103                 }
104                 request.setStatusMessage(errorMsg);
105                 execution.setVariable("ErrorMessage", errorMsg);
106             }
107             if (ebb != null && ebb.getBuildingBlock() != null) {
108                 String flowStatus = "";
109                 if (rollbackCompletedSuccessfully) {
110                     flowStatus = "All Rollback flows have completed successfully";
111                 } else {
112                     flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
113                 }
114                 request.setFlowStatus(flowStatus);
115                 execution.setVariable("flowStatus", flowStatus);
116             }
117
118             request.setProgress(Long.valueOf(100));
119             request.setRequestStatus("FAILED");
120             request.setLastModifiedBy("CamundaBPMN");
121             request.setEndTime(new Timestamp(System.currentTimeMillis()));
122             requestDbclient.updateInfraActiveRequests(request);
123         } catch (Exception e) {
124             workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
125         }
126     }
127
128     private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
129         String errorMsg = "";
130         try {
131             WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
132             if (exception != null && (exception.getErrorMessage() != null || !exception.getErrorMessage().equals(""))) {
133                 errorMsg = exception.getErrorMessage();
134             }
135             if (errorMsg == null || errorMsg.equals("")) {
136                 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
137             }
138             return Optional.of(errorMsg);
139         } catch (Exception ex) {
140             logger.error("Failed to extract workflow exception from execution.", ex);
141         }
142         return Optional.empty();
143     }
144
145     public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
146         execution.setVariable("isRollbackComplete", true);
147         updateRequestStatusToFailed(execution);
148     }
149
150     public void abortCallErrorHandling(DelegateExecution execution) {
151         String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
152         logger.error(msg);
153         throw new BpmnError(msg);
154     }
155 }