be3e06c9ea8bbf83ff35e2f72691b2f20add8556
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.workflow.tasks;
24
25 import java.sql.Timestamp;
26 import java.util.Optional;
27 import org.camunda.bpm.engine.delegate.BpmnError;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.core.WorkflowException;
30 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
31 import org.onap.so.constants.Status;
32 import org.onap.so.db.request.beans.InfraActiveRequests;
33 import org.onap.so.db.request.client.RequestsDbClient;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class WorkflowActionBBFailure {
41
42     private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
43     public static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
44     @Autowired
45     private RequestsDbClient requestDbclient;
46     @Autowired
47     private WorkflowAction workflowAction;
48
49     protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
50         try {
51             String requestId = (String) execution.getVariable("mso-request-id");
52             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
53             String errorMsg = "";
54             Optional<String> errorMsgOp = retrieveErrorMessage(execution);
55             if (errorMsgOp.isPresent()) {
56                 errorMsg = errorMsgOp.get();
57             } else {
58                 errorMsg = "Failed to determine error message";
59             }
60             Boolean isRollback = (Boolean) execution.getVariable("isRollback");
61             if (!Boolean.TRUE.equals(isRollback)) {
62                 request.setStatusMessage(errorMsg);
63             } else {
64                 request.setRollbackStatusMessage(errorMsg);
65             }
66             request.setProgress(Long.valueOf(100));
67             request.setLastModifiedBy("CamundaBPMN");
68             request.setEndTime(new Timestamp(System.currentTimeMillis()));
69             requestDbclient.updateInfraActiveRequests(request);
70         } catch (Exception e) {
71             logger.error(
72                     "Failed to update Request db with the status message after retry or rollback has been initialized.",
73                     e);
74         }
75     }
76
77     public void updateRequestStatusToFailed(DelegateExecution execution) {
78         try {
79             String requestId = (String) execution.getVariable("mso-request-id");
80             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
81             String rollbackErrorMsg = "";
82             String errorMsg = "";
83             Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
84             Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
85             ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
86             if (rollbackCompletedSuccessfully == null)
87                 rollbackCompletedSuccessfully = false;
88
89             if (isRollbackFailure == null)
90                 isRollbackFailure = false;
91
92             if (rollbackCompletedSuccessfully) {
93                 rollbackErrorMsg = "Rollback has been completed successfully.";
94                 request.setRollbackStatusMessage(rollbackErrorMsg);
95                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
96                 String rollbackTargetState = (String) execution.getVariable(ROLLBACK_TARGET_STATE);
97                 request.setRequestStatus(rollbackTargetState);
98             } else if (isRollbackFailure) {
99                 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
100                 if (rollbackErrorMsgOp.isPresent()) {
101                     rollbackErrorMsg = rollbackErrorMsgOp.get();
102                 } else {
103                     rollbackErrorMsg = "Failed to determine rollback error message.";
104                 }
105                 request.setRollbackStatusMessage(rollbackErrorMsg);
106                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
107                 request.setRequestStatus(Status.FAILED.toString());
108             } else {
109                 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
110                 if (errorMsgOp.isPresent()) {
111                     errorMsg = errorMsgOp.get();
112                 } else {
113                     errorMsg = "Failed to determine error message";
114                 }
115                 request.setStatusMessage(errorMsg);
116                 execution.setVariable("ErrorMessage", errorMsg);
117                 String handlingCode = (String) execution.getVariable("handlingCode");
118                 if ("Abort".equalsIgnoreCase(handlingCode)) {
119                     request.setRequestStatus(Status.ABORTED.toString());
120                 } else {
121                     request.setRequestStatus(Status.FAILED.toString());
122                 }
123             }
124             if (ebb != null && ebb.getBuildingBlock() != null) {
125                 String flowStatus = "";
126                 if (rollbackCompletedSuccessfully) {
127                     flowStatus = "All Rollback flows have completed successfully";
128                 } else {
129                     flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
130                 }
131                 request.setFlowStatus(flowStatus);
132                 execution.setVariable("flowStatus", flowStatus);
133             }
134
135             request.setProgress(Long.valueOf(100));
136             request.setLastModifiedBy("CamundaBPMN");
137             request.setEndTime(new Timestamp(System.currentTimeMillis()));
138             requestDbclient.updateInfraActiveRequests(request);
139         } catch (Exception e) {
140             workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
141         }
142     }
143
144     private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
145         String errorMsg = null;
146         try {
147             WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
148             if (exception != null && (exception.getErrorMessage() != null || !"".equals(exception.getErrorMessage()))) {
149                 errorMsg = exception.getErrorMessage();
150             }
151             if (errorMsg == null || "".equals(errorMsg)) {
152                 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
153             }
154             if (errorMsg == null) {
155                 throw new IllegalStateException(
156                         "could not find WorkflowException or WorkflowExceptionErrorMessage in execution");
157             }
158             return Optional.of(errorMsg);
159         } catch (Exception ex) {
160             logger.error("Failed to extract workflow exception from execution.", ex);
161         }
162         return Optional.empty();
163     }
164
165     public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
166         execution.setVariable("isRollbackComplete", true);
167         updateRequestStatusToFailed(execution);
168     }
169
170     public void abortCallErrorHandling() {
171         String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
172         logger.error(msg);
173         throw new BpmnError(msg);
174     }
175 }