From b6ff67fa1d21765f2f52f3643946c96b2f13aa07 Mon Sep 17 00:00:00 2001 From: Pawel Date: Thu, 12 Nov 2020 12:15:55 +0100 Subject: Extract pm-dicrionary validation Issue-ID: VNFSDK-713 Signed-off-by: Pawel Change-Id: Iee5a23a3a6c9215927aa2c453faab62d30453444 --- .../onap/validation/yaml/YamlFileValidator.java | 72 ------------- .../java/org/onap/validation/yaml/YamlLoader.java | 61 ----------- .../org/onap/validation/yaml/YamlValidator.java | 40 -------- .../yaml/error/SchemaValidationError.java | 36 ------- .../yaml/error/YamlDocumentValidationError.java | 42 -------- .../yaml/exception/YamlProcessingException.java | 33 ------ .../onap/validation/yaml/model/YamlDocument.java | 56 ----------- .../validation/yaml/model/YamlDocumentFactory.java | 52 ---------- .../yaml/model/YamlParameterListFactory.java | 42 -------- .../validation/yaml/model/YamlParametersList.java | 34 ------- .../yaml/process/YamlValidationProcess.java | 111 --------------------- .../yaml/process/YamlValidationStep.java | 45 --------- .../onap/validation/yaml/schema/YamlSchema.java | 37 ------- .../validation/yaml/schema/YamlSchemaFactory.java | 59 ----------- .../yaml/schema/node/YamlSchemaBranchNode.java | 80 --------------- .../yaml/schema/node/YamlSchemaLeafNode.java | 50 ---------- .../yaml/schema/node/YamlSchemaNode.java | 66 ------------ .../yaml/schema/node/YamlSchemaNodeFactory.java | 84 ---------------- 18 files changed, 1000 deletions(-) delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationStep.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchemaFactory.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNode.java delete mode 100644 csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactory.java (limited to 'csarvalidation/src/main/java/org/onap') diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java deleted file mode 100644 index 2de4f48..0000000 --- a/csarvalidation/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/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java b/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java deleted file mode 100644 index 1a5eef9..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java +++ /dev/null @@ -1,61 +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.exception.YamlProcessingException; -import org.onap.validation.yaml.model.YamlDocument; -import org.onap.validation.yaml.model.YamlDocumentFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -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; - -class YamlLoader { - - private static final Logger LOGGER = LoggerFactory.getLogger(YamlLoader.class); - - List loadMultiDocumentYamlFile(URL path) - throws YamlDocumentFactory.YamlDocumentParsingException { - List documentsFromFile = new ArrayList<>(); - try (InputStream yamlStream = path.openStream()) { - for (Object yamlDocument : new Yaml().loadAll(yamlStream)) { - documentsFromFile.add( - new YamlDocumentFactory().createYamlDocument(yamlDocument) - ); - } - } catch (IOException e) { - LOGGER.error("Failed to load multi document YAML file",e); - } - 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 deleted file mode 100644 index 9430df4..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java +++ /dev/null @@ -1,40 +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.exception.YamlProcessingException; -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; - -import java.util.List; - -public class YamlValidator { - - private final YamlSchema schema; - - YamlValidator(YamlSchema schema) { - this.schema = schema; - } - - public List validate(YamlDocument document) throws YamlProcessingException { - return new YamlValidationProcess(schema,document).validate(); - } - -} 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 deleted file mode 100644 index 6ffe6d4..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/error/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.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 deleted file mode 100644 index f04708f..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java +++ /dev/null @@ -1,42 +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.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 deleted file mode 100644 index 99c2437..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java +++ /dev/null @@ -1,33 +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.exception; - -public class YamlProcessingException extends Exception { - - public YamlProcessingException(String message, Throwable throwable) { - 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/YamlDocument.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java deleted file mode 100644 index 557b6fd..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java +++ /dev/null @@ -1,56 +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; - -import java.util.Map; - -public class YamlDocument { - - private final Map yaml; - - YamlDocument(Map yaml) { - this.yaml = yaml; - } - - public Map getYaml() { - return yaml; - } - - public boolean containsKey(String key) { - return yaml.containsKey(key); - } - - public String getValue(String key) { - return yaml.get(key).toString(); - } - - public YamlParametersList getListOfValues(String key) { - return new YamlParameterListFactory().createYamlParameterList( - yaml.get(key) - ); - } - - public YamlDocument getSubStructure(String name) - throws YamlDocumentFactory.YamlDocumentParsingException { - return new YamlDocumentFactory().createYamlDocument( - yaml.get(name) - ); - } -} - - diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java deleted file mode 100644 index b56422c..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java +++ /dev/null @@ -1,52 +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; - -import org.onap.validation.yaml.exception.YamlProcessingException; - -import java.util.HashMap; -import java.util.Map; - -public class YamlDocumentFactory { - - public YamlDocument createYamlDocument(Object yaml) throws YamlDocumentParsingException { - try { - Map parsedYaml = transformMap((Map) yaml); - return new YamlDocument(parsedYaml); - } catch (ClassCastException e) { - throw new YamlDocumentParsingException( - String.format("Fail to parse given objects: %s as yaml document.", yaml), e - ); - } - } - - private Map transformMap(Map yaml) { - Map parsedYaml = new HashMap<>(); - for (Map.Entry entry: yaml.entrySet()) { - parsedYaml.put(entry.getKey().toString(), entry.getValue()); - } - return parsedYaml; - } - - public static class YamlDocumentParsingException extends YamlProcessingException { - YamlDocumentParsingException(String message, Throwable throwable) { - super(message, throwable); - } - } - -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java deleted file mode 100644 index 5f41c5c..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java +++ /dev/null @@ -1,42 +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; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class YamlParameterListFactory { - - public YamlParametersList createEmptyYamlParameterList() { - return new YamlParametersList(Collections.emptyList()); - } - - public YamlParametersList createYamlParameterList(Object yaml) { - List parametersList = new ArrayList<>(); - if( yaml instanceof List) { - for (Object element : (List) yaml) { - parametersList.add(element.toString()); - } - } else { - parametersList.add(yaml.toString()); - } - return new YamlParametersList(parametersList); - } - -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java b/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java deleted file mode 100644 index 2b93c74..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java +++ /dev/null @@ -1,34 +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; - -import java.util.List; - -public class YamlParametersList { - - private final List parameters; - - YamlParametersList(List parameters) { - this.parameters = parameters; - } - - public List getParameters() { - return parameters; - } - -} 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 deleted file mode 100644 index 273014b..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java +++ /dev/null @@ -1,111 +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.process; - -import org.onap.validation.yaml.exception.YamlProcessingException; -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; - -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Queue; - -public class YamlValidationProcess { - - private final Queue validationSteps; - private final List errors; - private final YamlSchema schema; - private final YamlDocument document; - - public YamlValidationProcess(YamlSchema schema, YamlDocument document) { - this.schema = schema; - this.document = document; - errors = new ArrayList<>(); - validationSteps = new LinkedList<>(); - } - - public List validate() throws YamlProcessingException { - validationSteps.add(new YamlValidationStep(schema.getRootNodes(), document)); - while (!validationSteps.isEmpty()) { - YamlValidationStep nextValidationNode = validationSteps.poll(); - validateStep(nextValidationNode); - } - return errors; - } - - private void validateStep(YamlValidationStep validationNode) - throws YamlProcessingException { - for (YamlSchemaNode schemaNode : validationNode.getSchemaNodes()) { - validateNode(validationNode.getDocument(), schemaNode); - } - } - - private void validateNode(YamlDocument document, YamlSchemaNode schemaNode) - throws YamlProcessingException { - - if (document.containsKey(schemaNode.getName())) { - if (schemaNode.isContainingSubStructure()) { - addNextLevelNodeToValidationNodesQueue(document, schemaNode); - } else if (!isValueOfNodeInAcceptedValuesList(document, schemaNode)) { - addIncorrectValueError(document, schemaNode); - } - } else if (schemaNode.isRequired()) { - addRequiredKeyNotFoundError(schemaNode); - } - } - - private boolean isValueOfNodeInAcceptedValuesList(YamlDocument document, YamlSchemaNode node) { - return node.getAcceptedValues().isEmpty() || - node.getAcceptedValues().containsAll( - document.getListOfValues(node.getName()).getParameters() - ); - } - - private void addNextLevelNodeToValidationNodesQueue(YamlDocument document, YamlSchemaNode node) - throws YamlProcessingException { - validationSteps.add( - new YamlValidationStep( - node.getNextNodes(), - document.getSubStructure(node.getName()) - ) - ); - } - - private void addRequiredKeyNotFoundError(YamlSchemaNode node) { - errors.add( - new SchemaValidationError( - node.getPath(), - String.format("Key not found: %s", node.getName()) - ) - ); - } - - private void addIncorrectValueError(YamlDocument document, YamlSchemaNode node) { - errors.add( - new SchemaValidationError( - node.getPath() + node.getName(), - String.format( - "Value(s) is/are not in array of accepted values.%n value(s): %s%n accepted value(s): %s", - document.getValue(node.getName()), node.getAcceptedValues()) - ) - ); - } -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationStep.java b/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationStep.java deleted file mode 100644 index eb5ab8e..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationStep.java +++ /dev/null @@ -1,45 +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.process; - -import org.onap.validation.yaml.model.YamlDocument; -import org.onap.validation.yaml.schema.node.YamlSchemaNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -class YamlValidationStep { - - private final List schemaNodes; - private final YamlDocument document; - - YamlValidationStep(List nodes, YamlDocument yaml) { - this.schemaNodes = new ArrayList<>(nodes); - this.document = yaml; - } - - List getSchemaNodes() { - return Collections.unmodifiableList(schemaNodes); - } - - YamlDocument getDocument() { - return document; - } - -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java deleted file mode 100644 index 69bb6cd..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java +++ /dev/null @@ -1,37 +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.schema; - -import org.onap.validation.yaml.schema.node.YamlSchemaNode; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class YamlSchema { - - private final List rootNodes; - - public List getRootNodes() { - return Collections.unmodifiableList(rootNodes); - } - - YamlSchema(List rootNodes) { - this.rootNodes = new ArrayList<>(rootNodes); - } -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchemaFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchemaFactory.java deleted file mode 100644 index df7d673..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchemaFactory.java +++ /dev/null @@ -1,59 +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.schema; - -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.node.YamlSchemaNode; -import org.onap.validation.yaml.schema.node.YamlSchemaNodeFactory; - -import java.util.ArrayList; -import java.util.List; - -public class YamlSchemaFactory { - - - private static final String ROOT_PATH = "/"; - - public YamlSchema createTreeStructuredYamlSchema(YamlDocument schema) - throws YamlProcessingException { - - return new YamlSchema(getRootNodes(schema)); - } - - private List getRootNodes(YamlDocument yamlDocument) - throws YamlProcessingException { - - List nextNodes = new ArrayList<>(); - for(String nodeName: yamlDocument.getYaml().keySet()) { - nextNodes.add( - new YamlSchemaNodeFactory().createNode( - nodeName, - ROOT_PATH, - new YamlDocumentFactory().createYamlDocument( - yamlDocument.getYaml().get(nodeName) - ) - ) - ); - } - return nextNodes; - } - -} 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 deleted file mode 100644 index 0f5b480..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java +++ /dev/null @@ -1,80 +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.schema.node; - -import org.onap.validation.yaml.exception.YamlProcessingException; -import org.onap.validation.yaml.model.YamlDocument; -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; - - YamlSchemaBranchNode(String name, String path, boolean required, String comment, - YamlDocument nextNodesInLazyForm) { - super(name, path, required, comment); - this.nextNodesInLazyForm = nextNodesInLazyForm; - this.nextNodes = Optional.empty(); - } - - @Override - public boolean isContainingSubStructure() { - return true; - } - - @Override - public List getAcceptedValues() { - return Collections.emptyList(); - } - - @Override - public synchronized List getNextNodes() throws YamlSchemaProcessingException { - try { - return nextNodes.orElseGet(this::loadNextNodes); - } catch (YamlSchemaLazyLoadingException lazyLoadingException) { - throw new YamlSchemaProcessingException(lazyLoadingException); - } - } - - private List loadNextNodes() { - try { - List loadedNextNodes = new ArrayList<>(); - for (String key : nextNodesInLazyForm.getYaml().keySet()) { - YamlDocument substructure = new YamlDocumentFactory() - .createYamlDocument(nextNodesInLazyForm.getYaml().get(key)); - loadedNextNodes.add(new YamlSchemaNodeFactory().createNode(key, getPath() + getName() + "/", substructure)); - } - nextNodes = Optional.of(loadedNextNodes); - return loadedNextNodes; - } catch (YamlProcessingException e) { - throw new YamlSchemaLazyLoadingException("Lazy loading failed, due to yaml parsing exception.",e); - } - } - - static class YamlSchemaLazyLoadingException extends RuntimeException { - YamlSchemaLazyLoadingException(String message, Throwable throwable) { - super(message, throwable); - } - } -} 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 deleted file mode 100644 index c98f41e..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java +++ /dev/null @@ -1,50 +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.schema.node; - -import org.onap.validation.yaml.model.YamlParametersList; - -import java.util.Collections; -import java.util.List; - -public class YamlSchemaLeafNode extends YamlSchemaNode { - - private final YamlParametersList acceptedValues; - - YamlSchemaLeafNode(String name, String path, boolean required, String comment, - YamlParametersList acceptedValues) { - super(name, path, required, comment); - this.acceptedValues = acceptedValues; - } - - @Override - public List getAcceptedValues() { - return acceptedValues.getParameters(); - } - - @Override - public List getNextNodes() { - return Collections.emptyList(); - } - - @Override - public boolean isContainingSubStructure() { - return false; - } - -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNode.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNode.java deleted file mode 100644 index 28913a2..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNode.java +++ /dev/null @@ -1,66 +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.schema.node; - -import org.onap.validation.yaml.exception.YamlProcessingException; - -import java.util.List; - -public abstract class YamlSchemaNode { - - private final String path; - private final String name; - private final boolean required; - private final String comment; - - - public String getName() { - return name; - } - - public String getPath() { - return path; - } - - public boolean isRequired() { - return required; - } - - public abstract List getAcceptedValues(); - - public abstract List getNextNodes() throws YamlSchemaProcessingException; - - public abstract boolean isContainingSubStructure(); - - public String getComment() { - return comment; - } - - YamlSchemaNode(String name, String path, boolean required, String comment) { - this.name = name; - this.path = path; - this.required = required; - this.comment = comment; - } - - static class YamlSchemaProcessingException extends YamlProcessingException { - YamlSchemaProcessingException(Throwable throwable) { - super(throwable); - } - } -} diff --git a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactory.java b/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactory.java deleted file mode 100644 index 79a8f14..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactory.java +++ /dev/null @@ -1,84 +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.schema.node; - -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.model.YamlParameterListFactory; -import org.onap.validation.yaml.model.YamlParametersList; - -import static org.onap.validation.yaml.model.YamlDocumentFactory.YamlDocumentParsingException; - -public class YamlSchemaNodeFactory { - - public static final String EMPTY_COMMENT = "no comment available"; - static final String STRUCTURE_KEY = "structure"; - static final String COMMENT_KEY = "comment"; - static final String VALUE_KET = "value"; - static final String PRESENCE_KEY = "presence"; - static final String PRESENCE_REQUIRED_KEY = "required"; - - public YamlSchemaNode createNode(String nodeName, String path, YamlDocument yamlDocument) - throws YamlProcessingException { - - YamlSchemaNode yamlSchemaNode; - if(isYamlContainingKey(yamlDocument, STRUCTURE_KEY)) { - yamlSchemaNode = new YamlSchemaBranchNode( - nodeName, path, getIsPresenceRequired(yamlDocument), getComment(yamlDocument), - getNextNodes(yamlDocument) - ); - } else { - yamlSchemaNode = new YamlSchemaLeafNode( - nodeName, path, getIsPresenceRequired(yamlDocument), getComment(yamlDocument), - getAcceptedValues(yamlDocument) - ); - } - return yamlSchemaNode; - } - - private YamlDocument getNextNodes(YamlDocument yamlDocument) - throws YamlDocumentParsingException { - return new YamlDocumentFactory().createYamlDocument(yamlDocument.getYaml().get(STRUCTURE_KEY)); - } - - private String getComment(YamlDocument yamlDocument) { - - return isYamlContainingKey(yamlDocument, COMMENT_KEY) - ? yamlDocument.getYaml().get(COMMENT_KEY).toString() - : EMPTY_COMMENT; - } - - private YamlParametersList getAcceptedValues(YamlDocument yamlDocument) { - - return isYamlContainingKey(yamlDocument, VALUE_KET) - ? new YamlParameterListFactory().createYamlParameterList(yamlDocument.getYaml().get(VALUE_KET)) - : new YamlParameterListFactory().createEmptyYamlParameterList(); - } - - private boolean getIsPresenceRequired(YamlDocument yamlDocument) { - - return isYamlContainingKey(yamlDocument, PRESENCE_KEY) - && yamlDocument.getYaml().get(PRESENCE_KEY).equals(PRESENCE_REQUIRED_KEY); - } - - private boolean isYamlContainingKey(YamlDocument yamlDocument, String structureKey) { - return yamlDocument.getYaml().containsKey(structureKey); - } - -} -- cgit 1.2.3-korg