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