From 4ce8c75c2559bde35b5fca46ec2c94d6b5070b6f Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Thu, 9 Jul 2020 14:02:23 +0200 Subject: use YAML schema validator in rule R816745 Issue-ID: VNFSDK-594 Signed-off-by: Bartosz Gardziejewski Change-Id: I70c6150662b69833d4e190a6adc1047840a78975 --- .../cvc/csar/cc/sol004/VTPValidateCSARR816745.java | 74 ++++++++++++++++++++-- .../onap/validation/yaml/YamlFileValidator.java | 72 +++++++++++++++++++++ .../java/org/onap/validation/yaml/YamlLoader.java | 14 +++- .../org/onap/validation/yaml/YamlValidator.java | 2 +- .../yaml/error/SchemaValidationError.java | 36 +++++++++++ .../yaml/error/YamlDocumentValidationError.java | 42 ++++++++++++ .../yaml/exception/YamlProcessingException.java | 4 ++ .../yaml/model/SchemaValidationError.java | 36 ----------- .../yaml/process/YamlValidationProcess.java | 2 +- .../yaml/schema/node/YamlSchemaBranchNode.java | 3 - .../yaml/schema/node/YamlSchemaLeafNode.java | 2 +- 11 files changed, 238 insertions(+), 49 deletions(-) create mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java (limited to 'csarvalidation/src/main/java/org') diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java index 47b963e..f1bbffc 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java @@ -19,20 +19,58 @@ package org.onap.cvc.csar.cc.sol004; import org.onap.cli.fw.schema.OnapCommandSchema; import org.onap.cvc.csar.CSARArchive; import org.onap.cvc.csar.cc.VTPValidateCSARBase; +import org.onap.validation.yaml.YamlFileValidator; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.onap.validation.yaml.exception.YamlProcessingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.nio.file.Path; import java.util.List; import java.util.Map; @OnapCommandSchema(schema = "vtp-validate-csar-r816745.yaml") public class VTPValidateCSARR816745 extends VTPValidateCSARBase { + private static final Logger LOGGER = LoggerFactory.getLogger(VTPValidateCSARR816745.class); + + private static class CSARPmDictionaryValidationError extends CSARArchive.CSARError { + + CSARPmDictionaryValidationError(int documentNumber, String file, String path, String message) { + super("0x1000"); + this.message = String.format( + "Invalid YAML document in PM_Dictionary file. %n" + + "In document number %s (excluding document with schema) error occur. %n" + + "Path: %s%n" + + "%s", + documentNumber, path, message + ); + this.file = file; + } + + } + + private static class CSARPmDictionaryLoadingError extends CSARArchive.CSARError { + + CSARPmDictionaryLoadingError(String file, String message) { + super("0x2000"); + this.message = String.format( + "Fail to load PM_Dictionary With error: %s", + message + ); + this.file = file; + } + + } + private static final String PM_DICTIONARY = "onap_pm_dictionary"; @Override - protected void validateCSAR(CSARArchive csar) throws Exception { + protected void validateCSAR(CSARArchive csar) { Map>> nonManoFields = csar.getManifest().getNonMano(); - if(nonManoFields.containsKey(PM_DICTIONARY)) { - validateYamlFile(getLocationOfPmDictionaryFile(nonManoFields)); + String rootPath = csar.getWorkspace().getPathToCsarFolder().map(Path::toString).orElse("/"); + if (nonManoFields.containsKey(PM_DICTIONARY)) { + validateYamlFile(rootPath+"/",getLocationOfPmDictionaryFile(nonManoFields)); } } @@ -40,8 +78,34 @@ public class VTPValidateCSARR816745 extends VTPValidateCSARBase { return nonManoFields.get(PM_DICTIONARY).get("source").get(0); } - private void validateYamlFile(String path) { - throw new UnsupportedOperationException("Under development"); + private void validateYamlFile(String rootPath, String artifactPath) { + try { + List validationErrors = + new YamlFileValidator().validateYamlFileWithSchema(rootPath+artifactPath); + addAllErrorsReportedByVaidator(artifactPath, validationErrors); + } catch (YamlProcessingException e) { + LOGGER.error("Failed to load PM_Dictionary file.", e); + errors.add(new CSARPmDictionaryLoadingError( + artifactPath, + e.getMessage() + )); + } + + } + + private void addAllErrorsReportedByVaidator(String artifactPath, List validationErrors) { + for(YamlDocumentValidationError validationError: validationErrors) { + addPmDictionaryValidationError(artifactPath, validationError); + } + } + + private void addPmDictionaryValidationError(String artifactPath, YamlDocumentValidationError validationError) { + errors.add(new CSARPmDictionaryValidationError( + validationError.getYamlDocumentNumber(), + artifactPath, + validationError.getPath(), + validationError.getMessage() + )); } @Override diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java new file mode 100644 index 0000000..2de4f48 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java @@ -0,0 +1,72 @@ +/* + * 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. + * 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.onap.validation.yaml; + +import org.onap.validation.yaml.error.SchemaValidationError; +import org.onap.validation.yaml.error.YamlDocumentValidationError; +import org.onap.validation.yaml.exception.YamlProcessingException; +import org.onap.validation.yaml.model.YamlDocument; +import org.onap.validation.yaml.schema.YamlSchema; +import org.onap.validation.yaml.schema.YamlSchemaFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class YamlFileValidator { + + private static final int FIRST_DOCUMENT_INDEX = 1; + + public List validateYamlFileWithSchema(String pathToFile) + throws YamlProcessingException { + + List documents = new YamlLoader().loadMultiDocumentYamlFile(pathToFile); + if(!documents.isEmpty()) { + return validateDocuments(documents); + } else { + throw new YamlProcessingException("PM_Dictionary YAML file is empty"); + } + } + + private List validateDocuments(List documents) + throws YamlProcessingException { + + List yamlFileValidationErrors = new ArrayList<>(); + YamlSchema schema = extractSchema(documents); + YamlValidator validator = new YamlValidator(schema); + + for (int index = FIRST_DOCUMENT_INDEX; index < documents.size(); index++) { + List validationErrors = validator.validate(documents.get(index)); + yamlFileValidationErrors.addAll(transformErrors(index,validationErrors)); + } + + return yamlFileValidationErrors; + } + + private List transformErrors(int index, List validationErrors) { + return validationErrors + .stream() + .map(error->new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) + .collect(Collectors.toList()); + } + + private YamlSchema extractSchema(List documents) throws YamlProcessingException { + return new YamlSchemaFactory().createTreeStructuredYamlSchema(documents.get(0)); + } + +} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java index 804f1ea..1a5eef9 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java @@ -17,6 +17,7 @@ package org.onap.validation.yaml; +import org.onap.validation.yaml.exception.YamlProcessingException; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.model.YamlDocumentFactory; import org.slf4j.Logger; @@ -25,15 +26,16 @@ import org.yaml.snakeyaml.Yaml; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; -public class YamlLoader { +class YamlLoader { private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); - public List loadMultiDocumentYamlFile(URL path) + List loadMultiDocumentYamlFile(URL path) throws YamlDocumentFactory.YamlDocumentParsingException { List documentsFromFile = new ArrayList<>(); try (InputStream yamlStream = path.openStream()) { @@ -48,4 +50,12 @@ public class YamlLoader { return documentsFromFile; } + List loadMultiDocumentYamlFile(String path) + throws YamlProcessingException { + try { + return loadMultiDocumentYamlFile(new URL("file://" + path)); + } catch (MalformedURLException e) { + throw new YamlProcessingException("Fail to read file under given path.", e); + } + } } diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java index 17ebd1f..9430df4 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java @@ -18,7 +18,7 @@ package org.onap.validation.yaml; import org.onap.validation.yaml.exception.YamlProcessingException; -import org.onap.validation.yaml.model.SchemaValidationError; +import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.process.YamlValidationProcess; import org.onap.validation.yaml.schema.YamlSchema; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java new file mode 100644 index 0000000..6ffe6d4 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java @@ -0,0 +1,36 @@ +/* + * 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. + * 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.onap.validation.yaml.error; + +public class SchemaValidationError { + private final String path; + private final String message; + + public String getPath() { + return path; + } + + public String getMessage() { + return message; + } + + public SchemaValidationError(String path, String message) { + this.path = path; + this.message = message; + } +} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java new file mode 100644 index 0000000..f04708f --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java @@ -0,0 +1,42 @@ +/* + * 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. + * 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.onap.validation.yaml.error; + +public class YamlDocumentValidationError { + private final int yamlDocumentNumber; + private final String path; + private final String message; + + public YamlDocumentValidationError(int yamlDocumentNumber, String path, String message) { + this.yamlDocumentNumber = yamlDocumentNumber; + this.path = path; + this.message = message; + } + + public int getYamlDocumentNumber() { + return yamlDocumentNumber; + } + + public String getPath() { + return path; + } + + public String getMessage() { + return message; + } +} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java b/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java index edfdbb8..99c2437 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java @@ -23,6 +23,10 @@ public class YamlProcessingException extends Exception { super(message, throwable); } + public YamlProcessingException(String message) { + super(message); + } + public YamlProcessingException(Throwable throwable) { super(throwable); } diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java deleted file mode 100644 index 45f1647..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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. - * 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.onap.validation.yaml.model; - -public class SchemaValidationError { - private final String path; - private final String message; - - public String getPath() { - return path; - } - - public String getMessage() { - return message; - } - - public SchemaValidationError(String path, String message) { - this.path = path; - this.message = message; - } -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java b/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java index f55b729..e3fadb6 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java @@ -18,7 +18,7 @@ package org.onap.validation.yaml.process; import org.onap.validation.yaml.exception.YamlProcessingException; -import org.onap.validation.yaml.model.SchemaValidationError; +import org.onap.validation.yaml.error.SchemaValidationError; import org.onap.validation.yaml.model.YamlDocument; import org.onap.validation.yaml.schema.YamlSchema; import org.onap.validation.yaml.schema.node.YamlSchemaNode; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java index 8ff3569..0f5b480 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java @@ -26,9 +26,6 @@ import java.util.Collections; import java.util.List; import java.util.Optional; -import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException; -import static org.onap.validation.yaml.model.YamlParameterListFactory.YamlParameterListParsingException; - public class YamlSchemaBranchNode extends YamlSchemaNode { private final YamlDocument nextNodesInLazyForm; diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java index 73470d4..c98f41e 100644 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java +++ b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java @@ -24,7 +24,7 @@ import java.util.List; public class YamlSchemaLeafNode extends YamlSchemaNode { - private YamlParametersList acceptedValues; + private final YamlParametersList acceptedValues; YamlSchemaLeafNode(String name, String path, boolean required, String comment, YamlParametersList acceptedValues) { -- cgit 1.2.3-korg