summaryrefslogtreecommitdiffstats
path: root/csarvalidation/src/main/java
diff options
context:
space:
mode:
authorBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-07-09 14:02:23 +0200
committerBartosz Gardziejewski <bartosz.gardziejewski@nokia.com>2020-07-14 14:52:49 +0200
commit4ce8c75c2559bde35b5fca46ec2c94d6b5070b6f (patch)
tree56f7440b88a28214a8b476dbf0205e9329e3936d /csarvalidation/src/main/java
parentae6a1ca9e670a9ab2c7302af89f0dd17bf54ba83 (diff)
use YAML schema validator in rule R816745
Issue-ID: VNFSDK-594 Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> Change-Id: I70c6150662b69833d4e190a6adc1047840a78975
Diffstat (limited to 'csarvalidation/src/main/java')
-rw-r--r--csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java74
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/YamlFileValidator.java72
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/YamlLoader.java14
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/YamlValidator.java2
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java (renamed from csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java)2
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/error/YamlDocumentValidationError.java42
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/exception/YamlProcessingException.java4
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/process/YamlValidationProcess.java2
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaBranchNode.java3
-rw-r--r--csarvalidation/src/main/java/org/onap/validation/yaml/schema/node/YamlSchemaLeafNode.java2
10 files changed, 203 insertions, 14 deletions
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<String, Map<String, List<String>>> 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<YamlDocumentValidationError> 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<YamlDocumentValidationError> 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<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/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<YamlDocument> loadMultiDocumentYamlFile(URL path)
+ List<YamlDocument> loadMultiDocumentYamlFile(URL path)
throws YamlDocumentFactory.YamlDocumentParsingException {
List<YamlDocument> documentsFromFile = new ArrayList<>();
try (InputStream yamlStream = path.openStream()) {
@@ -48,4 +50,12 @@ public class YamlLoader {
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/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/model/SchemaValidationError.java b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java
index 45f1647..6ffe6d4 100644
--- a/csarvalidation/src/main/java/org/onap/validation/yaml/model/SchemaValidationError.java
+++ b/csarvalidation/src/main/java/org/onap/validation/yaml/error/SchemaValidationError.java
@@ -15,7 +15,7 @@
*
*/
-package org.onap.validation.yaml.model;
+package org.onap.validation.yaml.error;
public class SchemaValidationError {
private final String path;
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/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) {