2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
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;
38 public class WorkflowActionBBFailure {
40 private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
41 public static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
43 private RequestsDbClient requestDbclient;
45 private WorkflowAction workflowAction;
47 protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
49 String requestId = (String) execution.getVariable("mso-request-id");
50 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
52 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
53 if (errorMsgOp.isPresent()) {
54 errorMsg = errorMsgOp.get();
56 errorMsg = "Failed to determine error message";
58 Boolean isRollback = (Boolean) execution.getVariable("isRollback");
59 if (!Boolean.TRUE.equals(isRollback)) {
60 request.setStatusMessage(errorMsg);
62 request.setRollbackStatusMessage(errorMsg);
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) {
70 "Failed to update Request db with the status message after retry or rollback has been initialized.",
75 public void updateRequestStatusToFailed(DelegateExecution execution) {
77 String requestId = (String) execution.getVariable("mso-request-id");
78 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
79 String rollbackErrorMsg = "";
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;
87 if (isRollbackFailure == null)
88 isRollbackFailure = false;
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();
101 rollbackErrorMsg = "Failed to determine rollback error message.";
103 request.setRollbackStatusMessage(rollbackErrorMsg);
104 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
105 request.setRequestStatus(Status.FAILED.toString());
107 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
108 if (errorMsgOp.isPresent()) {
109 errorMsg = errorMsgOp.get();
111 errorMsg = "Failed to determine error message";
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());
119 request.setRequestStatus(Status.FAILED.toString());
122 if (ebb != null && ebb.getBuildingBlock() != null) {
123 String flowStatus = "";
124 if (rollbackCompletedSuccessfully) {
125 flowStatus = "All Rollback flows have completed successfully";
127 flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
129 request.setFlowStatus(flowStatus);
130 execution.setVariable("flowStatus", flowStatus);
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);
142 private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
143 String errorMsg = null;
145 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
146 if (exception != null && (exception.getErrorMessage() != null || !exception.getErrorMessage().equals(""))) {
147 errorMsg = exception.getErrorMessage();
149 if (errorMsg == null || errorMsg.equals("")) {
150 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
152 if (errorMsg == null) {
153 throw new IllegalStateException(
154 "could not find WorkflowException or WorkflowExceptionErrorMessage in execution");
156 return Optional.of(errorMsg);
157 } catch (Exception ex) {
158 logger.error("Failed to extract workflow exception from execution.", ex);
160 return Optional.empty();
163 public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
164 execution.setVariable("isRollbackComplete", true);
165 updateRequestStatusToFailed(execution);
168 public void abortCallErrorHandling(DelegateExecution execution) {
169 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
171 throw new BpmnError(msg);