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;
27 import org.camunda.bpm.engine.delegate.BpmnError;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.core.WorkflowException;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
31 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
32 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
33 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
34 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
35 import org.onap.so.db.catalog.client.CatalogDbClient;
36 import org.onap.so.db.request.beans.InfraActiveRequests;
37 import org.onap.so.db.request.client.RequestsDbClient;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.core.env.Environment;
42 import org.springframework.stereotype.Component;
45 public class ExecuteBuildingBlockRainyDay {
47 private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockRainyDay.class);
48 public static final String HANDLING_CODE = "handlingCode";
51 private CatalogDbClient catalogDbClient;
53 private RequestsDbClient requestDbclient;
54 private static final String ASTERISK = "*";
57 private Environment environment;
58 protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
59 protected String defaultCode = "mso.rainyDay.defaultCode";
60 protected String maxRetries = "mso.rainyDay.maxRetries";
62 public void setRetryTimer(DelegateExecution execution) {
64 int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
65 int retryCount = (int) execution.getVariable("retryCount");
66 int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
67 String RetryDuration = "PT" + retryTimeToWait + "S";
68 execution.setVariable("RetryDuration", RetryDuration);
69 } catch (Exception e) {
70 logger.error("Exception occurred", e);
71 throw new BpmnError("Unknown error incrementing retry counter");
75 public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
77 ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
78 String bbName = ebb.getBuildingBlock().getBpmnFlowName();
79 GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
80 String requestId = (String) execution.getVariable("mso-request-id");
81 Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
82 String serviceType = ASTERISK;
83 boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
84 boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
85 String handlingCode = "";
86 if (suppressRollback) {
87 handlingCode = "Abort";
90 serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
91 .getModelInfoServiceInstance().getServiceType();
92 } catch (Exception ex) {
93 // keep default serviceType value
95 String vnfType = ASTERISK;
97 for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
99 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
100 vnfType = vnf.getVnfType();
103 } catch (Exception ex) {
104 // keep default vnfType value
106 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
107 String errorCode = ASTERISK;
109 errorCode = "" + workflowException.getErrorCode();
110 } catch (Exception ex) {
111 // keep default errorCode value
114 errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
115 } catch (Exception ex) {
116 // keep default errorCode value
119 String workStep = ASTERISK;
121 workStep = workflowException.getWorkStep();
122 } catch (Exception ex) {
123 // keep default workStep value
127 // Extract error data to be returned to WorkflowAction
128 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
129 } catch (Exception e) {
130 logger.error("No WorkflowException Found",e);
132 RainyDayHandlerStatus rainyDayHandlerStatus;
133 rainyDayHandlerStatus = catalogDbClient
134 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
135 serviceType, vnfType, errorCode, workStep);
136 if (rainyDayHandlerStatus == null) {
137 rainyDayHandlerStatus = catalogDbClient
138 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
139 ASTERISK, ASTERISK, errorCode, ASTERISK);
142 if (rainyDayHandlerStatus == null) {
143 rainyDayHandlerStatus = catalogDbClient
144 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
145 ASTERISK, ASTERISK, ASTERISK, ASTERISK);
146 if (rainyDayHandlerStatus == null) {
147 handlingCode = "Abort";
150 handlingCode = rainyDayHandlerStatus.getPolicy();
152 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
157 handlingCode = rainyDayHandlerStatus.getPolicy();
159 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
162 if (!primaryPolicy) {
164 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
165 request.setRetryStatusMessage("Retries have been exhausted.");
166 requestDbclient.updateInfraActiveRequests(request);
167 } catch (Exception ex) {
168 logger.error("Failed to update Request Db Infra Active Requests with Retry Status",ex);
171 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
172 handlingCode = "Rollback";
175 logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
176 execution.setVariable(HANDLING_CODE, handlingCode);
177 } catch (Exception e) {
178 String code = this.environment.getProperty(defaultCode);
179 logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
180 execution.setVariable(HANDLING_CODE, code);
183 int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
184 execution.setVariable("maxRetries", envMaxRetries);
185 } catch (Exception ex) {
186 logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
187 execution.setVariable("maxRetries", 5);
191 public void setHandlingStatusSuccess(DelegateExecution execution) {
192 execution.setVariable(HANDLING_CODE, "Success");