From e58e011a89ae5c8ada209a82d9b0f3ad1b0ca58f Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Tue, 26 Mar 2019 14:19:31 +0100 Subject: PNF TC R146092 Change-Id: I27c881a8673957dde346fae393fffa6e946c2cd4 Issue-ID: VNFSDK-388 Signed-off-by: Zebek Bogumil --- .../java/org/onap/cvc/csar/PnfManifestParser.java | 93 ++++++++++++++++------ 1 file changed, 70 insertions(+), 23 deletions(-) (limited to 'csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java') diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java index ea52841..5ef74fd 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java @@ -21,6 +21,7 @@ import org.onap.cvc.csar.PnfCSARError.PnfCSARErrorEntryMissing; import org.onap.cvc.csar.PnfCSARError.PnfCSARErrorInvalidEntry; import org.onap.cvc.csar.PnfCSARError.PnfCSARErrorWarning; +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -34,8 +35,9 @@ import java.util.stream.Stream; class PnfManifestParser { - private static final String METADATA_SECTION = "metadata"; - private static final String NON_MANO_ARTIFACT_SETS_SECTION = "non_mano_artifact_sets"; + private static final String METADATA_SECTION_TAG_SECTION = "metadata"; + private static final String SOURCE_TAG_SECTION = "source"; + private static final String NON_MANO_ARTIFACT_SETS_TAG_SECTION = "non_mano_artifact_sets"; private static final String PRODUCT_NAME = "pnfd_name"; private static final String PROVIDER_ID = "pnfd_provider"; private static final String VERSION = "pnfd_archive_version"; @@ -49,13 +51,14 @@ class PnfManifestParser { this.fileName = fileName; } - static PnfManifestParser getInstance(String fileName) throws IOException { + static PnfManifestParser getInstance(File pnfManifestFile) throws IOException { + String fileName = pnfManifestFile.getAbsolutePath(); try (Stream stream = Files.lines(Paths.get(fileName))) { List lines = stream .map(String::trim) .collect(Collectors.toList()); - return new PnfManifestParser(lines, fileName); + return new PnfManifestParser(lines, pnfManifestFile.getName()); } } @@ -64,20 +67,16 @@ class PnfManifestParser { List errors = new ArrayList<>(); boolean isMetadataSectionAvailable = false; - int lineNumber = 0; - for (String line : lines) { - lineNumber++; - if (line.trim().isEmpty() || line.trim().startsWith("#")){ - continue; - } else if (line.startsWith(METADATA_SECTION)) { + + for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) { + String line = lines.get(lineNumber); + Pair data = parseLine(line); + + if(data.getKey().toLowerCase().equals(METADATA_SECTION_TAG_SECTION)) { isMetadataSectionAvailable = true; - }else if (isMetadataSectionAvailable) { - Pair data = parseLine(line); + }else if (isMetadataSectionAvailable && !isLineExcluded(line)) { - if (isNewSection(data)) { - if(!isSectionSupported(data.getKey())) { - errors.add(new PnfCSARErrorWarning(data.getKey(), this.fileName, lineNumber)); - } + if (shouldStopProcessing(data, errors, lineNumber)) { break; } @@ -86,13 +85,39 @@ class PnfManifestParser { } if (!isMetadataSectionAvailable) { - errors.add(new PnfCSARErrorEntryMissing(METADATA_SECTION, this.fileName, -1)); + errors.add(new PnfCSARErrorEntryMissing(METADATA_SECTION_TAG_SECTION, this.fileName, -1)); } return Pair.of(metadata, errors); } + Pair, List> fetchSourcesSection() { + List sources = new ArrayList<>(); + List errors = new ArrayList<>(); + boolean isSpecialTagReached = false; + boolean sourceSectionParsing = false; + for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) { + String line = lines.get(lineNumber); + if (sourceSectionParsing && (startsWith(line, METADATA_SECTION_TAG_SECTION) || startsWith(line, NON_MANO_ARTIFACT_SETS_TAG_SECTION))) { + isSpecialTagReached = true; + }else if (!isSpecialTagReached && startsWith(line, SOURCE_TAG_SECTION)) { + sourceSectionParsing = true; + Pair data = parseLine(line); + + String value = data.getValue(); + if (value.isEmpty()) { + errors.add(new PnfCSARErrorWarning(data.getKey(), this.fileName, lineNumber)); + break; + } else { + sources.add(value); + } + } + } + + return Pair.of(sources, errors); + } + Pair>>, List> fetchNonManoArtifacts() { Map>> nonManoArtifacts = new HashMap<>(); List errors = new ArrayList<>(); @@ -102,15 +127,14 @@ class PnfManifestParser { for (String line : lines) { - if (line.trim().isEmpty() || line.trim().startsWith("#")) { - continue; - } else if (line.startsWith(NON_MANO_ARTIFACT_SETS_SECTION)) { + if (startsWith(line, NON_MANO_ARTIFACT_SETS_TAG_SECTION)) { isNonManoArtifactsSectionAvailable = true; } else if (isNonManoArtifactsSectionAvailable) { Pair data = parseLine(line); if (isNewSection(data)) { attributeName = data.getKey(); + nonManoArtifacts.put(attributeName, new HashMap<>()); continue; } @@ -119,12 +143,32 @@ class PnfManifestParser { } if (!isNonManoArtifactsSectionAvailable) { - errors.add(new PnfCSARErrorEntryMissing(NON_MANO_ARTIFACT_SETS_SECTION, this.fileName, -1)); + errors.add(new PnfCSARErrorEntryMissing(NON_MANO_ARTIFACT_SETS_TAG_SECTION, this.fileName, -1)); } return Pair.of(nonManoArtifacts, errors); } + private boolean isLineExcluded(String line) { + return line.trim().isEmpty() + || startsWith(line, "#") + || startsWith(line,SOURCE_TAG_SECTION); + } + + private boolean shouldStopProcessing(Pair data, List errors, int lineNumber) { + if (isNewSection(data) || data.getKey().toLowerCase().equals(SOURCE_TAG_SECTION)) { + if(!isSectionSupported(data.getKey())) { + errors.add(new PnfCSARErrorWarning(data.getKey(), this.fileName, lineNumber)); + } + return true; + } + return false; + } + + private boolean startsWith(String line, String word){ + return line.trim().toLowerCase().startsWith(word); + } + private void handleMetadataLine( CSARArchive.Manifest.Metadata metadata, List errors, @@ -172,14 +216,17 @@ class PnfManifestParser { } private boolean isSectionSupported(String key) { - return Lists.newArrayList(METADATA_SECTION, NON_MANO_ARTIFACT_SETS_SECTION).contains(key); + return Lists.newArrayList( + METADATA_SECTION_TAG_SECTION, + SOURCE_TAG_SECTION, + NON_MANO_ARTIFACT_SETS_TAG_SECTION).contains(key.toLowerCase()); } private boolean isNewSection(Pair data) { String key = data.getKey().trim(); String value = data.getValue().trim(); - return key.matches("[a-zA-z_0-9]+") && (value.isEmpty() || value.startsWith("#")); + return key.matches("[a-zA-z_0-9]+") && (value.isEmpty() || startsWith(value,"#")); } -- cgit 1.2.3-korg