From 3a88d01cee90090f7bf012ea33fe73576e55192a Mon Sep 17 00:00:00 2001 From: wejs Date: Wed, 7 Feb 2018 17:19:59 +0100 Subject: Heat Validation - VirtualMachineInterface Issue-ID: SDC-994 Change-Id: I0fd23862eebf65437ab0e1a1ad1d44c9a2944990 Signed-off-by: katy.rotman --- .../impl/util/HeatValidationService.java | 56 +++++++- .../VirtualMachineInterfaceValidator.java | 153 +++++++++++++++++++++ 2 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/heatresource/VirtualMachineInterfaceValidator.java (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 469bfad3af..dbd13ed8e0 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 @@ -26,10 +26,12 @@ import org.openecomp.sdc.heat.datatypes.model.Environment; import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; import org.openecomp.sdc.heat.datatypes.model.Parameter; import org.openecomp.sdc.heat.datatypes.model.Resource; +import org.openecomp.sdc.heat.services.HeatStructureUtil; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.validation.impl.validators.HeatValidator; +import org.openecomp.sdc.validation.type.HeatResourceValidationContext; import java.io.InputStream; import java.util.Collection; @@ -105,11 +107,11 @@ public class HeatValidationService { * @param nestedParameters nested parameters. * @param nestedParametersNames nested parameter names. */ - public static void checkNestedParameters(String parentFileName, String nestedFileName, - GlobalValidationContext globalContext, - Map parentParameters, - Map nestedParameters, - Set nestedParametersNames) { + private static void checkNestedParameters(String parentFileName, String nestedFileName, + GlobalValidationContext globalContext, + Map parentParameters, + Map nestedParameters, + Set nestedParametersNames) { HeatOrchestrationTemplate parentHeatOrchestrationTemplate; HeatOrchestrationTemplate nestedHeatOrchestrationTemplate; @@ -276,6 +278,10 @@ public class HeatValidationService { } return false; } + + + + private static HeatOrchestrationTemplate getNestedHeatOrchestrationTemplate( String nestedFileName, GlobalValidationContext globalContext) throws Exception { Optional fileContent = globalContext.getFileContent(nestedFileName); @@ -319,9 +325,45 @@ public class HeatValidationService { return envContent; } + /** + * + * @param fileName on which the validation is currently run + * @param globalContext gloabl 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; + } + return hasSingleParentPort; + } + + 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); + } - public static String getResourceGroupResourceName(String resourceCallingToResourceGroup) { - return "OS::Heat::ResourceGroup in " + resourceCallingToResourceGroup; + return isString; } } 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 new file mode 100644 index 0000000000..b5fa80e381 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/heatresource/VirtualMachineInterfaceValidator.java @@ -0,0 +1,153 @@ +/* + * Copyright © 2016-2017 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.validation.impl.validators.heatresource; + +import org.openecomp.core.validation.ErrorMessageCode; +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"); + private static final ErrorMessageCode ERROR_CODE_VLAN2 = new ErrorMessageCode("VLAN2"); + + + @Override + public void validate(String fileName, Map.Entry resourceEntry, + GlobalValidationContext globalContext, ValidationContext validationContext) { + if (ToggleableFeature.VLAN_TAGGING.isActive()) { + HeatResourceValidationContext heatResourceValidationContext = + (HeatResourceValidationContext) validationContext; + Optional tagPropertyValue = getVlanTagPropertyValue(resourceEntry.getValue()); + + tagPropertyValue + .ifPresent(o -> validateHasSingleParentPort(fileName, resourceEntry, globalContext, + heatResourceValidationContext)); + validateHasTwoProperties(fileName, resourceEntry, globalContext); + + } + } + + + private void validateHasSingleParentPort(String fileName, + Map.Entry resourceEntry, + GlobalValidationContext globalContext, + HeatResourceValidationContext heatResourceValidationContext) { + Object refsPropertyValue = resourceEntry.getValue().getProperties() + .get(HeatConstants.VMI_REFS_PROPERTY_NAME); + if (Objects.isNull(refsPropertyValue)) { + return; + } + boolean hasSingleParentPort= HeatValidationService.hasSingleParentPort(fileName, globalContext, + heatResourceValidationContext, + refsPropertyValue); + if (!hasSingleParentPort) { + globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder + .getErrorWithParameters(ERROR_CODE_VLAN1, + Messages.VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT.getErrorMessage(), + resourceEntry.getKey())); + } + + + } + + + 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); + if (Objects.nonNull(vmiProperties) && vmiProperties instanceof Map) { + return Optional.ofNullable(((Map) vmiProperties) + .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME)); + } + return Optional.empty(); + } + + private Optional getRefsPropertyValue(Resource resource) { + Object refsProperty = resource.getProperties() + .get(HeatConstants.VMI_REFS_PROPERTY_NAME); + return Optional.ofNullable(refsProperty); + + } + + + 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]"), + VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY("VLAN Tag property " + + "virtual_machine_interface_properties_sub_interface_vlan_tag is missing in VLAN Resource ID [%s]"), + VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY("Parent port property virtual_machine_interface_refs " + + "is missing in VLAN Resource ID [%s]"); + + String getErrorMessage() { + return errorMessage; + } + + private final String errorMessage; + + Messages(String errorMessage) { + this.errorMessage = errorMessage; + } + + } + +} -- cgit 1.2.3-korg