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 Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
43 public static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
45 private RequestsDbClient requestDbclient;
47 private WorkflowAction workflowAction;
49 protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
51 String requestId = (String) execution.getVariable("mso-request-id");
52 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
54 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
55 if (errorMsgOp.isPresent()) {
56 errorMsg = errorMsgOp.get();
58 errorMsg = "Failed to determine error message";
60 Boolean isRollback = (Boolean) execution.getVariable("isRollback");
61 if (!Boolean.TRUE.equals(isRollback)) {
62 request.setStatusMessage(errorMsg);
64 request.setRollbackStatusMessage(errorMsg);
66 request.setProgress(Long.valueOf(100));
67 request.setLastModifiedBy("CamundaBPMN");
68 request.setEndTime(new Timestamp(System.currentTimeMillis()));
69 requestDbclient.updateInfraActiveRequests(request);
70 } catch (Exception e) {
72 "Failed to update Request db with the status message after retry or rollback has been initialized.",
77 public void updateRequestStatusToFailed(DelegateExecution execution) {
79 String requestId = (String) execution.getVariable("mso-request-id");
80 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
81 String rollbackErrorMsg = "";
83 Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
84 Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
85 ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
86 if (rollbackCompletedSuccessfully == null)
87 rollbackCompletedSuccessfully = false;
89 if (isRollbackFailure == null)
90 isRollbackFailure = false;
92 if (rollbackCompletedSuccessfully) {
93 rollbackErrorMsg = "Rollback has been completed successfully.";
94 request.setRollbackStatusMessage(rollbackErrorMsg);
95 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
96 String rollbackTargetState = (String) execution.getVariable(ROLLBACK_TARGET_STATE);
97 request.setRequestStatus(rollbackTargetState);
98 } else if (isRollbackFailure) {
99 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
100 if (rollbackErrorMsgOp.isPresent()) {
101 rollbackErrorMsg = rollbackErrorMsgOp.get();
103 rollbackErrorMsg = "Failed to determine rollback error message.";
105 request.setRollbackStatusMessage(rollbackErrorMsg);
106 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
107 request.setRequestStatus(Status.FAILED.toString());
109 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
110 if (errorMsgOp.isPresent()) {
111 errorMsg = errorMsgOp.get();
113 errorMsg = "Failed to determine error message";
115 request.setStatusMessage(errorMsg);
116 execution.setVariable("ErrorMessage", errorMsg);
117 String handlingCode = (String) execution.getVariable("handlingCode");
118 if ("Abort".equalsIgnoreCase(handlingCode)) {
119 request.setRequestStatus(Status.ABORTED.toString());
121 request.setRequestStatus(Status.FAILED.toString());
124 if (ebb != null && ebb.getBuildingBlock() != null) {
125 String flowStatus = "";
126 if (rollbackCompletedSuccessfully) {
127 flowStatus = "All Rollback flows have completed successfully";
129 flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
131 request.setFlowStatus(flowStatus);
132 execution.setVariable("flowStatus", flowStatus);
135 request.setProgress(Long.valueOf(100));
136 request.setLastModifiedBy("CamundaBPMN");
137 request.setEndTime(new Timestamp(System.currentTimeMillis()));
138 requestDbclient.updateInfraActiveRequests(request);
139 } catch (Exception e) {
140 workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
144 private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
145 String errorMsg = null;
147 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
148 if (exception != null && (exception.getErrorMessage() != null || !"".equals(exception.getErrorMessage()))) {
149 errorMsg = exception.getErrorMessage();
151 if (errorMsg == null || "".equals(errorMsg)) {
152 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
154 if (errorMsg == null) {
155 throw new IllegalStateException(
156 "could not find WorkflowException or WorkflowExceptionErrorMessage in execution");
158 return Optional.of(errorMsg);
159 } catch (Exception ex) {
160 logger.error("Failed to extract workflow exception from execution.", ex);
162 return Optional.empty();
165 public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
166 execution.setVariable("isRollbackComplete", true);
167 updateRequestStatusToFailed(execution);
170 public void abortCallErrorHandling() {
171 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
173 throw new BpmnError(msg);