Support instantiation of same model vnfs/vf-modules
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / servicedecomposition / tasks / BBInputSetup.java
index e65e1a8..569f6b1 100644 (file)
@@ -31,16 +31,17 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.UUID;
+import org.apache.commons.lang3.StringUtils;
 import org.camunda.bpm.engine.delegate.DelegateExecution;
 import org.camunda.bpm.engine.delegate.JavaDelegate;
 import org.javatuples.Pair;
 import org.onap.aaiclient.client.aai.AAICommonObjectMapperProvider;
-import org.onap.aaiclient.client.aai.AAIObjectType;
 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
 import org.onap.aaiclient.client.aai.entities.Relationships;
 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
+import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
 import org.onap.so.bpmn.common.BuildingBlockExecution;
 import org.onap.so.bpmn.common.DelegateExecutionImpl;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
@@ -54,6 +55,7 @@ import org.onap.so.bpmn.servicedecomposition.bbobjects.LineOfBusiness;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.NetworkPolicy;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.OwningEntity;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Platform;
+import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Project;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.RouteTableReference;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
@@ -62,7 +64,6 @@ import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceSubscription;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Tenant;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.Vnfc;
-import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.VpnBinding;
 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
@@ -74,6 +75,7 @@ import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
 import org.onap.so.bpmn.servicedecomposition.generalobjects.OrchestrationContext;
 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.NoServiceInstanceFoundException;
+import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.ResourceNotFoundException;
 import org.onap.so.bpmn.servicedecomposition.tasks.exceptions.ServiceModelNotFoundException;
 import org.onap.so.client.exception.ExceptionBuilder;
 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
