diff options
author | Pawel <pawel.kasperkiewicz@nokia.com> | 2020-11-12 12:15:55 +0100 |
---|---|---|
committer | Pawel <pawel.kasperkiewicz@nokia.com> | 2020-11-24 09:08:06 +0100 |
commit | b6ff67fa1d21765f2f52f3643946c96b2f13aa07 (patch) | |
tree | a3c03d814bccabd9de7d16c2da87a0e3a0f7ea08 /pmdictionaryvalidation/src/main | |
parent | e1be6d1ea065340aaabce761705755970348c6c4 (diff) |
Extract pm-dicrionary validation
Issue-ID: VNFSDK-713
Signed-off-by: Pawel <pawel.kasperkiewicz@nokia.com>
Change-Id: Iee5a23a3a6c9215927aa2c453faab62d30453444
Diffstat (limited to 'pmdictionaryvalidation/src/main')
19 files changed, 1048 insertions, 0 deletions
diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java new file mode 100644 index 0000000..2de4f48 --- /dev/null +++ b/pmdictionaryvalidation/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<YamlDocumentValidationError> validateYamlFileWithSchema(String pathToFile) + throws YamlProcessingException { + + List<YamlDocument> documents = new YamlLoader().loadMultiDocumentYamlFile(pathToFile); + if(!documents.isEmpty()) { + return validateDocuments(documents); + } else { + throw new YamlProcessingException("PM_Dictionary YAML file is empty"); + } + } + + private List<YamlDocumentValidationError> validateDocuments(List<YamlDocument> documents) + throws YamlProcessingException { + + List<YamlDocumentValidationError> yamlFileValidationErrors = new ArrayList<>(); + YamlSchema schema = extractSchema(documents); + YamlValidator validator = new YamlValidator(schema); + + for (int index = FIRST_DOCUMENT_INDEX; index < documents.size(); index++) { + List<SchemaValidationError> validationErrors = validator.validate(documents.get(index)); + yamlFileValidationErrors.addAll(transformErrors(index,validationErrors)); + } + + return yamlFileValidationErrors; + } + + private List<YamlDocumentValidationError> transformErrors(int index, List<SchemaValidationError> validationErrors) { + return validationErrors + .stream() + .map(error->new YamlDocumentValidationError(index, error.getPath(), error.getMessage())) + .collect(Collectors.toList()); + } + + private YamlSchema extractSchema(List<YamlDocument> 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 new file mode 100644 index 0000000..1a5eef9 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java @@ -0,0 +1,61 @@ +/* + * 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<YamlDocument> loadMultiDocumentYamlFile(URL path) + throws YamlDocumentFactory.YamlDocumentParsingException { + List<YamlDocument> 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<YamlDocument> 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java new file mode 100644 index 0000000..30ba646 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java @@ -0,0 +1,40 @@ +/* + * 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.exception.YamlProcessingException; +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<SchemaValidationError> validate(YamlDocument document) throws YamlProcessingException { + return new YamlValidationProcess(schema,document).validate(); + } + +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java new file mode 100644 index 0000000..6ffe6d4 --- /dev/null +++ b/pmdictionaryvalidation/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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java new file mode 100644 index 0000000..f04708f --- /dev/null +++ b/pmdictionaryvalidation/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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java new file mode 100644 index 0000000..99c2437 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java @@ -0,0 +1,33 @@ +/* + * 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java new file mode 100644 index 0000000..557b6fd --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocument.java @@ -0,0 +1,56 @@ +/* + * 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<String, Object> yaml; + + YamlDocument(Map<String, Object> yaml) { + this.yaml = yaml; + } + + public Map<String, Object> 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java new file mode 100644 index 0000000..b56422c --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlDocumentFactory.java @@ -0,0 +1,52 @@ +/* + * 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<String, Object> 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<String, Object> transformMap(Map<Object, Object> yaml) { + Map<String, Object> parsedYaml = new HashMap<>(); + for (Map.Entry<Object, Object> 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.java new file mode 100644 index 0000000..5f41c5c --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlParameterListFactory.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.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<String> 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java new file mode 100644 index 0000000..2b93c74 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/model/YamlParametersList.java @@ -0,0 +1,34 @@ +/* + * 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<String> parameters; + + YamlParametersList(List<String> parameters) { + this.parameters = parameters; + } + + public List<String> getParameters() { + return parameters; + } + +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java new file mode 100644 index 0000000..ebd37ce --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java @@ -0,0 +1,111 @@ +/* + * 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.error.SchemaValidationError; +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.node.YamlSchemaNode; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +public class YamlValidationProcess { + + private final Queue<YamlValidationStep> validationSteps; + private final List<SchemaValidationError> 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<SchemaValidationError> 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationStep.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationStep.java new file mode 100644 index 0000000..eb5ab8e --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationStep.java @@ -0,0 +1,45 @@ +/* + * 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<YamlSchemaNode> schemaNodes; + private final YamlDocument document; + + YamlValidationStep(List<YamlSchemaNode> nodes, YamlDocument yaml) { + this.schemaNodes = new ArrayList<>(nodes); + this.document = yaml; + } + + List<YamlSchemaNode> getSchemaNodes() { + return Collections.unmodifiableList(schemaNodes); + } + + YamlDocument getDocument() { + return document; + } + +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java new file mode 100644 index 0000000..69bb6cd --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchema.java @@ -0,0 +1,37 @@ +/* + * 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<YamlSchemaNode> rootNodes; + + public List<YamlSchemaNode> getRootNodes() { + return Collections.unmodifiableList(rootNodes); + } + + YamlSchema(List<YamlSchemaNode> rootNodes) { + this.rootNodes = new ArrayList<>(rootNodes); + } +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchemaFactory.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchemaFactory.java new file mode 100644 index 0000000..df7d673 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/YamlSchemaFactory.java @@ -0,0 +1,59 @@ +/* + * 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<YamlSchemaNode> getRootNodes(YamlDocument yamlDocument) + throws YamlProcessingException { + + List<YamlSchemaNode> 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/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 new file mode 100644 index 0000000..0f5b480 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java @@ -0,0 +1,80 @@ +/* + * 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<List<YamlSchemaNode>> 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<String> getAcceptedValues() { + return Collections.emptyList(); + } + + @Override + public synchronized List<YamlSchemaNode> getNextNodes() throws YamlSchemaProcessingException { + try { + return nextNodes.orElseGet(this::loadNextNodes); + } catch (YamlSchemaLazyLoadingException lazyLoadingException) { + throw new YamlSchemaProcessingException(lazyLoadingException); + } + } + + private List<YamlSchemaNode> loadNextNodes() { + try { + List<YamlSchemaNode> 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java new file mode 100644 index 0000000..c98f41e --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java @@ -0,0 +1,50 @@ +/* + * 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<String> getAcceptedValues() { + return acceptedValues.getParameters(); + } + + @Override + public List<YamlSchemaNode> getNextNodes() { + return Collections.emptyList(); + } + + @Override + public boolean isContainingSubStructure() { + return false; + } + +} diff --git a/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNode.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNode.java new file mode 100644 index 0000000..28913a2 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNode.java @@ -0,0 +1,66 @@ +/* + * 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<String> getAcceptedValues(); + + public abstract List<YamlSchemaNode> 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/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactory.java b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactory.java new file mode 100644 index 0000000..79a8f14 --- /dev/null +++ b/pmdictionaryvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaNodeFactory.java @@ -0,0 +1,84 @@ +/* + * 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); + } + +} diff --git a/pmdictionaryvalidation/src/main/resources/log4j2.properties b/pmdictionaryvalidation/src/main/resources/log4j2.properties new file mode 100644 index 0000000..f3202ee --- /dev/null +++ b/pmdictionaryvalidation/src/main/resources/log4j2.properties @@ -0,0 +1,48 @@ +# Copyright Nokia 2020,2020 Huawei Technologies Co., Ltd. +# +# 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. + +# By default, log4j2 will look for a configuration file named log4j2.xml on the classpath. +# reference: https://logging.apache.org/log4j/2.x/faq.html#troubleshooting + +rootLogger.level=ERROR +rootLogger.appenderRefs=file +rootLogger.appenderRef.file.ref=RollingFile + +logger.onap.name = org.onap +logger.onap.level=ERROR +logger.onap.additivity=false +logger.onap.appenderRef.stdout.ref=STDOUT + +appenders=stdout, file + +# Direct log messages to stdout +appender.stdout.type=Console +appender.stdout.name=STDOUT +appender.stdout.target=SYSTEM_OUT +appender.stdout.layout.type=PatternLayout +appender.stdout.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +# Redirect log messages to a log file, support file rolling. +appender.file.type = RollingFile +appender.file.name = RollingFile +appender.file.fileName=./pmdictionary-validate.log +appender.file.filePattern=./pmdictionary-validate.%d{yyyy-MM-dd-HH:mm:ss}.log +appender.file.append=true +appender.file.policies.type=Policies +appender.file.policies.size.type=SizeBasedTriggeringPolicy +appender.file.policies.size.size=5MB +appender.file.strategy.type=DefaultRolloverStrategy +appender.file.strategy.max=10 +appender.file.layout.type=PatternLayout +appender.file.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n |