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