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