diff options
author | 2021-01-15 17:38:30 +0100 | |
---|---|---|
committer | 2021-01-18 16:58:27 +0100 | |
commit | f3b0ef4dc7cc21b273ea160781b5170b2d105e1a (patch) | |
tree | 04550aed49f28b599d2702a519ad7d51a855aac8 /openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org | |
parent | bd5a1006210092f9ac5c48352cc94f6264e961ef (diff) |
Map VSP PM_DICTIONARY Type to VF PM_DICTIONARY Type
File defined in ZIP VSP package as PM_DICTIONARY will be now mapped to PM_DICTIONARY type in VF. Also PmDictionaryValidator is run on files with PM_DICTIONARY type in ZIP Manifest file, instead of file naming convention.
Issue-ID: SDC-3390
Signed-off-by: Adam Wudzinski <adam.wudzinski@nokia.com>
Change-Id: I2e21353b9e80b6bb68c4c6d408ad1ffa33314e7b
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org')
4 files changed, 100 insertions, 39 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/FileExtensionUtils.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/FileExtensionUtils.java index c49062acc0..c4215c9783 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/FileExtensionUtils.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/FileExtensionUtils.java @@ -23,16 +23,6 @@ import java.util.Set; class FileExtensionUtils { - private static final Set<String> VALID_PM_DICTIONARY_EXTENSIONS = Set.of( - "pmdict.yml", - "pmdict.yaml", - "pm_dict.yml", - "pm_dict.yaml", - "pmdictionary.yml", - "pmdictionary.yaml", - "pm_dictionary.yml", - "pm_dictionary.yaml" - ); private static final Set<String> VALID_YAML_EXTENSIONS = Set.of( ".yaml", ".yml", @@ -40,16 +30,9 @@ class FileExtensionUtils { ); static boolean isYaml(String fileName) { - return isValidExt(fileName, VALID_YAML_EXTENSIONS); - } - - static boolean isPmDictionary(String fileName) { - return isValidExt(fileName, VALID_PM_DICTIONARY_EXTENSIONS); - } - - private static boolean isValidExt(String fileName, Set<String> validExtensions) { String fileNameLower = fileName.toLowerCase(); - return validExtensions.stream() + return FileExtensionUtils.VALID_YAML_EXTENSIONS.stream() .anyMatch(fileNameLower::endsWith); } + } diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/GlobalContextUtil.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/GlobalContextUtil.java new file mode 100644 index 0000000000..4467bd1311 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/GlobalContextUtil.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2021 Nokia 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.impl.validators; + +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.sdc.heat.datatypes.manifest.FileData; +import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent; +import org.openecomp.sdc.heat.services.manifest.ManifestUtil; +import org.openecomp.sdc.validation.util.ValidationUtil; + +import java.util.Map; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +class GlobalContextUtil { + + private GlobalContextUtil() {} + + static Set<String> findPmDictionaryFiles(GlobalValidationContext globalContext) { + if (isManifestMissing(globalContext)) { + return Set.of(); + } + + Map<String, FileData.Type> filesWithTypes = readAllFilesWithTypes(globalContext); + return filterPmDictionaryFiles(filesWithTypes); + } + + private static boolean isManifestMissing(GlobalValidationContext globalContext) { + return globalContext.getFileContent("MANIFEST.json") + .isEmpty(); + } + + private static Set<String> filterPmDictionaryFiles(Map<String, FileData.Type> filesWithTypes) { + return filesWithTypes.entrySet().stream() + .filter(isPmDictionaryType()) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); + } + + private static Map<String, FileData.Type> readAllFilesWithTypes(GlobalValidationContext globalContext) { + ManifestContent manifestContent = ValidationUtil.validateManifest(globalContext); + return ManifestUtil.getFileTypeMap(manifestContent); + } + + private static Predicate<Map.Entry<String, FileData.Type>> isPmDictionaryType() { + return entry -> entry.getValue() + .equals(FileData.Type.PM_DICTIONARY); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/PmDictionaryValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/PmDictionaryValidator.java index f5338c5d3b..05021fdf4f 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/PmDictionaryValidator.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/PmDictionaryValidator.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * SDC * ================================================================================ - * Copyright (C) 2020 Nokia Intellectual Property. All rights reserved. + * Copyright (C) 2020-2021 Nokia 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. @@ -22,8 +22,6 @@ package org.openecomp.sdc.validation.impl.validators; import io.vavr.control.Option; import io.vavr.control.Try; -import java.io.InputStream; -import java.util.List; import org.onap.validation.yaml.YamlContentValidator; import org.onap.validation.yaml.error.YamlDocumentValidationError; import org.openecomp.core.validation.ErrorMessageCode; @@ -32,16 +30,25 @@ import org.openecomp.core.validation.types.GlobalValidationContext; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.validation.Validator; +import java.io.InputStream; +import java.util.List; +import java.util.Set; + public class PmDictionaryValidator implements Validator { private static final ErrorMessageCode PM_DICT_ERROR_CODE = new ErrorMessageCode("PM_DICT"); @Override public void validate(GlobalValidationContext globalContext) { - globalContext.getFiles().stream() - .filter(FileExtensionUtils::isPmDictionary) - .map(fileName -> new ValidationHelper(globalContext, fileName)) - .forEach(ValidationHelper::validate); + Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext); + validatePmDictionaryFiles(globalContext, pmDictionaryFiles); + } + + + private void validatePmDictionaryFiles(GlobalValidationContext globalContext, Set<String> pmDictionaryFiles) { + pmDictionaryFiles.stream() + .map(fileName -> new ValidationHelper(globalContext, fileName)) + .forEach(ValidationHelper::validate); } private static class ValidationHelper { @@ -56,35 +63,35 @@ public class PmDictionaryValidator implements Validator { public void validate() { Option.ofOptional(globalContext.getFileContent(fileName)) - .peek(this::validateFileContent) - .onEmpty(() -> addErrorToContext(formatMessage("File is empty"))); + .peek(this::validateFileContent) + .onEmpty(() -> addErrorToContext(formatMessage("File is empty"))); } private void validateFileContent(InputStream inputStream) { Try.of(inputStream::readAllBytes) - .mapTry(fileContent -> new YamlContentValidator().validate(fileContent)) - .onSuccess(this::reportValidationErrorsIfPresent) - .onFailure(e -> addErrorToContext(formatMessage(e.getMessage()))); + .mapTry(fileContent -> new YamlContentValidator().validate(fileContent)) + .onSuccess(this::reportValidationErrorsIfPresent) + .onFailure(e -> addErrorToContext(formatMessage(e.getMessage()))); } private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) { validationErrors.stream() - .map(this::prepareValidationMessage) - .forEach(this::addErrorToContext); + .map(this::prepareValidationMessage) + .forEach(this::addErrorToContext); } private String prepareValidationMessage(YamlDocumentValidationError error) { final String errorMessage = String.format("Document Number: %s, Path: %s, Problem: %s", - error.getYamlDocumentNumber(), - error.getPath(), - error.getMessage() + error.getYamlDocumentNumber(), + error.getPath(), + error.getMessage() ); return formatMessage(errorMessage); } private String formatMessage(String message) { return ErrorMessagesFormatBuilder - .getErrorWithParameters(PM_DICT_ERROR_CODE, message); + .getErrorWithParameters(PM_DICT_ERROR_CODE, message); } private void addErrorToContext(String message) { diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/YamlValidator.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/YamlValidator.java index 4196ad2929..b11ff5e341 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/YamlValidator.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/YamlValidator.java @@ -1,6 +1,6 @@ /* * Copyright © 2016-2017 European Support Limited - * Copyright © 2020 Nokia + * Copyright © 2020-2021 Nokia * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import java.io.InputStream; import java.util.Collection; import java.util.Map; import java.util.Optional; +import java.util.Set; public class YamlValidator implements Validator { private static final ErrorMessageCode ERROR_CODE_YML_1 = new ErrorMessageCode("YML1"); @@ -37,9 +38,11 @@ public class YamlValidator implements Validator { @Override public void validate(GlobalValidationContext globalContext) { + Set<String> pmDictionaryFiles = GlobalContextUtil.findPmDictionaryFiles(globalContext); + Collection<String> files = globalContext.files( (fileName, globalValidationContext) -> FileExtensionUtils.isYaml(fileName) - && !FileExtensionUtils.isPmDictionary(fileName)); + && !pmDictionaryFiles.contains(fileName)); files.forEach(fileName -> validate(fileName, globalContext)); } |