diff options
author | Adam Wudzinski <adam.wudzinski@nokia.com> | 2020-11-27 12:50:27 +0100 |
---|---|---|
committer | Vasyl Razinkov <vasyl.razinkov@est.tech> | 2020-12-02 12:59:56 +0000 |
commit | ad99b805537fc8f3103fa5646abe5342924e10cd (patch) | |
tree | 266fbadbef9ea2412455b907e9ac688f9e746d60 /openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java | |
parent | e7f2dfba0cb4d917c8da83c319b47ae279a6151e (diff) |
[SDC] PM_Dictionary file validation for ZIP onboarding
Use PM_Dictionary validator from VNF-SDK to validate PM_Dictionary files when onboarding ZIP package
Issue-ID: SDC-3390
Signed-off-by: Adam Wudzinski <adam.wudzinski@nokia.com>
Change-Id: I03fd622393d675977527b9845b6cf8b87b2ec0a3
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java')
3 files changed, 154 insertions, 4 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 new file mode 100644 index 0000000000..c49062acc0 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/FileExtensionUtils.java @@ -0,0 +1,55 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 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 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", + ".env" + ); + + 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() + .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/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 new file mode 100644 index 0000000000..f5338c5d3b --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-impl/src/main/java/org/openecomp/sdc/validation/impl/validators/PmDictionaryValidator.java @@ -0,0 +1,94 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 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 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; +import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder; +import org.openecomp.core.validation.types.GlobalValidationContext; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.validation.Validator; + +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); + } + + private static class ValidationHelper { + + private final GlobalValidationContext globalContext; + private final String fileName; + + private ValidationHelper(GlobalValidationContext globalContext, String fileName) { + this.globalContext = globalContext; + this.fileName = fileName; + } + + public void validate() { + Option.ofOptional(globalContext.getFileContent(fileName)) + .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()))); + } + + private void reportValidationErrorsIfPresent(List<YamlDocumentValidationError> validationErrors) { + validationErrors.stream() + .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() + ); + return formatMessage(errorMessage); + } + + private String formatMessage(String message) { + return ErrorMessagesFormatBuilder + .getErrorWithParameters(PM_DICT_ERROR_CODE, message); + } + + private void addErrorToContext(String message) { + globalContext.addMessage(fileName, ErrorLevel.ERROR, 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 fba39e063e..4196ad2929 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,5 +1,6 @@ /* * Copyright © 2016-2017 European Support Limited + * Copyright © 2020 Nokia * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,15 +38,15 @@ public class YamlValidator implements Validator { @Override public void validate(GlobalValidationContext globalContext) { Collection<String> files = globalContext.files( - (fileName, globalValidationContext) -> fileName.endsWith(".yaml") - || fileName.endsWith(".yml") || fileName.endsWith(".env")); + (fileName, globalValidationContext) -> FileExtensionUtils.isYaml(fileName) + && !FileExtensionUtils.isPmDictionary(fileName)); - files.stream().forEach(fileName -> validate(fileName, globalContext)); + files.forEach(fileName -> validate(fileName, globalContext)); } private void validate(String fileName, GlobalValidationContext globalContext) { Optional<InputStream> rowContent = globalContext.getFileContent(fileName); - if (!rowContent.isPresent()) { + if (rowContent.isEmpty()) { globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder .getErrorWithParameters(ERROR_CODE_YML_1, Messages .INVALID_YAML_FORMAT_REASON.getErrorMessage(), |