fix catalogdb cvfnc customization
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / WorkflowAction.java
index d8a6fc2..867be2e 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
@@ -27,7 +29,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import java.util.Set;
 import java.util.UUID;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -40,6 +41,7 @@ import org.onap.aai.domain.yang.GenericVnf;
 import org.onap.aai.domain.yang.L3Network;
 import org.onap.aai.domain.yang.Relationship;
 import org.onap.aai.domain.yang.ServiceInstance;
+import org.onap.aai.domain.yang.Vnfc;
 import org.onap.aai.domain.yang.VolumeGroup;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
@@ -49,22 +51,28 @@ import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetup;
 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
-import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
+import org.onap.so.client.aai.AAICommonObjectMapperProvider;
+import org.onap.so.client.aai.AAIObjectType;
+import org.onap.so.client.aai.entities.AAIResultWrapper;
+import org.onap.so.client.aai.entities.Relationships;
+import org.onap.so.client.aai.entities.uri.AAIResourceUri;
+import org.onap.so.client.aai.entities.uri.AAIUriFactory;
 import org.onap.so.client.exception.ExceptionBuilder;
 import org.onap.so.client.orchestration.AAIConfigurationResources;
 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
 import org.onap.so.db.catalog.beans.CvnfcCustomization;
-import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
 import org.onap.so.db.catalog.beans.VfModuleCustomization;
-import org.onap.so.db.catalog.beans.VnfVfmoduleCvnfcConfigurationCustomization;
+import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
 import org.onap.so.db.catalog.beans.macro.NorthBoundRequest;
 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
 import org.onap.so.db.catalog.client.CatalogDbClient;
 import org.onap.so.serviceinstancebeans.ModelInfo;
 import org.onap.so.serviceinstancebeans.ModelType;
 import org.onap.so.serviceinstancebeans.Networks;
+import org.onap.so.serviceinstancebeans.RelatedInstance;
+import org.onap.so.serviceinstancebeans.RelatedInstanceList;
 import org.onap.so.serviceinstancebeans.RequestDetails;
 import org.onap.so.serviceinstancebeans.Service;
 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
