From 3fed3de85164c56081075dece8695d7f84ae48af Mon Sep 17 00:00:00 2001 From: Pawel Date: Wed, 25 Nov 2020 12:53:49 +0100 Subject: Add possibility to validation pm-dictionary from byte array Issue-ID: VNFSDK-713 Signed-off-by: Pawel Change-Id: I2d2de2cfe066e1cbbdea3bc8fac771914779e117 --- .../onap/validation/yaml/YamlContentValidator.java | 83 ++++++++++++++++++++++ .../onap/validation/yaml/YamlFileValidator.java | 72 ------------------- .../java/org/onap/validation/yaml/YamlLoader.java | 43 ++++++++--- .../yaml/schema/node/YamlSchemaBranchNode.java | 8 +-- 4 files changed, 118 insertions(+), 88 deletions(-) create mode 100644 pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java delete mode 100644 pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java (limited to 'pmdictionaryvalidation/src/main/java/org') diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java new file mode 100644 index 0000000..5e1238b --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlContentValidator.java @@ -0,0 +1,83 @@ +/* + * 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.model.YamlDocumentFactory; +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 YamlContentValidator { + + private static final int FIRST_DOCUMENT_INDEX = 1; + private static final YamlLoader YAML_LOADER = new YamlLoader(new YamlDocumentFactory()); + + public List validate(String pathToFile) + throws YamlProcessingException { + List documents = YAML_LOADER.loadMultiDocumentYamlFile(pathToFile); + return getYamlDocumentValidationErrors(documents); + } + + public List validate(byte[] yamlWithSchema) + throws YamlProcessingException { + List documents = YAML_LOADER.loadMultiDocumentYaml(yamlWithSchema); + return getYamlDocumentValidationErrors(documents); + } + + private List getYamlDocumentValidationErrors(List documents) throws YamlProcessingException { + if (documents.isEmpty()) { + throw new YamlProcessingException("PM_Dictionary YAML file is empty"); + } else { + return validateDocuments(documents); + } + } + + 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java deleted file mode 100644 index 2de4f48..0000000 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java +++ /dev/null @@ -1,72 +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; - -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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java index 1a5eef9..c23da0a 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java @@ -24,6 +24,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -33,29 +34,49 @@ import java.util.List; class YamlLoader { - private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); + private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); + private final YamlDocumentFactory documentFactory; + + YamlLoader(YamlDocumentFactory documentFactory) { + this.documentFactory = documentFactory; + } + + List loadMultiDocumentYaml(byte[] yamlWithSchema) + throws YamlDocumentFactory.YamlDocumentParsingException { + List documents = new ArrayList<>(); + try (InputStream yamlStream = new ByteArrayInputStream(yamlWithSchema)) { + documents.addAll(loadMultiDocumentYaml(yamlStream)); + } catch (IOException e) { + LOGGER.error("Failed to load multi document YAML", e); + } + return documents; + } List loadMultiDocumentYamlFile(URL path) - throws YamlDocumentFactory.YamlDocumentParsingException { - List documentsFromFile = new ArrayList<>(); + throws YamlDocumentFactory.YamlDocumentParsingException { + List documents = new ArrayList<>(); try (InputStream yamlStream = path.openStream()) { - for (Object yamlDocument : new Yaml().loadAll(yamlStream)) { - documentsFromFile.add( - new YamlDocumentFactory().createYamlDocument(yamlDocument) - ); - } + documents.addAll(loadMultiDocumentYaml(yamlStream)); } catch (IOException e) { - LOGGER.error("Failed to load multi document YAML file",e); + LOGGER.error("Failed to load multi document YAML file", e); } - return documentsFromFile; + return documents; } List loadMultiDocumentYamlFile(String path) - throws YamlProcessingException { + throws YamlProcessingException { try { return loadMultiDocumentYamlFile(new URL("file://" + path)); } catch (MalformedURLException e) { throw new YamlProcessingException("Fail to read file under given path.", e); } } + + private List loadMultiDocumentYaml(InputStream yamlStream) throws YamlDocumentFactory.YamlDocumentParsingException { + List documents = new ArrayList<>(); + for (Object yamlDocument : new Yaml().loadAll(yamlStream)) { + documents.add(documentFactory.createYamlDocument(yamlDocument)); + } + return documents; + } } diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java index 0f5b480..a3e9636 100644 --- a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java @@ -24,18 +24,16 @@ import org.onap.validation.yaml.model.YamlDocumentFactory; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Optional; public class YamlSchemaBranchNode extends YamlSchemaNode { private final YamlDocument nextNodesInLazyForm; - private Optional> nextNodes; + private List nextNodes = Collections.emptyList(); YamlSchemaBranchNode(String name, String path, boolean required, String comment, YamlDocument nextNodesInLazyForm) { super(name, path, required, comment); this.nextNodesInLazyForm = nextNodesInLazyForm; - this.nextNodes = Optional.empty(); } @Override @@ -51,7 +49,7 @@ public class YamlSchemaBranchNode extends YamlSchemaNode { @Override public synchronized List getNextNodes() throws YamlSchemaProcessingException { try { - return nextNodes.orElseGet(this::loadNextNodes); + return nextNodes.isEmpty() ? this.loadNextNodes() : nextNodes; } catch (YamlSchemaLazyLoadingException lazyLoadingException) { throw new YamlSchemaProcessingException(lazyLoadingException); } @@ -65,7 +63,7 @@ public class YamlSchemaBranchNode extends YamlSchemaNode { .createYamlDocument(nextNodesInLazyForm.getYaml().get(key)); loadedNextNodes.add(new YamlSchemaNodeFactory().createNode(key, getPath() + getName() + "/", substructure)); } - nextNodes = Optional.of(loadedNextNodes); + nextNodes = loadedNextNodes; return loadedNextNodes; } catch (YamlProcessingException e) { throw new YamlSchemaLazyLoadingException("Lazy loading failed, due to yaml parsing exception.",e); -- cgit 1.2.3-korg