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 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
85 if (workflowException != null) {
86 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
88 logger.debug("WorkflowException is null, unable to set WorkflowExceptionErrorMessage");
90 String handlingCode = "";
92 if (suppressRollback) {
93 handlingCode = "Abort";
96 serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
97 .getModelInfoServiceInstance().getServiceType();
98 if (serviceType == null || serviceType.isEmpty()) {
99 serviceType = ASTERISK;
101 } catch (Exception ex) {
102 // keep default serviceType value
104 String vnfType = ASTERISK;
106 for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
108 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
109 vnfType = vnf.getVnfType();
112 } catch (Exception ex) {
113 // keep default vnfType value
116 String errorCode = ASTERISK;
118 errorCode = "" + workflowException.getErrorCode();
119 } catch (Exception ex) {
120 // keep default errorCode value
124 errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
125 } catch (Exception ex) {
126 // keep default errorCode value
129 String workStep = ASTERISK;
131 workStep = workflowException.getWorkStep();
132 } catch (Exception ex) {
133 // keep default workStep value
136 String errorMessage = ASTERISK;
138 errorMessage = workflowException.getErrorMessage();
139 } catch (Exception ex) {
140 // keep default workStep value
143 RainyDayHandlerStatus rainyDayHandlerStatus;
144 rainyDayHandlerStatus = catalogDbClient.getRainyDayHandlerStatus(bbName, serviceType, vnfType,
145 errorCode, workStep, errorMessage);
147 if (rainyDayHandlerStatus == null) {
148 handlingCode = "Abort";
151 handlingCode = rainyDayHandlerStatus.getPolicy();
153 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
156 if (!primaryPolicy) {
158 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
159 request.setRetryStatusMessage("Retries have been exhausted.");
160 requestDbclient.updateInfraActiveRequests(request);
161 } catch (Exception ex) {
162 logger.error("Failed to update Request Db Infra Active Requests with Retry Status", ex);
165 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
166 handlingCode = "Rollback";
169 logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
170 execution.setVariable(HANDLING_CODE, handlingCode);
171 } catch (Exception e) {
172 String code = this.environment.getProperty(defaultCode);
173 logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
174 execution.setVariable(HANDLING_CODE, code);
177 int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
178 execution.setVariable("maxRetries", envMaxRetries);
179 } catch (Exception ex) {
180 logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
181 execution.setVariable("maxRetries", 5);
185 public void setHandlingStatusSuccess(DelegateExecution execution) {
186 execution.setVariable(HANDLING_CODE, "Success");