@@ -193,6 +195,12 @@ public class BBInputSetup implements JavaDelegate {
             execution.setVariable(GBB_INPUT_VAR_NAME, outputBB);
             execution.setVariable(LOOKUP_KEY_MAP_VAR_NAME, lookupKeyMap);
 
+            if (outputBB.getRequestContext().getIsHelm()) {
+                execution.setVariable("isHelm", true);
+            } else {
+                execution.setVariable("isHelm", false);
+            }
+
             BuildingBlockExecution gBuildingBlockExecution = new DelegateExecutionImpl(execution);
             execution.setVariable("gBuildingBlockExecution", gBuildingBlockExecution);
             execution.setVariable("RetryCount", 1);
@@ -252,6 +260,8 @@ public class BBInputSetup implements JavaDelegate {
         lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, workflowResourceIds.getVolumeGroupId());
         lookupKeyMap.put(ResourceKey.CONFIGURATION_ID, workflowResourceIds.getConfigurationId());
         lookupKeyMap.put(ResourceKey.INSTANCE_GROUP_ID, workflowResourceIds.getInstanceGroupId());
+        lookupKeyMap.put(ResourceKey.VNF_INSTANCE_NAME, workflowResourceIds.getVnfInstanceName());
+        lookupKeyMap.put(ResourceKey.VF_MODULE_INSTANCE_NAME, workflowResourceIds.getVfModuleInstanceName());
     }
 
     protected GeneralBuildingBlock getGBBALaCarteNonService(ExecuteBuildingBlock executeBB,
@@ -404,15 +414,7 @@ public class BBInputSetup implements JavaDelegate {
             parameter.getLookupKeyMap().put(ResourceKey.VOLUME_GROUP_ID, parameter.getResourceId());
             this.populateVolumeGroup(parameter);
         } else if (modelType.equals(ModelType.vfModule)) {
-            if (parameter.getBbName().contains("Configuration")) {
-                parameter.setResourceId(parameter.getLookupKeyMap().get(ResourceKey.CONFIGURATION_ID));
-                parameter.getModelInfo().setModelCustomizationUuid(parameter.getConfigurationKey());
-                populateConfiguration(parameter);
-            } else {
-                parameter.getLookupKeyMap().put(ResourceKey.VF_MODULE_ID, parameter.getResourceId());
-                parameter.setCloudConfiguration(parameter.getRequestDetails().getCloudConfiguration());
-                this.populateVfModule(parameter);
-            }
+            populateVfModuleOnAssignAndCreateFlows(parameter);
         } else if (modelType.equals(ModelType.instanceGroup)) {
             parameter.getLookupKeyMap().put(ResourceKey.INSTANCE_GROUP_ID, parameter.getResourceId());
             this.populateInstanceGroup(parameter);
@@ -429,13 +431,37 @@ public class BBInputSetup implements JavaDelegate {
         parameter.getServiceInstance().getInstanceGroups().add(instanceGroup);
     }
 
+    protected void populateVfModuleOnAssignAndCreateFlows(BBInputSetupParameter parameter) throws Exception {
+        if (parameter.getBbName().contains("Configuration")) {
+            parameter.setResourceId(parameter.getLookupKeyMap().get(ResourceKey.CONFIGURATION_ID));
+            parameter.getModelInfo().setModelCustomizationUuid(parameter.getConfigurationKey());
+            populateConfiguration(parameter);
+        } else {
+            parameter.getLookupKeyMap().put(ResourceKey.VF_MODULE_ID, parameter.getResourceId());
+            parameter.setCloudConfiguration(parameter.getRequestDetails().getCloudConfiguration());
+            this.populateVfModule(parameter);
+        }
+    }
+
     protected void mapCatalogInstanceGroup(InstanceGroup instanceGroup, ModelInfo modelInfo, Service service) {
         // @TODO: this will populate the instanceGroup model info.
-        // Dependent on MSO-5821 653458 US - MSO - Enhance Catalog DB Schema & Adapter to support VNF Groups
+        // Dependent on MSO-5821 653458 US - MSO - Enhance Catalog DB Schema & Adapter
+        // to support VNF Groups
     }
 
     protected void populateConfiguration(BBInputSetupParameter parameter) {
         Configuration configuration = null;
+        String replaceVnfModelCustomizationUUID = "";
+        if (parameter.getRelatedInstanceList() != null) {
+            for (RelatedInstanceList relatedInstList : parameter.getRelatedInstanceList()) {
+                RelatedInstance relatedInstance = relatedInstList.getRelatedInstance();
+                if (relatedInstance.getModelInfo().getModelType().equals(ModelType.vnf)) {
+                    if (parameter.getIsReplace()) {
+                        replaceVnfModelCustomizationUUID = relatedInstance.getModelInfo().getModelCustomizationId();
+                    }
+                }
+            }
+        }
         for (Configuration configurationTemp : parameter.getServiceInstance().getConfigurations()) {
             if (parameter.getLookupKeyMap().get(ResourceKey.CONFIGURATION_ID) != null
                     && configurationTemp.getConfigurationId()
@@ -458,8 +484,17 @@ public class BBInputSetup implements JavaDelegate {
         if (configuration != null && parameter.getBbName().contains("Fabric")) {
             Vnfc vnfc = getVnfcToConfiguration(parameter.getConfigurationResourceKeys().getVnfcName());
             configuration.setVnfc(vnfc);
-            this.mapCatalogConfiguration(configuration, parameter.getModelInfo(), parameter.getService(),
-                    parameter.getConfigurationResourceKeys());
+            if (!parameter.getBbName().contains("Delete")) {
+                if (parameter.getIsReplace()) {
+                    parameter.getConfigurationResourceKeys()
+                            .setVnfResourceCustomizationUUID(replaceVnfModelCustomizationUUID);
+                    mapCatalogConfiguration(configuration, parameter.getModelInfo(),
+                            parameter.getServiceModel().getNewService(), parameter.getConfigurationResourceKeys());
+                } else {
+                    mapCatalogConfiguration(configuration, parameter.getModelInfo(),
+                            parameter.getServiceModel().getCurrentService(), parameter.getConfigurationResourceKeys());
+                }
+            }
         } else if (configuration != null && parameter.getBbName().contains("Vrf")) {
             configuration.setModelInfoConfiguration(mapperLayer.mapCatalogConfigurationToConfiguration(
                     findConfigurationResourceCustomization(parameter.getModelInfo(), parameter.getService()), null));
@@ -469,7 +504,7 @@ public class BBInputSetup implements JavaDelegate {
     }
 
     protected Vnfc getVnfcToConfiguration(String vnfcName) {
-        AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VNFC, vnfcName);
+        AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().vnfc(vnfcName));
         Optional<org.onap.aai.domain.yang.Vnfc> vnfcOp =
                 bbInputSetupUtils.getAAIResourceDepthOne(uri).asBean(org.onap.aai.domain.yang.Vnfc.class);
         if (vnfcOp.isPresent()) {
@@ -613,6 +648,11 @@ public class BBInputSetup implements JavaDelegate {
                             parameter.getServiceModel().getCurrentService(), vnfModelCustomizationUUID);
                 }
             }
+            if (vfModule != null && vfModule.getModelInfoVfModule() != null
+                    && vfModule.getModelInfoVfModule().getModelName() != null
+                    && vfModule.getModelInfoVfModule().getModelName().contains("helm")) {
+                parameter.setIsHelm(true);
+            }
         } else {
             logger.debug("Related VNF instance Id not found: {}",
                     parameter.getLookupKeyMap().get(ResourceKey.GENERIC_VNF_ID));
@@ -1018,9 +1058,18 @@ public class BBInputSetup implements JavaDelegate {
             if (requestDetails.getOwningEntity() != null)
                 owningEntity = mapperLayer.mapRequestOwningEntity(requestDetails.getOwningEntity());
 
+            String modelVersionId = requestDetails.getModelInfo().getModelVersionId();
 
-            Service service =
-                    bbInputSetupUtils.getCatalogServiceByModelUUID(requestDetails.getModelInfo().getModelVersionId());
+            if (ModelType.vnf == requestDetails.getModelInfo().getModelType()) {
+                for (RelatedInstanceList relatedInstanceList : requestDetails.getRelatedInstanceList()) {
+                    if (ModelType.service == relatedInstanceList.getRelatedInstance().getModelInfo().getModelType()) {
+                        modelVersionId = relatedInstanceList.getRelatedInstance().getModelInfo().getModelVersionId();
+                        break;
+                    }
+                }
+            }
+
+            Service service = bbInputSetupUtils.getCatalogServiceByModelUUID(modelVersionId);
             if (service == null) {
                 service = bbInputSetupUtils.getCatalogServiceByModelVersionAndModelInvariantUUID(
                         requestDetails.getModelInfo().getModelVersion(),
@@ -1094,6 +1143,7 @@ public class BBInputSetup implements JavaDelegate {
         RequestContext requestContext = mapperLayer.mapRequestContext(parameter.getRequestDetails());
         requestContext.setAction(parameter.getRequestAction());
         requestContext.setMsoRequestId(parameter.getExecuteBB().getRequestId());
+        requestContext.setIsHelm(parameter.getIsHelm());
         org.onap.aai.domain.yang.CloudRegion aaiCloudRegion =
                 bbInputSetupUtils.getCloudRegion(parameter.getRequestDetails().getCloudConfiguration());
         CloudRegion cloudRegion =
@@ -1447,6 +1497,11 @@ public class BBInputSetup implements JavaDelegate {
                                 lookupKeyMap.put(ResourceKey.VOLUME_GROUP_ID, volumeGroupIdOp.get());
                             }
                         }
+                        if (vfModule.getModelInfoVfModule() != null
+                                && vfModule.getModelInfoVfModule().getModelName() != null
+                                && vfModule.getModelInfoVfModule().getModelName().contains("helm")) {
+                            gBB.getRequestContext().setIsHelm(true);
+                        }
                         break;
                     }
                 }
@@ -1530,7 +1585,13 @@ public class BBInputSetup implements JavaDelegate {
                         .setBbName(bbName).setServiceInstance(serviceInstance).setLookupKeyMap(lookupKeyMap).build();
         if (bbName.contains(VNF) || (bbName.contains(CONTROLLER)
                 && (VNF).equalsIgnoreCase(executeBB.getBuildingBlock().getBpmnScope()))) {
-            vnfs = findVnfsByKey(key, resources, vnfs);
+            String vnfInstanceName = lookupKeyMap.get(ResourceKey.VNF_INSTANCE_NAME);
+            if (StringUtils.isNotBlank(vnfInstanceName)) {
+                vnfs = findVnfsByInstanceName(vnfInstanceName, resources);
+            } else {
+                vnfs = findVnfsByKey(key, resources);
+            }
+
             String vnfId = lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID);
             // This stores the vnf id in request db to be retrieved later when
             // working on a vf module or volume group
@@ -1552,22 +1613,23 @@ public class BBInputSetup implements JavaDelegate {
             }
             parameter.setApplicationId(applicationId);
             this.populateGenericVnf(parameter);
-        } else if (bbName.contains(PNF)) {
+        } else if (bbName.contains(PNF) || (bbName.contains(CONTROLLER)
+                && (PNF).equalsIgnoreCase(executeBB.getBuildingBlock().getBpmnScope()))) {
             String pnfId = lookupKeyMap.get(ResourceKey.PNF);
             resources.getPnfs().stream()
                     .filter(pnfs -> Objects.equals(key, pnfs.getModelInfo().getModelCustomizationId())).findFirst()
                     .ifPresent(pnfs -> BBInputSetupPnf.populatePnfToServiceInstance(pnfs, pnfId, serviceInstance));
         } else if (bbName.contains(VF_MODULE) || bbName.contains(VOLUME_GROUP) || (bbName.contains(CONTROLLER)
                 && (VF_MODULE).equalsIgnoreCase(executeBB.getBuildingBlock().getBpmnScope()))) {
-            Pair<Vnfs, VfModules> vnfsAndVfModules = getVfModulesAndItsVnfsByKey(key, resources);
-            if (vnfsAndVfModules != null) {
-                vfModules = vnfsAndVfModules.getValue1();
-                vnfs = vnfsAndVfModules.getValue0();
+            String vfModuleInstanceName = lookupKeyMap.get(ResourceKey.VF_MODULE_INSTANCE_NAME);
+            if (StringUtils.isNotBlank(vfModuleInstanceName)) {
+                vfModules = getVfModulesByInstanceName(vfModuleInstanceName, resources);
+            } else {
+                vfModules = getVfModulesByKey(key, resources);
             }
+
             lookupKeyMap.put(ResourceKey.GENERIC_VNF_ID, getVnfId(executeBB, lookupKeyMap));
-            if (vnfs == null) {
-                throw new Exception("Could not find Vnf to orchestrate VfModule");
-            }
+
             parameter.setModelInfo(vfModules.getModelInfo());
             if (bbName.contains(VOLUME_GROUP)) {
                 parameter.setResourceId(lookupKeyMap.get(ResourceKey.VOLUME_GROUP_ID));
@@ -1590,6 +1652,7 @@ public class BBInputSetup implements JavaDelegate {
                 parameter.setInstanceName(vfModules.getInstanceName());
                 parameter.setInstanceParams(vfModules.getInstanceParams());
                 this.populateVfModule(parameter);
+                gBB.getRequestContext().setIsHelm(parameter.getIsHelm());
             }
         } else if (bbName.contains(NETWORK)) {
             networks = findNetworksByKey(key, resources);
@@ -1629,25 +1692,45 @@ public class BBInputSetup implements JavaDelegate {
         return null;
     }
 
-    protected Pair<Vnfs, VfModules> getVfModulesAndItsVnfsByKey(String key, Resources resources) {
+    protected VfModules getVfModulesByInstanceName(String vfModuleInstanceName, Resources resources) {
+        for (Vnfs vnfs : resources.getVnfs()) {
+            for (VfModules vfModules : vnfs.getVfModules()) {
+                if (vfModules.getInstanceName().equals(vfModuleInstanceName)) {
+                    return vfModules;
+                }
+            }
+        }
+        throw new ResourceNotFoundException(
+                "Could not find vf-module with instanceName: " + vfModuleInstanceName + " in userparams");
+    }
+
+    protected VfModules getVfModulesByKey(String key, Resources resources) {
         for (Vnfs vnfs : resources.getVnfs()) {
             for (VfModules vfModules : vnfs.getVfModules()) {
                 if (vfModules.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
-                    return new Pair<Vnfs, VfModules>(vnfs, vfModules);
+                    return vfModules;
                 }
             }
         }
-        return null;
+        throw new ResourceNotFoundException("Could not find vf-module with key: " + key + " in userparams");
     }
 
-    protected Vnfs findVnfsByKey(String key, Resources resources, Vnfs vnfs) {
+    protected Vnfs findVnfsByInstanceName(String instanceName, Resources resources) {
+        for (Vnfs tempVnfs : resources.getVnfs()) {
+            if (tempVnfs.getInstanceName().equals(instanceName)) {
+                return tempVnfs;
+            }
+        }
+        throw new ResourceNotFoundException("Could not find vnf with instanceName: " + instanceName + " in userparams");
+    }
+
+    protected Vnfs findVnfsByKey(String key, Resources resources) {
         for (Vnfs tempVnfs : resources.getVnfs()) {
             if (tempVnfs.getModelInfo().getModelCustomizationId().equalsIgnoreCase(key)) {
-                vnfs = tempVnfs;
-                break;
+                return tempVnfs;
             }
         }
-        return vnfs;
+        throw new ResourceNotFoundException("Could not find vnf with key: " + key + " in userparams");
     }
 
     protected CloudRegion getCloudRegionFromMacroRequest(CloudConfiguration cloudConfiguration, Resources resources) {
@@ -1851,14 +1934,13 @@ public class BBInputSetup implements JavaDelegate {
     }
 
     private void mapRelationship(ServiceInstance serviceInstance, Relationships relationships) {
-        this.mapProject(relationships.getByType(AAIObjectType.PROJECT), serviceInstance);
-        this.mapOwningEntity(relationships.getByType(AAIObjectType.OWNING_ENTITY), serviceInstance);
-        this.mapL3Networks(relationships.getRelatedAAIUris(AAIObjectType.L3_NETWORK), serviceInstance.getNetworks());
-        this.mapGenericVnfs(relationships.getRelatedAAIUris(AAIObjectType.GENERIC_VNF), serviceInstance.getVnfs());
-        this.mapPnfs(relationships.getRelatedAAIUris(AAIObjectType.PNF), serviceInstance.getPnfs());
-        this.mapCollection(relationships.getByType(AAIObjectType.COLLECTION), serviceInstance);
-        this.mapConfigurations(relationships.getRelatedAAIUris(AAIObjectType.CONFIGURATION),
-                serviceInstance.getConfigurations());
+        this.mapProject(relationships.getByType(Types.PROJECT, uri -> uri.nodesOnly(true)), serviceInstance);
+        this.mapOwningEntity(relationships.getByType(Types.OWNING_ENTITY, uri -> uri.nodesOnly(true)), serviceInstance);
+        this.mapL3Networks(relationships.getRelatedUris(Types.L3_NETWORK), serviceInstance.getNetworks());
+        this.mapGenericVnfs(relationships.getRelatedUris(Types.GENERIC_VNF), serviceInstance.getVnfs());
+        this.mapPnfs(relationships.getRelatedUris(Types.PNF), serviceInstance.getPnfs());
+        this.mapCollection(relationships.getByType(Types.COLLECTION), serviceInstance);
+        this.mapConfigurations(relationships.getRelatedUris(Types.CONFIGURATION), serviceInstance.getConfigurations());
     }
 
     protected void mapConfigurations(List<AAIResourceUri> relatedAAIUris, List<Configuration> configurations) {
@@ -1897,11 +1979,11 @@ public class BBInputSetup implements JavaDelegate {
         Optional<Relationships> relationshipsOp = aaiGenericVnfWrapper.getRelationships();
         if (relationshipsOp.isPresent()) {
             Relationships relationships = relationshipsOp.get();
-            this.mapPlatform(relationships.getByType(AAIObjectType.PLATFORM), genericVnf);
-            this.mapLineOfBusiness(relationships.getByType(AAIObjectType.LINE_OF_BUSINESS), genericVnf);
-            genericVnf.getVolumeGroups().addAll(mapVolumeGroups(relationships.getByType(AAIObjectType.VOLUME_GROUP)));
-            genericVnf.getInstanceGroups()
-                    .addAll(mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP)));
+            this.mapPlatform(relationships.getByType(Types.PLATFORM, uri -> uri.nodesOnly(true)), genericVnf);
+            this.mapLineOfBusiness(relationships.getByType(Types.LINE_OF_BUSINESS, uri -> uri.nodesOnly(true)),
+                    genericVnf);
+            genericVnf.getVolumeGroups().addAll(mapVolumeGroups(relationships.getByType(Types.VOLUME_GROUP)));
+            genericVnf.getInstanceGroups().addAll(mapInstanceGroups(relationships.getByType(Types.INSTANCE_GROUP)));
         }
 
         return genericVnf;
@@ -2008,8 +2090,7 @@ public class BBInputSetup implements JavaDelegate {
     }
 
     private void setInstanceGroupForCollection(Collection collection, Relationships relationships) {
-        List<InstanceGroup> instanceGroupsList =
-                mapInstanceGroups(relationships.getByType(AAIObjectType.INSTANCE_GROUP));
+        List<InstanceGroup> instanceGroupsList = mapInstanceGroups(relationships.getByType(Types.INSTANCE_GROUP));
         if (!instanceGroupsList.isEmpty()) {
             collection.setInstanceGroup(instanceGroupsList.get(0));
         }
@@ -2046,9 +2127,8 @@ public class BBInputSetup implements JavaDelegate {
         Optional<Relationships> relationshipsOp = aaiNetworkWrapper.getRelationships();
         if (relationshipsOp.isPresent()) {
             Relationships relationships = relationshipsOp.get();
-            this.mapNetworkPolicies(relationships.getByType(AAIObjectType.NETWORK_POLICY),
-                    network.getNetworkPolicies());
-            mapRouteTableReferences(relationships.getByType(AAIObjectType.ROUTE_TABLE_REFERENCE),
+            this.mapNetworkPolicies(relationships.getByType(Types.NETWORK_POLICY), network.getNetworkPolicies());
+            mapRouteTableReferences(relationships.getByType(Types.ROUTE_TABLE_REFERENCE),
                     network.getContrailNetworkRouteTableReferences());
         }