d8f9a66568198bb242352153ba3601140f51a0b7
[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.logger.MsoLogger;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class ExecuteBuildingBlockRainyDay {
40         
41         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ExecuteBuildingBlockRainyDay.class);
42         public static final String HANDLING_CODE = "handlingCode";
43         
44         @Autowired
45         private CatalogDbClient catalogDbClient;
46         private static final String ASTERISK = "*";
47
48         public void setRetryTimer(DelegateExecution execution) {
49                 try {
50                         int retryCount = (int) execution.getVariable("retryCount");
51                         int retryTimeToWait = (int) Math.pow(5, retryCount);
52                         String RetryDuration = "PT" + retryTimeToWait + "M";
53                         execution.setVariable("RetryDuration", RetryDuration);
54                 } catch (Exception e) {
55                         msoLogger.error(e);
56                         throw new BpmnError("Unknown error incrementing retry counter");
57                 }
58         }
59         
60         public void queryRainyDayTable(DelegateExecution execution) {
61                 try {
62                         ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
63                         String bbName = ebb.getBuildingBlock().getBpmnFlowName();
64                         GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
65                         Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
66                         String serviceType = ASTERISK;
67                         try {
68                                 serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0).getModelInfoServiceInstance().getServiceType();
69                         } catch (Exception ex) {
70                                 // keep default serviceType value
71                         }
72                         String vnfType = ASTERISK;
73                         try {
74                                 for(GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0).getVnfs()) {
75                                         if(vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
76                                                 vnfType = vnf.getVnfType();
77                                         }
78                                 }
79                         } catch (Exception ex) {
80                                 // keep default vnfType value
81                         }
82                         WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
83                         String errorCode = ASTERISK;
84                         try {
85                                 errorCode = "" + workflowException.getErrorCode();
86                         } catch (Exception ex) {
87                                 // keep default errorCode value
88                         }
89                         String workStep = ASTERISK;
90                         try {
91                                 workStep = workflowException.getWorkStep();
92                         } catch (Exception ex) {
93                                 // keep default workStep value
94                         }
95                         //Extract error data to be returned to WorkflowAction
96                         execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
97                         RainyDayHandlerStatus rainyDayHandlerStatus;
98                         String handlingCode = "";
99                         rainyDayHandlerStatus = catalogDbClient.getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,serviceType,vnfType,errorCode,workStep);
100                         if(rainyDayHandlerStatus==null){
101                                 rainyDayHandlerStatus = catalogDbClient.getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,ASTERISK,ASTERISK,ASTERISK,ASTERISK);
102                                 if(rainyDayHandlerStatus==null){
103                                         handlingCode = "Abort";
104                                 }else{
105                                         handlingCode = rainyDayHandlerStatus.getPolicy();
106                                 }
107                         }else{
108                                 handlingCode = rainyDayHandlerStatus.getPolicy();
109                         }
110                         msoLogger.debug("RainyDayHandler Status Code is: " + handlingCode);
111                         execution.setVariable(HANDLING_CODE, handlingCode);
112                 } catch (Exception e) {
113                         msoLogger.debug("RainyDayHandler Status Code is: Abort");
114                         execution.setVariable(HANDLING_CODE, "Abort");
115                 }
116         }
117         
118         public void setHandlingStatusSuccess(DelegateExecution execution){
119                 execution.setVariable(HANDLING_CODE, "Success");
120         }
121 }