Merge "Enable DeleteChildService functionality" into recursive-orch
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowActionBBTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks;
22
23 import java.sql.Timestamp;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.UUID;
28 import java.util.stream.Collectors;
29 import javax.persistence.EntityNotFoundException;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.onap.aai.domain.yang.GenericVnf;
32 import org.onap.aai.domain.yang.InstanceGroup;
33 import org.onap.aai.domain.yang.L3Network;
34 import org.onap.aai.domain.yang.ServiceInstance;
35 import org.onap.aai.domain.yang.VfModule;
36 import org.onap.aai.domain.yang.Vnfc;
37 import org.onap.aai.domain.yang.VolumeGroup;
38 import org.onap.aaiclient.client.aai.entities.Configuration;
39 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
40 import org.onap.so.bpmn.common.BBConstants;
41 import org.onap.so.bpmn.common.DelegateExecutionImpl;
42 import org.onap.so.bpmn.common.listener.db.RequestsDbListenerRunner;
43 import org.onap.so.bpmn.common.listener.flowmanipulator.FlowManipulatorListenerRunner;
44 import org.onap.so.bpmn.common.workflow.context.WorkflowCallbackResponse;
45 import org.onap.so.bpmn.common.workflow.context.WorkflowContextHolder;
46 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
47 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
48 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
49 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
50 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
51 import org.onap.so.client.exception.ExceptionBuilder;
52 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
53 import org.onap.so.db.catalog.client.CatalogDbClient;
54 import org.onap.so.db.request.beans.InfraActiveRequests;
55 import org.onap.so.db.request.client.RequestsDbClient;
56 import org.onap.so.serviceinstancebeans.ModelType;
57 import org.onap.so.serviceinstancebeans.RelatedInstance;
58 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
59 import org.onap.so.serviceinstancebeans.RequestReferences;
60 import org.onap.so.serviceinstancebeans.ServiceInstancesResponse;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.core.env.Environment;
65 import org.springframework.stereotype.Component;
66 import com.fasterxml.jackson.core.JsonProcessingException;
67 import com.fasterxml.jackson.databind.ObjectMapper;
68 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_ERROR;
69 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_MESSAGE_NAME;
70 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_STATUS;
71 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.IS_CHILD_PROCESS;
72 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.CHILD_SVC_REQ_CORRELATION_ID;
73 import static org.onap.so.bpmn.infrastructure.service.composition.ServiceCompositionConstants.PARENT_CORRELATION_ID;
74
75 @Component
76 public class WorkflowActionBBTasks {
77
78     private static final String RETRY_COUNT = "retryCount";
79     private static final String FABRIC_CONFIGURATION = "FabricConfiguration";
80     private static final String ADD_FABRIC_CONFIGURATION_BB = "AddFabricConfigurationBB";
81     private static final String COMPLETED = "completed";
82     private static final String HANDLINGCODE = "handlingCode";
83     private static final String ROLLBACKTOCREATED = "RollbackToCreated";
84     private static final String ROLLBACKTOCREATEDNOCONFIGURATION = "RollbackToCreatedNoConfiguration";
85     private static final String REPLACEINSTANCE = "replaceInstance";
86     private static final String VFMODULE = "VfModule";
87     private static final String CONFIGURATION_PATTERN = "(Ad|De)(.*)FabricConfiguration(.*)";
88     protected String maxRetries = "mso.rainyDay.maxRetries";
89     private static final Logger logger = LoggerFactory.getLogger(WorkflowActionBBTasks.class);
90
91     @Autowired
92     private RequestsDbClient requestDbclient;
93     @Autowired
94     private WorkflowAction workflowAction;
95     @Autowired
96     private WorkflowActionBBFailure workflowActionBBFailure;
97     @Autowired
98     private Environment environment;
99     @Autowired
100     private BBInputSetupUtils bbInputSetupUtils;
101     @Autowired
102     private CatalogDbClient catalogDbClient;
103     @Autowired
104     private FlowManipulatorListenerRunner flowManipulatorListenerRunner;
105     @Autowired
106     private RequestsDbListenerRunner requestsDbListener;
107
108     public void selectBB(DelegateExecution execution) {
109         try {
110             List<ExecuteBuildingBlock> flowsToExecute =
111                     (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
112             execution.setVariable("MacroRollback", false);
113             try {
114                 flowManipulatorListenerRunner.modifyFlows(flowsToExecute, new DelegateExecutionImpl(execution));
115             } catch (NullPointerException ex) {
116                 workflowAction.buildAndThrowException(execution, "Error in FlowManipulator Modify Flows", ex);
117             }
118             int currentSequence = (int) execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
119
120             boolean completed = false;
121             if (currentSequence < flowsToExecute.size()) {
122                 ExecuteBuildingBlock ebb = flowsToExecute.get(currentSequence);
123                 execution.setVariable("buildingBlock", ebb);
124                 execution.setVariable(BBConstants.G_CURRENT_SEQUENCE, currentSequence + 1);
125             } else {
126                 completed = true;
127             }
128             execution.setVariable(COMPLETED, completed);
129         } catch (Exception e) {
130             workflowAction.buildAndThrowException(execution, "Internal Error occured during selectBB", e);
131         }
132     }
133
134     public void updateFlowStatistics(DelegateExecution execution) {
135         try {
136             int currentSequence = (int) execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
137             if (currentSequence > 1) {
138                 InfraActiveRequests request = this.getUpdatedRequest(execution, currentSequence);
139                 requestDbclient.updateInfraActiveRequests(request);
140             }
141         } catch (Exception ex) {
142             logger.warn(
143                     "Bpmn Flow Statistics was unable to update Request Db with the new completion percentage. Competion percentage may be invalid.",
144                     ex);
145         }
146     }
147
148     protected InfraActiveRequests getUpdatedRequest(DelegateExecution execution, int currentSequence) {
149         List<ExecuteBuildingBlock> flowsToExecute =
150                 (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
151         String requestId = (String) execution.getVariable(BBConstants.G_REQUEST_ID);
152         InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
153         ExecuteBuildingBlock completedBB = flowsToExecute.get(currentSequence - 2);
154         ExecuteBuildingBlock nextBB = flowsToExecute.get(currentSequence - 1);
155         int completedBBs = currentSequence - 1;
156         int totalBBs = flowsToExecute.size();
157         int remainingBBs = totalBBs - completedBBs;
158         String statusMessage = this.getStatusMessage(completedBB.getBuildingBlock().getBpmnFlowName(),
159                 nextBB.getBuildingBlock().getBpmnFlowName(), completedBBs, remainingBBs);
160         Long percentProgress = this.getPercentProgress(completedBBs, totalBBs);
161         request.setFlowStatus(statusMessage);
162         request.setProgress(percentProgress);
163         request.setLastModifiedBy("CamundaBPMN");
164         return request;
165     }
166
167     protected Long getPercentProgress(int completedBBs, int totalBBs) {
168         double ratio = (completedBBs / (totalBBs * 1.0));
169         int percentProgress = (int) (ratio * 95);
170         return (long) (percentProgress + 5);
171     }
172
173     protected String getStatusMessage(String completedBB, String nextBB, int completedBBs, int remainingBBs) {
174         return "Execution of " + completedBB + " has completed successfully, next invoking " + nextBB
175                 + " (Execution Path progress: BBs completed = " + completedBBs + "; BBs remaining = " + remainingBBs
176                 + ").";
177     }
178
179     public void sendSyncAck(DelegateExecution execution) {
180         final String requestId = (String) execution.getVariable(BBConstants.G_REQUEST_ID);
181         final String resourceId = (String) execution.getVariable("resourceId");
182         ServiceInstancesResponse serviceInstancesResponse = new ServiceInstancesResponse();
183         RequestReferences requestRef = new RequestReferences();
184         requestRef.setInstanceId(resourceId);
185         requestRef.setRequestId(requestId);
186         serviceInstancesResponse.setRequestReferences(requestRef);
187         ObjectMapper mapper = new ObjectMapper();
188         String jsonRequest = "";
189         try {
190             jsonRequest = mapper.writeValueAsString(serviceInstancesResponse);
191         } catch (JsonProcessingException e) {
192             workflowAction.buildAndThrowException(execution,
193                     "Could not marshall ServiceInstancesRequest to Json string to respond to API Handler.", e);
194         }
195         WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse();
196         callbackResponse.setStatusCode(200);
197         callbackResponse.setMessage("Success");
198         callbackResponse.setResponse(jsonRequest);
199         String processKey = execution.getProcessEngineServices().getRepositoryService()
200                 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
201         WorkflowContextHolder.getInstance().processCallback(processKey, execution.getProcessInstanceId(), requestId,
202                 callbackResponse);
203         logger.info("Successfully sent sync ack.");
204         updateInstanceId(execution);
205     }
206
207     public void sendErrorSyncAck(DelegateExecution execution) {
208         final String requestId = (String) execution.getVariable(BBConstants.G_REQUEST_ID);
209         try {
210             ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
211             String errorMsg = (String) execution.getVariable("WorkflowActionErrorMessage");
212             if (errorMsg == null) {
213                 errorMsg = "WorkflowAction failed unexpectedly.";
214             }
215             String processKey = exceptionBuilder.getProcessKey(execution);
216             String buildworkflowException =
217                     "<aetgt:WorkflowException xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\"><aetgt:ErrorMessage>"
218                             + errorMsg
219                             + "</aetgt:ErrorMessage><aetgt:ErrorCode>7000</aetgt:ErrorCode></aetgt:WorkflowException>";
220             WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse();
221             callbackResponse.setStatusCode(500);
222             callbackResponse.setMessage("Fail");
223             callbackResponse.setResponse(buildworkflowException);
224             WorkflowContextHolder.getInstance().processCallback(processKey, execution.getProcessInstanceId(), requestId,
225                     callbackResponse);
226             execution.setVariable("sentSyncResponse", true);
227         } catch (Exception ex) {
228             logger.error(" Sending Sync Error Activity Failed. {}", ex.getMessage(), ex);
229         }
230     }
231
232     public void updateRequestStatusToComplete(DelegateExecution execution) {
233         try {
234             final String requestId = (String) execution.getVariable(BBConstants.G_REQUEST_ID);
235             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
236             final String action = (String) execution.getVariable(BBConstants.G_ACTION);
237             final boolean aLaCarte = (boolean) execution.getVariable(BBConstants.G_ALACARTE);
238             final String resourceName = (String) execution.getVariable("resourceName");
239             String statusMessage = (String) execution.getVariable("StatusMessage");
240
241             if (Boolean.TRUE.equals(execution.getVariable(IS_CHILD_PROCESS))) {
242                 String parentCorrelationId = (String) execution.getVariable(PARENT_CORRELATION_ID);
243                 logger.info("Child service request completed. Sending message to parent process with correlationId: "
244                         + parentCorrelationId);
245                 execution.getProcessEngineServices().getRuntimeService()
246                         .createMessageCorrelation(CHILD_SVC_REQ_MESSAGE_NAME)
247                         .setVariable(CHILD_SVC_REQ_STATUS, "COMPLETED").setVariable(CHILD_SVC_REQ_ERROR, "")
248                         .processInstanceVariableEquals(CHILD_SVC_REQ_CORRELATION_ID, parentCorrelationId).correlate();
249             }
250
251             String macroAction;
252             if (statusMessage == null) {
253                 if (aLaCarte) {
254                     macroAction = "ALaCarte-" + resourceName + "-" + action + " request was executed correctly.";
255                 } else {
256                     macroAction = "Macro-" + resourceName + "-" + action + " request was executed correctly.";
257                 }
258             } else {
259                 macroAction = statusMessage;
260             }
261             execution.setVariable("finalStatusMessage", macroAction);
262             Timestamp endTime = new Timestamp(System.currentTimeMillis());
263             request.setEndTime(endTime);
264             request.setFlowStatus("Successfully completed all Building Blocks");
265             request.setStatusMessage(macroAction);
266             request.setProgress(100L);
267             request.setRequestStatus("COMPLETE");
268             request.setLastModifiedBy("CamundaBPMN");
269             requestsDbListener.post(request, new DelegateExecutionImpl(execution));
270             requestDbclient.updateInfraActiveRequests(request);
271         } catch (Exception ex) {
272             workflowAction.buildAndThrowException(execution, "Error Updating Request Database", ex);
273         }
274     }
275
276     public void checkRetryStatus(DelegateExecution execution) {
277         String handlingCode = (String) execution.getVariable(HANDLINGCODE);
278         String requestId = (String) execution.getVariable(BBConstants.G_REQUEST_ID);
279         String retryDuration = (String) execution.getVariable("RetryDuration");
280         int retryCount = (int) execution.getVariable(RETRY_COUNT);
281         int envMaxRetries;
282         try {
283             envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
284         } catch (Exception ex) {
285             logger.error("Could not read maxRetries from config file. Setting max to 5 retries", ex);
286             envMaxRetries = 5;
287         }
288         int nextCount = retryCount + 1;
289         if ("Retry".equals(handlingCode)) {
290             workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
291             try {
292                 InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
293                 request.setRetryStatusMessage(
294                         "Retry " + nextCount + "/" + envMaxRetries + " will be started in " + retryDuration);
295                 requestDbclient.updateInfraActiveRequests(request);
296             } catch (Exception ex) {
297                 logger.warn("Failed to update Request Db Infra Active Requests with Retry Status", ex);
298             }
299             if (retryCount < envMaxRetries) {
300                 int currSequence = (int) execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
301                 execution.setVariable(BBConstants.G_CURRENT_SEQUENCE, currSequence - 1);
302                 execution.setVariable(RETRY_COUNT, nextCount);
303             } else {
304                 workflowAction.buildAndThrowException(execution,
305                         "Exceeded maximum retries. Ending flow with status Abort");
306             }
307         } else {
308             execution.setVariable(RETRY_COUNT, 0);
309         }
310     }
311
312     /**
313      * Rollback will only handle Create/Activate/Assign Macro flows. Execute layer will rollback the flow its currently
314      * working on.
315      */
316     public void rollbackExecutionPath(DelegateExecution execution) {
317         final String action = (String) execution.getVariable(BBConstants.G_ACTION);
318         final String resourceName = (String) execution.getVariable("resourceName");
319         if (!(boolean) execution.getVariable("isRollback")) {
320             List<ExecuteBuildingBlock> flowsToExecute =
321                     (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
322
323             List<ExecuteBuildingBlock> flowsToExecuteChangeBBs = flowsToExecute.stream()
324                     .filter(buildingBlock -> buildingBlock.getBuildingBlock().getBpmnFlowName().startsWith("Change"))
325                     .collect(Collectors.toList());
326
327             List<ExecuteBuildingBlock> rollbackFlows = new ArrayList<>();
328             int currentSequence = (int) execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
329             int listSize = flowsToExecute.size();
330
331             for (int i = listSize - 1; i >= 0; i--) {
332                 if (i > currentSequence - 1) {
333                     flowsToExecute.remove(i);
334                 } else {
335                     String flowName = flowsToExecute.get(i).getBuildingBlock().getBpmnFlowName();
336                     if (flowName.startsWith("Assign")) {
337                         flowName = flowName.replaceFirst("Assign", "Unassign");
338                     } else if (flowName.startsWith("Create")) {
339                         flowName = flowName.replaceFirst("Create", "Delete");
340                     } else if (flowName.startsWith("Activate")) {
341                         flowName = flowName.replaceFirst("Activate", "Deactivate");
342                     } else if (flowName.startsWith("Add")) {
343                         flowName = flowName.replaceFirst("Add", "Delete");
344                     } else if (flowName.startsWith("VNF")) {
345                         if (flowName.startsWith("VNFSet")) {
346                             flowName = flowName.replaceFirst("VNFSet", "VNFUnset");
347                         } else if (flowName.startsWith("VNFLock")) {
348                             flowName = flowName.replaceFirst("VNFLock", "VNFUnlock");
349                         } else if (flowName.startsWith("VNFStop")) {
350                             flowName = flowName.replaceFirst("VNFStop", "VNFStart");
351                         } else if (flowName.startsWith("VNFQuiesce")) {
352                             flowName = flowName.replaceFirst("VNFQuiesce", "VNFResume");
353                         } else {
354                             continue;
355                         }
356                     } else {
357                         continue;
358                     }
359                     flowsToExecute.get(i).getBuildingBlock().setBpmnFlowName(flowName);
360                     rollbackFlows.add(flowsToExecute.get(i));
361                 }
362             }
363
364             String handlingCode = (String) execution.getVariable(HANDLINGCODE);
365             List<ExecuteBuildingBlock> rollbackFlowsFiltered = new ArrayList<>(rollbackFlows);
366             if ("RollbackToAssigned".equals(handlingCode) || ROLLBACKTOCREATED.equals(handlingCode)
367                     || ROLLBACKTOCREATEDNOCONFIGURATION.equals(handlingCode)) {
368                 for (ExecuteBuildingBlock rollbackFlow : rollbackFlows) {
369                     if (rollbackFlow.getBuildingBlock().getBpmnFlowName().contains("Unassign")
370                             && !rollbackFlow.getBuildingBlock().getBpmnFlowName().contains("FabricConfiguration")) {
371                         rollbackFlowsFiltered.remove(rollbackFlow);
372                     } else if (rollbackFlow.getBuildingBlock().getBpmnFlowName().contains("Delete")
373                             && ((!rollbackFlow.getBuildingBlock().getBpmnFlowName().contains("FabricConfiguration")
374                                     && (ROLLBACKTOCREATED.equals(handlingCode)
375                                             || ROLLBACKTOCREATEDNOCONFIGURATION.equals(handlingCode)))
376                                     || (rollbackFlow.getBuildingBlock().getBpmnFlowName()
377                                             .contains("FabricConfiguration")
378                                             && ROLLBACKTOCREATEDNOCONFIGURATION.equals(handlingCode)))) {
379                         rollbackFlowsFiltered.remove(rollbackFlow);
380                     }
381                 }
382             }
383
384             List<ExecuteBuildingBlock> rollbackFlowsFilteredNonChangeBBs = new ArrayList<>();
385             if (action.equals(REPLACEINSTANCE) && resourceName.equals(VFMODULE)) {
386                 for (ExecuteBuildingBlock executeBuildingBlock : rollbackFlowsFiltered) {
387                     if (!executeBuildingBlock.getBuildingBlock().getBpmnFlowName().startsWith("Change")) {
388                         rollbackFlowsFilteredNonChangeBBs.add(executeBuildingBlock);
389                     }
390                 }
391                 rollbackFlowsFiltered.clear();
392                 rollbackFlowsFiltered.addAll(flowsToExecuteChangeBBs);
393                 rollbackFlowsFiltered.addAll(rollbackFlowsFilteredNonChangeBBs);
394             }
395
396             workflowActionBBFailure.updateRequestErrorStatusMessage(execution);
397             execution.setVariable("isRollbackNeeded", !rollbackFlows.isEmpty());
398             execution.setVariable("flowsToExecute", rollbackFlowsFiltered);
399             execution.setVariable(HANDLINGCODE, "PreformingRollback");
400             execution.setVariable("isRollback", true);
401             execution.setVariable(BBConstants.G_CURRENT_SEQUENCE, 0);
402             execution.setVariable(RETRY_COUNT, 0);
403         } else {
404             workflowAction.buildAndThrowException(execution,
405                     "Rollback has already been called. Cannot rollback a request that is currently in the rollback state.");
406         }
407     }
408
409     protected void updateInstanceId(DelegateExecution execution) {
410         try {
411             String requestId = (String) execution.getVariable(BBConstants.G_REQUEST_ID);
412             String resourceId = (String) execution.getVariable("resourceId");
413             WorkflowType resourceType = (WorkflowType) execution.getVariable("resourceType");
414             InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
415             if (resourceType == WorkflowType.SERVICE) {
416                 request.setServiceInstanceId(resourceId);
417             } else if (resourceType == WorkflowType.VNF) {
418                 request.setVnfId(resourceId);
419             } else if (resourceType == WorkflowType.VFMODULE) {
420                 request.setVfModuleId(resourceId);
421             } else if (resourceType == WorkflowType.VOLUMEGROUP) {
422                 request.setVolumeGroupId(resourceId);
423             } else if (resourceType == WorkflowType.NETWORK) {
424                 request.setNetworkId(resourceId);
425             } else if (resourceType == WorkflowType.CONFIGURATION) {
426                 request.setConfigurationId(resourceId);
427             } else if (resourceType == WorkflowType.INSTANCE_GROUP) {
428                 request.setInstanceGroupId(resourceId);
429             }
430             setInstanceName(resourceId, resourceType, request);
431             request.setLastModifiedBy("CamundaBPMN");
432             requestDbclient.updateInfraActiveRequests(request);
433         } catch (Exception ex) {
434             logger.error("Exception in updateInstanceId", ex);
435             workflowAction.buildAndThrowException(execution, "Failed to update Request db with instanceId");
436         }
437     }
438
439     public void postProcessingExecuteBB(DelegateExecution execution) {
440         try {
441             List<ExecuteBuildingBlock> flowsToExecute =
442                     (List<ExecuteBuildingBlock>) execution.getVariable("flowsToExecute");
443             String handlingCode = (String) execution.getVariable(HANDLINGCODE);
444             final boolean aLaCarte = (boolean) execution.getVariable(BBConstants.G_ALACARTE);
445             int currentSequence = (int) execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
446             logger.debug("Current Sequence: {}", currentSequence);
447             if (currentSequence >= flowsToExecute.size()) {
448                 execution.setVariable(COMPLETED, true);
449             }
450             ExecuteBuildingBlock ebb = flowsToExecute.get(currentSequence - 1);
451             String bbFlowName = ebb.getBuildingBlock().getBpmnFlowName();
452             if ("ActivateVfModuleBB".equalsIgnoreCase(bbFlowName) && aLaCarte
453                     && "Success".equalsIgnoreCase(handlingCode)) {
454                 postProcessingExecuteBBActivateVfModule(execution, ebb, flowsToExecute);
455             }
456
457             flowManipulatorListenerRunner.postModifyFlows(flowsToExecute, new DelegateExecutionImpl(execution));
458         } catch (Exception ex) {
459             logger.error("Exception in postProcessingExecuteBB", ex);
460             workflowAction.buildAndThrowException(execution, "Failed to post process Execute BB");
461         }
462     }
463
464     protected void postProcessingExecuteBBActivateVfModule(DelegateExecution execution, ExecuteBuildingBlock ebb,
465             List<ExecuteBuildingBlock> flowsToExecute) {
466         try {
467             String requestAction = (String) execution.getVariable(BBConstants.G_ACTION);
468             String serviceInstanceId = ebb.getWorkflowResourceIds().getServiceInstanceId();
469             String vnfId = ebb.getWorkflowResourceIds().getVnfId();
470             String vfModuleId = ebb.getResourceId();
471             ebb.getWorkflowResourceIds().setVfModuleId(vfModuleId);
472             String serviceModelUUID = "";
473             String vnfCustomizationUUID = "";
474             String vfModuleCustomizationUUID = "";
475             if (requestAction.equalsIgnoreCase("replaceInstance")
476                     || requestAction.equalsIgnoreCase("replaceInstanceRetainAssignments")) {
477                 for (RelatedInstanceList relatedInstList : ebb.getRequestDetails().getRelatedInstanceList()) {
478                     RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
479                     if (relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
480                         vnfCustomizationUUID = relatedInstance.getModelInfo().getModelCustomizationId();
481                     }
482                     if (relatedInstance.getModelInfo().getModelType().equals(ModelType.service)) {
483                         serviceModelUUID = relatedInstance.getModelInfo().getModelVersionId();
484                     }
485                 }
486                 vfModuleCustomizationUUID = ebb.getRequestDetails().getModelInfo().getModelCustomizationId();
487             } else {
488                 serviceModelUUID = bbInputSetupUtils.getAAIServiceInstanceById(serviceInstanceId).getModelVersionId();
489                 vnfCustomizationUUID = bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId();
490                 vfModuleCustomizationUUID =
491                         bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId).getModelCustomizationId();
492             }
493             List<Vnfc> vnfcs = workflowAction.getRelatedResourcesInVfModule(vnfId, vfModuleId, Vnfc.class, Types.VNFC);
494             logger.debug("Vnfc Size: {}", vnfcs.size());
495             for (Vnfc vnfc : vnfcs) {
496                 String modelCustomizationId = vnfc.getModelCustomizationId();
497                 logger.debug("Processing Vnfc: {}", modelCustomizationId);
498                 CvnfcConfigurationCustomization fabricConfig = catalogDbClient.getCvnfcCustomization(serviceModelUUID,
499                         vnfCustomizationUUID, vfModuleCustomizationUUID, modelCustomizationId);
500                 if (fabricConfig != null && fabricConfig.getConfigurationResource() != null
501                         && fabricConfig.getConfigurationResource().getToscaNodeType() != null
502                         && fabricConfig.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)) {
503                     String configurationId = getConfigurationId(vnfc);
504                     ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
505                     configurationResourceKeys.setCvnfcCustomizationUUID(modelCustomizationId);
506                     configurationResourceKeys.setVfModuleCustomizationUUID(vfModuleCustomizationUUID);
507                     configurationResourceKeys.setVnfResourceCustomizationUUID(vnfCustomizationUUID);
508                     configurationResourceKeys.setVnfcName(vnfc.getVnfcName());
509                     ExecuteBuildingBlock addConfigBB = getExecuteBBForConfig(ADD_FABRIC_CONFIGURATION_BB, ebb,
510                             configurationId, configurationResourceKeys);
511                     flowsToExecute.add(addConfigBB);
512                     flowsToExecute.stream()
513                             .forEach(executeBB -> logger.info("Flows to Execute After Post Processing: {}",
514                                     executeBB.getBuildingBlock().getBpmnFlowName()));
515                     execution.setVariable("flowsToExecute", flowsToExecute);
516                     execution.setVariable(COMPLETED, false);
517                 } else {
518                     logger.debug("No cvnfcCustomization found for customizationId: {}", modelCustomizationId);
519                 }
520             }
521         } catch (EntityNotFoundException e) {
522             logger.debug("Will not be running Fabric Config Building Blocks", e);
523         } catch (Exception e) {
524             String errorMessage = "Error occurred in post processing of Vf Module create";
525             execution.setVariable(HANDLINGCODE, ROLLBACKTOCREATED);
526             execution.setVariable("WorkflowActionErrorMessage", errorMessage);
527             logger.error(errorMessage, e);
528         }
529     }
530
531     protected String getConfigurationId(Vnfc vnfc) throws Exception {
532         Configuration configuration =
533                 workflowAction.getRelatedResourcesInVnfc(vnfc, Configuration.class, Types.CONFIGURATION);
534         if (configuration != null) {
535             return configuration.getConfigurationId();
536         } else {
537             return UUID.randomUUID().toString();
538         }
539     }
540
541     protected ExecuteBuildingBlock getExecuteBBForConfig(String bbName, ExecuteBuildingBlock ebb,
542             String configurationId, ConfigurationResourceKeys configurationResourceKeys) {
543         BuildingBlock buildingBlock =
544                 new BuildingBlock().setBpmnFlowName(bbName).setMsoId(UUID.randomUUID().toString());
545
546         WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds(ebb.getWorkflowResourceIds());
547         workflowResourceIds.setConfigurationId(configurationId);
548         return new ExecuteBuildingBlock().setaLaCarte(ebb.isaLaCarte()).setApiVersion(ebb.getApiVersion())
549                 .setRequestAction(ebb.getRequestAction()).setVnfType(ebb.getVnfType()).setRequestId(ebb.getRequestId())
550                 .setRequestDetails(ebb.getRequestDetails()).setBuildingBlock(buildingBlock)
551                 .setWorkflowResourceIds(workflowResourceIds).setConfigurationResourceKeys(configurationResourceKeys);
552     }
553
554     protected void setInstanceName(String resourceId, WorkflowType resourceType, InfraActiveRequests request) {
555         logger.debug("Setting instanceName in infraActiveRequest");
556         try {
557             if (resourceType == WorkflowType.SERVICE && request.getServiceInstanceName() == null) {
558                 ServiceInstance service = bbInputSetupUtils.getAAIServiceInstanceById(resourceId);
559                 if (service != null) {
560                     request.setServiceInstanceName(service.getServiceInstanceName());
561                 }
562             } else if (resourceType == WorkflowType.VNF && request.getVnfName() == null) {
563                 GenericVnf vnf = bbInputSetupUtils.getAAIGenericVnf(resourceId);
564                 if (vnf != null) {
565                     request.setVnfName(vnf.getVnfName());
566                 }
567             } else if (resourceType == WorkflowType.VFMODULE && request.getVfModuleName() == null) {
568                 VfModule vfModule = bbInputSetupUtils.getAAIVfModule(request.getVnfId(), resourceId);
569                 if (vfModule != null) {
570                     request.setVfModuleName(vfModule.getVfModuleName());
571                 }
572             } else if (resourceType == WorkflowType.VOLUMEGROUP && request.getVolumeGroupName() == null) {
573                 Optional<VolumeGroup> volumeGroup =
574                         bbInputSetupUtils.getRelatedVolumeGroupByIdFromVnf(request.getVnfId(), resourceId);
575                 volumeGroup.ifPresent(group -> request.setVolumeGroupName(group.getVolumeGroupName()));
576             } else if (resourceType == WorkflowType.NETWORK && request.getNetworkName() == null) {
577                 L3Network network = bbInputSetupUtils.getAAIL3Network(resourceId);
578                 if (network != null) {
579                     request.setNetworkName(network.getNetworkName());
580                 }
581             } else if (resourceType == WorkflowType.CONFIGURATION && request.getConfigurationName() == null) {
582                 org.onap.aai.domain.yang.Configuration configuration =
583                         bbInputSetupUtils.getAAIConfiguration(resourceId);
584                 if (configuration != null) {
585                     request.setConfigurationName(configuration.getConfigurationName());
586                 }
587             } else if (resourceType == WorkflowType.INSTANCE_GROUP && request.getInstanceGroupName() == null) {
588                 InstanceGroup instanceGroup = bbInputSetupUtils.getAAIInstanceGroup(resourceId);
589                 if (instanceGroup != null) {
590                     request.setInstanceGroupName(instanceGroup.getInstanceGroupName());
591                 }
592             }
593         } catch (Exception ex) {
594             logger.error("Exception in setInstanceName", ex);
595         }
596     }
597 }