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