2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.servicedecomposition.tasks;
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;
42 public class ExecuteBuildingBlockRainyDay {
44 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL,
45 ExecuteBuildingBlockRainyDay.class);
46 public static final String HANDLING_CODE = "handlingCode";
49 private CatalogDbClient catalogDbClient;
51 private RequestsDbClient requestDbclient;
52 private static final String ASTERISK = "*";
55 private Environment environment;
56 protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
57 protected String defaultCode = "mso.rainyDay.defaultCode";
58 protected String maxRetries = "mso.rainyDay.maxRetries";
60 public void setRetryTimer(DelegateExecution execution) {
62 int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
63 int retryCount = (int) execution.getVariable("retryCount");
64 int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
65 String RetryDuration = "PT" + retryTimeToWait + "S";
66 execution.setVariable("RetryDuration", RetryDuration);
67 } catch (Exception e) {
69 throw new BpmnError("Unknown error incrementing retry counter");
73 public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
75 ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
76 String bbName = ebb.getBuildingBlock().getBpmnFlowName();
77 GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
78 String requestId = (String) execution.getVariable("mso-request-id");
79 Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
80 String serviceType = ASTERISK;
81 boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
82 boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
83 String handlingCode = "";
84 if (suppressRollback) {
85 handlingCode = "Abort";
88 serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
89 .getModelInfoServiceInstance().getServiceType();
90 } catch (Exception ex) {
91 // keep default serviceType value
93 String vnfType = ASTERISK;
95 for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
97 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
98 vnfType = vnf.getVnfType();
101 } catch (Exception ex) {
102 // keep default vnfType value
104 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
105 String errorCode = ASTERISK;
107 errorCode = "" + workflowException.getErrorCode();
108 } catch (Exception ex) {
109 // keep default errorCode value
112 errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
113 } catch (Exception ex) {
114 // keep default errorCode value
117 String workStep = ASTERISK;
119 workStep = workflowException.getWorkStep();
120 } catch (Exception ex) {
121 // keep default workStep value
125 // Extract error data to be returned to WorkflowAction
126 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
127 } catch (Exception e) {
128 msoLogger.error("No WorkflowException Found",e);
130 RainyDayHandlerStatus rainyDayHandlerStatus;
131 rainyDayHandlerStatus = catalogDbClient
132 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
133 serviceType, vnfType, errorCode, workStep);
134 if (rainyDayHandlerStatus == null) {
135 rainyDayHandlerStatus = catalogDbClient
136 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
137 ASTERISK, ASTERISK, errorCode, ASTERISK);
140 if (rainyDayHandlerStatus == null) {
141 rainyDayHandlerStatus = catalogDbClient
142 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
143 ASTERISK, ASTERISK, ASTERISK, ASTERISK);
144 if (rainyDayHandlerStatus == null) {
145 handlingCode = "Abort";
148 handlingCode = rainyDayHandlerStatus.getPolicy();
150 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
155 handlingCode = rainyDayHandlerStatus.getPolicy();
157 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
160 if (!primaryPolicy) {
162 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
163 request.setRetryStatusMessage("Retries have been exhausted.");
164 requestDbclient.updateInfraActiveRequests(request);
165 } catch (Exception ex) {
166 msoLogger.error("Failed to update Request Db Infra Active Requests with Retry Status",ex);
169 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
170 handlingCode = "Rollback";
173 msoLogger.debug("RainyDayHandler Status Code is: " + handlingCode);
174 execution.setVariable(HANDLING_CODE, handlingCode);
175 } catch (Exception e) {
176 String code = this.environment.getProperty(defaultCode);
177 msoLogger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = "+ code, e);
178 execution.setVariable(HANDLING_CODE, code);
181 int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
182 execution.setVariable("maxRetries", envMaxRetries);
183 } catch (Exception ex) {
184 msoLogger.error("Could not read maxRetries from config file. Setting max to 5 retries");
185 execution.setVariable("maxRetries", 5);
189 public void setHandlingStatusSuccess(DelegateExecution execution) {
190 execution.setVariable(HANDLING_CODE, "Success");