b210b5ed2f43e802e5aba39744085e34b3b7afcb
[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
59         public void setRetryTimer(DelegateExecution execution) {
60                 try {
61                         int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
62                         int retryCount = (int) execution.getVariable("retryCount");
63                         int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
64                         String RetryDuration = "PT" + retryTimeToWait + "S";
65                         execution.setVariable("RetryDuration", RetryDuration);
66                 } catch (Exception e) {
67                         msoLogger.error(e);
68                         throw new BpmnError("Unknown error incrementing retry counter");
69                 }
70         }
71
72         public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
73                 try {
74                         ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
75                         String bbName = ebb.getBuildingBlock().getBpmnFlowName();
76                         GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
77                         String requestId = (String) execution.getVariable("mso-request-id");
78                         Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
79                         String serviceType = ASTERISK;
80                         boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
81                         boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
82                         String handlingCode = "";
83                         if (suppressRollback) {
84                                 handlingCode = "Abort";
85                         } else {
86                                 try {
87                                         serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
88                                                         .getModelInfoServiceInstance().getServiceType();
89                                 } catch (Exception ex) {
90                                         // keep default serviceType value
91                                 }
92                                 String vnfType = ASTERISK;
93                                 try {
94                                         for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
95                                                         .getVnfs()) {
96                                                 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
97                                                         vnfType = vnf.getVnfType();
98                                                 }
99                                         }
100                                 } catch (Exception ex) {
101                                         // keep default vnfType value
102                                 }
103                                 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
104                                 String errorCode = ASTERISK;
105                                 try {
106                                         errorCode = "" + workflowException.getErrorCode();
107                                 } catch (Exception ex) {
108                                         // keep default errorCode value
109                                 }
110                                 try {
111                                         errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
112                                 } catch (Exception ex) {
113                                         // keep default errorCode value
114                                 }
115
116                                 String workStep = ASTERISK;
117                                 try {
118                                         workStep = workflowException.getWorkStep();
119                                 } catch (Exception ex) {
120                                         // keep default workStep value
121                                 }
122                                 
123                                 try {
124                                         // Extract error data to be returned to WorkflowAction
125                                         execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
126                                 } catch (Exception e) {
127                                         msoLogger.error("No WorkflowException Found",e);
128                                 }
129                                 RainyDayHandlerStatus rainyDayHandlerStatus;
130                                 rainyDayHandlerStatus = catalogDbClient
131                                                 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
132                                                                 serviceType, vnfType, errorCode, workStep);
133                                 if (rainyDayHandlerStatus == null) {
134                                         rainyDayHandlerStatus = catalogDbClient
135                                                         .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
136                                                                         ASTERISK, ASTERISK, errorCode, ASTERISK);
137                                 }
138                                 
139                                 if (rainyDayHandlerStatus == null) {
140                                         rainyDayHandlerStatus = catalogDbClient
141                                                         .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
142                                                                         ASTERISK, ASTERISK, ASTERISK, ASTERISK);
143                                         if (rainyDayHandlerStatus == null) {
144                                                 handlingCode = "Abort";
145                                         } else {
146                                                 if (primaryPolicy) {
147                                                         handlingCode = rainyDayHandlerStatus.getPolicy();
148                                                 } else {
149                                                         handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
150                                                 }
151                                         }
152                                 } else {
153                                         if (primaryPolicy) {
154                                                 handlingCode = rainyDayHandlerStatus.getPolicy();
155                                         } else {
156                                                 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
157                                         }
158                                 }
159                                 if (!primaryPolicy) {
160                                         try {
161                                                 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
162                                                 request.setRetryStatusMessage("Retries have been exhausted.");
163                                                 requestDbclient.updateInfraActiveRequests(request);
164                                         } catch (Exception ex) {
165                                                 msoLogger.error("Failed to update Request Db Infra Active Requests with Retry Status",ex);
166                                         }
167                                 }
168                                 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
169                                         handlingCode = "Rollback";
170                                 }
171                         }
172                         msoLogger.debug("RainyDayHandler Status Code is: " + handlingCode);
173                         execution.setVariable(HANDLING_CODE, handlingCode);
174                 } catch (Exception e) {
175                         msoLogger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = Abort", e);
176                         String code = this.environment.getProperty(defaultCode);
177                         execution.setVariable(HANDLING_CODE, code);
178                 }
179         }
180
181         public void setHandlingStatusSuccess(DelegateExecution execution) {
182                 execution.setVariable(HANDLING_CODE, "Success");
183         }
184 }