From ed5c51313c1bd3745a554888c969e840341599b0 Mon Sep 17 00:00:00 2001 From: Michael Pruss Date: Mon, 31 May 2021 18:28:08 -0400 Subject: Support instantiation of same model vnfs/vf-modules When instantiating a service with multiple vnfs and/or vfmodules SO would differentiate using ModelCustomizationId. This would cause issues when creating multiple instances of same model resource, and each lookup would return the same object. Instead of using ModelCustomizationId, this patch enables SO to use instanceName parameter to differentiate the resources. Validation was added to ensure that instanceNames are provided if there are multiple resources of the same model. If there are no duplicate resources and instanceName is not set, SO will default to previous logic using ModelCustomizationId. In order to properly associate vfmodules with their parent vnfs, the Resource class was augmented with a parent reference which maintains the relationship in further processing. Id generation logic was corrected to ensure that multiple resources of the same model are assigned unique instance ids and references to parent instanceId. Issue-ID: SO-3677 Change-Id: If39a0138f501177e12262f8e911137012e287fca Signed-off-by: Michael Pruss --- .../validation/UserParamsValidation.java | 63 +++++++++++++++++++--- 1 file changed, 57 insertions(+), 6 deletions(-) (limited to 'mso-api-handlers/mso-api-handler-infra/src/main/java') diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/validation/UserParamsValidation.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/validation/UserParamsValidation.java index 067e8611d0..8646a74a2f 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/validation/UserParamsValidation.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/validation/UserParamsValidation.java @@ -22,7 +22,13 @@ package org.onap.so.apihandlerinfra.validation; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.commons.lang3.StringUtils; import org.onap.so.apihandlerinfra.Action; import org.onap.so.apihandlerinfra.Actions; import org.onap.so.exceptions.ValidationException; @@ -36,8 +42,6 @@ public class UserParamsValidation implements ValidationRule { @Override public ValidationInformation validate(ValidationInformation info) throws ValidationException { Service validate = info.getUserParams(); - Actions action = info.getAction(); - if (validate.getModelInfo() == null) { throw new ValidationException("modelInfo in userParams", true); } else if (validate.getModelInfo().getModelType() == null) { @@ -49,6 +53,10 @@ public class UserParamsValidation implements ValidationRule { if (validate.getInstanceName() != null && info.getRequestInfo().getInstanceName() != null) { instanceNameValidation(info, validate); } + + Actions action = info.getAction(); + Map> vnfCustomIdToInstanceNames = new HashMap<>(); + Map> vfModuleCustomIdToInstanceNames = new HashMap<>(); for (Vnfs vnf : validate.getResources().getVnfs()) { if (vnf.getModelInfo() == null) { throw new ValidationException("modelInfo in userParams vnf resources", true); @@ -71,19 +79,42 @@ public class UserParamsValidation implements ValidationRule { if (vnf.getPlatform() != null && vnf.getPlatform().getPlatformName() == null) { throw new ValidationException("platformName in userParams vnf resources", true); } + + String vnfCustomizationId = vnf.getModelInfo().getModelCustomizationId(); + vnfCustomIdToInstanceNames.putIfAbsent(vnfCustomizationId, new HashSet<>()); + String vnfInstanceName = StringUtils.defaultString(vnf.getInstanceName()); + Set vnfVisitedInstanceNames = vnfCustomIdToInstanceNames.get(vnfCustomizationId); + if (!vnfVisitedInstanceNames.add(vnfInstanceName)) { + throw new ValidationException( + "instanceName: same instanceName with same modelCustomizationId in userParams vnf resources", + true); + } if (vnf.getVfModules().isEmpty()) { throw new ValidationException("vfModules in userParams vnf resources", true); } - for (VfModules vfModules : vnf.getVfModules()) { - if (vfModules.getModelInfo() == null) { + + for (VfModules vfModule : vnf.getVfModules()) { + if (vfModule.getModelInfo() == null) { throw new ValidationException("modelInfo in userParams vfModules resources", true); - } else if (vfModules.getModelInfo().getModelCustomizationId() == null) { + } else if (vfModule.getModelInfo().getModelCustomizationId() == null) { throw new ValidationException("modelCustomizationId in userParams vfModule resources", true); - } else if (vfModules.getModelInfo().getModelVersionId() == null) { + } else if (vfModule.getModelInfo().getModelVersionId() == null) { throw new ValidationException("modelVersionId in userParams vfModule resources", true); } + + String vfModulecustomizationId = vfModule.getModelInfo().getModelCustomizationId(); + vfModuleCustomIdToInstanceNames.putIfAbsent(vfModulecustomizationId, new HashSet<>()); + String vfModuleInstanceName = StringUtils.defaultString(vfModule.getInstanceName()); + Set vfModuleVisitedInstanceNames = vfModuleCustomIdToInstanceNames.get(vfModulecustomizationId); + if (!vfModuleVisitedInstanceNames.add(vfModuleInstanceName)) { + throw new ValidationException( + "instanceName: same instanceName with same modelCustomizationId in userParams vfModule resources", + true); + } } } + validateDuplicateInstanceNames(vnfCustomIdToInstanceNames, "vnf"); + validateDuplicateInstanceNames(vfModuleCustomIdToInstanceNames, "vfModule"); List validateNetworks = new ArrayList<>(); validateNetworks = validate.getResources().getNetworks(); @@ -135,4 +166,24 @@ public class UserParamsValidation implements ValidationRule { "modelCustomizationId in userParams service"); } } + + private void validateDuplicateInstanceNames(Map> duplicateValidator, String type) + throws ValidationException { + Set allInstanceNames = new HashSet<>(); + for (Map.Entry> entry : duplicateValidator.entrySet()) { + Set instanceNames = entry.getValue(); + if (instanceNames.size() > 1 && instanceNames.contains("")) + throw new ValidationException(String.format( + "instanceName: instanceName is missing or empty with same modelCustomizationId in userParams %s resources", + type), true); + + for (String instanceName : instanceNames) { + if (!instanceName.isBlank() && !allInstanceNames.add(instanceName)) { + throw new ValidationException(String.format( + "instanceName: same instanceName but different modelCustomizationId (instanceName should be unique) in userParams %s resources", + type), true); + } + } + } + } } -- cgit 1.2.3-korg