diff options
author | Bogumil Zebek <bogumil.zebek@nokia.com> | 2019-09-18 11:37:56 +0200 |
---|---|---|
committer | Zebek Bogumil <bogumil.zebek@nokia.com> | 2019-09-18 11:37:56 +0200 |
commit | e0a0f45ecaf19e0122e745e8301b79ca4baf2e42 (patch) | |
tree | 9a07fefc7ac667378ab94d5fe89d121376ca0a8e /csarvalidation | |
parent | 3e0efbc34176ad3ad2e6d731a699f5684c0a527d (diff) |
Sonar fix
Change-Id: I7c39a2d83d067382ffb91ed136ad840dab85ccd8
Issue-ID: VNFSDK-477
Signed-off-by: Zebek Bogumil <bogumil.zebek@nokia.com>
Diffstat (limited to 'csarvalidation')
5 files changed, 345 insertions, 78 deletions
diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/CSARArchive.java b/csarvalidation/src/main/java/org/onap/cvc/csar/CSARArchive.java index 4bb7cf3..2055cf2 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/CSARArchive.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/CSARArchive.java @@ -15,13 +15,19 @@ */ package org.onap.cvc.csar; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.onap.cvc.csar.parser.SourcesParser; +import org.onap.cvc.csar.parser.vnf.DefinitionMetadataParser; +import org.yaml.snakeyaml.Yaml; + import java.io.File; import java.io.FileInputStream; -import java.io.FilenameFilter; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -29,14 +35,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.tuple.Pair; -import org.onap.cvc.csar.parser.SourcesParser; -import org.yaml.snakeyaml.Yaml; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - /** * Verify the CSAR package by following the SOL004 specifications and ONAP VNFREQS for TOSCA. * @@ -83,11 +81,6 @@ public class CSARArchive implements AutoCloseable { public static final String ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_1 = "tosca_simple_yaml_1_1"; public static final String ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_2 = "tosca_simple_yaml_1_2"; - protected static final String[] Entry_Definition__tosca_definitions_versions = new String[] { - ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_0, - ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_1, - ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_2 - }; public static final String ENTRY_DEFINITION_METADATA = "metadata"; public static final String ENTRY_DEFINITION_TEMPLATE_NAME = "template_name"; @@ -680,7 +673,7 @@ public class CSARArchive implements AutoCloseable { private String templateVersion; - public String getTempalteName() { + public String getTemplateName() { return tempalteName; } @@ -953,67 +946,14 @@ public class CSARArchive implements AutoCloseable { try(FileInputStream ipStream = new FileInputStream(this.definitionYamlFile)) { Map<String, ?> yaml = (Map<String, ?>) new Yaml().load(ipStream); - //yaml is empty or version string missing - if (yaml == null || !yaml.keySet().contains(ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION)) { - errors.add( - new CSARErrorEntryMissingToscaDefinitionVersion( - this.definitionYamlFile.getName())); - } else { - String version = (String) yaml.get(ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION); - if (!Arrays.asList(Entry_Definition__tosca_definitions_versions).contains(version)) { - errors.add(new CSARErrorInvalidEntry(ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION, - this.definitionYamlFile.getName(), -1, "Should be " + ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_1)); - } else { - this.definition.setToscaDefinitionVersion(version); - - if (this.toscaMeta.getMode().equals(Mode.WITHOUT_TOSCA_META_DIR)) { - //metadata section should be there - if (!yaml.keySet().contains(ENTRY_DEFINITION_METADATA)) { - errors.add( - new CSARErrorInvalidEntryValueToscaDefinitionVersion( - this.definitionYamlFile.getName())); - } else { - Map<String, String> metadata = (Map<String, String>) yaml.get(ENTRY_DEFINITION_METADATA); - - for(Map.Entry<String, String> entry: metadata.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - - //continue till it reaches the metadata section - if (key.equalsIgnoreCase(ENTRY_DEFINITION_TEMPLATE_AUTHOR)) { - this.definition.getMetadata().setTemplateAuthor(value); - } else if (key.equalsIgnoreCase(ENTRY_DEFINITION_TEMPLATE_NAME)) { - this.definition.getMetadata().setTempalteName(value); - } else if (key.equalsIgnoreCase(ENTRY_DEFINITION_TEMPLATE_VERSION)) { - this.definition.getMetadata().setTemplateVersion(value); - } else { - errors.add( - new CSARErrorIgnored( - key, - this.definitionYamlFile.getName(), - -1, - null)); - } - } - - if (this.definition.getMetadata().getTemplateAuthor() == null) { - this.errors.add( - new CSARErrorEntryMissingToscaDefinitionMetadataTemplateAuthor( - this.definitionYamlFile.getName())); - } - if (this.definition.getMetadata().getTempalteName() == null) { - this.errors.add(new CSARErrorEntryMissingToscaDefinitionMetadataTemplateName( - this.definitionYamlFile.getName())); - } - - if (this.definition.getMetadata().getTemplateVersion() == null) { - this.errors.add(new CSARErrorEntryMissingToscaDefinitionMetadataTemplateVersion( - this.definitionYamlFile.getName())); - } - } - } - } - } + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, + this.definitionYamlFile.getName(), + this.toscaMeta.getMode() + ); + final Pair<Definition, List<CSARError>> data = definitionMetadataParser.parse(); + this.definition = data.getLeft(); + this.errors.addAll(data.getRight()); } } diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestFileSplitter.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestFileSplitter.java index 03a628c..d780e67 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestFileSplitter.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestFileSplitter.java @@ -18,6 +18,9 @@ package org.onap.cvc.csar.parser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -29,6 +32,8 @@ import java.util.stream.Stream; public class ManifestFileSplitter { + private static final Logger LOG = LoggerFactory.getLogger(ManifestFileSplitter.class); + public ManifestFileModel split(File manifestFile) { String fileName = manifestFile.getAbsolutePath(); List<String> data = new ArrayList<>(); @@ -39,6 +44,7 @@ public class ManifestFileSplitter { return createManifestFileModel(data, cms, lines); } catch (IOException e) { + LOG.error("Unable to process manifest file!", e); throw new IllegalArgumentException(String.format("Unable to process manifest file! Wrong file path: '%s'", fileName)); } } diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/vnf/DefinitionMetadataParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/vnf/DefinitionMetadataParser.java new file mode 100644 index 0000000..6743741 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/vnf/DefinitionMetadataParser.java @@ -0,0 +1,133 @@ +/* + * Copyright 2019 Nokia + * <p> + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.cvc.csar.parser.vnf; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.cvc.csar.CSARArchive; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * Code extracted from CSARArchive class. + */ +public class DefinitionMetadataParser { + + private static final String[] ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSIONS = { + CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_0, + CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_1, + CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_2 + }; + + private Map<String, ?> yaml; + private String definitionYamlFileName; + private CSARArchive.Mode mode; + + public DefinitionMetadataParser(Map<String, ?> yaml, String definitionYamlFileName, CSARArchive.Mode mode) { + this.yaml = yaml; + this.definitionYamlFileName = definitionYamlFileName; + this.mode = mode; + } + + public Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> parse(){ + List<CSARArchive.CSARError> errors = new ArrayList<>(); + CSARArchive.Definition definition = new CSARArchive.Definition(); + + //yaml is empty or version string missing + if (yaml == null || !yaml.containsKey(CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION)) { + errors.add( + new CSARArchive.CSARErrorEntryMissingToscaDefinitionVersion( + this.definitionYamlFileName)); + } else { + convertYamlIntoDefinitionModel(yaml, definition, errors); + } + return Pair.of(definition,errors); + } + + private void convertYamlIntoDefinitionModel(Map<String, ?> yaml, CSARArchive.Definition definition, List<CSARArchive.CSARError> errors) { + String version = (String) yaml.get(CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION); + if (!Arrays.asList(ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSIONS).contains(version)) { + errors.add(new CSARArchive.CSARErrorInvalidEntry(CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION, + this.definitionYamlFileName, -1, "Should be " + CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION_SIMPLE_1_1)); + } else { + updateDefinitionModel(definition, version, errors); + } + } + + private void updateDefinitionModel(CSARArchive.Definition definition, String version, List<CSARArchive.CSARError> errors) { + definition.setToscaDefinitionVersion(version); + + if (mode.equals(CSARArchive.Mode.WITHOUT_TOSCA_META_DIR)) { + //metadata section should be there + if (!yaml.containsKey(CSARArchive.ENTRY_DEFINITION_METADATA)) { + errors.add( + new CSARArchive.CSARErrorInvalidEntryValueToscaDefinitionVersion( + this.definitionYamlFileName)); + } else { + Map<String, String> metadata = (Map<String, String>) yaml.get(CSARArchive.ENTRY_DEFINITION_METADATA); + final CSARArchive.Definition.Metadata metadataModel = definition.getMetadata(); + populateMetadataModel(metadata, errors, metadataModel); + validateMetadataModel(errors, metadataModel); + } + } + } + + private void populateMetadataModel(Map<String, String> metadata, List<CSARArchive.CSARError> errors, CSARArchive.Definition.Metadata metadataModel) { + + for(Map.Entry<String, String> entry: metadata.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + + //continue till it reaches the metadata section + if (key.equalsIgnoreCase(CSARArchive.ENTRY_DEFINITION_TEMPLATE_AUTHOR)) { + metadataModel.setTemplateAuthor(value); + } else if (key.equalsIgnoreCase(CSARArchive.ENTRY_DEFINITION_TEMPLATE_NAME)) { + metadataModel.setTempalteName(value); + } else if (key.equalsIgnoreCase(CSARArchive.ENTRY_DEFINITION_TEMPLATE_VERSION)) { + metadataModel.setTemplateVersion(value); + } else { + errors.add( + new CSARArchive.CSARErrorIgnored( + key, + this.definitionYamlFileName, + -1, + null)); + } + } + } + + private void validateMetadataModel(List<CSARArchive.CSARError> errors, CSARArchive.Definition.Metadata metadataModel) { + if (metadataModel.getTemplateAuthor() == null) { + errors.add( + new CSARArchive.CSARErrorEntryMissingToscaDefinitionMetadataTemplateAuthor( + this.definitionYamlFileName)); + } + if (metadataModel.getTemplateName() == null) { + errors.add(new CSARArchive.CSARErrorEntryMissingToscaDefinitionMetadataTemplateName( + this.definitionYamlFileName)); + } + + if (metadataModel.getTemplateVersion() == null) { + errors.add(new CSARArchive.CSARErrorEntryMissingToscaDefinitionMetadataTemplateVersion( + this.definitionYamlFileName)); + } + } + +} diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/DefinitionMetadataParserTest.java b/csarvalidation/src/test/java/org/onap/cvc/csar/DefinitionMetadataParserTest.java new file mode 100644 index 0000000..e8adde8 --- /dev/null +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/DefinitionMetadataParserTest.java @@ -0,0 +1,188 @@ +/* + * Copyright 2019 Nokia + * <p> + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.cvc.csar; + +import org.apache.commons.lang3.tuple.Pair; +import org.junit.Test; +import org.onap.cvc.csar.parser.vnf.DefinitionMetadataParser; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.onap.cvc.csar.CSARArchive.Definition; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TEMPLATE_AUTHOR; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TEMPLATE_NAME; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TEMPLATE_VERSION; +import static org.onap.cvc.csar.CSARArchive.ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION; +import static org.onap.cvc.csar.cc.sol004.IntegrationTestUtils.convertToMessagesList; + +public class DefinitionMetadataParserTest { + + private static final String DEFINITION_YAML = "definition.yaml"; + + @Test + public void shouldReportThatDefinitionYamlDataAreEmpty(){ + // given + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + new HashMap<>(), DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> data = definitionMetadataParser.parse(); + + // then + assertThat(convertToMessagesList(data.getRight())).contains( + "Missing. Entry [tosca_definitions_version]" + ); + } + + @Test + public void shouldReportThatDefinitionYamlDataIsInUnsupportedVersion(){ + // given + final HashMap<String, Object> yaml = givenEmptyYamlData("tosca_simple_yaml_0_0"); + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> data = definitionMetadataParser.parse(); + + // then + assertThat(convertToMessagesList(data.getRight())).contains( + "Invalid. Entry [tosca_definitions_version]. Should be tosca_simple_yaml_1_1" + ); + } + + @Test + public void shouldSetOnlyAVersionWhenMetaIsForToscaDirMode(){ + // given + final HashMap<String, Object> yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITH_TOSCA_META_DIR); + + // when + final Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> data = definitionMetadataParser.parse(); + + // then + final CSARArchive.Definition definition = data.getLeft(); + final List<CSARArchive.CSARError> errors = data.getRight(); + assertThat(definition.getToscaDefinitionVersion()).isEqualTo("tosca_simple_yaml_1_1"); + assertThat(errors).isEmpty(); + } + + @Test + public void shouldReportAnErrorWhenMetaIsNotSet(){ + // given + final HashMap<String, Object> yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> data = definitionMetadataParser.parse(); + + // then + final CSARArchive.Definition definition = data.getLeft(); + final List<CSARArchive.CSARError> errors = data.getRight(); + assertThat(definition.getToscaDefinitionVersion()).isEqualTo("tosca_simple_yaml_1_1"); + assertThat(convertToMessagesList(errors)).contains( + "Invalid value. Entry [tosca_definitions_version]. Valid values are [tosca_simple_yaml_1_1]" + ); + } + + @Test + public void shouldPopulateMetadataModel(){ + // given + final Map<String, Object> metadata = givenPopulateMetadataData(); + final HashMap<String, Object> yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + yaml.put("metadata", metadata); + + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> data = definitionMetadataParser.parse(); + + //then + final CSARArchive.Definition definition = data.getLeft(); + final List<CSARArchive.CSARError> errors = data.getRight(); + assertThat(definition.getToscaDefinitionVersion()).isEqualTo("tosca_simple_yaml_1_1"); + assertThat(convertToMessagesList(errors)).isEmpty(); + final Definition.Metadata metadataModel = definition.getMetadata(); + assertThat(metadataModel.getTemplateAuthor()).isEqualTo("author"); + assertThat(metadataModel.getTemplateName()).isEqualTo("template name"); + assertThat(metadataModel.getTemplateVersion()).isEqualTo("template version"); + } + + @Test + public void shouldReportAnErrorWhenMetadataParameterIsNotSupported(){ + // given + final Map<String, Object> metadata = givenPopulateMetadataData(); + metadata.put("NEW ENTRY DEFINITION TEMPLATE PARAMETER", "some value"); + final HashMap<String, Object> yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + yaml.put("metadata", metadata); + + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> data = definitionMetadataParser.parse(); + + // then + final List<CSARArchive.CSARError> errors = data.getRight(); + assertThat(convertToMessagesList(errors)).contains( + "Ignored. Entry [NEW ENTRY DEFINITION TEMPLATE PARAMETER]" + ); + } + + @Test + public void shouldReportErrorsWhenMetadataModelIsEmpty(){ + // given + final Map<String, Object> metadata = new HashMap<>(); + final HashMap<String, Object> yaml = givenEmptyYamlData("tosca_simple_yaml_1_1"); + yaml.put("metadata", metadata); + + DefinitionMetadataParser definitionMetadataParser = new DefinitionMetadataParser( + yaml, DEFINITION_YAML, CSARArchive.Mode.WITHOUT_TOSCA_META_DIR); + + // when + final Pair<CSARArchive.Definition, List<CSARArchive.CSARError>> data = definitionMetadataParser.parse(); + + // then + final List<CSARArchive.CSARError> errors = data.getRight(); + assertThat(convertToMessagesList(errors)).contains( + "Missing. Entry [template_author]", + "Missing. Entry [template_name]", + "Missing. Entry [template_version]" + ); + } + + + private HashMap<String, Object> givenEmptyYamlData(String tosca_simple_yaml_0_0) { + final HashMap<String, Object> yaml = new HashMap<>(); + yaml.put(ENTRY_DEFINITION_TOSCA_DEFINITIONS_VERSION, tosca_simple_yaml_0_0); + return yaml; + } + + private Map<String, Object> givenPopulateMetadataData() { + final Map<String, Object> metadata = new HashMap<>(); + metadata.put(ENTRY_DEFINITION_TEMPLATE_AUTHOR, "author"); + metadata.put(ENTRY_DEFINITION_TEMPLATE_NAME, "template name"); + metadata.put(ENTRY_DEFINITION_TEMPLATE_VERSION, "template version"); + return metadata; + } +} diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java index a0bb25d..6f183d7 100644 --- a/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/cc/sol004/IntegrationTestUtils.java @@ -84,7 +84,7 @@ public class IntegrationTestUtils { } } - static List<String> convertToMessagesList(List<CSARArchive.CSARError> errors) { + public static List<String> convertToMessagesList(List<CSARArchive.CSARError> errors) { return errors.stream().map(CSARArchive.CSARError::getMessage).collect(Collectors.toList()); } } |