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;
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.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) {
58 logger.error("Failed to update Request db with the status message after retry or rollback has been initialized.",e);
62 public void updateRequestStatusToFailed(DelegateExecution execution) {
64 String requestId = (String) execution.getVariable("mso-request-id");
65 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
66 String rollbackErrorMsg = "";
68 Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
69 Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
71 if(rollbackCompletedSuccessfully==null)
72 rollbackCompletedSuccessfully = false;
74 if(isRollbackFailure==null)
75 isRollbackFailure = false;
77 if(rollbackCompletedSuccessfully){
78 rollbackErrorMsg = "Rollback has been completed successfully.";
79 request.setRollbackStatusMessage(rollbackErrorMsg);
80 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
81 }else if(isRollbackFailure){
82 Optional<String> rollbackErrorMsgOp = retrieveErrorMessage(execution);
83 if(rollbackErrorMsgOp.isPresent()){
84 rollbackErrorMsg = rollbackErrorMsgOp.get();
86 rollbackErrorMsg = "Failed to determine rollback error message.";
88 request.setRollbackStatusMessage(rollbackErrorMsg);
89 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
91 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
92 if(errorMsgOp.isPresent()){
93 errorMsg = errorMsgOp.get();
95 errorMsg = "Failed to determine error message";
97 request.setStatusMessage(errorMsg);
98 execution.setVariable("ErrorMessage", errorMsg);
100 request.setProgress(Long.valueOf(100));
101 request.setRequestStatus("FAILED");
102 request.setLastModifiedBy("CamundaBPMN");
103 requestDbclient.updateInfraActiveRequests(request);
104 } catch (Exception e) {
105 workflowAction.buildAndThrowException(execution, "Error Updating Request Database", e);
109 private Optional<String> retrieveErrorMessage (DelegateExecution execution){
110 String errorMsg = "";
112 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
113 if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){
114 errorMsg = exception.getErrorMessage();
116 if(errorMsg == null || errorMsg.equals("")){
117 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
119 return Optional.of(errorMsg);
120 } catch (Exception ex) {
121 logger.error("Failed to extract workflow exception from execution.",ex);
123 return Optional.empty();
126 public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
127 execution.setVariable("isRollbackComplete", true);
128 updateRequestStatusToFailed(execution);
131 public void abortCallErrorHandling(DelegateExecution execution) {
132 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
134 throw new BpmnError(msg);