Bump the version for the SO core to Kohn
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBFailure.java
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 String DEACTIVATE_FABRIC_CONFIGURATION_FLOW = "DeactivateFabricConfigurationBB";
43     private static final String UNASSIGN_FABRIC_CONFIGURATION_FLOW = "UnassignFabricConfigurationBB";
44     private static final String DELETE_FABRIC_CONFIGURATION_FLOW = "DeleteFabricConfigurationBB";
45     private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
46     public static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
47     @Autowired
48     private RequestsDbClient requestDbclient;
49     @Autowired
50     private WorkflowAction workflowAction;
51
52     protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
53         try {
54             String requestId = (String) execution.getVariable("mso-request-id");
55             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
56             String errorMsg = "";
57             Optional<String> errorMsgOp = retrieveErrorMessage(execution);
58             if (errorMsgOp.isPresent()) {
59                 errorMsg = errorMsgOp.get();
60             } else {
61                 errorMsg = "Failed to determine error message";
62             }
63             Boolean isRollback = (Boolean) execution.getVariable("isRollback");
64             if (!Boolean.TRUE.equals(isRollback)) {
65                 request.setStatusMessage(errorMsg);
66             } else {
67                 request.setRollbackStatusMessage(errorMsg);
68             }
69             request.setProgress(Long.valueOf(100));
70             request.setLastModifiedBy("CamundaBPMN");
71             request.setEndTime(new Timestamp(System.currentTimeMillis()));
72             requestDbclient.updateInfraActiveRequests(request);
73         } catch (Exception e) {
74             logger.error(
75                     "Failed to update Request db with the status message after retry or rollback has been initialized.",
76                     e);
77         }
78     }
79
80     public void updateRequestStatusToFailed(DelegateExecution execution) {
81         try {
82             String requestId = (String) execution.getVariable("mso-request-id");
83             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
84             String rollbackErrorMsg = "";
85             String errorMsg = "";
86             Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
87             Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
88             ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
89             if (rollbackCompletedSuccessfully == null)
90                 rollbackCompletedSuccessfully = false;
91
92             if (isRollbackFailure == null)
93                 isRollbackFailure = false;
94
95             if (rollbackCompletedSuccessfully) {
96                 rollbackErrorMsg = "Rollback has been completed successfully.";
97                 request.setRollbackStatusMessage(rollbackErrorMsg);
98                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
99                 String rollbackTargetState = (String) execution.getVariable(ROLLBACK_TARGET_STATE);
100                 request.setRequestStatus(rollbackTargetState);
101             } else if (isRollbackFailure) {
102                 if (ebb != null && ebb.getBuildingBlock() != null && ebb.getBuildingBlock().getBpmnFlowName() != null) {
103                     String flowName = ebb.getBuildingBlock().getBpmnFlowName();
104                     if (DEACTIVATE_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)
105                             || UNASSIGN_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)
106                             || DELETE_FABRIC_CONFIGURATION_FLOW.equalsIgnoreCase(flowName)) {
107                         String statusMessage = String.format(
108                                 "%s Warning: The vf-module is active but configuration was not removed completely for one or more VMs.",
109                                 request.getStatusMessage());
110                         request.setStatusMessage(statusMessage);
111                     }
112                 }
113                 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
114                 if (rollbackErrorMsgOp.isPresent()) {
115                     rollbackErrorMsg = rollbackErrorMsgOp.get();
116                 } else {
117                     rollbackErrorMsg = "Failed to determine rollback error message.";
118                 }
119                 request.setRollbackStatusMessage(rollbackErrorMsg);
120                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
121                 request.setRequestStatus(Status.FAILED.toString());
122             } else {
123                 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
124                 if (errorMsgOp.isPresent()) {
125                     errorMsg = errorMsgOp.get();
126                 } else {
127                     errorMsg = "Failed to determine error message";
128                 }
129                 request.setStatusMessage(errorMsg);
130                 execution.setVariable("ErrorMessage", errorMsg);
131                 String handlingCode = (String) execution.getVariable("handlingCode");
132                 if ("Abort".equalsIgnoreCase(handlingCode)) {
133                     request.setRequestStatus(Status.ABORTED.toString());
134                 } else {
135                     request.setRequestStatus(Status.FAILED.toString());
136                 }
137             }
138             if (ebb != null && ebb.getBuildingBlock() != null) {
139                 String flowStatus = "";
140                 if (rollbackCompletedSuccessfully) {
141                     flowStatus = "All Rollback flows have completed successfully";
142                 } else {
143                     flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
144                 }
145                 request.setFlowStatus(flowStatus);
146                 execution.setVariable("flowStatus", flowStatus);
147             }
148
149             request.setProgress(Long.valueOf(100));
150             request.setLastModifiedBy("CamundaBPMN");
151             request.setEndTime(new Timestamp(System.currentTimeMillis()));
152             requestDbclient.updateInfraActiveRequests(request);
153         } catch (Exception e) {
154             workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
155         }
156     }
157
158     private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
159         String errorMsg = null;
160         try {
161             WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
162             if (exception != null && (exception.getErrorMessage() != null || !"".equals(exception.getErrorMessage()))) {
163                 errorMsg = exception.getErrorMessage();
164             }
165             if (errorMsg == null || "".equals(errorMsg)) {
166                 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
167             }
168             if (errorMsg == null) {
169                 throw new IllegalStateException(
170                         "could not find WorkflowException or WorkflowExceptionErrorMessage in execution");
171             }
172             return Optional.of(errorMsg);
173         } catch (Exception ex) {
174             logger.error("Failed to extract workflow exception from execution.", ex);
175         }
176         return Optional.empty();
177     }
178
179     public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
180         execution.setVariable("isRollbackComplete", true);
181         updateRequestStatusToFailed(execution);
182     }
183
184     public void abortCallErrorHandling() {
185         String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
186         logger.error(msg);
187         throw new BpmnError(msg);
188     }
189 }