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