Set workflowexceptionerrormessage in all cases
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / servicedecomposition / tasks / ExecuteBuildingBlockRainyDay.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.so.bpmn.servicedecomposition.tasks;
24
25 import java.util.Map;
26 import org.camunda.bpm.engine.delegate.BpmnError;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.onap.so.bpmn.core.WorkflowException;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
30 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
32 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
33 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
34 import org.onap.so.db.catalog.client.CatalogDbClient;
35 import org.onap.so.db.request.beans.InfraActiveRequests;
36 import org.onap.so.db.request.client.RequestsDbClient;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.core.env.Environment;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 public class ExecuteBuildingBlockRainyDay {
45
46     private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockRainyDay.class);
47     public static final String HANDLING_CODE = "handlingCode";
48
49     @Autowired
50     private CatalogDbClient catalogDbClient;
51     @Autowired
52     private RequestsDbClient requestDbclient;
53     private static final String ASTERISK = "*";
54
55     @Autowired
56     private Environment environment;
57     protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
58     protected String defaultCode = "mso.rainyDay.defaultCode";
59     protected String maxRetries = "mso.rainyDay.maxRetries";
60
61     public void setRetryTimer(DelegateExecution execution) {
62         try {
63             int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
64             int retryCount = (int) execution.getVariable("retryCount");
65             int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
66             String RetryDuration = "PT" + retryTimeToWait + "S";
67             execution.setVariable("RetryDuration", RetryDuration);
68         } catch (Exception e) {
69             logger.error("Exception occurred", e);
70             throw new BpmnError("Unknown error incrementing retry counter");
71         }
72     }
73
74     public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
75         try {
76             ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
77             String bbName = ebb.getBuildingBlock().getBpmnFlowName();
78             GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
79             String requestId = (String) execution.getVariable("mso-request-id");
80             Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
81             String serviceType = ASTERISK;
82             boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
83             boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
84             WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
85             if (workflowException != null) {
86                 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
87             } else {
88                 logger.debug("WorkflowException is null, unable to set WorkflowExceptionErrorMessage");
89             }
90             String handlingCode = "";
91
92             if (suppressRollback) {
93                 handlingCode = "Abort";
94             } else {
95                 try {
96                     serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
97                             .getModelInfoServiceInstance().getServiceType();
98                     if (serviceType == null || serviceType.isEmpty()) {
99                         serviceType = ASTERISK;
100                     }
101                 } catch (Exception ex) {
102                     // keep default serviceType value
103                 }
104                 String vnfType = ASTERISK;
105                 try {
106                     for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
107                             .getVnfs()) {
108                         if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
109                             vnfType = vnf.getVnfType();
110                         }
111                     }
112                 } catch (Exception ex) {
113                     // keep default vnfType value
114                 }
115
116                 String errorCode = ASTERISK;
117                 try {
118                     errorCode = "" + workflowException.getErrorCode();
119                 } catch (Exception ex) {
120                     // keep default errorCode value
121                 }
122
123                 try {
124                     errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
125                 } catch (Exception ex) {
126                     // keep default errorCode value
127                 }
128
129                 String workStep = ASTERISK;
130                 try {
131                     workStep = workflowException.getWorkStep();
132                 } catch (Exception ex) {
133                     // keep default workStep value
134                 }
135
136                 String errorMessage = ASTERISK;
137                 try {
138                     errorMessage = workflowException.getErrorMessage();
139                 } catch (Exception ex) {
140                     // keep default workStep value
141                 }
142
143                 RainyDayHandlerStatus rainyDayHandlerStatus;
144                 rainyDayHandlerStatus = catalogDbClient.getRainyDayHandlerStatus(bbName, serviceType, vnfType,
145                         errorCode, workStep, errorMessage);
146
147                 if (rainyDayHandlerStatus == null) {
148                     handlingCode = "Abort";
149                 } else {
150                     if (primaryPolicy) {
151                         handlingCode = rainyDayHandlerStatus.getPolicy();
152                     } else {
153                         handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
154                     }
155                 }
156                 if (!primaryPolicy) {
157                     try {
158                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
159                         request.setRetryStatusMessage("Retries have been exhausted.");
160                         requestDbclient.updateInfraActiveRequests(request);
161                     } catch (Exception ex) {
162                         logger.error("Failed to update Request Db Infra Active Requests with Retry Status", ex);
163                     }
164                 }
165                 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
166                     handlingCode = "Rollback";
167                 }
168             }
169             logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
170             execution.setVariable(HANDLING_CODE, handlingCode);
171         } catch (Exception e) {
172             String code = this.environment.getProperty(defaultCode);
173             logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
174             execution.setVariable(HANDLING_CODE, code);
175         }
176         try {
177             int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
178             execution.setVariable("maxRetries", envMaxRetries);
179         } catch (Exception ex) {
180             logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
181             execution.setVariable("maxRetries", 5);
182         }
183     }
184
185     public void setHandlingStatusSuccess(DelegateExecution execution) {
186         execution.setVariable(HANDLING_CODE, "Success");
187     }
188 }