2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.servicedecomposition.tasks;
26 import org.camunda.bpm.engine.delegate.BpmnError;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.onap.so.bpmn.core.WorkflowException;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
30 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
32 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
33 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
34 import org.onap.so.db.catalog.client.CatalogDbClient;
35 import org.onap.so.db.request.beans.InfraActiveRequests;
36 import org.onap.so.db.request.client.RequestsDbClient;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.core.env.Environment;
41 import org.springframework.stereotype.Component;
44 public class ExecuteBuildingBlockRainyDay {
46 private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockRainyDay.class);
47 public static final String HANDLING_CODE = "handlingCode";
50 private CatalogDbClient catalogDbClient;
52 private RequestsDbClient requestDbclient;
53 private static final String ASTERISK = "*";
56 private Environment environment;
57 protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
58 protected String defaultCode = "mso.rainyDay.defaultCode";
59 protected String maxRetries = "mso.rainyDay.maxRetries";
61 public void setRetryTimer(DelegateExecution execution) {
63 int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
64 int retryCount = (int) execution.getVariable("retryCount");
65 int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
66 String RetryDuration = "PT" + retryTimeToWait + "S";
67 execution.setVariable("RetryDuration", RetryDuration);
68 } catch (Exception e) {
69 logger.error("Exception occurred", e);
70 throw new BpmnError("Unknown error incrementing retry counter");
74 public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
76 ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
77 String bbName = ebb.getBuildingBlock().getBpmnFlowName();
78 GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
79 String requestId = (String) execution.getVariable("mso-request-id");
80 Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
81 String serviceType = ASTERISK;
82 boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
83 boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
84 String handlingCode = "";
86 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
88 // Extract error data to be returned to WorkflowAction
89 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
90 } catch (Exception e) {
91 logger.error("No WorkflowException Found", e);
94 if (suppressRollback) {
95 handlingCode = "Abort";
98 serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
99 .getModelInfoServiceInstance().getServiceType();
100 } catch (Exception ex) {
101 // keep default serviceType value
103 String vnfType = ASTERISK;
105 for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
107 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
108 vnfType = vnf.getVnfType();
111 } catch (Exception ex) {
112 // keep default vnfType value
115 String errorCode = ASTERISK;
117 errorCode = "" + workflowException.getErrorCode();
118 } catch (Exception ex) {
119 // keep default errorCode value
122 errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
123 } catch (Exception ex) {
124 // keep default errorCode value
127 String workStep = ASTERISK;
129 workStep = workflowException.getWorkStep();
130 } catch (Exception ex) {
131 // keep default workStep value
134 RainyDayHandlerStatus rainyDayHandlerStatus;
135 rainyDayHandlerStatus = catalogDbClient
136 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
137 serviceType, vnfType, errorCode, workStep);
138 if (rainyDayHandlerStatus == null) {
139 rainyDayHandlerStatus = catalogDbClient
140 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
141 ASTERISK, ASTERISK, errorCode, ASTERISK);
144 if (rainyDayHandlerStatus == null) {
145 rainyDayHandlerStatus = catalogDbClient
146 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
147 ASTERISK, ASTERISK, ASTERISK, ASTERISK);
148 if (rainyDayHandlerStatus == null) {
149 handlingCode = "Abort";
152 handlingCode = rainyDayHandlerStatus.getPolicy();
154 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
159 handlingCode = rainyDayHandlerStatus.getPolicy();
161 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
164 if (!primaryPolicy) {
166 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
167 request.setRetryStatusMessage("Retries have been exhausted.");
168 requestDbclient.updateInfraActiveRequests(request);
169 } catch (Exception ex) {
170 logger.error("Failed to update Request Db Infra Active Requests with Retry Status", ex);
173 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
174 handlingCode = "Rollback";
177 logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
178 execution.setVariable(HANDLING_CODE, handlingCode);
179 } catch (Exception e) {
180 String code = this.environment.getProperty(defaultCode);
181 logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
182 execution.setVariable(HANDLING_CODE, code);
185 int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
186 execution.setVariable("maxRetries", envMaxRetries);
187 } catch (Exception ex) {
188 logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
189 execution.setVariable("maxRetries", 5);
193 public void setHandlingStatusSuccess(DelegateExecution execution) {
194 execution.setVariable(HANDLING_CODE, "Success");