ee7999f995750290d5680ca52d1050fd5f80be01
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.servicedecomposition.tasks;
22
23 import java.util.Map;
24
25 import org.camunda.bpm.engine.delegate.BpmnError;
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.onap.so.bpmn.core.WorkflowException;
28 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
29 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
30 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
32 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
33 import org.onap.so.db.catalog.client.CatalogDbClient;
34 import org.onap.so.db.request.beans.InfraActiveRequests;
35 import org.onap.so.db.request.client.RequestsDbClient;
36 import org.onap.so.logger.MsoLogger;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.core.env.Environment;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class ExecuteBuildingBlockRainyDay {
43
44         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL,
45                         ExecuteBuildingBlockRainyDay.class);
46         public static final String HANDLING_CODE = "handlingCode";
47
48         @Autowired
49         private CatalogDbClient catalogDbClient;
50         @Autowired
51         private RequestsDbClient requestDbclient;
52         private static final String ASTERISK = "*";
53
54         @Autowired
55         private Environment environment;
56         protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
57         protected String defaultCode = "mso.rainyDay.defaultCode";
58         protected String maxRetries = "mso.rainyDay.maxRetries";
59
60         public void setRetryTimer(DelegateExecution execution) {
61                 try {
62                         int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
63                         int retryCount = (int) execution.getVariable("retryCount");
64                         int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
65                         String RetryDuration = "PT" + retryTimeToWait + "S";
66                         execution.setVariable("RetryDuration", RetryDuration);
67                 } catch (Exception e) {
68                         msoLogger.error(e);
69                         throw new BpmnError("Unknown error incrementing retry counter");
70                 }
71         }
72
73         public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
74                 try {
75                         ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
76                         String bbName = ebb.getBuildingBlock().getBpmnFlowName();
77                         GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
78                         String requestId = (String) execution.getVariable("mso-request-id");
79                         Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
80                         String serviceType = ASTERISK;
81                         boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
82                         boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
83                         String handlingCode = "";
84                         if (suppressRollback) {
85                                 handlingCode = "Abort";
86                         } else {
87                                 try {
88                                         serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
89                                                         .getModelInfoServiceInstance().getServiceType();
90                                 } catch (Exception ex) {
91                                         // keep default serviceType value
92                                 }
93                                 String vnfType = ASTERISK;
94                                 try {
95                                         for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
96                                                         .getVnfs()) {
97                                                 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
98                                                         vnfType = vnf.getVnfType();
99                                                 }
100                                         }
101                                 } catch (Exception ex) {
102                                         // keep default vnfType value
103                                 }
104                                 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
105                                 String errorCode = ASTERISK;
106                                 try {
107                                         errorCode = "" + workflowException.getErrorCode();
108                                 } catch (Exception ex) {
109                                         // keep default errorCode value
110                                 }
111                                 try {
112                                         errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
113                                 } catch (Exception ex) {
114                                         // keep default errorCode value
115                                 }
116
117                                 String workStep = ASTERISK;
118                                 try {
119                                         workStep = workflowException.getWorkStep();
120                                 } catch (Exception ex) {
121                                         // keep default workStep value
122                                 }
123                                 
124                                 try {
125                                         // Extract error data to be returned to WorkflowAction
126                                         execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
127                                 } catch (Exception e) {
128                                         msoLogger.error("No WorkflowException Found",e);
129                                 }
130                                 RainyDayHandlerStatus rainyDayHandlerStatus;
131                                 rainyDayHandlerStatus = catalogDbClient
132                                                 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
133                                                                 serviceType, vnfType, errorCode, workStep);
134                                 if (rainyDayHandlerStatus == null) {
135                                         rainyDayHandlerStatus = catalogDbClient
136                                                         .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
137                                                                         ASTERISK, ASTERISK, errorCode, ASTERISK);
138                                 }
139                                 
140                                 if (rainyDayHandlerStatus == null) {
141                                         rainyDayHandlerStatus = catalogDbClient
142                                                         .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
143                                                                         ASTERISK, ASTERISK, ASTERISK, ASTERISK);
144                                         if (rainyDayHandlerStatus == null) {
145                                                 handlingCode = "Abort";
146                                         } else {
147                                                 if (primaryPolicy) {
148                                                         handlingCode = rainyDayHandlerStatus.getPolicy();
149                                                 } else {
150                                                         handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
151                                                 }
152                                         }
153                                 } else {
154                                         if (primaryPolicy) {
155                                                 handlingCode = rainyDayHandlerStatus.getPolicy();
156                                         } else {
157                                                 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
158                                         }
159                                 }
160                                 if (!primaryPolicy) {
161                                         try {
162                                                 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
163                                                 request.setRetryStatusMessage("Retries have been exhausted.");
164                                                 requestDbclient.updateInfraActiveRequests(request);
165                                         } catch (Exception ex) {
166                                                 msoLogger.error("Failed to update Request Db Infra Active Requests with Retry Status",ex);
167                                         }
168                                 }
169                                 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
170                                         handlingCode = "Rollback";
171                                 }
172                         }
173                         msoLogger.debug("RainyDayHandler Status Code is: " + handlingCode);
174                         execution.setVariable(HANDLING_CODE, handlingCode);
175                 } catch (Exception e) {
176                         String code = this.environment.getProperty(defaultCode);
177                         msoLogger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = "+ code, e);
178                         execution.setVariable(HANDLING_CODE, code);
179                 }
180                 try{
181                         int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
182                         execution.setVariable("maxRetries", envMaxRetries);
183                 } catch (Exception ex) {
184                         msoLogger.error("Could not read maxRetries from config file. Setting max to 5 retries");
185                         execution.setVariable("maxRetries", 5);
186                 }
187         }
188
189         public void setHandlingStatusSuccess(DelegateExecution execution) {
190                 execution.setVariable(HANDLING_CODE, "Success");
191         }
192 }