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 = "";
87 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
89 // Extract error data to be returned to WorkflowAction
90 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
91 } catch (Exception e) {
92 logger.error("No WorkflowException Found",e);
95 if (suppressRollback) {
96 handlingCode = "Abort";
99 serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
100 .getModelInfoServiceInstance().getServiceType();
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
123 errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
124 } catch (Exception ex) {
125 // keep default errorCode value
128 String workStep = ASTERISK;
130 workStep = workflowException.getWorkStep();
131 } catch (Exception ex) {
132 // keep default workStep value
135 RainyDayHandlerStatus rainyDayHandlerStatus;
136 rainyDayHandlerStatus = catalogDbClient
137 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
138 serviceType, vnfType, errorCode, workStep);
139 if (rainyDayHandlerStatus == null) {
140 rainyDayHandlerStatus = catalogDbClient
141 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
142 ASTERISK, ASTERISK, errorCode, ASTERISK);
145 if (rainyDayHandlerStatus == null) {
146 rainyDayHandlerStatus = catalogDbClient
147 .getRainyDayHandlerStatusByFlowNameAndServiceTypeAndVnfTypeAndErrorCodeAndWorkStep(bbName,
148 ASTERISK, ASTERISK, ASTERISK, ASTERISK);
149 if (rainyDayHandlerStatus == null) {
150 handlingCode = "Abort";
153 handlingCode = rainyDayHandlerStatus.getPolicy();
155 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
160 handlingCode = rainyDayHandlerStatus.getPolicy();
162 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
165 if (!primaryPolicy) {
167 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
168 request.setRetryStatusMessage("Retries have been exhausted.");
169 requestDbclient.updateInfraActiveRequests(request);
170 } catch (Exception ex) {
171 logger.error("Failed to update Request Db Infra Active Requests with Retry Status",ex);
174 if (handlingCode.equals("RollbackToAssigned") && !aLaCarte) {
175 handlingCode = "Rollback";
178 logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
179 execution.setVariable(HANDLING_CODE, handlingCode);
180 } catch (Exception e) {
181 String code = this.environment.getProperty(defaultCode);
182 logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
183 execution.setVariable(HANDLING_CODE, code);
186 int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
187 execution.setVariable("maxRetries", envMaxRetries);
188 } catch (Exception ex) {
189 logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
190 execution.setVariable("maxRetries", 5);
194 public void setHandlingStatusSuccess(DelegateExecution execution) {
195 execution.setVariable(HANDLING_CODE, "Success");