2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure.workflow.tasks;
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;
40 public class WorkflowActionBBFailure {
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";
48 private RequestsDbClient requestDbclient;
50 private WorkflowAction workflowAction;
52 protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
54 String requestId = (String) execution.getVariable("mso-request-id");
55 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
57 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
58 if (errorMsgOp.isPresent()) {
59 errorMsg = errorMsgOp.get();
61 errorMsg = "Failed to determine error message";
63 Boolean isRollback = (Boolean) execution.getVariable("isRollback");
64 if (!Boolean.TRUE.equals(isRollback)) {
65 request.setStatusMessage(errorMsg);
67 request.setRollbackStatusMessage(errorMsg);
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) {
75 "Failed to update Request db with the status message after retry or rollback has been initialized.",
80 public void updateRequestStatusToFailed(DelegateExecution execution) {
82 String requestId = (String) execution.getVariable("mso-request-id");
83 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
84 String rollbackErrorMsg = "";
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;
92 if (isRollbackFailure == null)
93 isRollbackFailure = false;
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);
113 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
114 if (rollbackErrorMsgOp.isPresent()) {
115 rollbackErrorMsg = rollbackErrorMsgOp.get();
117 rollbackErrorMsg = "Failed to determine rollback error message.";
119 request.setRollbackStatusMessage(rollbackErrorMsg);
120 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
121 request.setRequestStatus(Status.FAILED.toString());
123 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
124 if (errorMsgOp.isPresent()) {
125 errorMsg = errorMsgOp.get();
127 errorMsg = "Failed to determine error message";
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());
135 request.setRequestStatus(Status.FAILED.toString());
138 if (ebb != null && ebb.getBuildingBlock() != null) {
139 String flowStatus = "";
140 if (rollbackCompletedSuccessfully) {
141 flowStatus = "All Rollback flows have completed successfully";
143 flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
145 request.setFlowStatus(flowStatus);
146 execution.setVariable("flowStatus", flowStatus);
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);
158 private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
159 String errorMsg = null;
161 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
162 if (exception != null && (exception.getErrorMessage() != null || !"".equals(exception.getErrorMessage()))) {
163 errorMsg = exception.getErrorMessage();
165 if (errorMsg == null || "".equals(errorMsg)) {
166 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
168 if (errorMsg == null) {
169 throw new IllegalStateException(
170 "could not find WorkflowException or WorkflowExceptionErrorMessage in execution");
172 return Optional.of(errorMsg);
173 } catch (Exception ex) {
174 logger.error("Failed to extract workflow exception from execution.", ex);
176 return Optional.empty();
179 public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
180 execution.setVariable("isRollbackComplete", true);
181 updateRequestStatusToFailed(execution);
184 public void abortCallErrorHandling() {
185 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
187 throw new BpmnError(msg);