2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
23 import java.sql.Timestamp;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.UUID;
27 import javax.persistence.EntityNotFoundException;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.aai.domain.yang.Vnfc;
30 import org.onap.so.bpmn.common.DelegateExecutionImpl;
31 import org.onap.so.bpmn.common.listener.db.RequestsDbListenerRunner;
32 import org.onap.so.bpmn.common.listener.flowmanipulator.FlowManipulatorListenerRunner;
33 import org.onap.so.bpmn.common.workflow.context.WorkflowCallbackResponse;
34 import org.onap.so.bpmn.common.workflow.context.WorkflowContextHolder;
35 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
36 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
37 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
38 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
39 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
40 import org.onap.so.client.aai.AAIObjectType;
41 import org.onap.so.client.exception.ExceptionBuilder;
42 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
43 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
44 import org.onap.so.db.catalog.client.CatalogDbClient;
45 import org.onap.so.db.request.beans.InfraActiveRequests;
46 import org.onap.so.db.request.client.RequestsDbClient;
47 import org.onap.so.serviceinstancebeans.RequestReferences;
48 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.core.env.Environment;
53 import org.springframework.stereotype.Component;
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
58 public class WorkflowActionBBTasks {
60 private static final String G_CURRENT_SEQUENCE = "gCurrentSequence";
61 private static final String G_REQUEST_ID = "mso-request-id";
62 private static final String G_ALACARTE = "aLaCarte";
63 private static final String G_ACTION = "requestAction";
64 private static final String RETRY_COUNT = "retryCount";
65 private static final String FABRIC_CONFIGURATION = "FabricConfiguration";
66 private static final String ASSIGN_FABRIC_CONFIGURATION_BB = "AssignFabricConfigurationBB";
67 private static final String ACTIVATE_FABRIC_CONFIGURATION_BB = "ActivateFabricConfigurationBB";
68 protected String maxRetries = "mso.rainyDay.maxRetries";
69 private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBTasks.class);
72 private RequestsDbClient requestDbclient;
74 private WorkflowAction workflowAction;
76 private WorkflowActionBBFailure workflowActionBBFailure;
78 private Environment environment;
80 private BBInputSetupUtils bbInputSetupUtils;
82 private CatalogDbClient catalogDbClient;
84 private FlowManipulatorListenerRunner flowManipulatorListenerRunner;
86 private RequestsDbListenerRunner requestsDbListener;
88 public void selectBB(DelegateExecution execution) {
89 List<ExecuteBuildingBlock> flowsToExecute =
90 (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
91 execution.setVariable("MacroRollback", false);
93 flowManipulatorListenerRunner.modifyFlows(flowsToExecute, new DelegateExecutionImpl(execution));
94 int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE);
96 ExecuteBuildingBlock ebb = flowsToExecute.get(currentSequence);
98 execution.setVariable("buildingBlock", ebb);
100 if (currentSequence >= flowsToExecute.size()) {
101 execution.setVariable("completed", true);
103 execution.setVariable("completed", false);
105 execution.setVariable(G_CURRENT_SEQUENCE, currentSequence);
108 public void updateFlowStatistics(DelegateExecution execution) {
110 int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE);
111 if (currentSequence > 1) {
112 InfraActiveRequests request = this.getUpdatedRequest(execution, currentSequence);
113 requestDbclient.updateInfraActiveRequests(request);
115 } catch (Exception ex) {
117 "Bpmn Flow Statistics was unable to update Request Db with the new completion percentage. Competion percentage may be invalid.");
121 protected InfraActiveRequests getUpdatedRequest(DelegateExecution execution, int currentSequence) {
122 List<ExecuteBuildingBlock> flowsToExecute =
123 (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
124 String requestId = (String) execution.getVariable(G_REQUEST_ID);
125 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
126 ExecuteBuildingBlock completedBB = flowsToExecute.get(currentSequence - 2);
127 ExecuteBuildingBlock nextBB = flowsToExecute.get(currentSequence - 1);
128 int completedBBs = currentSequence - 1;
129 int totalBBs = flowsToExecute.size();
130 int remainingBBs = totalBBs - completedBBs;
131 String statusMessage = this.getStatusMessage(completedBB.getBuildingBlock().getBpmnFlowName(),
132 nextBB.getBuildingBlock().getBpmnFlowName(), completedBBs, remainingBBs);
133 Long percentProgress = this.getPercentProgress(completedBBs, totalBBs);
134 request.setFlowStatus(statusMessage);
135 request.setProgress(percentProgress);
136 request.setLastModifiedBy("CamundaBPMN");
140 protected Long getPercentProgress(int completedBBs, int totalBBs) {
141 double ratio = (completedBBs / (totalBBs * 1.0));
142 int percentProgress = (int) (ratio * 95);
143 return new Long(percentProgress + 5);
146 protected String getStatusMessage(String completedBB, String nextBB, int completedBBs, int remainingBBs) {
147 return "Execution of " + completedBB + " has completed successfully, next invoking " + nextBB
148 + " (Execution Path progress: BBs completed = " + completedBBs + "; BBs remaining = " + remainingBBs
152 public void sendSyncAck(DelegateExecution execution) {
153 final String requestId = (String) execution.getVariable(G_REQUEST_ID);
154 final String resourceId = (String) execution.getVariable("resourceId");
155 ServiceInstancesResponse serviceInstancesResponse = new ServiceInstancesResponse();
156 RequestReferences requestRef = new RequestReferences();
157 requestRef.setInstanceId(resourceId);
158 requestRef.setRequestId(requestId);
159 serviceInstancesResponse.setRequestReferences(requestRef);
160 ObjectMapper mapper = new ObjectMapper();
161 String jsonRequest = "";
163 jsonRequest = mapper.writeValueAsString(serviceInstancesResponse);
164 } catch (JsonProcessingException e) {
165 workflowAction.buildAndThrowException(execution,
166 "Could not marshall ServiceInstancesRequest to Json string to respond to API Handler.", e);
168 WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse();
169 callbackResponse.setStatusCode(200);
170 callbackResponse.setMessage("Success");
171 callbackResponse.setResponse(jsonRequest);
172 String processKey = execution.getProcessEngineServices().getRepositoryService()
173 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
174 WorkflowContextHolder.getInstance().processCallback(processKey, execution.getProcessInstanceId(), requestId,
176 logger.info("Successfully sent sync ack.");
177 updateInstanceId(execution);
180 public void sendErrorSyncAck(DelegateExecution execution) {
181 final String requestId = (String) execution.getVariable(G_REQUEST_ID);
183 ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
184 String errorMsg = (String) execution.getVariable("WorkflowActionErrorMessage");
185 if (errorMsg == null) {
186 errorMsg = "WorkflowAction failed unexpectedly.";
188 String processKey = exceptionBuilder.getProcessKey(execution);
189 String buildworkflowException =
190 "<aetgt:WorkflowException xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\"><aetgt:ErrorMessage>"
192 + "</aetgt:ErrorMessage><aetgt:ErrorCode>7000</aetgt:ErrorCode></aetgt:WorkflowException>";
193 WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse();
194 callbackResponse.setStatusCode(500);
195 callbackResponse.setMessage("Fail");
196 callbackResponse.setResponse(buildworkflowException);
197 WorkflowContextHolder.getInstance().processCallback(processKey, execution.getProcessInstanceId(), requestId,
199 execution.setVariable("sentSyncResponse", true);
200 } catch (Exception ex) {
201 logger.error(" Sending Sync Error Activity Failed. {}", ex.getMessage(), ex);
205 public void updateRequestStatusToComplete(DelegateExecution execution) {
207 final String requestId = (String) execution.getVariable(G_REQUEST_ID);
208 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
209 final String action = (String) execution.getVariable(G_ACTION);
210 final boolean aLaCarte = (boolean) execution.getVariable(G_ALACARTE);
211 final String resourceName = (String) execution.getVariable("resourceName");
212 String statusMessage = (String) execution.getVariable("StatusMessage");
213 String macroAction = "";
214 if (statusMessage == null) {
216 macroAction = "ALaCarte-" + resourceName + "-" + action + " request was executed correctly.";
218 macroAction = "Macro-" + resourceName + "-" + action + " request was executed correctly.";
221 macroAction = statusMessage;
223 execution.setVariable("finalStatusMessage", macroAction);
224 Timestamp endTime = new Timestamp(System.currentTimeMillis());
225 request.setEndTime(endTime);
226 request.setFlowStatus("Successfully completed all Building Blocks");
227 request.setStatusMessage(macroAction);
228 request.setProgress(Long.valueOf(100));
229 request.setRequestStatus("COMPLETE");
230 request.setLastModifiedBy("CamundaBPMN");
231 requestsDbListener.post(request, new DelegateExecutionImpl(execution));
232 requestDbclient.updateInfraActiveRequests(request);
233 } catch (Exception ex) {
234 workflowAction.buildAndThrowException(execution, "Error Updating Request Database", ex);
238 public void checkRetryStatus(DelegateExecution execution) {
239 String handlingCode = (String) execution.getVariable("handlingCode");
240 String requestId = (String) execution.getVariable(G_REQUEST_ID);
241 String retryDuration = (String) execution.getVariable("RetryDuration");
242 int retryCount = (int) execution.getVariable(RETRY_COUNT);
245 envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
246 } catch (Exception ex) {
247 logger.error("Could not read maxRetries from config file. Setting max to 5 retries");
250 int nextCount = retryCount + 1;
251 if (handlingCode.equals("Retry")) {
252 workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
254 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
255 request.setRetryStatusMessage(
256 "Retry " + nextCount + "/" + envMaxRetries + " will be started in " + retryDuration);
257 requestDbclient.updateInfraActiveRequests(request);
258 } catch (Exception ex) {
259 logger.warn("Failed to update Request Db Infra Active Requests with Retry Status", ex);
261 if (retryCount < envMaxRetries) {
262 int currSequence = (int) execution.getVariable("gCurrentSequence");
263 execution.setVariable("gCurrentSequence", currSequence - 1);
264 execution.setVariable(RETRY_COUNT, nextCount);
266 workflowAction.buildAndThrowException(execution,
267 "Exceeded maximum retries. Ending flow with status Abort");
270 execution.setVariable(RETRY_COUNT, 0);
275 * Rollback will only handle Create/Activate/Assign Macro flows. Execute layer will rollback the flow its currently
278 public void rollbackExecutionPath(DelegateExecution execution) {
279 if (!(boolean) execution.getVariable("isRollback")) {
280 List<ExecuteBuildingBlock> flowsToExecute =
281 (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
282 List<ExecuteBuildingBlock> rollbackFlows = new ArrayList();
283 int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE);
284 int listSize = flowsToExecute.size();
285 for (int i = listSize - 1; i >= 0; i--) {
286 if (i > currentSequence - 1) {
287 flowsToExecute.remove(i);
289 String flowName = flowsToExecute.get(i).getBuildingBlock().getBpmnFlowName();
290 if (flowName.contains("Assign")) {
291 flowName = "Unassign" + flowName.substring(6, flowName.length());
292 } else if (flowName.contains("Create")) {
293 flowName = "Delete" + flowName.substring(6, flowName.length());
294 } else if (flowName.contains("Activate")) {
295 flowName = "Deactivate" + flowName.substring(8, flowName.length());
299 flowsToExecute.get(i).getBuildingBlock().setBpmnFlowName(flowName);
300 rollbackFlows.add(flowsToExecute.get(i));
304 String handlingCode = (String) execution.getVariable("handlingCode");
305 List<ExecuteBuildingBlock> rollbackFlowsFiltered = new ArrayList<>();
306 rollbackFlowsFiltered.addAll(rollbackFlows);
307 if (handlingCode.equals("RollbackToAssigned") || handlingCode.equals("RollbackToCreated")) {
308 for (int i = 0; i < rollbackFlows.size(); i++) {
309 if (rollbackFlows.get(i).getBuildingBlock().getBpmnFlowName().contains("Unassign")) {
310 rollbackFlowsFiltered.remove(rollbackFlows.get(i));
311 } else if (rollbackFlows.get(i).getBuildingBlock().getBpmnFlowName().contains("Delete")
312 && handlingCode.equals("RollbackToCreated")) {
313 rollbackFlowsFiltered.remove(rollbackFlows.get(i));
318 workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
319 if (rollbackFlows.isEmpty())
320 execution.setVariable("isRollbackNeeded", false);
322 execution.setVariable("isRollbackNeeded", true);
323 execution.setVariable("flowsToExecute", rollbackFlowsFiltered);
324 execution.setVariable("handlingCode", "PreformingRollback");
325 execution.setVariable("isRollback", true);
326 execution.setVariable("gCurrentSequence", 0);
327 execution.setVariable(RETRY_COUNT, 0);
329 workflowAction.buildAndThrowException(execution,
330 "Rollback has already been called. Cannot rollback a request that is currently in the rollback state.");
334 protected void updateInstanceId(DelegateExecution execution) {
336 String requestId = (String) execution.getVariable(G_REQUEST_ID);
337 String resourceId = (String) execution.getVariable("resourceId");
338 WorkflowType resourceType = (WorkflowType) execution.getVariable("resourceType");
339 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
340 if (resourceType == WorkflowType.SERVICE) {
341 request.setServiceInstanceId(resourceId);
342 } else if (resourceType == WorkflowType.VNF) {
343 request.setVnfId(resourceId);
344 } else if (resourceType == WorkflowType.VFMODULE) {
345 request.setVfModuleId(resourceId);
346 } else if (resourceType == WorkflowType.VOLUMEGROUP) {
347 request.setVolumeGroupId(resourceId);
348 } else if (resourceType == WorkflowType.NETWORK) {
349 request.setNetworkId(resourceId);
350 } else if (resourceType == WorkflowType.CONFIGURATION) {
351 request.setConfigurationId(resourceId);
352 } else if (resourceType == WorkflowType.INSTANCE_GROUP) {
353 request.setInstanceGroupId(resourceId);
355 requestDbclient.updateInfraActiveRequests(request);
356 } catch (Exception ex) {
357 workflowAction.buildAndThrowException(execution, "Failed to update Request db with instanceId");
361 public void postProcessingExecuteBB(DelegateExecution execution) {
362 List<ExecuteBuildingBlock> flowsToExecute =
363 (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
364 String handlingCode = (String) execution.getVariable("handlingCode");
365 final boolean aLaCarte = (boolean) execution.getVariable(G_ALACARTE);
366 int currentSequence = (int) execution.getVariable(G_CURRENT_SEQUENCE);
367 ExecuteBuildingBlock ebb = flowsToExecute.get(currentSequence - 1);
368 String bbFlowName = ebb.getBuildingBlock().getBpmnFlowName();
369 if (bbFlowName.equalsIgnoreCase("ActivateVfModuleBB") && aLaCarte && handlingCode.equalsIgnoreCase("Success")) {
370 postProcessingExecuteBBActivateVfModule(execution, ebb, flowsToExecute);
374 protected void postProcessingExecuteBBActivateVfModule(DelegateExecution execution, ExecuteBuildingBlock ebb,
375 List<ExecuteBuildingBlock> flowsToExecute) {
377 String serviceInstanceId = ebb.getWorkflowResourceIds().getServiceInstanceId();
378 String vnfId = ebb.getWorkflowResourceIds().getVnfId();
379 String vfModuleId = ebb.getResourceId();
380 ebb.getWorkflowResourceIds().setVfModuleId(vfModuleId);
381 String serviceModelUUID =
382 bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId).getModelVersionId();
383 String vnfCustomizationUUID = bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId();
384 String vfModuleCustomizationUUID =
385 bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId).getModelCustomizationId();
387 workflowAction.getRelatedResourcesInVfModule(vnfId, vfModuleId, Vnfc.class, AAIObjectType.VNFC);
388 logger.debug("Vnfc Size: {}", vnfcs.size());
389 for (Vnfc vnfc : vnfcs) {
390 String modelCustomizationId = vnfc.getModelCustomizationId();
391 logger.debug("Processing Vnfc: {}", modelCustomizationId);
392 CvnfcConfigurationCustomization fabricConfig = catalogDbClient.getCvnfcCustomization(serviceModelUUID,
393 vnfCustomizationUUID, vfModuleCustomizationUUID, modelCustomizationId);
394 if (fabricConfig != null && fabricConfig.getConfigurationResource() != null
395 && fabricConfig.getConfigurationResource().getToscaNodeType() != null
396 && fabricConfig.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)) {
397 String configurationId = UUID.randomUUID().toString();
398 ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
399 configurationResourceKeys.setCvnfcCustomizationUUID(modelCustomizationId);
400 configurationResourceKeys.setVfModuleCustomizationUUID(vfModuleCustomizationUUID);
401 configurationResourceKeys.setVnfResourceCustomizationUUID(vnfCustomizationUUID);
402 configurationResourceKeys.setVnfcName(vnfc.getVnfcName());
403 ExecuteBuildingBlock assignConfigBB = getExecuteBBForConfig(ASSIGN_FABRIC_CONFIGURATION_BB, ebb,
404 configurationId, configurationResourceKeys);
405 ExecuteBuildingBlock activateConfigBB = getExecuteBBForConfig(ACTIVATE_FABRIC_CONFIGURATION_BB, ebb,
406 configurationId, configurationResourceKeys);
407 flowsToExecute.add(assignConfigBB);
408 flowsToExecute.add(activateConfigBB);
409 flowsToExecute.stream()
410 .forEach(executeBB -> logger.info("Flows to Execute After Post Processing: {}",
411 executeBB.getBuildingBlock().getBpmnFlowName()));
412 execution.setVariable("flowsToExecute", flowsToExecute);
413 execution.setVariable("completed", false);
415 logger.debug("No cvnfcCustomization found for customizationId: " + modelCustomizationId);
418 } catch (EntityNotFoundException e) {
419 logger.debug(e.getMessage() + " Will not be running Fabric Config Building Blocks");
420 } catch (Exception e) {
421 String errorMessage = "Error occurred in post processing of Vf Module create";
422 execution.setVariable("handlingCode", "RollbackToCreated");
423 execution.setVariable("WorkflowActionErrorMessage", errorMessage);
424 logger.error(errorMessage, e);
428 protected ExecuteBuildingBlock getExecuteBBForConfig(String bbName, ExecuteBuildingBlock ebb,
429 String configurationId, ConfigurationResourceKeys configurationResourceKeys) {
430 ExecuteBuildingBlock configBB = new ExecuteBuildingBlock();
431 BuildingBlock buildingBlock = new BuildingBlock();
432 buildingBlock.setBpmnFlowName(bbName);
433 buildingBlock.setMsoId(UUID.randomUUID().toString());
434 configBB.setaLaCarte(ebb.isaLaCarte());
435 configBB.setApiVersion(ebb.getApiVersion());
436 configBB.setRequestAction(ebb.getRequestAction());
437 configBB.setVnfType(ebb.getVnfType());
438 configBB.setRequestId(ebb.getRequestId());
439 configBB.setRequestDetails(ebb.getRequestDetails());
440 configBB.setBuildingBlock(buildingBlock);
441 WorkflowResourceIds workflowResourceIds = ebb.getWorkflowResourceIds();
442 workflowResourceIds.setConfigurationId(configurationId);
443 configBB.setWorkflowResourceIds(workflowResourceIds);
444 configBB.setConfigurationResourceKeys(configurationResourceKeys);