From 4cc47ac3abe00eda7c04b9955e56dafc819e2657 Mon Sep 17 00:00:00 2001 From: "katy.rotman" Date: Wed, 21 Feb 2018 10:18:53 +0200 Subject: Heat Resource Validator code +tests Change-Id: Ic1e10c15fbbc01580311057e71edf05506bd9e8f Issue-ID: SDC-994 Signed-off-by: katy.rotman --- .../impl/util/HeatValidationService.java | 50 +++++------ .../VirtualMachineInterfaceValidator.java | 98 ++++++++++++---------- 2 files changed, 79 insertions(+), 69 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main') diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/util/HeatValidationService.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/util/HeatValidationService.java index dbd13ed8e0..e9a265d7bc 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/util/HeatValidationService.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/util/HeatValidationService.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * 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. @@ -326,44 +326,40 @@ public class HeatValidationService { } /** - * + * This method verifies whether the propertyValue contains a single parent port * @param fileName on which the validation is currently run - * @param globalContext gloabl validation context + * @param globalContext global validation context * @param heatResourceValidationContext heat resource validation context * @param propertyValue the value which is examined * @return whether the vlan has single parent port */ public static boolean hasSingleParentPort(String fileName, GlobalValidationContext globalContext, - HeatResourceValidationContext heatResourceValidationContext, - Object propertyValue) { - boolean hasSingleParentPort; - if (propertyValue instanceof List && ((List) propertyValue).size() == 1) { - final Object listValue = ((List) propertyValue).get(0); - - final Set getParamValues = - HeatStructureUtil.getReferencedValuesByFunctionName(fileName, "get_param", - listValue, globalContext); - hasSingleParentPort = getParamValues.isEmpty() || (getParamValues.size() == 1) && - validateGetParamValueOfType(getParamValues, heatResourceValidationContext, - DefinedHeatParameterTypes.STRING.getType()); - } else { - hasSingleParentPort = false; + HeatResourceValidationContext heatResourceValidationContext, + Object propertyValue) { + final boolean isList = propertyValue instanceof List; + if (!isList || ((List) propertyValue).size() != 1) { + return false; } - return hasSingleParentPort; + + final Object listValue = ((List) propertyValue).get(0); + + final Set getParamValues = + HeatStructureUtil.getReferencedValuesByFunctionName(fileName, "get_param", + listValue, globalContext); + + return getParamValues.isEmpty() || (getParamValues.size() == 1) && + validateGetParamValueOfType(getParamValues, heatResourceValidationContext, + DefinedHeatParameterTypes.STRING.getType()); + } + private static boolean validateGetParamValueOfType(Set values, HeatResourceValidationContext - heatResourceValidationContext,String type) { - Optional value = values.stream().findAny(); - boolean isString = false; - if (value.isPresent()) { - isString = - Objects.equals(heatResourceValidationContext.getHeatOrchestrationTemplate - ().getParameters().get(value.get()).getType(), type); - } + heatResourceValidationContext, String type) { - return isString; + return values.stream().anyMatch(e -> Objects.equals( + heatResourceValidationContext.getHeatOrchestrationTemplate().getParameters().get(e).getType(), type)); } } diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/heatresource/VirtualMachineInterfaceValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/heatresource/VirtualMachineInterfaceValidator.java index b5fa80e381..fc817c467d 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/heatresource/VirtualMachineInterfaceValidator.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/heatresource/VirtualMachineInterfaceValidator.java @@ -21,20 +21,16 @@ import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; import org.openecomp.core.validation.types.GlobalValidationContext; import org.openecomp.sdc.common.togglz.ToggleableFeature; 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.HeatStructureUtil; import org.openecomp.sdc.validation.ResourceValidator; import org.openecomp.sdc.validation.ValidationContext; import org.openecomp.sdc.validation.impl.util.HeatValidationService; import org.openecomp.sdc.validation.type.HeatResourceValidationContext; -import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.Set; public class VirtualMachineInterfaceValidator implements ResourceValidator { private static final ErrorMessageCode ERROR_CODE_VLAN1 = new ErrorMessageCode("VLAN1"); @@ -47,14 +43,55 @@ public class VirtualMachineInterfaceValidator implements ResourceValidator { if (ToggleableFeature.VLAN_TAGGING.isActive()) { HeatResourceValidationContext heatResourceValidationContext = (HeatResourceValidationContext) validationContext; - Optional tagPropertyValue = getVlanTagPropertyValue(resourceEntry.getValue()); + final ValidityStatus status = calculateValidityStatus(resourceEntry.getValue()); + + switch (status) { + case BOTH_PROPERTIES_PRESENT: + validateHasSingleParentPort(fileName, resourceEntry, globalContext, + heatResourceValidationContext); + break; + case REFS_PROPERTY_MISSING: + globalContext + .addMessage(fileName, ErrorLevel.WARNING, + ErrorMessagesFormatBuilder + .getErrorWithParameters( + ERROR_CODE_VLAN2, + Messages.VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY.getErrorMessage(), + resourceEntry.getKey())); + break; + case VLAN_TAG_PROPERTY_MISSING: + globalContext + .addMessage(fileName, ErrorLevel.WARNING, + ErrorMessagesFormatBuilder + .getErrorWithParameters( + ERROR_CODE_VLAN2, + Messages.VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY.getErrorMessage(), + resourceEntry.getKey())); + validateHasSingleParentPort(fileName, resourceEntry, globalContext, + heatResourceValidationContext); + break; + case BOTH_PROPERTIES_MISSING: + // this is a port and not a VLAN, no further validation required + break; + default : + throw new IllegalArgumentException("Received a value for which no handling is " + + "available " + status); + } + } + } - tagPropertyValue - .ifPresent(o -> validateHasSingleParentPort(fileName, resourceEntry, globalContext, - heatResourceValidationContext)); - validateHasTwoProperties(fileName, resourceEntry, globalContext); + private ValidityStatus calculateValidityStatus(Resource resource) { + Optional refsPropertyValue = getRefsPropertyValue(resource); + Optional tagPropertyValue = getVlanTagPropertyValue(resource); + if (refsPropertyValue.isPresent() && tagPropertyValue.isPresent()) { + return ValidityStatus.BOTH_PROPERTIES_PRESENT; } + if (!refsPropertyValue.isPresent() && !tagPropertyValue.isPresent()) { + return ValidityStatus.BOTH_PROPERTIES_MISSING; + } + return refsPropertyValue.map(o -> ValidityStatus.VLAN_TAG_PROPERTY_MISSING) + .orElse(ValidityStatus.REFS_PROPERTY_MISSING); } @@ -67,9 +104,9 @@ public class VirtualMachineInterfaceValidator implements ResourceValidator { if (Objects.isNull(refsPropertyValue)) { return; } - boolean hasSingleParentPort= HeatValidationService.hasSingleParentPort(fileName, globalContext, + boolean hasSingleParentPort = HeatValidationService.hasSingleParentPort(fileName, globalContext, heatResourceValidationContext, - refsPropertyValue); + refsPropertyValue); if (!hasSingleParentPort) { globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder .getErrorWithParameters(ERROR_CODE_VLAN1, @@ -81,37 +118,6 @@ public class VirtualMachineInterfaceValidator implements ResourceValidator { } - private void validateHasTwoProperties(String fileName, Map.Entry resourceEntry, - GlobalValidationContext globalContext) { - - Optional refsPropertyValue = getRefsPropertyValue(resourceEntry.getValue()); - Optional tagPropertyValue = getVlanTagPropertyValue(resourceEntry.getValue()); - - - if (refsPropertyValue.isPresent() && !tagPropertyValue.isPresent()) { - globalContext - .addMessage(fileName, ErrorLevel.WARNING, - ErrorMessagesFormatBuilder - .getErrorWithParameters( - ERROR_CODE_VLAN2, - Messages.VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY.getErrorMessage(), - resourceEntry.getKey()) - ); - - } else if (!refsPropertyValue.isPresent() && tagPropertyValue.isPresent()) { - globalContext - .addMessage(fileName, ErrorLevel.WARNING, - ErrorMessagesFormatBuilder - .getErrorWithParameters( - ERROR_CODE_VLAN2, - Messages.VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY.getErrorMessage(), - resourceEntry.getKey())); - - } - - } - - private Optional getVlanTagPropertyValue(Resource resource) { Object vmiProperties = resource.getProperties() .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME); @@ -130,6 +136,14 @@ public class VirtualMachineInterfaceValidator implements ResourceValidator { } + private enum ValidityStatus { + BOTH_PROPERTIES_MISSING, + BOTH_PROPERTIES_PRESENT, + REFS_PROPERTY_MISSING, + VLAN_TAG_PROPERTY_MISSING + + } + private enum Messages { VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT( "More than one parent port found, there should be only one parent port for a VLAN sub-interface ID [%s]"), -- cgit 1.2.3-korg