4fb480892acb153c5f536978023350c44b7b66a2
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
22
23 import java.util.Optional;
24
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;
34
35 @Component
36 public class WorkflowActionBBFailure {
37
38         private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBFailure.class);
39         @Autowired
40         private RequestsDbClient requestDbclient;
41         @Autowired
42         private WorkflowAction workflowAction;
43         
44         protected void updateRequestErrorStatusMessage(DelegateExecution execution) {
45                 try {
46                         String requestId = (String) execution.getVariable("mso-request-id");
47                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
48                         String errorMsg = "";
49                         Optional<String> errorMsgOp = retrieveErrorMessage(execution);
50                         if(errorMsgOp.isPresent()){
51                                 errorMsg = errorMsgOp.get();
52                         }else{
53                                 errorMsg = "Failed to determine error message";
54                         }
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);
59                 }
60         }
61         
62         public void updateRequestStatusToFailed(DelegateExecution execution) {
63                 try {
64                         String requestId = (String) execution.getVariable("mso-request-id");
65                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
66                         String rollbackErrorMsg = "";
67                         String errorMsg = "";
68                         Boolean rollbackCompletedSuccessfully = (Boolean) execution.getVariable("isRollbackComplete");
69                         Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
70                         
71                         if(rollbackCompletedSuccessfully==null)
72                                 rollbackCompletedSuccessfully = false;
73                         
74                         if(isRollbackFailure==null)
75                                 isRollbackFailure = false;
76                         
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();
85                                 }else{
86                                         rollbackErrorMsg = "Failed to determine rollback error message.";
87                                 }
88                                 request.setRollbackStatusMessage(rollbackErrorMsg);
89                                 execution.setVariable("RollbackErrorMessage", rollbackErrorMsg);
90                         }else{
91                                 Optional<String> errorMsgOp = retrieveErrorMessage(execution);
92                                 if(errorMsgOp.isPresent()){
93                                         errorMsg = errorMsgOp.get();
94                                 }else{
95                                         errorMsg = "Failed to determine error message";
96                                 }
97                                 request.setStatusMessage(errorMsg);
98                                 execution.setVariable("ErrorMessage", errorMsg);
99                         }
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);
106                 }
107         }
108         
109         private Optional<String> retrieveErrorMessage (DelegateExecution execution){
110                 String errorMsg = "";
111                 try {
112                         WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
113                         if(exception != null && (exception.getErrorMessage()!=null || !exception.getErrorMessage().equals(""))){
114                                 errorMsg = exception.getErrorMessage();
115                         }
116                         if(errorMsg == null || errorMsg.equals("")){
117                                 errorMsg = (String) execution.getVariable("WorkflowExceptionErrorMessage");
118                         }
119                         return Optional.of(errorMsg);
120                 } catch (Exception ex) {
121                         logger.error("Failed to extract workflow exception from execution.",ex);
122                 }
123                 return Optional.empty();
124         }
125         
126         public void updateRequestStatusToFailedWithRollback(DelegateExecution execution) {
127                 execution.setVariable("isRollbackComplete", true);
128                 updateRequestStatusToFailed(execution);
129         }
130
131         public void abortCallErrorHandling(DelegateExecution execution) {
132                 String msg = "Flow has failed. Rainy day handler has decided to abort the process.";
133                 logger.error(msg);
134                 throw new BpmnError(msg);
135         }
136 }