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