Replaced all tabs with spaces in java and pom.xml
[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             String handlingCode = "";
85
86             WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
87             try {
88                 // Extract error data to be returned to WorkflowAction
89                 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
90             } catch (Exception e) {
91                 logger.error("No WorkflowException Found", e);
92             }
93
94             if (suppressRollback) {
95                 handlingCode = "Abort";
96             } else {
97                 try {
98                     serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
99                             .getModelInfoServiceInstance().getServiceType();
100                 } catch (Exception ex) {
101                     // keep default serviceType value
102                 }
103                 String vnfType = ASTERISK;
104                 try {
105                     for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
106                             .getVnfs()) {
107                         if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
108                             vnfType = vnf.getVnfType();
109                         }
110                     }
111                 } catch (Exception ex) {
112                     // keep default vnfType value
113                 }
114
115                 String errorCode = ASTERISK;
116                 try {
117                     errorCode = "" + workflowException.getErrorCode();
118                 } catch (Exception ex) {
119                     // keep default errorCode value
120                 }
121                 try {
122                     errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
123                 } catch (Exception ex) {
124                     // keep default errorCode value
125                 }
126
127                 String workStep = ASTERISK;
128                 try {
129                     workStep = workflowException.getWorkStep();
130                 } catch (Exception ex) {
131                     // keep default workStep value
132                 }
133
134                 RainyDayHandlerStatus rainyDayHandlerStatus;
135                 rainyDayHandlerStatus = catalogDbClient
136                         .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
137                                 serviceType, vnfType, errorCode, workStep);
138                 if (rainyDayHandlerStatus == null) {
139                     rainyDayHandlerStatus = catalogDbClient
140                             .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
141                                     ASTERISK, ASTERISK, errorCode, ASTERISK);
142                 }
143
144                 if (rainyDayHandlerStatus == null) {
145                     rainyDayHandlerStatus = catalogDbClient
146                             .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
147                                     ASTERISK, ASTERISK, ASTERISK, ASTERISK);
148                     if (rainyDayHandlerStatus == null) {
149                         handlingCode = "Abort";
150                     } else {
151                         if (primaryPolicy) {
152                             handlingCode = rainyDayHandlerStatus.getPolicy();
153                         } else {
154                             handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
155                         }
156                     }
157                 } else {
158                     if (primaryPolicy) {
159                         handlingCode = rainyDayHandlerStatus.getPolicy();
160                     } else {
161                         handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
162                     }
163                 }
164                 if (!primaryPolicy) {
165                     try {
166                         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
167                         request.setRetryStatusMessage("Retries have been exhausted.");
168                         requestDbclient.updateInfraActiveRequests(request);
169                     } catch (Exception ex) {
170                         logger.error("Failed to update Request Db Infra Active Requests with Retry Status", ex);
171                     }
172                 }
173                 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
174                     handlingCode = "Rollback";
175                 }
176             }
177             logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
178             execution.setVariable(HANDLING_CODE, handlingCode);
179         } catch (Exception e) {
180             String code = this.environment.getProperty(defaultCode);
181             logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
182             execution.setVariable(HANDLING_CODE, code);
183         }
184         try {
185             int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
186             execution.setVariable("maxRetries", envMaxRetries);
187         } catch (Exception ex) {
188             logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
189             execution.setVariable("maxRetries", 5);
190         }
191     }
192
193     public void setHandlingStatusSuccess(DelegateExecution execution) {
194         execution.setVariable(HANDLING_CODE, "Success");
195     }
196 }