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