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