aa992d6153d9e3c89f0c3efe9aa39ebc7ca49f7b
[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
27 import org.camunda.bpm.engine.delegate.BpmnError;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.core.WorkflowException;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
31 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
32 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
33 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
34 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
35 import org.onap.so.db.catalog.client.CatalogDbClient;
36 import org.onap.so.db.request.beans.InfraActiveRequests;
37 import org.onap.so.db.request.client.RequestsDbClient;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.core.env.Environment;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class ExecuteBuildingBlockRainyDay {
46
47         private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockRainyDay.class);
48         public static final String HANDLING_CODE = "handlingCode";
49
50         @Autowired
51         private CatalogDbClient catalogDbClient;
52         @Autowired
53         private RequestsDbClient requestDbclient;
54         private static final String ASTERISK = "*";
55
56         @Autowired
57         private Environment environment;
58         protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
59         protected String defaultCode = "mso.rainyDay.defaultCode";
60         protected String maxRetries = "mso.rainyDay.maxRetries";
61
62         public void setRetryTimer(DelegateExecution execution) {
63                 try {
64                         int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
65                         int retryCount = (int) execution.getVariable("retryCount");
66                         int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
67                         String RetryDuration = "PT" + retryTimeToWait + "S";
68                         execution.setVariable("RetryDuration", RetryDuration);
69                 } catch (Exception e) {
70                         logger.error("Exception occurred", e);
71                         throw new BpmnError("Unknown error incrementing retry counter");
72                 }
73         }
74
75         public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
76                 try {
77                         ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
78                         String bbName = ebb.getBuildingBlock().getBpmnFlowName();
79                         GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
80                         String requestId = (String) execution.getVariable("mso-request-id");
81                         Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
82                         String serviceType = ASTERISK;
83                         boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
84                         boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
85                         String handlingCode = "";
86                         
87                         WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
88                         try {
89                                 // Extract error data to be returned to WorkflowAction
90                                 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
91                         } catch (Exception e) {
92                                 logger.error("No WorkflowException Found",e);
93                         }
94                         
95                         if (suppressRollback) {
96                                 handlingCode = "Abort";
97                         } else {
98                                 try {
99                                         serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
100                                                         .getModelInfoServiceInstance().getServiceType();
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                                 try {
123                                         errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
124                                 } catch (Exception ex) {
125                                         // keep default errorCode value
126                                 }
127
128                                 String workStep = ASTERISK;
129                                 try {
130                                         workStep = workflowException.getWorkStep();
131                                 } catch (Exception ex) {
132                                         // keep default workStep value
133                                 }
134                                 
135                                 RainyDayHandlerStatus rainyDayHandlerStatus;
136                                 rainyDayHandlerStatus = catalogDbClient
137                                                 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
138                                                                 serviceType, vnfType, errorCode, workStep);
139                                 if (rainyDayHandlerStatus == null) {
140                                         rainyDayHandlerStatus = catalogDbClient
141                                                         .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
142                                                                         ASTERISK, ASTERISK, errorCode, ASTERISK);
143                                 }
144                                 
145                                 if (rainyDayHandlerStatus == null) {
146                                         rainyDayHandlerStatus = catalogDbClient
147                                                         .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
148                                                                         ASTERISK, ASTERISK, ASTERISK, ASTERISK);
149                                         if (rainyDayHandlerStatus == null) {
150                                                 handlingCode = "Abort";
151                                         } else {
152                                                 if (primaryPolicy) {
153                                                         handlingCode = rainyDayHandlerStatus.getPolicy();
154                                                 } else {
155                                                         handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
156                                                 }
157                                         }
158                                 } else {
159                                         if (primaryPolicy) {
160                                                 handlingCode = rainyDayHandlerStatus.getPolicy();
161                                         } else {
162                                                 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
163                                         }
164                                 }
165                                 if (!primaryPolicy) {
166                                         try {
167                                                 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
168                                                 request.setRetryStatusMessage("Retries have been exhausted.");
169                                                 requestDbclient.updateInfraActiveRequests(request);
170                                         } catch (Exception ex) {
171                                                 logger.error("Failed to update Request Db Infra Active Requests with Retry Status",ex);
172                                         }
173                                 }
174                                 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
175                                         handlingCode = "Rollback";
176                                 }
177                         }
178                         logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
179                         execution.setVariable(HANDLING_CODE, handlingCode);
180                 } catch (Exception e) {
181                         String code = this.environment.getProperty(defaultCode);
182                         logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
183                         execution.setVariable(HANDLING_CODE, code);
184                 }
185                 try{
186                         int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
187                         execution.setVariable("maxRetries", envMaxRetries);
188                 } catch (Exception ex) {
189                         logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
190                         execution.setVariable("maxRetries", 5);
191                 }
192         }
193
194         public void setHandlingStatusSuccess(DelegateExecution execution) {
195                 execution.setVariable(HANDLING_CODE, "Success");
196         }
197 }