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