@@ -72,6 +80,7 @@ import org.onap.so.serviceinstancebeans.VfModules;
 import org.onap.so.serviceinstancebeans.Vnfs;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Component;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -103,9 +112,11 @@ public class WorkflowAction {
        private static final String ASSIGNINSTANCE = "assignInstance";
        private static final String CREATEINSTANCE = "createInstance";
        private static final String USERPARAMSERVICE = "service";
-       private static final String supportedTypes = "vnfs|vfModules|networks|networkCollections|volumeGroups|serviceInstances";
+       private static final String supportedTypes = "vnfs|vfModules|networks|networkCollections|volumeGroups|serviceInstances|instanceGroups";
        private static final String HOMINGSOLUTION = "Homing_Solution";
-       private static final String FABRIC_CONFIGURATION = "FabricConfiguration";       
+       private static final String FABRIC_CONFIGURATION = "FabricConfiguration";
+       private static final String G_SERVICE_TYPE = "serviceType";
+       private static final String SERVICE_TYPE_TRANSPORT = "TRANSPORT";
        private static final Logger logger = LoggerFactory.getLogger(WorkflowAction.class);
        
        @Autowired
@@ -118,6 +129,12 @@ public class WorkflowAction {
        private CatalogDbClient catalogDbClient;
        @Autowired
        private AAIConfigurationResources aaiConfigurationResources;
+       @Autowired
+       private WorkflowActionExtractResourcesAAI workflowActionUtils;
+
+       @Autowired
+        private Environment environment;
+       private String defaultCloudOwner = "org.onap.so.cloud-owner";
 
        public void setBbInputSetupUtils(BBInputSetupUtils bbInputSetupUtils) {
                this.bbInputSetupUtils = bbInputSetupUtils;
@@ -135,6 +152,9 @@ public class WorkflowAction {
                final String apiVersion = (String) execution.getVariable(G_APIVERSION);
                final String uri = (String) execution.getVariable(G_URI);
                final String vnfType = (String) execution.getVariable(VNF_TYPE);
+               String serviceInstanceId = (String) execution.getVariable("serviceInstanceId");
+               final String serviceType = Optional.ofNullable((String) execution.getVariable(G_SERVICE_TYPE)).orElse("");
+
                List<OrchestrationFlow> orchFlows = (List<OrchestrationFlow>) execution.getVariable(G_ORCHESTRATION_FLOW);
                List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
                WorkflowResourceIds workflowResourceIds = populateResourceIdsFromApiHandler(execution);
@@ -149,6 +169,19 @@ public class WorkflowAction {
                        execution.setVariable(G_ISTOPLEVELFLOW, true);
                        ServiceInstancesRequest sIRequest = mapper.readValue(bpmnRequest, ServiceInstancesRequest.class);
                        RequestDetails requestDetails = sIRequest.getRequestDetails();
+                       String cloudOwner = "";
+                       try{
+                               cloudOwner = requestDetails.getCloudConfiguration().getCloudOwner();
+                       } catch (Exception ex) {
+                               cloudOwner = environment.getProperty(defaultCloudOwner);
+                       }
+                       boolean suppressRollback = false;
+                       try{
+                               suppressRollback = requestDetails.getRequestInfo().getSuppressRollback();
+                       } catch (Exception ex) {
+                               suppressRollback = false;
+                       }
+                       execution.setVariable("suppressRollback", suppressRollback);
                        Resource resource = extractResourceIdAndTypeFromUri(uri);
                        WorkflowType resourceType = resource.getResourceType();
                        execution.setVariable("resourceName", resourceType.toString());
@@ -160,35 +193,33 @@ public class WorkflowAction {
                        } else {
                                resourceId = resource.getResourceId();
                        }
+                       if((serviceInstanceId == null || serviceInstanceId.equals("")) && resourceType == WorkflowType.SERVICE){
+                               serviceInstanceId = resourceId;
+                       }
                        execution.setVariable("resourceId", resourceId);
                        execution.setVariable("resourceType", resourceType);
 
-                       if (sIRequest.getRequestDetails().getRequestParameters().getUserParams() != null) {
-                               List<Map<String, Object>> userParams = sIRequest.getRequestDetails().getRequestParameters()
-                                               .getUserParams();
-                               for (Map<String, Object> params : userParams) {
-                                       if (params.containsKey(HOMINGSOLUTION)) {
-                                               execution.setVariable("homing", true);
-                                               execution.setVariable("callHoming", true);
-                                               execution.setVariable("homingSolution", params.get(HOMINGSOLUTION));
-                                               execution.setVariable("homingService", params.get(HOMINGSOLUTION));
-                                       }
-                               }
-                       }
-
                        if (aLaCarte) {
                                if (orchFlows == null || orchFlows.isEmpty()) {
-                                       orchFlows = queryNorthBoundRequestCatalogDb(execution, requestAction, resourceType, aLaCarte);
+                                               orchFlows = queryNorthBoundRequestCatalogDb(execution, requestAction, resourceType, aLaCarte, cloudOwner, serviceType);
                                }
-                               orchFlows = filterOrchFlows(orchFlows, resourceType, execution);
                                String key = "";
                                ModelInfo modelInfo = sIRequest.getRequestDetails().getModelInfo();
-                               if(modelInfo.getModelType().equals(ModelType.service)) {
-                                       key = modelInfo.getModelVersionId();
-                               } else {
-                                       key = modelInfo.getModelCustomizationId();
+                               if(modelInfo != null) {
+                                       if(modelInfo.getModelType().equals(ModelType.service)) {
+                                               key = modelInfo.getModelVersionId();
+                                       } else {
+                                               key = modelInfo.getModelCustomizationId();
+                                       }
                                }
+                               boolean isConfiguration = isConfiguration(orchFlows);
                                Resource resourceKey = new Resource(resourceType, key, aLaCarte);
+                               if(isConfiguration && !requestAction.equalsIgnoreCase(CREATEINSTANCE)) {
+                                       List<ExecuteBuildingBlock> configBuildingBlocks = getConfigBuildingBlocks(sIRequest, orchFlows, requestId, resourceKey, apiVersion, resourceId, requestAction, aLaCarte, vnfType,
+                                                       workflowResourceIds, requestDetails, execution);
+                                       flowsToExecute.addAll(configBuildingBlocks);
+                               }
+                               orchFlows = orchFlows.stream().filter(item -> !item.getFlowName().contains(FABRIC_CONFIGURATION)).collect(Collectors.toList());
                                for (OrchestrationFlow orchFlow : orchFlows) {
                                        ExecuteBuildingBlock ebb = buildExecuteBuildingBlock(orchFlow, requestId, resourceKey, apiVersion, resourceId,
                                                        requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, false, null, false);
@@ -251,10 +282,11 @@ public class WorkflowAction {
                                } else if (resourceType == WorkflowType.SERVICE
                                                && requestAction.equalsIgnoreCase("deactivateInstance")) {
                                        resourceCounter.add(new Resource(WorkflowType.SERVICE,"",false));
+                               } else if (resourceType == WorkflowType.VNF && (requestAction.equalsIgnoreCase("replaceInstance") || (requestAction.equalsIgnoreCase("recreateInstance")))) {
+                                       traverseAAIVnf(execution, resourceCounter, workflowResourceIds.getServiceInstanceId(), workflowResourceIds.getVnfId(), aaiResourceIds);
                                } else {
                                        buildAndThrowException(execution, "Current Macro Request is not supported");
                                }
-
                                String foundObjects = "";
                                for(WorkflowType type : WorkflowType.values()){
                                        foundObjects = foundObjects + type + " - " + resourceCounter.stream().filter(x -> type.equals(x.getResourceType())).collect(Collectors.toList()).size() + "    ";
@@ -262,7 +294,7 @@ public class WorkflowAction {
                                logger.info("Found {}", foundObjects);
 
                                if (orchFlows == null || orchFlows.isEmpty()) {
-                                       orchFlows = queryNorthBoundRequestCatalogDb(execution, requestAction, resourceType, aLaCarte);
+                                       orchFlows = queryNorthBoundRequestCatalogDb(execution, requestAction, resourceType, aLaCarte, cloudOwner, serviceType);
                                }
                                flowsToExecute = buildExecuteBuildingBlockList(orchFlows, resourceCounter, requestId, apiVersion, resourceId,
                                                resourceType, requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails);
@@ -270,28 +302,45 @@ public class WorkflowAction {
                                        logger.info("Sorting for Vlan Tagging");
                                        flowsToExecute = sortExecutionPathByObjectForVlanTagging(flowsToExecute, requestAction);
                                }
+                               // By default, enable homing at VNF level for CREATEINSTANCE and ASSIGNINSTANCE
                                if (resourceType == WorkflowType.SERVICE
-                                               && (requestAction.equals(CREATEINSTANCE) || requestAction.equals(ASSIGNINSTANCE))
-                                               && !resourceCounter.stream().filter(x -> WorkflowType.VNF.equals(x.getResourceType())).collect(Collectors.toList()).isEmpty()) {
+                                       && (requestAction.equals(CREATEINSTANCE) || requestAction.equals(ASSIGNINSTANCE))
+                                       && !resourceCounter.stream().filter(x -> WorkflowType.VNF.equals(x.getResourceType())).collect(Collectors.toList()).isEmpty()) {
                                        execution.setVariable("homing", true);
                                        execution.setVariable("calledHoming", false);
                                }
                                if (resourceType == WorkflowType.SERVICE && (requestAction.equalsIgnoreCase(ASSIGNINSTANCE) || requestAction.equalsIgnoreCase(CREATEINSTANCE))){
-                                       generateResourceIds(flowsToExecute, resourceCounter);
+                                       generateResourceIds(flowsToExecute, resourceCounter, serviceInstanceId);
                                }else{
-                                       updateResourceIdsFromAAITraversal(flowsToExecute, resourceCounter, aaiResourceIds);
+                                       updateResourceIdsFromAAITraversal(flowsToExecute, resourceCounter, aaiResourceIds, serviceInstanceId);
+                               }
+                       }
+
+                       // If the user set "Homing_Solution" to "none", disable homing, else if "Homing_Solution" is specified, enable it.                      
+                       if (sIRequest.getRequestDetails().getRequestParameters() != null && 
+                                       sIRequest.getRequestDetails().getRequestParameters().getUserParams() != null) {
+                               List<Map<String, Object>> userParams = sIRequest.getRequestDetails().getRequestParameters().getUserParams();
+                               for (Map<String, Object> params : userParams) {
+                                       if (params.containsKey(HOMINGSOLUTION)) {
+                                               if (params.get(HOMINGSOLUTION).equals("none")) {
+                                                       execution.setVariable("homing", false);
+                                               } else {
+                                                       execution.setVariable("homing", true);
+                                               }
+                                       }
                                }
                        }
 
                        if (flowsToExecute.isEmpty()) {
                                throw new IllegalStateException("Macro did not come up with a valid execution path.");
                        }
-
+                       List<String> flowNames = new ArrayList<>();
                        logger.info("List of BuildingBlocks to execute:");
                        for (ExecuteBuildingBlock ebb : flowsToExecute) {
                                logger.info(ebb.getBuildingBlock().getBpmnFlowName());
+                               flowNames.add(ebb.getBuildingBlock().getBpmnFlowName());
                        }
-
+                       execution.setVariable("flowNames", flowNames);
                        execution.setVariable(G_CURRENT_SEQUENCE, 0);
                        execution.setVariable("retryCount", 0);
                        execution.setVariable("isRollback", false);
@@ -302,6 +351,88 @@ public class WorkflowAction {
                        buildAndThrowException(execution, "Exception in create execution list " + ex.getMessage(), ex);
                }
        }
+       
+       protected <T> List<T> getRelatedResourcesInVfModule(String vnfId, String vfModuleId, Class<T> resultClass, AAIObjectType type) {
+               List<T> vnfcs = new ArrayList<>();
+               AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
+               AAIResultWrapper vfModuleResultsWrapper = bbInputSetupUtils.getAAIResourceDepthOne(uri);
+               Optional<Relationships> relationshipsOp = vfModuleResultsWrapper.getRelationships();
+               if (!relationshipsOp.isPresent()) {
+                       logger.debug("No relationships were found for vfModule in AAI");
+               } else {
+                       Relationships relationships = relationshipsOp.get();
+                       List<AAIResultWrapper> vnfcResultWrappers = relationships.getByType(type);
+                       for(AAIResultWrapper vnfcResultWrapper : vnfcResultWrappers) {
+                               Optional<T> vnfcOp = vnfcResultWrapper.asBean(resultClass);
+                               if(vnfcOp.isPresent()) {
+                                       vnfcs.add(vnfcOp.get());
+                               }
+                       }
+               }
+               return vnfcs;
+       }
+       
+       protected boolean isConfiguration(List<OrchestrationFlow> orchFlows) {
+               for(OrchestrationFlow flow : orchFlows) {
+                       if(flow.getFlowName().contains("Configuration")) {
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       protected List<ExecuteBuildingBlock> getConfigBuildingBlocks(ServiceInstancesRequest sIRequest, List<OrchestrationFlow> orchFlows, String requestId, Resource resourceKey,
+                       String apiVersion, String resourceId, String requestAction, boolean aLaCarte, String vnfType,
+                       WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails, DelegateExecution execution) {
+               List<ExecuteBuildingBlock> flowsToExecuteConfigs = new ArrayList<>();
+               List<OrchestrationFlow> result = new ArrayList<>(orchFlows);
+               result = orchFlows.stream().filter(item -> item.getFlowName().contains(FABRIC_CONFIGURATION)).collect(Collectors.toList());
+               String vnfId = workflowResourceIds.getVnfId();
+               String vfModuleId = workflowResourceIds.getVfModuleId();
+               
+               String vnfCustomizationUUID = bbInputSetupUtils.getAAIGenericVnf(vnfId).getModelCustomizationId();
+               String vfModuleCustomizationUUID = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId).getModelCustomizationId();
+               List<org.onap.aai.domain.yang.Configuration> configurations = getRelatedResourcesInVfModule(vnfId, vfModuleId, org.onap.aai.domain.yang.Configuration.class, AAIObjectType.CONFIGURATION);
+               
+               for(org.onap.aai.domain.yang.Configuration configuration : configurations) {
+                       workflowResourceIds.setConfigurationId(configuration.getConfigurationId());
+                       for(OrchestrationFlow orchFlow : result) {
+                               resourceKey.setVfModuleCustomizationId(vfModuleCustomizationUUID);
+                               resourceKey.setCvnfModuleCustomizationId(configuration.getModelCustomizationId());
+                               resourceKey.setVnfCustomizationId(vnfCustomizationUUID);
+                               ExecuteBuildingBlock ebb = buildExecuteBuildingBlock(orchFlow, requestId, resourceKey, apiVersion, resourceId,
+                                               requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, false, null, true);
+                               String vnfcName = getVnfcNameForConfiguration(configuration);
+                               if(vnfcName == null || vnfcName.isEmpty()) {
+                                       buildAndThrowException(execution, "Exception in create execution list " + ": VnfcName does not exist or is null while there is a configuration for the vfModule", new Exception("Vnfc and Configuration do not match"));
+                               }
+                               ebb.getConfigurationResourceKeys().setVnfcName(vnfcName);
+                               flowsToExecuteConfigs.add(ebb);
+                       }
+               }
+               return flowsToExecuteConfigs;
+       }
+       
+       protected String getVnfcNameForConfiguration(org.onap.aai.domain.yang.Configuration configuration) {
+               AAIResultWrapper wrapper = new AAIResultWrapper(configuration);
+               Optional<Relationships> relationshipsOp = wrapper.getRelationships();
+               if (!relationshipsOp.isPresent()) {
+                       logger.debug("No relationships were found for Configuration in AAI");
+                       return null;
+               } else {
+                       Relationships relationships = relationshipsOp.get();
+                       List<AAIResultWrapper> vnfcResultWrappers = relationships.getByType(AAIObjectType.VNFC);
+                       if(vnfcResultWrappers.size() > 1 || vnfcResultWrappers.isEmpty()) {
+                               logger.debug("Too many vnfcs or no vnfc found that are related to configuration");
+                       }
+                       Optional<Vnfc> vnfcOp = vnfcResultWrappers.get(0).asBean(Vnfc.class);
+                       if(vnfcOp.isPresent()) {
+                               return vnfcOp.get().getVnfcName();
+                       } else {
+                               return null;
+                       }
+               }
+       }
 
        protected List<Resource> sortVfModulesByBaseFirst(List<Resource> vfModuleResources) {
                int count = 0;
@@ -314,9 +445,21 @@ public class WorkflowAction {
                }
                return vfModuleResources;
        }
+       
+       protected List<Resource> sortVfModulesByBaseLast(List<Resource> vfModuleResources) {
+               int count = 0;
+               for(Resource resource : vfModuleResources){
+                       if(resource.isBaseVfModule()){
+                               Collections.swap(vfModuleResources, vfModuleResources.size()-1, count);
+                               break;
+               }
+                       count++;
+               }
+               return vfModuleResources;
+       }
 
        private void updateResourceIdsFromAAITraversal(List<ExecuteBuildingBlock> flowsToExecute,
-                       List<Resource> resourceCounter, List<Pair<WorkflowType, String>> aaiResourceIds) {
+                       List<Resource> resourceCounter, List<Pair<WorkflowType, String>> aaiResourceIds, String serviceInstanceId) {
                for(Pair<WorkflowType,String> pair : aaiResourceIds){
                        logger.debug(pair.getValue0() + ", " + pair.getValue1());
                }
@@ -324,7 +467,7 @@ public class WorkflowAction {
                Arrays.stream(WorkflowType.values()).filter(type -> !type.equals(WorkflowType.SERVICE)).forEach(type -> {
                        List<Resource> resources = resourceCounter.stream().filter(x -> type.equals(x.getResourceType())).collect(Collectors.toList());
                        for(int i = 0; i < resources.size(); i++){
-                               updateWorkflowResourceIds(flowsToExecute, type, resources.get(i).getResourceId(), retrieveAAIResourceId(aaiResourceIds,type), null);
+                               updateWorkflowResourceIds(flowsToExecute, type, resources.get(i).getResourceId(), retrieveAAIResourceId(aaiResourceIds,type), null, serviceInstanceId);
                }
                });
        }
@@ -340,17 +483,18 @@ public class WorkflowAction {
                }
                return id;
        }
-       private void generateResourceIds(List<ExecuteBuildingBlock> flowsToExecute, List<Resource> resourceCounter) {
+       private void generateResourceIds(List<ExecuteBuildingBlock> flowsToExecute, List<Resource> resourceCounter, String serviceInstanceId) {
                Arrays.stream(WorkflowType.values()).filter(type -> !type.equals(WorkflowType.SERVICE)).forEach(type -> {
                        List<Resource> resources = resourceCounter.stream().filter(x -> type.equals(x.getResourceType())).collect(Collectors.toList());
                        for(int i = 0; i < resources.size(); i++){
                                Resource resource = resourceCounter.stream().filter(x -> type.equals(x.getResourceType()))
                                                .collect(Collectors.toList()).get(i);
-                               updateWorkflowResourceIds(flowsToExecute, type, resource.getResourceId(), null, resource.getVirtualLinkKey());                  }
+                               updateWorkflowResourceIds(flowsToExecute, type, resource.getResourceId(), null, resource.getVirtualLinkKey(),serviceInstanceId);
+                       }
                });
        }       
        
-       protected void updateWorkflowResourceIds(List<ExecuteBuildingBlock> flowsToExecute, WorkflowType resource, String key, String id, String virtualLinkKey){
+       protected void updateWorkflowResourceIds(List<ExecuteBuildingBlock> flowsToExecute, WorkflowType resource, String key, String id, String virtualLinkKey, String serviceInstanceId){
                String resourceId = id;
                if(resourceId==null){
                        resourceId = UUID.randomUUID().toString();
@@ -358,6 +502,7 @@ public class WorkflowAction {
                for(ExecuteBuildingBlock ebb : flowsToExecute){
                        if(key != null && key.equalsIgnoreCase(ebb.getBuildingBlock().getKey()) && ebb.getBuildingBlock().getBpmnFlowName().contains(resource.toString())){
                                WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
+                               workflowResourceIds.setServiceInstanceId(serviceInstanceId);
                                if(resource == WorkflowType.VNF){
                                        workflowResourceIds.setVnfId(resourceId);
                                }else if(resource == WorkflowType.VFMODULE){
@@ -376,6 +521,7 @@ public class WorkflowAction {
                        if(virtualLinkKey != null && ebb.getBuildingBlock().getIsVirtualLink() 
                                        && virtualLinkKey.equalsIgnoreCase(ebb.getBuildingBlock().getVirtualLinkKey())) {
                                WorkflowResourceIds workflowResourceIds = new WorkflowResourceIds();
+                               workflowResourceIds.setServiceInstanceId(serviceInstanceId);
                                workflowResourceIds.setNetworkId(resourceId);
                                ebb.setWorkflowResourceIds(workflowResourceIds);
                        }
@@ -386,8 +532,7 @@ public class WorkflowAction {
                CollectionResourceCustomization networkCollection = null;
                int count = 0;
                for(CollectionResourceCustomization collectionCust : service.getCollectionResourceCustomizations()){
-                       if(catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCust.getModelCustomizationUUID()) 
-                                       instanceof NetworkCollectionResourceCustomization) {
+                       if(catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCust.getModelCustomizationUUID()) != null) {
                                networkCollection = collectionCust;
                                count++;
                        }
@@ -537,6 +682,67 @@ public class WorkflowAction {
                }
        }
 
+       private void traverseAAIVnf(DelegateExecution execution, List<Resource> resourceCounter, String serviceId, String vnfId,
+                       List<Pair<WorkflowType, String>> aaiResourceIds) {
+               try{
+                       ServiceInstance serviceInstanceAAI = bbInputSetupUtils.getAAIServiceInstanceById(serviceId);
+                       org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance serviceInstanceMSO = bbInputSetup
+                                       .getExistingServiceInstance(serviceInstanceAAI);
+                       resourceCounter.add(new Resource(WorkflowType.SERVICE,serviceInstanceMSO.getServiceInstanceId(),false));
+                       if (serviceInstanceMSO.getVnfs() != null) {
+                               for (org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf vnf : serviceInstanceMSO
+                                               .getVnfs()) {
+                                       if(vnf.getVnfId().equals(vnfId)){
+                                               aaiResourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VNF, vnf.getVnfId()));
+                                               resourceCounter.add(new Resource(WorkflowType.VNF,vnf.getVnfId(),false));
+                                               if (vnf.getVfModules() != null) {
+                                                       for (VfModule vfModule : vnf.getVfModules()) {
+                                                               aaiResourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VFMODULE, vfModule.getVfModuleId()));
+                                                               resourceCounter.add(new Resource(WorkflowType.VFMODULE,vfModule.getVfModuleId(),false));        
+                                                               findConfigurationsInsideVfModule(execution, vnf.getVnfId(), vfModule.getVfModuleId(), resourceCounter, aaiResourceIds);
+                                                       }
+                                               }
+                                               if (vnf.getVolumeGroups() != null) {
+                                                       for (org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup volumeGroup : vnf
+                                                                       .getVolumeGroups()) {
+                                                               aaiResourceIds.add(new Pair<WorkflowType, String>(WorkflowType.VOLUMEGROUP, volumeGroup.getVolumeGroupId()));
+                                                               resourceCounter.add(new Resource(WorkflowType.VOLUMEGROUP,volumeGroup.getVolumeGroupId(),false));
+                                                       }
+                                               }
+                                               break;
+                                       }
+                               }
+                       }
+               } catch (Exception ex) {
+                       buildAndThrowException(execution,
+                                       "Could not find existing Vnf or related Instances to execute the request on.");
+               }
+       }
+
+       private void findConfigurationsInsideVfModule(DelegateExecution execution, String vnfId, String vfModuleId, List<Resource> resourceCounter, 
+                       List<Pair<WorkflowType, String>> aaiResourceIds) {
+               try{
+                       org.onap.aai.domain.yang.VfModule aaiVfModule = bbInputSetupUtils.getAAIVfModule(vnfId, vfModuleId);
+                       AAIResultWrapper vfModuleWrapper = new AAIResultWrapper(
+                                       new AAICommonObjectMapperProvider().getMapper().writeValueAsString(aaiVfModule));
+                       Optional<Relationships> relationshipsOp;
+                       relationshipsOp = vfModuleWrapper.getRelationships();
+                       if(relationshipsOp.isPresent()) {
+                               relationshipsOp = workflowActionUtils.extractRelationshipsVnfc(relationshipsOp.get());
+                               if(relationshipsOp.isPresent()){
+                                       Optional<Configuration> config = workflowActionUtils.extractRelationshipsConfiguration(relationshipsOp.get());
+                                       if(config.isPresent()){
+                                               aaiResourceIds.add(new Pair<WorkflowType, String>(WorkflowType.CONFIGURATION, config.get().getConfigurationId()));
+                                               resourceCounter.add(new Resource(WorkflowType.CONFIGURATION, config.get().getConfigurationId(), false));
+                                       }
+                               }
+                       }
+               }catch (Exception ex){
+                       buildAndThrowException(execution,
+                                       "Failed to find Configuration object from the vfModule.");
+               }
+       }
+       
        protected boolean traverseUserParamsService(DelegateExecution execution, List<Resource> resourceCounter,
                        ServiceInstancesRequest sIRequest, String requestAction)
                        throws IOException {
@@ -586,9 +792,9 @@ public class WorkflowAction {
                                                                                                vfModuleCustomizationUUID = vfModule.getModelInfo().getModelCustomizationUuid();
                                                                                        }
                                                                                        if(!vnfCustomizationUUID.equals("")&&!vfModuleCustomizationUUID.equals("")){
-                                                                                               List<String> configs = traverseCatalogDbForConfiguration(vnfCustomizationUUID,vfModuleCustomizationUUID);
-                                                                                               for(String config : configs){
-                                                                                                       Resource configResource = new Resource(WorkflowType.CONFIGURATION,config,false);
+                                                                                               List<CvnfcConfigurationCustomization> configs = traverseCatalogDbForConfiguration(validate.getModelInfo().getModelVersionId(),vnfCustomizationUUID,vfModuleCustomizationUUID);
+                                                                                               for(CvnfcConfigurationCustomization config : configs){
+                                                                                                       Resource configResource = new Resource(WorkflowType.CONFIGURATION,config.getConfigurationResource().getModelUUID(),false);
                                                                                                        resource.setVnfCustomizationId(vnf.getModelInfo().getModelCustomizationId());
                                                                                                        resource.setVfModuleCustomizationId(vfModule.getModelInfo().getModelCustomizationId());
                                                                                                        resourceCounter.add(configResource);
@@ -622,20 +828,19 @@ public class WorkflowAction {
                }
                return foundRelated;
        }
-       
 
-       protected List<String> traverseCatalogDbForConfiguration(String vnfCustomizationUUID, String vfModuleCustomizationUUID) {
-               List<String> configurations = new ArrayList<>();
+       protected List<CvnfcConfigurationCustomization> traverseCatalogDbForConfiguration(String serviceModelUUID, String vnfCustomizationUUID, String vfModuleCustomizationUUID) {
+               List<CvnfcConfigurationCustomization> configurations = new ArrayList<>();
                try{
-                       List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomizationByVnfCustomizationUUIDAndVfModuleCustomizationUUID(vnfCustomizationUUID, vfModuleCustomizationUUID);
+                       List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomization(serviceModelUUID,vnfCustomizationUUID, vfModuleCustomizationUUID);
                        for(CvnfcCustomization cvnfc : cvnfcCustomizations){
-                               for(VnfVfmoduleCvnfcConfigurationCustomization customization : cvnfc.getVnfVfmoduleCvnfcConfigurationCustomization()){
+                               for(CvnfcConfigurationCustomization customization : cvnfc.getCvnfcConfigurationCustomization()){
                                        if(customization.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)){
-                                               configurations.add(customization.getConfigurationResource().getModelUUID());
+                                               configurations.add(customization);
                                        }
                                }
                        }
-                       logger.debug("found {} configurations" , configurations.size() );
+                       logger.debug("found {} fabric configuration(s)" , configurations.size() );
                        return configurations;
                } catch (Exception ex){
                        logger.error("Error in finding configurations", ex);
@@ -662,6 +867,7 @@ public class WorkflowAction {
                workflowResourceIds.setVfModuleId((String) execution.getVariable("vfModuleId"));
                workflowResourceIds.setVnfId((String) execution.getVariable("vnfId"));
                workflowResourceIds.setVolumeGroupId((String) execution.getVariable("volumeGroupId"));
+               workflowResourceIds.setInstanceGroupId((String) execution.getVariable("instanceGroupId"));
                return workflowResourceIds;
        }
 
@@ -875,8 +1081,14 @@ public class WorkflowAction {
                                                        requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, true, resource.getVirtualLinkKey(), false));
                                }
                        } else if (orchFlow.getFlowName().contains(VFMODULE)) {
-                               List<Resource> vfModuleResourcesSorted = sortVfModulesByBaseFirst(resourceCounter.stream().filter(x -> WorkflowType.VFMODULE == x.getResourceType())
+                               List<Resource> vfModuleResourcesSorted = null;
+                               if(requestAction.equals("createInstance")||requestAction.equals("assignInstance")||requestAction.equals("activateInstance")){
+                                       vfModuleResourcesSorted = sortVfModulesByBaseFirst(resourceCounter.stream().filter(x -> WorkflowType.VFMODULE == x.getResourceType())
                                                .collect(Collectors.toList()));
+                               }else{
+                                       vfModuleResourcesSorted = sortVfModulesByBaseLast(resourceCounter.stream().filter(x -> WorkflowType.VFMODULE == x.getResourceType())
+                                                       .collect(Collectors.toList()));
+                               }
                                for (int i = 0; i < vfModuleResourcesSorted.size(); i++) {
                                        flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, vfModuleResourcesSorted.get(i), apiVersion, resourceId,
                                                        requestAction, aLaCarte, vnfType, workflowResourceIds, requestDetails, false, null, false));
@@ -930,21 +1142,35 @@ public class WorkflowAction {
                executeBuildingBlock.setRequestId(requestId);
                executeBuildingBlock.setBuildingBlock(buildingBlock);
                executeBuildingBlock.setRequestDetails(requestDetails);
-               if(isConfiguration){
+               if(resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))){
                        ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
-                       configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
-                       configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
-                       configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
+                       if (resource != null){
+                               configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
+                               configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
+                               configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
+                       }
                        executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
                }
                return executeBuildingBlock;
        }
 
        protected List<OrchestrationFlow> queryNorthBoundRequestCatalogDb(DelegateExecution execution, String requestAction,
-                       WorkflowType resourceName, boolean aLaCarte) {
+                       WorkflowType resourceName, boolean aLaCarte, String cloudOwner) {
+               return this.queryNorthBoundRequestCatalogDb(execution, requestAction, resourceName, aLaCarte, cloudOwner, "");
+       }
+       
+       protected List<OrchestrationFlow> queryNorthBoundRequestCatalogDb(DelegateExecution execution, String requestAction,
+                       WorkflowType resourceName, boolean aLaCarte, String cloudOwner, String serviceType) {
                List<OrchestrationFlow> listToExecute = new ArrayList<>();
-               NorthBoundRequest northBoundRequest = catalogDbClient
-                               .getNorthBoundRequestByActionAndIsALaCarteAndRequestScope(requestAction, resourceName.toString(), aLaCarte);
+               NorthBoundRequest northBoundRequest = null;
+               if (serviceType.equalsIgnoreCase(SERVICE_TYPE_TRANSPORT)) {
+                       northBoundRequest = catalogDbClient
+                                       .getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwnerAndServiceType(requestAction,
+                                                       resourceName.toString(), aLaCarte, cloudOwner, serviceType);
+               } else {
+                       northBoundRequest = catalogDbClient.getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwner(
+                                       requestAction, resourceName.toString(), aLaCarte, cloudOwner);
+               }
                if(northBoundRequest == null){
                        if(aLaCarte){
                                buildAndThrowException(execution,"The request: ALaCarte " + resourceName + " " + requestAction + " is not supported by GR_API.");
@@ -972,17 +1198,6 @@ public class WorkflowAction {
                }
                return listToExecute;
        }
-       
-       protected List<OrchestrationFlow> filterOrchFlows(List<OrchestrationFlow> orchFlows, WorkflowType resourceType, DelegateExecution execution) {
-               List<OrchestrationFlow> result = new ArrayList<>(orchFlows);
-               if (resourceType.equals(WorkflowType.VFMODULE)) {
-                       List<String> fabricCustomizations = traverseCatalogDbForConfiguration((String)execution.getVariable("vnfId"), (String)execution.getVariable("vfModuleId"));
-                       if (fabricCustomizations.isEmpty()) {
-                               result = orchFlows.stream().filter(item -> !item.getFlowName().contains(FABRIC_CONFIGURATION)).collect(Collectors.toList());
-                       }
-               }
-               return result;
-       }
 
        protected void buildAndThrowException(DelegateExecution execution, String msg, Exception ex) {
                logger.error(msg, ex);