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.util.Optional;
24 import org.camunda.bpm.engine.delegate.BpmnError;
25 import org.camunda.bpm.engine.delegate.DelegateExecution;
26 import org.onap.so.bpmn.core.WorkflowException;
27 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
28 import org.onap.so.db.request.beans.InfraActiveRequests;
29 import org.onap.so.db.request.client.RequestsDbClient;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
36 public class WorkflowActionBBFailure {
38 private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
40 private RequestsDbClient requestDbclient;
42 private WorkflowAction workflowAction;
44 protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
46 String requestId = (String) execution.getVariable("mso-request-id");
47 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
49 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
50 if (errorMsgOp.isPresent()) {
51 errorMsg = errorMsgOp.get();
53 errorMsg = "Failed to determine error message";
55 request.setStatusMessage(errorMsg);
56 requestDbclient.updateInfraActiveRequests(request);
57 } catch (Exception e) {
59 "Failed to update Request db with the status message after retry or rollback has been initialized.",
64 public void updateRequestStatusToFailed(DelegateExecution execution) {
66 String requestId = (String) execution.getVariable("mso-request-id");
67 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
68 String rollbackErrorMsg = "";
70 Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
71 Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
72 ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
73 if (rollbackCompletedSuccessfully == null)
74 rollbackCompletedSuccessfully = false;
76 if (isRollbackFailure == null)
77 isRollbackFailure = false;
79 if (rollbackCompletedSuccessfully) {
80 rollbackErrorMsg = "Rollback has been completed successfully.";
81 request.setRollbackStatusMessage(rollbackErrorMsg);
82 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
83 } else if (isRollbackFailure) {
84 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
85 if (rollbackErrorMsgOp.isPresent()) {
86 rollbackErrorMsg = rollbackErrorMsgOp.get();
88 rollbackErrorMsg = "Failed to determine rollback error message.";
90 request.setRollbackStatusMessage(rollbackErrorMsg);
91 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
93 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
94 if (errorMsgOp.isPresent()) {
95 errorMsg = errorMsgOp.get();
97 errorMsg = "Failed to determine error message";
99 request.setStatusMessage(errorMsg);
100 execution.setVariable("ErrorMessage", errorMsg);
102 if (ebb != null && ebb.getBuildingBlock() != null) {
103 String flowStatus = "";
104 if (rollbackCompletedSuccessfully) {
105 flowStatus = "All Rollback flows have completed successfully";
107 flowStatus = ebb.getBuildingBlock().getBpmnFlowName() + " has failed.";
109 request.setFlowStatus(flowStatus);
110 execution.setVariable("flowStatus", flowStatus);
113 request.setProgress(Long.valueOf(100));
114 request.setRequestStatus("FAILED");
115 request.setLastModifiedBy("CamundaBPMN");
116 requestDbclient.updateInfraActiveRequests(request);
117 } catch (Exception e) {
118 workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
122 private Optional<String> retrieveErrorMessage(DelegateExecution execution) {
123 String errorMsg = "";
125 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
126 if (exception != null && (exception.getErrorMessage() != null || !exception.getErrorMessage().equals(""))) {
127 errorMsg = exception.getErrorMessage();
129 if (errorMsg == null || errorMsg.equals("")) {
130 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
132 return Optional.of(errorMsg);
133 } catch (Exception ex) {
134 logger.error("Failed to extract workflow exception from execution.", ex);
136 return Optional.empty();
139 public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
140 execution.setVariable("isRollbackComplete", true);
141 updateRequestStatusToFailed(execution);
144 public void abortCallErrorHandling(DelegateExecution execution) {
145 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
147 throw new BpmnError(msg);