diff options
author | 2020-07-09 14:02:23 +0200 | |
---|---|---|
committer | 2020-07-14 14:52:49 +0200 | |
commit | 4ce8c75c2559bde35b5fca46ec2c94d6b5070b6f (patch) | |
tree | 56f7440b88a28214a8b476dbf0205e9329e3936d /csarvalidation/src/main/java/org/onap/cvc/csar | |
parent | ae6a1ca9e670a9ab2c7302af89f0dd17bf54ba83 (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/org/onap/cvc/csar')
-rw-r--r-- | csarvalidation/src/main/java/org/onap/cvc/csar/cc/sol004/VTPValidateCSARR816745.java | 74 |
1 files changed, 69 insertions, 5 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 |