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