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.constants.Status;
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.onap.so.utils.TargetEntities;
39 import org.onap.so.utils.TargetEntity;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.core.env.Environment;
44 import org.springframework.stereotype.Component;
47 public class ExecuteBuildingBlockRainyDay {
49 private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockRainyDay.class);
50 public static final String HANDLING_CODE = "handlingCode";
51 public static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";
54 private CatalogDbClient catalogDbClient;
56 private RequestsDbClient requestDbclient;
57 private static final String ASTERISK = "*";
60 private Environment environment;
61 protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
62 protected String defaultCode = "mso.rainyDay.defaultCode";
63 protected String maxRetries = "mso.rainyDay.maxRetries";
65 public void setRetryTimer(DelegateExecution execution) {
67 int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
68 int retryCount = (int) execution.getVariable("retryCount");
69 int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
70 String RetryDuration = "PT" + retryTimeToWait + "S";
71 execution.setVariable("RetryDuration", RetryDuration);
72 } catch (Exception e) {
73 logger.error("Exception occurred", e);
74 throw new BpmnError("Unknown error incrementing retry counter");
78 public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
80 ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
81 String bbName = ebb.getBuildingBlock().getBpmnFlowName();
82 GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
83 String requestId = (String) execution.getVariable("mso-request-id");
84 Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
85 String serviceType = ASTERISK;
86 boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
87 boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
88 WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
89 if (workflowException != null) {
90 execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
92 logger.debug("WorkflowException is null, unable to set WorkflowExceptionErrorMessage");
94 String handlingCode = "";
96 if (suppressRollback) {
97 handlingCode = "Abort";
100 serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
101 .getModelInfoServiceInstance().getServiceType();
102 if (serviceType == null || serviceType.isEmpty()) {
103 serviceType = ASTERISK;
105 } catch (Exception ex) {
106 // keep default serviceType value
107 logger.error("Exception in serviceType retrivel", ex);
109 String vnfType = ASTERISK;
111 for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
113 if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
114 vnfType = vnf.getVnfType();
117 } catch (Exception ex) {
118 // keep default vnfType value
119 logger.error("Exception in vnfType retrivel", ex);
122 String errorCode = ASTERISK;
124 errorCode = "" + workflowException.getErrorCode();
125 } catch (Exception ex) {
126 // keep default errorCode value
127 logger.error("Exception in errorCode retrivel", ex);
131 errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
132 } catch (Exception ex) {
133 // keep default errorCode value
134 logger.error("Exception in errorCode retrivel", ex);
137 String workStep = ASTERISK;
139 workStep = workflowException.getWorkStep();
140 } catch (Exception ex) {
141 // keep default workStep value
142 logger.error("Exception in workStep retrivel", ex);
145 String errorMessage = ASTERISK;
147 errorMessage = workflowException.getErrorMessage();
148 } catch (Exception ex) {
149 // keep default workStep value
150 logger.error("Exception in errorMessage retrivel", ex);
153 String serviceRole = ASTERISK;
155 serviceRole = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
156 .getModelInfoServiceInstance().getServiceRole();
157 if (serviceRole == null || serviceRole.isEmpty()) {
158 serviceRole = ASTERISK;
160 } catch (Exception ex) {
161 // keep default serviceRole value
164 RainyDayHandlerStatus rainyDayHandlerStatus;
165 rainyDayHandlerStatus = catalogDbClient.getRainyDayHandlerStatus(bbName, serviceType, vnfType,
166 errorCode, workStep, errorMessage, serviceRole);
168 if (rainyDayHandlerStatus == null) {
169 handlingCode = "Abort";
172 handlingCode = rainyDayHandlerStatus.getPolicy();
174 handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
177 if (!primaryPolicy) {
179 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
180 request.setRetryStatusMessage("Retries have been exhausted.");
181 requestDbclient.updateInfraActiveRequests(request);
182 } catch (Exception ex) {
183 logger.error("Failed to update Request Db Infra Active Requests with Retry Status", ex);
186 if ("RollbackToAssigned".equals(handlingCode) && !aLaCarte) {
187 handlingCode = "Rollback";
189 if (handlingCode.startsWith("Rollback")) {
190 String targetState = "";
191 if ("RollbackToAssigned".equalsIgnoreCase(handlingCode)) {
192 targetState = Status.ROLLED_BACK_TO_ASSIGNED.toString();
193 } else if ("RollbackToCreated".equalsIgnoreCase(handlingCode)) {
194 targetState = Status.ROLLED_BACK_TO_CREATED.toString();
196 targetState = Status.ROLLED_BACK.toString();
198 execution.setVariable(ROLLBACK_TARGET_STATE, targetState);
199 logger.debug("Rollback target state is: {}", targetState);
202 logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
203 execution.setVariable(HANDLING_CODE, handlingCode);
204 } catch (Exception e) {
205 String code = this.environment.getProperty(defaultCode);
206 logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
207 execution.setVariable(HANDLING_CODE, code);
210 int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
211 execution.setVariable("maxRetries", envMaxRetries);
212 } catch (Exception ex) {
213 logger.error("Could not read maxRetries from config file. Setting max to 5 retries", ex);
214 execution.setVariable("maxRetries", 5);
218 public void setHandlingStatusSuccess(DelegateExecution execution) {
219 execution.setVariable(HANDLING_CODE, "Success");
222 public void updateExtSystemErrorSource(DelegateExecution execution) {
224 String requestId = (String) execution.getVariable("mso-request-id");
225 WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
226 TargetEntities extSystemErrorSource = exception.getExtSystemErrorSource();
227 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
228 Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
229 if (isRollbackFailure == null) {
230 isRollbackFailure = false;
233 if (extSystemErrorSource != null) {
234 String extSystemErrorSourceString = extSystemErrorSource.toString();
235 if (isRollbackFailure) {
236 logger.debug("Updating extSystemErrorSource for isRollbackFailure to {} for request: {}",
237 extSystemErrorSourceString, requestId);
238 request.setRollbackExtSystemErrorSource(extSystemErrorSourceString);
240 logger.debug("Updating extSystemErrorSource to {} for request: {}", extSystemErrorSourceString,
242 request.setExtSystemErrorSource(extSystemErrorSourceString);
244 } else if (isRollbackFailure) {
246 "rollbackExtSystemErrorSource is null for isRollbackFailure. Setting rollbackExtSystemErrorSource to UNKNOWN");
247 request.setRollbackExtSystemErrorSource(TargetEntity.UNKNOWN.toString());
249 logger.debug("extSystemErrorSource is null. Setting extSystemErrorSource to UNKNOWN");
250 request.setExtSystemErrorSource(TargetEntity.UNKNOWN.toString());
253 request.setLastModifiedBy("CamundaBPMN");
254 requestDbclient.updateInfraActiveRequests(request);
255 } catch (Exception e) {
256 logger.error("Failed to update Request db with extSystemErrorSource or rollbackExtSystemErrorSource: ", e);