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.db.request.beans.InfraActiveRequests;
30 import org.onap.so.db.request.client.RequestsDbClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
37 public class WorkflowActionBBFailure {
39 private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
41 private RequestsDbClient requestDbclient;
43 private WorkflowAction workflowAction;
45 protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
47 String requestId = (String) execution.getVariable("mso-request-id");
48 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
50 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
51 if (errorMsgOp.isPresent()) {
52 errorMsg = errorMsgOp.get();
54 errorMsg = "Failed to determine error message";
56 request.setStatusMessage(errorMsg);
57 request.setProgress(Long.valueOf(100));
58 request.setLastModifiedBy("CamundaBPMN");
59 request.setEndTime(new Timestamp(System.currentTimeMillis()));
60 requestDbclient.updateInfraActiveRequests(request);
61 } catch (Exception e) {
63 "Failed to update Request db with the status message after retry or rollback has been initialized.",
68 public void updateRequestStatusToFailed(DelegateExecution execution) {
70 String requestId = (String) execution.getVariable("mso-request-id");
71 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
72 String rollbackErrorMsg = "";
74 Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
75 Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
76 ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
77 if (rollbackCompletedSuccessfully == null)
78 rollbackCompletedSuccessfully = false;
80 if (isRollbackFailure == null)
81 isRollbackFailure = false;
83 if (rollbackCompletedSuccessfully) {
84 rollbackErrorMsg = "Rollback has been completed successfully.";
85 request.setRollbackStatusMessage(rollbackErrorMsg);
86 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
87 } else if (isRollbackFailure) {
88 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
89 if (rollbackErrorMsgOp.isPresent()) {
90 rollbackErrorMsg = rollbackErrorMsgOp.get();
92 rollbackErrorMsg = "Failed to determine rollback error message.";
94 request.setRollbackStatusMessage(rollbackErrorMsg);
95 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
97 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
98 if (errorMsgOp.isPresent()) {
99 errorMsg = errorMsgOp.get();
101 errorMsg = "Failed to determine error message";
103 request.setStatusMessage(errorMsg);
104 execution.setVariable("ErrorMessage", errorMsg);
106 if (ebb != null && ebb.getBuildingBlock() != null) {
107 String flowStatus = "";
108 if (rollbackCompletedSuccessfully) {
109 flowStatus = "All Rollback flows have completed successfully";
111 flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
113 request.setFlowStatus(flowStatus);
114 execution.setVariable("flowStatus", flowStatus);
117 request.setProgress(Long.valueOf(100));
118 request.setRequestStatus("FAILED");
119 request.setLastModifiedBy("CamundaBPMN");
120 request.setEndTime(new Timestamp(System.currentTimeMillis()));
121 requestDbclient.updateInfraActiveRequests(request);
122 } catch (Exception e) {
123 workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
127 private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
128 String errorMsg = null;
130 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
131 if (exception != null && (exception.getErrorMessage() != null || !exception.getErrorMessage().equals(""))) {
132 errorMsg = exception.getErrorMessage();
134 if (errorMsg == null || errorMsg.equals("")) {
135 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
137 if (errorMsg == null) {
138 throw new IllegalStateException(
139 "could not find WorkflowException or WorkflowExceptionErrorMessage in execution");
141 return Optional.of(errorMsg);
142 } catch (Exception ex) {
143 logger.error("Failed to extract workflow exception from execution.", ex);
145 return Optional.empty();
148 public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
149 execution.setVariable("isRollbackComplete", true);
150 updateRequestStatusToFailed(execution);
153 public void abortCallErrorHandling(DelegateExecution execution) {
154 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
156 throw new BpmnError(msg);