From ec3b877ab6d9ed87bdecfe420fcfe0436b3cfe7a Mon Sep 17 00:00:00 2001 From: ojasdubey Date: Thu, 14 Jun 2018 20:14:39 +0530 Subject: VLAN Tagging - Display naming convention warning Vlan tagging HEAT validation doesn't show appropriate warning when naming convention is not followed Change-Id: I98d998ac425a66f0f0b362aecd5da6c867c5b7eb Issue-ID: SDC-1431 Signed-off-by: ojasdubey --- .../resources/uploads/vlan-tagging-positive.zip | Bin 2882 -> 2882 bytes .../sdc/heat/services/HeatResourceUtil.java | 110 +++++++++++++++++++++ .../consolidation/FilePortConsolidationData.java | 12 +-- .../heattotosca/ConsolidationDataUtil.java | 22 ++--- .../services/heattotosca/HeatToToscaUtil.java | 109 +++----------------- ...ailV2VlanToInterfaceResourceConnectionTest.java | 20 ++-- .../VirtualMachineInterfaceGuidelineValidator.java | 88 ++++++----------- ...tualMachineInterfaceGuidelineValidatorTest.java | 10 +- .../negative_get_resource/input/nested.yml | 2 +- .../negative_test/input/nested.yml | 2 +- .../negative_test_non_string/input/nested.yml | 2 +- .../positive_test/input/nested.yml | 2 +- .../positive_test/input/nested.yml | 2 +- .../negative_test/two_vlans/nested.yml | 4 +- .../negative_test/vlan_and_nova/nested.yml | 2 +- .../positive_test/input/nested.yml | 2 +- 16 files changed, 191 insertions(+), 198 deletions(-) create mode 100644 openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java diff --git a/openecomp-bdd/resources/uploads/vlan-tagging-positive.zip b/openecomp-bdd/resources/uploads/vlan-tagging-positive.zip index 22e66af340..b4519778c4 100644 Binary files a/openecomp-bdd/resources/uploads/vlan-tagging-positive.zip and b/openecomp-bdd/resources/uploads/vlan-tagging-positive.zip differ diff --git a/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java new file mode 100644 index 0000000000..a44b0196d4 --- /dev/null +++ b/openecomp-be/lib/openecomp-heat-lib/src/main/java/org/openecomp/sdc/heat/services/HeatResourceUtil.java @@ -0,0 +1,110 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.heat.services; + +import java.util.Objects; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; + +public class HeatResourceUtil { + + private static final String UNDERSCORE = "_"; + private static final String WORDS_REGEX = "(\\w+)"; + private static final String PORT_RESOURCE_ID_REGEX_SUFFIX = "(_\\d+)*"; + private static final String PORT_RESOURCE_ID_REGEX_PREFIX = + WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX; + private static final String PORT_INT_RESOURCE_ID_REGEX_PREFIX = PORT_RESOURCE_ID_REGEX_PREFIX + + UNDERSCORE + "int_"+ WORDS_REGEX + UNDERSCORE; + private static final String SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX = + PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "subint_"+ WORDS_REGEX + UNDERSCORE; + + public static Optional evaluateNetworkRoleFromResourceId(String resourceId, + String resourceType) { + Optional portType = getPortType(resourceType); + if (portType.isPresent()) { + String portResourceIdRegex = + PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + WORDS_REGEX + UNDERSCORE + + portType.get().getPortTypeName() + PORT_RESOURCE_ID_REGEX_SUFFIX; + String portIntResourceIdRegex = + PORT_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName() + + PORT_RESOURCE_ID_REGEX_SUFFIX; + + String portNetworkRole = getNetworkRole(resourceId, portResourceIdRegex); + String portIntNetworkRole = getNetworkRole(resourceId, portIntResourceIdRegex); + + return Optional.ofNullable(Objects.nonNull(portNetworkRole) + ? portNetworkRole : portIntNetworkRole); + } + return Optional.empty(); + } + + private static Optional getPortType(String resourceType) { + if (resourceType.equals( + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())) { + return Optional.of(PortType.VMI); + } else if (resourceType.equals( + HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) { + return Optional.of(PortType.PORT); + } + return Optional.empty(); + } + + public static Optional extractNetworkRoleFromSubInterfaceId(String resourceId, + String resourceType) { + Optional portType = getPortType(resourceType); + if (portType.isPresent()) { + String subInterfaceResourceIdRegex = + SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName() + + PORT_RESOURCE_ID_REGEX_SUFFIX; + + return Optional.ofNullable(getNetworkRole(resourceId, subInterfaceResourceIdRegex)); + } + return Optional.empty(); + } + + private enum PortType { + PORT("port"), + VMI("vmi"); + + private String portTypeName; + + PortType(String portTypeName) { + this.portTypeName = portTypeName; + } + + public String getPortTypeName() { + return portTypeName; + } + } + + private static String getNetworkRole(String portResourceId, String portIdRegex) { + Pattern pattern = Pattern.compile(portIdRegex); + Matcher matcher = pattern.matcher(portResourceId); + if (matcher.matches()) { + String networkRole = matcher.group(3); + //Assuming network role will not contain ONLY digits + if (!networkRole.matches("\\d+")) { + return matcher.group(3); + } + } + return null; + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/unifiedmodel/consolidation/FilePortConsolidationData.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/unifiedmodel/consolidation/FilePortConsolidationData.java index 9335dd4a97..3aaf47cae5 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/unifiedmodel/consolidation/FilePortConsolidationData.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/datatypes/heattotosca/unifiedmodel/consolidation/FilePortConsolidationData.java @@ -23,7 +23,7 @@ import java.util.Optional; import java.util.Set; import org.openecomp.sdc.heat.datatypes.model.Resource; -import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; +import org.openecomp.sdc.heat.services.HeatResourceUtil; public class FilePortConsolidationData { @@ -93,16 +93,14 @@ public class FilePortConsolidationData { return consolidationData; } - private PortTemplateConsolidationData createPortTemplateConsolidationData( - String portNodeTemplateId, String portResourceId, String portResourceType) { + public PortTemplateConsolidationData createPortTemplateConsolidationData(String portNodeTemplateId, + String portResourceId, + String portResourceType) { PortTemplateConsolidationData consolidationData = new PortTemplateConsolidationData(); consolidationData.setNodeTemplateId(portNodeTemplateId); - Optional portNetworkRole = HeatToToscaUtil.evaluateNetworkRoleFromResourceId(portResourceId, + Optional portNetworkRole = HeatResourceUtil.evaluateNetworkRoleFromResourceId(portResourceId, portResourceType); portNetworkRole.ifPresent(consolidationData::setNetworkRole); return consolidationData; } - - - } diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/ConsolidationDataUtil.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/ConsolidationDataUtil.java index 64c2168241..0d8a07a41f 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/ConsolidationDataUtil.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/ConsolidationDataUtil.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; + import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.onap.sdc.tosca.datatypes.model.RequirementAssignment; @@ -142,8 +143,8 @@ public class ConsolidationDataUtil { PortTemplateConsolidationData portTemplateConsolidationData = filePortConsolidationData.getPortTemplateConsolidationData(portNodeTemplateId); if (portTemplateConsolidationData == null) { - portTemplateConsolidationData = getInitPortTemplateConsolidationData(portNodeTemplateId, - portResourceId, portResourceType); + portTemplateConsolidationData = filePortConsolidationData + .createPortTemplateConsolidationData(portNodeTemplateId, portResourceId, portResourceType); filePortConsolidationData.setPortTemplateConsolidationData(portNodeTemplateId, portTemplateConsolidationData); } @@ -151,18 +152,6 @@ public class ConsolidationDataUtil { return portTemplateConsolidationData; } - private static PortTemplateConsolidationData getInitPortTemplateConsolidationData(String portNodeTemplateId, - String portResourceId, - String portResourceType) { - PortTemplateConsolidationData portTemplateConsolidationData = new PortTemplateConsolidationData(); - portTemplateConsolidationData.setNodeTemplateId(portNodeTemplateId); - Optional portNetworkRole = HeatToToscaUtil.evaluateNetworkRoleFromResourceId(portResourceId, - portResourceType); - portNetworkRole.ifPresent(portTemplateConsolidationData::setNetworkRole); - return portTemplateConsolidationData; - } - - public static Optional getSubInterfaceTemplateConsolidationData( TranslateTo subInterfaceTo, String subInterfaceNodeTemplateId) { @@ -197,8 +186,9 @@ public class ConsolidationDataUtil { Optional portResourceId = getSubInterfaceParentPortResourceId(parentPortNodeTemplateId, subInterfaceTo); if (portResourceId.isPresent()) { - portTemplateConsolidationData = getInitPortTemplateConsolidationData(parentPortNodeTemplateId, - portResourceId.get(), HeatToToscaUtil.getResourceType(portResourceId.get(), subInterfaceTo + portTemplateConsolidationData = filePortConsolidationData.createPortTemplateConsolidationData( + parentPortNodeTemplateId, portResourceId.get(), + HeatToToscaUtil.getResourceType(portResourceId.get(), subInterfaceTo .getHeatOrchestrationTemplate(), subInterfaceTo.getHeatFileName())); } else { portTemplateConsolidationData = new PortTemplateConsolidationData(); diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java index dd23e2ddbb..f2773ae01f 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/HeatToToscaUtil.java @@ -16,6 +16,7 @@ package org.openecomp.sdc.translator.services.heattotosca; +import static org.openecomp.sdc.heat.services.HeatResourceUtil.extractNetworkRoleFromSubInterfaceId; import static org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator.getFunctionTranslateTo; import java.io.IOException; @@ -29,8 +30,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; @@ -59,6 +58,7 @@ import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; import org.openecomp.core.validation.util.MessageContainerUtil; import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.SdcRuntimeException; import org.openecomp.sdc.common.utils.SdcCommon; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.datatypes.error.ErrorMessage; @@ -110,15 +110,6 @@ public class HeatToToscaUtil { private static final String GET_ATTR = "get_attr"; private static final String GET_RESOURCE = "get_resource"; private static final String UNDERSCORE = "_"; - private static final String WORDS_REGEX = "(\\w+)"; - private static final String PORT_RESOURCE_ID_REGEX_SUFFIX = "(_\\d+)*"; - private static final String PORT_RESOURCE_ID_REGEX_PREFIX = WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX; - private static final String PORT_INT_RESOURCE_ID_REGEX_PREFIX = - PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "int_" + WORDS_REGEX + UNDERSCORE; - private static final String SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX = - PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + "subint_" + WORDS_REGEX + UNDERSCORE; - private static final String SUB_INTERFACE_REGEX = - WORDS_REGEX + PORT_RESOURCE_ID_REGEX_SUFFIX + "_subint_(\\w_+)*vmi" + PORT_RESOURCE_ID_REGEX_SUFFIX; /** * Load and translate template data translator output. @@ -132,7 +123,7 @@ public class HeatToToscaUtil { try (InputStream fileContent = fileNameContentMap.getFileContent(SdcCommon.MANIFEST_NAME)) { heatToToscaTranslator.addManifest(SdcCommon.MANIFEST_NAME, FileUtils.toByteArray(fileContent)); } catch (IOException e) { - throw new RuntimeException("Failed to read manifest", e); + throw new SdcRuntimeException("Failed to read manifest", e); } fileNameContentMap.getFileList().stream().filter(fileName -> !(fileName.equals(SdcCommon.MANIFEST_NAME))) @@ -151,7 +142,7 @@ public class HeatToToscaUtil { return heatToToscaTranslator.translate(); } catch (IOException e) { // rethrow as a RuntimeException to keep the signature backward compatible - throw new RuntimeException("Failed to read Heat template tree", e); + throw new SdcRuntimeException("Failed to read Heat template tree", e); } } @@ -348,7 +339,7 @@ public class HeatToToscaUtil { Object propertyValue) { Object entity; - Object translatedId; + Object translatedId = null; if (Objects.isNull(propertyValue)) { return Optional.empty(); @@ -362,19 +353,16 @@ public class HeatToToscaUtil { String key = entry.getKey(); referenceType = getReferenceTypeFromAttachedResouce(key); - if (!FunctionTranslationFactory.getInstance(entry.getKey()).isPresent()) { - translatedId = null; - } else { - FunctionTranslator functionTranslator = new FunctionTranslator(getFunctionTranslateTo(null, null, - heatFileName, heatOrchestrationTemplate, context), null, entry.getValue(), null); - translatedId = FunctionTranslationFactory.getInstance(entry.getKey()).get() - .translateFunction(functionTranslator); - } - if (translatedId instanceof String - && !new FunctionTranslator().isResourceSupported((String) translatedId)) { - translatedId = null; - } - + if (FunctionTranslationFactory.getInstance(entry.getKey()).isPresent()) { + FunctionTranslator functionTranslator = new FunctionTranslator(getFunctionTranslateTo(null, null, + heatFileName, heatOrchestrationTemplate, context), null, entry.getValue(), null); + translatedId = FunctionTranslationFactory.getInstance(entry.getKey()).get() + .translateFunction(functionTranslator); + if (translatedId instanceof String + && !new FunctionTranslator().isResourceSupported((String) translatedId)) { + translatedId = null; + } + } } else { translatedId = propertyValue; entity = propertyValue; @@ -1412,71 +1400,4 @@ public class HeatToToscaUtil { return networkRole; } - public static Optional evaluateNetworkRoleFromResourceId(String resourceId, String resourceType) { - Optional portType = getPortType(resourceType); - if (portType.isPresent()) { - String portResourceIdRegex = - PORT_RESOURCE_ID_REGEX_PREFIX + UNDERSCORE + WORDS_REGEX + UNDERSCORE + portType.get() - .getPortTypeName() - + PORT_RESOURCE_ID_REGEX_SUFFIX; - String portIntResourceIdRegex = PORT_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName() - + PORT_RESOURCE_ID_REGEX_SUFFIX; - - String portNetworkRole = getNetworkRole(resourceId, portResourceIdRegex); - String portIntNetworkRole = getNetworkRole(resourceId, portIntResourceIdRegex); - - return Optional.ofNullable(Objects.nonNull(portNetworkRole) ? portNetworkRole : portIntNetworkRole); - } - return Optional.empty(); - } - - private static Optional getPortType(String resourceType) { - if (resourceType - .equals(HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())) { - return Optional.of(PortType.VMI); - } else if (resourceType.equals(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource())) { - return Optional.of(PortType.PORT); - } - return Optional.empty(); - } - - public static Optional extractNetworkRoleFromSubInterfaceId(String resourceId, String resourceType) { - Optional portType = getPortType(resourceType); - if (portType.isPresent()) { - String subInterfaceResourceIdRegex = - SUB_INTERFACE_INT_RESOURCE_ID_REGEX_PREFIX + portType.get().getPortTypeName() - + PORT_RESOURCE_ID_REGEX_SUFFIX; - - return Optional.ofNullable(getNetworkRole(resourceId, subInterfaceResourceIdRegex)); - } - return Optional.empty(); - } - - private enum PortType { - PORT("port"), VMI("vmi"); - - private String portTypeName; - - PortType(String portTypeName) { - this.portTypeName = portTypeName; - } - - public String getPortTypeName() { - return portTypeName; - } - } - - private static String getNetworkRole(String portResourceId, String portIdRegex) { - Pattern pattern = Pattern.compile(portIdRegex); - Matcher matcher = pattern.matcher(portResourceId); - if (matcher.matches()) { - String networkRole = matcher.group(3); - //Assuming network role will not contain ONLY digits - if (!networkRole.matches("\\d+")) { - return matcher.group(3); - } - } - return null; - } - } diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ContrailV2VlanToInterfaceResourceConnectionTest.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ContrailV2VlanToInterfaceResourceConnectionTest.java index 4a0615da1c..ada0a2caf5 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ContrailV2VlanToInterfaceResourceConnectionTest.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/test/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ContrailV2VlanToInterfaceResourceConnectionTest.java @@ -18,24 +18,26 @@ package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslati import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; import org.openecomp.sdc.heat.datatypes.model.Resource; import org.openecomp.sdc.heat.services.HeatConstants; +import org.openecomp.sdc.heat.services.HeatResourceUtil; import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.FilePortConsolidationData; import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.PortTemplateConsolidationData; import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.SubInterfaceTemplateConsolidationData; import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; -import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; - public class ContrailV2VlanToInterfaceResourceConnectionTest extends BaseResourceTranslationTest { private static final String PORT_NODE_TEMPLATE_ID_FOR_ATTR_TEST = "vdbe_untr_vmi"; @@ -136,7 +138,7 @@ public class ContrailV2VlanToInterfaceResourceConnectionTest extends BaseResourc "vm_type_subint_networkrole_vmi"); subInterfaceResourceIds.forEach(resourceId -> { - Optional networkRole = HeatToToscaUtil.extractNetworkRoleFromSubInterfaceId( + Optional networkRole = HeatResourceUtil.extractNetworkRoleFromSubInterfaceId( resourceId, HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()); Assert.assertTrue(networkRole.isPresent() @@ -151,7 +153,7 @@ public class ContrailV2VlanToInterfaceResourceConnectionTest extends BaseResourc "vm_type_11_subint_11_vmi_11"); subInterfaceResourceIds.forEach(resourceId -> { - Optional networkRole = HeatToToscaUtil.extractNetworkRoleFromSubInterfaceId( + Optional networkRole = HeatResourceUtil.extractNetworkRoleFromSubInterfaceId( resourceId, HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()); Assert.assertFalse(networkRole.isPresent()); diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidator.java index de4dc79207..4347d6cef9 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidator.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidator.java @@ -1,5 +1,17 @@ package org.openecomp.sdc.validation.impl.validators.namingconvention; +import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE; +import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE; +import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.openecomp.core.validation.ErrorMessageCode; import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; import org.openecomp.core.validation.types.GlobalValidationContext; @@ -7,29 +19,12 @@ import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.heat.datatypes.DefinedHeatParameterTypes; import org.openecomp.sdc.heat.datatypes.model.Resource; import org.openecomp.sdc.heat.services.HeatConstants; +import org.openecomp.sdc.heat.services.HeatResourceUtil; import org.openecomp.sdc.heat.services.HeatStructureUtil; import org.openecomp.sdc.validation.ResourceValidator; import org.openecomp.sdc.validation.ValidationContext; import org.openecomp.sdc.validation.type.NamingConventionValidationContext; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE; -import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE; -import static org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE; - -/** - * @author KATYR - * @since February 05, 2018 - */ - public class VirtualMachineInterfaceGuidelineValidator implements ResourceValidator { private static final ErrorMessageCode ERROR_CODE_VLAN_GUIDELINE1 = new ErrorMessageCode ("VlANG1"); @@ -37,8 +32,6 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida ("VlANG2"); private static final ErrorMessageCode ERROR_CODE_VLAN_GUIDELINE3 = new ErrorMessageCode ("VlANG3"); - private static final String UNDERSCORE = "_"; - private static final String VMI = "vmi"; @Override @@ -53,8 +46,7 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida namingConventionValidationContext); validateSingleVirtualMachineInterfaceInFile(fileName, globalContext, namingConventionValidationContext); - validateNamingConvention(fileName, resourceEntry, globalContext - ); + validateSubInterfaceNamingConvention(fileName, resourceEntry, globalContext); } } @@ -63,32 +55,28 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida GlobalValidationContext globalContext, NamingConventionValidationContext namingConventionValidationContext) { - Object refsPropertyValue = resourceEntry.getValue().getProperties() - .get(HeatConstants.VMI_REFS_PROPERTY_NAME); + Object refsPropertyValue = resourceEntry.getValue().getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME); if (Objects.isNull(refsPropertyValue)) { addViolationToContext(fileName, globalContext, ErrorLevel.WARNING, ERROR_CODE_VLAN_GUIDELINE1, - Messages.VLAN_GUIDELINE_VALIDATION_NOT_MODELED_THROUGH_RESOURCE_GROUP, - resourceEntry.getKey()); + Messages.VLAN_GUIDELINE_VALIDATION_NOT_MODELED_THROUGH_RESOURCE_GROUP, resourceEntry.getKey()); return; } - final boolean modeledThroughResourceGroup = - isModeledThroughResourceGroup(fileName, globalContext, - namingConventionValidationContext, - refsPropertyValue); + final boolean modeledThroughResourceGroup = isModeledThroughResourceGroup(fileName, globalContext, + namingConventionValidationContext, refsPropertyValue); if (!modeledThroughResourceGroup) { addViolationToContext(fileName, globalContext, ErrorLevel.WARNING, ERROR_CODE_VLAN_GUIDELINE1, - Messages.VLAN_GUIDELINE_VALIDATION_NOT_MODELED_THROUGH_RESOURCE_GROUP, - resourceEntry.getKey()); + Messages.VLAN_GUIDELINE_VALIDATION_NOT_MODELED_THROUGH_RESOURCE_GROUP, resourceEntry.getKey()); } } - private void validateNamingConvention(String fileName, Map.Entry - resourceEntry, GlobalValidationContext globalContext) { + private void validateSubInterfaceNamingConvention(String fileName, Map.Entry resourceEntry, + GlobalValidationContext globalContext) { final String resourceId = resourceEntry.getKey(); - final String networkRole = extractNetworkRoleFromResourceId(resourceId); - if (Objects.isNull(networkRole)) { + final Optional networkRole = HeatResourceUtil.extractNetworkRoleFromSubInterfaceId(resourceId, resourceEntry + .getValue().getType()); + if (!networkRole.isPresent()) { addViolationToContext(fileName, globalContext, ErrorLevel.WARNING, ERROR_CODE_VLAN_GUIDELINE3, Messages.VLAN_GUIDELINE_VALIDATION_NAMING_CONVENTION, resourceId); } @@ -115,8 +103,8 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida private boolean fileContainsNonVlanResources(Map resources, Set forbiddenTypes) { - for (String resourceName : resources.keySet()) { - if (forbiddenTypes.contains(resources.get(resourceName).getType())) { + for (Map.Entry resourceEntry : resources.entrySet()) { + if (forbiddenTypes.contains(resourceEntry.getValue().getType())) { return true; } } @@ -125,8 +113,8 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida private int countVlanResources(Map resources) { int numVlanResources = 0; - for (String resourceName : resources.keySet()) { - final String resourceType = resources.get(resourceName).getType(); + for (Map.Entry resourceEntry : resources.entrySet()) { + final String resourceType = resourceEntry.getValue().getType(); if (resourceType.equals (CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())) { numVlanResources++; @@ -149,8 +137,7 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida private Optional getVlanTagPropertyValue(Resource resource) { - Object vmiProperties = resource.getProperties() - .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME); + Object vmiProperties = resource.getProperties().get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME); if (Objects.nonNull(vmiProperties) && vmiProperties instanceof Map) { return Optional.ofNullable(((Map) vmiProperties) .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME)); @@ -205,19 +192,6 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida } - private static String extractNetworkRoleFromResourceId(String resourceId) { - - List splitSubInterfaceResourceId = - Arrays.asList(resourceId.toLowerCase().split(UNDERSCORE)); - - int vmiIndex = splitSubInterfaceResourceId.indexOf(VMI); - if (vmiIndex > 0) { - return splitSubInterfaceResourceId.get(vmiIndex - 1); - } - - return null; - } - private enum Messages { VLAN_GUIDELINE_VALIDATION_NOT_MODELED_THROUGH_RESOURCE_GROUP("VLAN Resource will not be " + @@ -225,9 +199,7 @@ public class VirtualMachineInterfaceGuidelineValidator implements ResourceValida VLAN_GUIDELINE_VALIDATION_SINGLE_VLAN("There should not be any Compute Server Node, Port, " + "Parent Port in nested file [%s]"), VLAN_GUIDELINE_VALIDATION_NAMING_CONVENTION( - "Network role associated with VLAN Sub-interface " + - "id" + - "[%s] is not following the naming convention"); + "Network role associated with VLAN Sub-interface id[%s] is not following the naming convention"); private final String errorMessage; diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidatorTest.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidatorTest.java index 407adae932..4d3718ee8d 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidatorTest.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/java/org/openecomp/sdc/validation/impl/validators/namingconvention/VirtualMachineInterfaceGuidelineValidatorTest.java @@ -1,5 +1,7 @@ package org.openecomp.sdc.validation.impl.validators.namingconvention; +import java.util.Map; + import org.openecomp.core.validation.types.MessageContainer; import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; import org.openecomp.sdc.validation.impl.validators.NamingConventionGuideLineValidator; @@ -7,8 +9,6 @@ import org.openecomp.sdc.validation.util.ValidationTestUtil; import org.testng.Assert; import org.testng.annotations.Test; -import java.util.Map; - public class VirtualMachineInterfaceGuidelineValidatorTest { private static final String PATH = "/org/openecomp/validation/validators/guideLineValidator/vlan_validation/"; @@ -47,7 +47,7 @@ public class VirtualMachineInterfaceGuidelineValidatorTest { Assert.assertEquals( messages.get("nested.yml").getErrorMessageList().get(0).getMessage(), "WARNING: [VlANG1]: VLAN Resource will not be translated as the VLAN Sub-interface " + - "[template_vmi_Vlan_2] is not modeled as resource group"); + "[vdbe_0_subint_untr_vmi_0] is not modeled as resource group"); } @Test @@ -68,7 +68,7 @@ public class VirtualMachineInterfaceGuidelineValidatorTest { Assert.assertEquals( messages.get("nested.yml").getErrorMessageList().get(0).getMessage(), "WARNING: [VlANG1]: VLAN Resource will not be translated as the VLAN Sub-interface " + - "[template_vmi_Vlan_2] is not modeled as resource group"); + "[vdbe_0_subint_untr_vmi_0] is not modeled as resource group"); } @Test @@ -89,7 +89,7 @@ public class VirtualMachineInterfaceGuidelineValidatorTest { Assert.assertEquals( messages.get("nested.yml").getErrorMessageList().get(0).getMessage(), "WARNING: [VlANG1]: VLAN Resource will not be translated as the VLAN Sub-interface " + - "[template_vmi_Vlan_2] is not modeled as resource group"); + "[vdbe_0_subint_untr_vmi_0] is not modeled as resource group"); } diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_get_resource/input/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_get_resource/input/nested.yml index a1846b72c7..6f8c5e7518 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_get_resource/input/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_get_resource/input/nested.yml @@ -29,7 +29,7 @@ parameters: resources: - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test/input/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test/input/nested.yml index 5b128f5ea2..4253933c16 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test/input/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test/input/nested.yml @@ -29,7 +29,7 @@ parameters: resources: - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test_non_string/input/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test_non_string/input/nested.yml index 1b4c03fa26..18eb29efb5 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test_non_string/input/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/negative_test_non_string/input/nested.yml @@ -29,7 +29,7 @@ parameters: resources: - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/positive_test/input/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/positive_test/input/nested.yml index 272543c2a1..6519bffb16 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/positive_test/input/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/modeled_through_resource_group/positive_test/input/nested.yml @@ -29,7 +29,7 @@ parameters: resources: - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/naming_convention/positive_test/input/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/naming_convention/positive_test/input/nested.yml index f45f6928c9..eed04cf3e3 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/naming_convention/positive_test/input/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/naming_convention/positive_test/input/nested.yml @@ -29,7 +29,7 @@ parameters: resources: - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/two_vlans/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/two_vlans/nested.yml index 0975493b7d..785bb794b7 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/two_vlans/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/two_vlans/nested.yml @@ -29,7 +29,7 @@ parameters: resources: - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: @@ -72,7 +72,7 @@ resources: virtual_network_refs: [{ get_param: net2 },{ get_param: net1 }] virtual_machine_interface_refs: [ { get_param: p1 }] - template_vmi_Vlan_3_arrayParameter: + vdbe_0_subint_untr_vmi_1: type: OS::ContrailV2::VirtualMachineInterface properties: name: diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/vlan_and_nova/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/vlan_and_nova/nested.yml index 32f9b9b578..f5f8d76728 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/vlan_and_nova/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/negative_test/vlan_and_nova/nested.yml @@ -35,7 +35,7 @@ resources: security_groups: [{get_param: p1}, {get_param: p2}] replacement_policy: AUTO - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/positive_test/input/nested.yml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/positive_test/input/nested.yml index f45f6928c9..eed04cf3e3 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/positive_test/input/nested.yml +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/test/resources/org/openecomp/validation/validators/guideLineValidator/vlan_validation/single_vlan_resource/positive_test/input/nested.yml @@ -29,7 +29,7 @@ parameters: resources: - template_vmi_Vlan_2: + vdbe_0_subint_untr_vmi_0: type: OS::ContrailV2::VirtualMachineInterface properties: name: -- cgit 1.2.3-korg