diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk')
11 files changed, 859 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml new file mode 100644 index 0000000000..d4fad5ae65 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml @@ -0,0 +1,45 @@ +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <artifactId>openecomp-sdc-validation-sdk</artifactId> + + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-lib</artifactId> + <version>1.1.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + + <dependencies> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-logging-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>${testng.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-validation-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.openecomp.core</groupId> + <artifactId>openecomp-heat-lib</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>${commons.io.version}</version> + </dependency> + </dependencies> + + +</project> diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml.versionsBackup b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml.versionsBackup new file mode 100644 index 0000000000..d179ae0469 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/pom.xml.versionsBackup @@ -0,0 +1,42 @@ +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <artifactId>openecomp-sdc-validation-sdk</artifactId> + <dependencies> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-logging-core</artifactId> + <version>1.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <version>6.9.10</version> + </dependency> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-validation-api</artifactId> + <version>1.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.openecomp.core</groupId> + <artifactId>openecomp-heat-lib</artifactId> + <version>1.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.5</version> + </dependency> + </dependencies> + + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-lib</artifactId> + <version>1.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + +</project>
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/ResourceValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/ResourceValidator.java new file mode 100644 index 0000000000..1bd4bc9c62 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/ResourceValidator.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.validation; + +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.sdc.heat.datatypes.model.Resource; + +import java.util.Map; + +/** + * Created by TALIO on 2/23/2017. + */ +public interface ResourceValidator { + + void validate(String fileName, Map.Entry<String, Resource> resourceEntry, + GlobalValidationContext globalContext, ValidationContext validationContext); +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/ValidationContext.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/ValidationContext.java new file mode 100644 index 0000000000..e2d0f163e5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/ValidationContext.java @@ -0,0 +1,8 @@ +package org.openecomp.sdc.validation; + +/** + * Created by TALIO on 2/27/2017. + */ +public interface ValidationContext { + +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/Validator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/Validator.java new file mode 100644 index 0000000000..d7798359fe --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/Validator.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + + package org.openecomp.sdc.validation; + +import org.openecomp.core.validation.types.GlobalValidationContext; + +import java.util.Map; + + +public interface Validator { + void validate(GlobalValidationContext globalContext); + + default void init(Map<String, Object> properties){ return; } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/base/ResourceBaseValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/base/ResourceBaseValidator.java new file mode 100644 index 0000000000..1a5b373833 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/base/ResourceBaseValidator.java @@ -0,0 +1,159 @@ +package org.openecomp.sdc.validation.base; + +import org.apache.commons.collections4.MapUtils; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; +import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; +import org.openecomp.sdc.heat.datatypes.model.Resource; +import org.openecomp.sdc.heat.services.HeatStructureUtil; +import org.openecomp.sdc.heat.services.manifest.ManifestUtil; +import org.openecomp.sdc.logging.types.LoggerErrorDescription; +import org.openecomp.sdc.logging.types.LoggerTragetServiceName; +import org.openecomp.sdc.validation.ResourceValidator; +import org.openecomp.sdc.validation.ValidationContext; +import org.openecomp.sdc.validation.Validator; +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.validation.type.ConfigConstants; +import org.openecomp.sdc.validation.util.ValidationUtil; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * Created by TALIO on 2/16/2017. + */ +public class ResourceBaseValidator implements Validator { + + protected Map<String, ImplementationConfiguration> resourceTypeToImpl = new HashMap<>(); + private static Logger logger = (Logger) LoggerFactory.getLogger(ResourceBaseValidator.class); + + + public void init(Map<String, Object> properties) { + if (MapUtils.isEmpty(properties)) { + return; + } + + properties.entrySet().stream() + .filter(entry -> getImplementationConfigurationFromProperties(entry.getValue()) != null). + forEach(entry -> resourceTypeToImpl + .put(entry.getKey(), getImplementationConfigurationFromProperties(entry.getValue()))); + } + + @Override + public void validate(GlobalValidationContext globalContext) { + ManifestContent manifestContent; + try { + manifestContent = ValidationUtil.checkValidationPreCondition(globalContext); + } catch (Exception exception) { + return; + } + + Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent); + Map<String, FileData> fileEnvMap = ManifestUtil.getFileAndItsEnv(manifestContent); + globalContext.getFiles().stream() + .filter(fileName -> FileData + .isHeatFile(fileTypeMap.get(fileName))) + .forEach(fileName -> validate(fileName, + fileEnvMap.get(fileName) != null ? fileEnvMap.get(fileName).getFile() : null, + globalContext)); + } + + private void validate(String fileName, String envFileName, + GlobalValidationContext globalContext) { + HeatOrchestrationTemplate heatOrchestrationTemplate = + ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalContext); + if (heatOrchestrationTemplate == null) { + return; + } + + ValidationContext validationContext = + createValidationContext(fileName, envFileName, heatOrchestrationTemplate, globalContext); + + Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources(); + if (MapUtils.isEmpty(resourcesMap)) { + return; + } + + for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) { + String resourceType = resourceEntry.getValue().getType(); + + if (Objects.isNull(resourceType)) { + globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder + .getErrorWithParameters(Messages.INVALID_RESOURCE_TYPE.getErrorMessage(), "null", + resourceEntry.getKey()), LoggerTragetServiceName.VALIDATE_RESOURCE_TYPE, + LoggerErrorDescription.INVALID_RESOURCE_TYPE); + } else { + ResourceValidator + resourceValidatorImpl = getResourceValidatorInstance(resourceType, resourceTypeToImpl); + if (Objects.nonNull(resourceValidatorImpl)) { + resourceValidatorImpl.validate(fileName, resourceEntry, globalContext, + validationContext); + } + } + } + } + + public ValidationContext createValidationContext(String fileName, + String envFileName, + HeatOrchestrationTemplate heatOrchestrationTemplate, + GlobalValidationContext globalContext) { + return null; + } + + private static boolean isSupportedResourceType(String resourceType, + Map<String, ImplementationConfiguration> resourceTypeToImpl) { + return resourceTypeToImpl.containsKey(resourceType) && resourceTypeToImpl.get(resourceType) + .isEnable(); + + } + + private static ResourceValidator getResourceValidatorInstance(String resourceType, + Map<String, ImplementationConfiguration> resourceTypeToImpl) { + ResourceValidator resourceBaseValidator = null; + if (isSupportedResourceType(resourceType, resourceTypeToImpl)) { + return getValidatorImpl(resourceType, resourceTypeToImpl); + } + if (HeatStructureUtil.isNestedResource(resourceType)) { + return getValidatorImpl("nestedResource", resourceTypeToImpl); + } + return resourceBaseValidator; + } + + private static ResourceValidator getValidatorImpl(String resourceType, + Map<String, ImplementationConfiguration> resourceTypeToImpl) { + String implementationClass = resourceTypeToImpl.get(resourceType) != null ? + resourceTypeToImpl.get(resourceType).getImplementationClass() : null; + return implementationClass == null ? null : CommonMethods + .newInstance(implementationClass, ResourceValidator.class); + } + + private ImplementationConfiguration getImplementationConfigurationFromProperties(Object value) { + ImplementationConfiguration implementationConfiguration = new ImplementationConfiguration(); + + if (!(value instanceof Map)) { + return null; + } + + Map<String, Object> valueAsMap = (Map<String, Object>) value; + if (!(valueAsMap.containsKey(ConfigConstants.Impl_Class))) { + return null; + } + + implementationConfiguration.setImplementationClass( + valueAsMap.get(ConfigConstants.Impl_Class).toString()); + if (valueAsMap.containsKey(ConfigConstants.Enable)) { + implementationConfiguration.setEnable((Boolean. + valueOf(valueAsMap.get(ConfigConstants.Enable).toString()))); + } + + return implementationConfiguration; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/ConfigConstants.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/ConfigConstants.java new file mode 100644 index 0000000000..2aea17b336 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/ConfigConstants.java @@ -0,0 +1,17 @@ +package org.openecomp.sdc.validation.type; + +/** + * Created by TALIO on 1/30/2017. + */ +public class ConfigConstants { + public static final String Enable = "enable"; + public static final String Impl_Class = "implementationClass"; + public static final String Resource_Base_Validator = "resourceBaseValidator"; + + public static final String Namespace = "validation"; + public static final String Mandatory_Namespace = "mandatoryValidation"; + public static final String Validator_Impl_Key = "validatorImplementations"; + public static final String Nested_Resource_Key = "nestedResource"; + + private ConfigConstants(){} +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/HeatResourceValidationContext.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/HeatResourceValidationContext.java new file mode 100644 index 0000000000..6067876532 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/HeatResourceValidationContext.java @@ -0,0 +1,44 @@ +package org.openecomp.sdc.validation.type; + +import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; +import org.openecomp.sdc.validation.ValidationContext; + +import java.util.List; +import java.util.Map; + +/** + * Created by TALIO on 2/23/2017. + */ +public class HeatResourceValidationContext implements ValidationContext { + + private HeatOrchestrationTemplate heatOrchestrationTemplate; + // key - resource type, value - map with key = resource id and + // value = map with key = pointing / pointed resource type and + // value = pointing / pointed resource id + private Map<String, Map<String, Map<String, List<String>>>> fileLevelResourceDependencies; + private String envFileName; + + + public HeatResourceValidationContext( + HeatOrchestrationTemplate heatOrchestrationTemplate, + Map<String, Map<String, Map<String, List<String>>>> fileLevelResourceDependencies, + String envFileName) { + + this.heatOrchestrationTemplate = heatOrchestrationTemplate; + this.fileLevelResourceDependencies = fileLevelResourceDependencies; +// this.zipLevelResourceDependencies = zipLevelResourceDependencies; + this.envFileName = envFileName; + } + + public HeatOrchestrationTemplate getHeatOrchestrationTemplate() { + return heatOrchestrationTemplate; + } + + public Map<String, Map<String, Map<String, List<String>>>> getFileLevelResourceDependencies() { + return fileLevelResourceDependencies; + } + + public String getEnvFileName() { + return envFileName; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/NamingConventionValidationContext.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/NamingConventionValidationContext.java new file mode 100644 index 0000000000..273cdedfa5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/type/NamingConventionValidationContext.java @@ -0,0 +1,28 @@ +package org.openecomp.sdc.validation.type; + +import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; +import org.openecomp.sdc.validation.ValidationContext; + +/** + * Created by TALIO on 2/23/2017. + */ +public class NamingConventionValidationContext implements ValidationContext { + + private HeatOrchestrationTemplate heatOrchestrationTemplate; + private String envFileName; + + public NamingConventionValidationContext( + HeatOrchestrationTemplate heatOrchestrationTemplate, + String envFileName) { + this.heatOrchestrationTemplate = heatOrchestrationTemplate; + this.envFileName = envFileName; + } + + public HeatOrchestrationTemplate getHeatOrchestrationTemplate() { + return heatOrchestrationTemplate; + } + + public String getEnvFileName() { + return envFileName; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/util/ValidationTestUtil.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/util/ValidationTestUtil.java new file mode 100644 index 0000000000..3702e9253f --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/util/ValidationTestUtil.java @@ -0,0 +1,192 @@ +package org.openecomp.sdc.validation.util; + +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.io.IOUtils; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.core.validation.types.MessageContainer; +import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; +import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; +import org.openecomp.sdc.heat.datatypes.model.Resource; +import org.openecomp.sdc.heat.services.HeatStructureUtil; +import org.openecomp.sdc.heat.services.manifest.ManifestUtil; +import org.openecomp.sdc.validation.ResourceValidator; +import org.openecomp.sdc.validation.ValidationContext; +import org.openecomp.sdc.validation.Validator; +import org.openecomp.sdc.validation.base.ResourceBaseValidator; +import org.testng.Assert; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.net.URL; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * Created by TALIO on 2/26/2017. + */ +public class ValidationTestUtil { + + public static GlobalValidationContext createGlobalContextFromPath(String path) { + GlobalValidationContext globalValidationContext = new GlobalValidationContext(); + Map<String, byte[]> contentMap = getContentMapByPath(path); + if (contentMap == null) { + return null; + } + contentMap.entrySet() + .forEach(entry -> globalValidationContext.addFileContext(entry.getKey(), entry.getValue())); + + return globalValidationContext; + } + + private static Map<String, byte[]> getContentMapByPath(String path) { + Map<String, byte[]> contentMap = new HashMap<>(); + byte[] fileContent; + FileInputStream fis; + URL url = ValidationTestUtil.class.getResource(path); + File pathFile = new File(url.getFile()); + File[] files; + if (pathFile.isDirectory()) { + files = pathFile.listFiles(); + } else { + files = new File[]{pathFile}; + } + + if (files == null || files.length == 0) { + return null; + } + + for (File file : files) { + try { + fis = new FileInputStream(file); + fileContent = FileUtils.toByteArray(fis); + contentMap.put(file.getName(), fileContent); + } catch (IOException e) { + e.printStackTrace(); + } + } + return contentMap; + } + + public static Map<String, MessageContainer> testValidator(Validator validator, String path) { + + GlobalValidationContext globalValidationContext = createGlobalContextFromPath(path); + validator.validate(globalValidationContext); + + assert globalValidationContext != null; + return globalValidationContext.getContextMessageContainers(); + + + } + + public static Map<String, MessageContainer> testValidator(ResourceBaseValidator baseValidator, + ResourceValidator resourceValidator, + String resourceTypeToValidate, String path) { + + GlobalValidationContext globalContext = createGlobalContextFromPath(path); + ManifestContent manifestContent = ValidationUtil.checkValidationPreCondition(globalContext); + Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent); + Map<String, FileData> fileEnvMap = ManifestUtil.getFileAndItsEnv(manifestContent); + + validateFiles(baseValidator, resourceValidator, globalContext, fileEnvMap, fileTypeMap, + resourceTypeToValidate); + + assert globalContext != null; + return globalContext.getContextMessageContainers(); + + + } + + private static void validateFiles(ResourceBaseValidator baseValidator, + ResourceValidator resourceValidator, + GlobalValidationContext globalContext, + Map<String, FileData> fileEnvMap, + Map<String, FileData.Type> fileTypeMap, + String resourceTypeToValidate) { + + Collection<String> files = globalContext.getFiles(); + for(String fileName : files){ + if(FileData.isHeatFile(fileTypeMap.get(fileName))) { + HeatOrchestrationTemplate heatOrchestrationTemplate = + ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalContext); + + if (Objects.isNull(heatOrchestrationTemplate)) { + continue; + } + + ValidationContext validationContext = baseValidator.createValidationContext + (fileName, + fileEnvMap.get(fileName) == null ? null : fileEnvMap.get(fileName).getFile(), + heatOrchestrationTemplate, globalContext); + + validateResources(fileName, resourceValidator, resourceTypeToValidate, validationContext, + globalContext); + } + } + } + + public static void validateResources(String fileName, + ResourceValidator resourceValidator, + String resourceTypeToValidate, + ValidationContext validationContext, + GlobalValidationContext globalValidationContext){ + + HeatOrchestrationTemplate heatOrchestrationTemplate = + ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalValidationContext); + Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources(); + + if(MapUtils.isEmpty(resourcesMap)){ + return; + } + + resourcesMap.entrySet() + .stream() + .filter(resourceEntry -> isResourceNeedToBeTested(resourceEntry.getValue().getType(), resourceTypeToValidate)) + .forEach(resourceEntry -> + resourceValidator.validate + (fileName, resourceEntry, globalValidationContext, validationContext)); + } + + private static boolean isResourceNeedToBeTested(String currResource, String resourceToTest){ + if(Objects.isNull(resourceToTest)){ + return HeatStructureUtil.isNestedResource(currResource); + } + + return currResource.equals(resourceToTest); + } + + public static void validateErrorMessage(String actualMessage, String expected, String... params) { + Assert.assertEquals(actualMessage.replace("\n", "").replace("\r", ""), + ErrorMessagesFormatBuilder.getErrorWithParameters(expected, params).replace("\n", "") + .replace("\r", "")); + + } + + public static Validator cerateValidatorImpl(ImplementationConfiguration validatorConf) { + Validator validator = null; + try { + validator = + CommonMethods.newInstance(validatorConf.getImplementationClass(), Validator.class); + validator.init(validatorConf.getProperties()); + } catch (IllegalArgumentException iae) { + return null; + } + return validator; + } + + public static Map<String, Object> getResourceMap(String configFileName) throws IOException { + URL mockResource = ValidationTestUtil.class.getResource(configFileName); + StringWriter writer = new StringWriter(); + String json = IOUtils.toString(mockResource.openStream(), "UTF-8"); + return JsonUtil.json2Object(json, Map.class); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/util/ValidationUtil.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/util/ValidationUtil.java new file mode 100644 index 0000000000..ee15df65cf --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-sdk/src/main/java/org/openecomp/sdc/validation/util/ValidationUtil.java @@ -0,0 +1,277 @@ +package org.openecomp.sdc.validation.util; + + +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.core.utilities.yaml.YamlUtil; +import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.common.utils.SdcCommon; +import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; +import org.openecomp.sdc.heat.datatypes.model.Environment; +import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; +import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes; +import org.openecomp.sdc.heat.datatypes.model.Resource; +import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions; +import org.openecomp.sdc.heat.services.HeatStructureUtil; +import org.openecomp.sdc.heat.services.manifest.ManifestUtil; +import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; +import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage; +import org.openecomp.sdc.logging.types.LoggerConstants; +import org.openecomp.sdc.logging.types.LoggerErrorCode; +import org.openecomp.sdc.logging.types.LoggerErrorDescription; +import org.openecomp.sdc.logging.types.LoggerTragetServiceName; +import org.openecomp.sdc.validation.ResourceValidator; + +import java.io.InputStream; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Pattern; + +import static java.util.Objects.nonNull; + +public class ValidationUtil { + + private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); + + public static void removeExposedResourcesCalledByGetResource(String fileName, + Set<String> actualExposedResources, + HeatOrchestrationTemplate + heatOrchestrationTemplate, + GlobalValidationContext globalContext) { + Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources(); + + for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) { + Set<String> referencedResources = + HeatStructureUtil.getReferencedValuesByFunctionName(fileName, ResourceReferenceFunctions + .GET_RESOURCE + .getFunction(), resourceEntry.getValue().getProperties(), globalContext); + + removeExposedResourcesCalledByGetResource(referencedResources, actualExposedResources, + resourcesMap); + } + } + + private static void removeExposedResourcesCalledByGetResource(Set<String> referencedResources, + Set<String> + actualExposedResources, + Map<String, Resource> resourcesMap) { + for (String referencedResourceName : referencedResources) { + Resource currResource = resourcesMap.get(referencedResourceName); + if (Objects.nonNull(currResource)) { + if (isExpectedToBeExposed(currResource.getType())) { + actualExposedResources.add(referencedResourceName); + } + } + } + } + + public static boolean isExpectedToBeExposed(String type) { + return HeatResourcesTypes.isResourceExpectedToBeExposed(type); + } + + public static String getWantedNameFromPropertyValueGetParam(Object value) { + Set<String> paramName = HeatStructureUtil + .getReferencedValuesByFunctionName(null, ResourceReferenceFunctions.GET_PARAM.getFunction(), + value, null); + if (paramName != null && CollectionUtils.isNotEmpty(paramName)) { + return (String) paramName.toArray()[0]; + } + return null; + } + + public static boolean evalPattern(Object paramVal, String[] regexList) { + String value = ""; + if (paramVal instanceof String) { + value = ((String) paramVal); + } + if (paramVal instanceof Integer) { + value = paramVal.toString(); + } + return evalPattern(value, regexList); + } + + private static boolean evalPattern(String paramVal, String[] regexList) { + + for (String regex : regexList) { + if (Pattern.matches(regex, paramVal)) { + return true; + } + } + + return false; + } + + public static String getMessagePartAccordingToResourceType(Map.Entry<String, Resource> + resourceEntry) { + HeatResourcesTypes resourcesType = + HeatResourcesTypes.findByHeatResource(resourceEntry.getValue().getType()); + if (resourcesType == HeatResourcesTypes.NOVA_SERVER_RESOURCE_TYPE) { + return "Server"; + } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_TEMPLATE) { + return "Service Template"; + } else if (resourcesType == HeatResourcesTypes.CONTRAIL_SERVICE_INSTANCE) { + return "Service Instance"; + } else { + return ""; + } + } + + public static Environment validateEnvContent(String envFileName, + GlobalValidationContext globalContext) { + + mdcDataDebugMessage.debugEntryMessage("file", envFileName); + + Environment envContent; + try { + Optional<InputStream> fileContent = globalContext.getFileContent(envFileName); + if (fileContent.isPresent()) { + envContent = new YamlUtil().yamlToObject(fileContent.get(), Environment.class); + } else { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API, + LoggerTragetServiceName.VALIDATE_ENV_FILE, ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.EMPTY_FILE); + throw new Exception("The file '" + envFileName + "' has no content"); + } + } catch (Exception exception) { + mdcDataDebugMessage.debugExitMessage("file", envFileName); + return null; + } + mdcDataDebugMessage.debugExitMessage("file", envFileName); + return envContent; + } + + public static boolean validateMapPropertyValue(String fileName, + Map.Entry<String, Resource> resourceEntry, + GlobalValidationContext globalContext, + String propertyName, Object nameValue, + String[] regexList) { + + mdcDataDebugMessage.debugEntryMessage("file", fileName); + + String propertyValue = getWantedNameFromPropertyValueGetParam(nameValue); + if (nonNull(propertyValue)) { + if (!evalPattern(propertyValue, regexList)) { + globalContext.addMessage( + fileName, + ErrorLevel.WARNING, + ErrorMessagesFormatBuilder.getErrorWithParameters( + Messages.PARAMETER_NAME_NOT_ALIGNED_WITH_GUIDELINES.getErrorMessage(), + getMessagePartAccordingToResourceType(resourceEntry), propertyName, propertyValue, + resourceEntry.getKey()), + LoggerTragetServiceName.VALIDATE_IMAGE_AND_FLAVOR_NAME, + LoggerErrorDescription.NAME_NOT_ALIGNED_WITH_GUIDELINES); + mdcDataDebugMessage.debugExitMessage("file", fileName); + return true; + } + } + + mdcDataDebugMessage.debugExitMessage("file", fileName); + return false; + } + + public static ManifestContent checkValidationPreCondition(GlobalValidationContext globalContext) { + Optional<InputStream> manifest = globalContext.getFileContent(SdcCommon.MANIFEST_NAME); + if (!manifest.isPresent()) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API, + LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.MISSING_FILE); + throw new RuntimeException("Can't load manifest file for Heat Validator"); + } + ManifestContent manifestContent; + try { + manifestContent = JsonUtil.json2Object(manifest.get(), ManifestContent.class); + } catch (Exception exception) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_API, + LoggerTragetServiceName.VALIDATE_MANIFEST_CONTENT, ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.INVALID_MANIFEST); + throw new RuntimeException("Can't load manifest file for Heat Validator"); + } + + return manifestContent; + } + + public static String getParserExceptionReason(Exception exception) { + String reason; + + if (exception.getCause() != null && exception.getCause().getCause() != null) { + reason = exception.getCause().getCause().getMessage(); + } else if (exception.getCause() != null) { + reason = exception.getCause().getMessage(); + } else { + reason = Messages.GENERAL_HEAT_PARSER_ERROR.getErrorMessage(); + } + return reason; + } + + public static HeatOrchestrationTemplate checkHeatOrchestrationPreCondition(String fileName, + GlobalValidationContext globalContext) { + + mdcDataDebugMessage.debugEntryMessage("file", fileName); + + HeatOrchestrationTemplate heatOrchestrationTemplate; + try { + Optional<InputStream> fileContent = globalContext.getFileContent(fileName); + if (fileContent.isPresent()) { + heatOrchestrationTemplate = + new YamlUtil().yamlToObject(fileContent.get(), HeatOrchestrationTemplate.class); + } else { + heatOrchestrationTemplate = null; + } + } catch (Exception exception) { + globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder + .getErrorWithParameters(Messages.INVALID_HEAT_FORMAT_REASON.getErrorMessage(), + getParserExceptionReason(exception)), + LoggerTragetServiceName.VALIDATE_HEAT_FORMAT, + LoggerErrorDescription.INVALID_HEAT_FORMAT); + mdcDataDebugMessage.debugExitMessage("file", fileName); + return null; + } + mdcDataDebugMessage.debugExitMessage("file", fileName); + return heatOrchestrationTemplate; + } + + public static Set<String> validateManifest(ManifestContent manifestContent, + GlobalValidationContext globalContext) { + + mdcDataDebugMessage.debugEntryMessage("file", SdcCommon.MANIFEST_NAME); + + Set<String> baseFiles = ManifestUtil.getBaseFiles(manifestContent); + if (baseFiles == null || baseFiles.size() == 0) { + globalContext.addMessage( + SdcCommon.MANIFEST_NAME, + ErrorLevel.WARNING, + ErrorMessagesFormatBuilder + .getErrorWithParameters(Messages.MISSIN_BASE_HEAT_FILE.getErrorMessage()), + LoggerTragetServiceName.VALIDATE_BASE_FILE, + LoggerErrorDescription.MISSING_BASE_HEAT); + } else if (baseFiles.size() > 1) { + String baseFileList = getElementListAsString(baseFiles); + globalContext.addMessage( + SdcCommon.MANIFEST_NAME, + ErrorLevel.WARNING, + ErrorMessagesFormatBuilder + .getErrorWithParameters(Messages.MULTI_BASE_HEAT_FILE.getErrorMessage(), + baseFileList), + LoggerTragetServiceName.VALIDATE_BASE_FILE, + LoggerErrorDescription.MULTI_BASE_HEAT); + } + + mdcDataDebugMessage.debugExitMessage("file", SdcCommon.MANIFEST_NAME); + return baseFiles; + } + + private static String getElementListAsString(Set<String> elementCollection) { + + return "[" + + CommonMethods.collectionToCommaSeparatedString(elementCollection) + + "]"; + } + +} |