diff options
author | 2019-07-25 07:01:22 +0000 | |
---|---|---|
committer | 2019-07-25 07:01:22 +0000 | |
commit | dc6775fbc0779eb2db77ab8215fcb7847b94a4cf (patch) | |
tree | 13b2a2cc1f1b37838cf2ad1945546d44a015befd /csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java | |
parent | 2a03c94357cb7f77e99f161c136f569ce84d32e9 (diff) | |
parent | b67f6263e48bfcf51c157a9cd7fe774353cc5f38 (diff) |
Merge "Option 1 - pnf only"
Diffstat (limited to 'csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java')
-rw-r--r-- | csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java | 209 |
1 files changed, 23 insertions, 186 deletions
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 8831082..250aa4f 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java @@ -15,18 +15,16 @@ */ package org.onap.cvc.csar; -import com.google.common.collect.Lists; import org.apache.commons.lang3.tuple.Pair; -import org.onap.cvc.csar.PnfCSARError.PnfCSARErrorEntryMissing; -import org.onap.cvc.csar.PnfCSARError.PnfCSARErrorInvalidEntry; -import org.onap.cvc.csar.PnfCSARError.PnfCSARErrorWarning; +import org.onap.cvc.csar.parser.CmsParser; +import org.onap.cvc.csar.parser.MetadataParser; +import org.onap.cvc.csar.parser.NonManoArtifactsParser; +import org.onap.cvc.csar.parser.SourcesParser; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -35,21 +33,20 @@ import java.util.stream.Stream; class PnfManifestParser { + private final List<String> lines; - 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"; - private static final String RELEASE_DATE_TIME = "pnfd_release_date_time"; + private final MetadataParser metadataParser; + private final SourcesParser sourcesParser; + private final NonManoArtifactsParser nonManoArtifactsParser; + private final CmsParser cmsParser; - private final List<String> lines; - private final String fileName; - PnfManifestParser(List<String> lines, String fileName) { + PnfManifestParser(List<String> lines, MetadataParser metadataParser, SourcesParser sourcesParser, NonManoArtifactsParser nonManoArtifactsParser, CmsParser cmsParser) { this.lines = lines; - this.fileName = fileName; + this.metadataParser = metadataParser; + this.sourcesParser = sourcesParser; + this.nonManoArtifactsParser = nonManoArtifactsParser; + this.cmsParser = cmsParser; } static PnfManifestParser getInstance(File pnfManifestFile) throws IOException { @@ -59,188 +56,28 @@ class PnfManifestParser { .map(String::trim) .collect(Collectors.toList()); - return new PnfManifestParser(lines, pnfManifestFile.getName()); + final String pnfManifestFileName = pnfManifestFile.getName(); + return new PnfManifestParser(lines, new MetadataParser(pnfManifestFileName), new SourcesParser(pnfManifestFileName), new NonManoArtifactsParser(), new CmsParser(pnfManifestFileName)); } } Pair<CSARArchive.Manifest.Metadata, List<CSARArchive.CSARError>> fetchMetadata() { - CSARArchive.Manifest.Metadata metadata = new CSARArchive.Manifest.Metadata(); - List<CSARArchive.CSARError> errors = new ArrayList<>(); - - boolean isMetadataSectionAvailable = false; - - for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) { - String line = lines.get(lineNumber); - Pair<String, String> data = parseLine(line); - - if(data.getKey().toLowerCase().equals(METADATA_SECTION_TAG_SECTION)) { - isMetadataSectionAvailable = true; - }else if (isMetadataSectionAvailable && !isLineExcluded(line)) { - - if (shouldStopProcessing(data, errors, lineNumber)) { - break; - } - - handleMetadataLine(metadata, errors, lineNumber, data); - } - } - - if (!isMetadataSectionAvailable) { - errors.add(new PnfCSARErrorEntryMissing(METADATA_SECTION_TAG_SECTION, this.fileName, -1)); - } - - return Pair.of(metadata, errors); - - } - - Pair<List<String>, List<CSARArchive.CSARError>> fetchSourcesSection() { - List<String> sources = new ArrayList<>(); - List<CSARArchive.CSARError> 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<String, String> 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); - } - - Optional<Pair<Map<String, Map<String, List<String>>>, List<CSARArchive.CSARError>>> fetchNonManoArtifacts() { - Map<String, Map<String, List<String>>> nonManoArtifacts = new HashMap<>(); - List<CSARArchive.CSARError> errors = new ArrayList<>(); - - boolean isNonManoArtifactsSectionAvailable = false; - String attributeName = null; - - for (String line : lines) { - - if (startsWith(line, NON_MANO_ARTIFACT_SETS_TAG_SECTION)) { - isNonManoArtifactsSectionAvailable = true; - } else if (isNonManoArtifactsSectionAvailable) { - Pair<String, String> data = parseLine(line); + return this.metadataParser.parse(this.lines); - if (isNewSection(data)) { - attributeName = data.getKey(); - nonManoArtifacts.put(attributeName, new HashMap<>()); - continue; - } - - handleNonManoArtifactLine(nonManoArtifacts, attributeName, data); - } - } - - if (!isNonManoArtifactsSectionAvailable) { - return Optional.empty(); - } - - return Optional.of(Pair.of(nonManoArtifacts, errors)); - } - - private boolean isLineExcluded(String line) { - return line.trim().isEmpty() - || startsWith(line, "#") - || startsWith(line,SOURCE_TAG_SECTION); - } - - private boolean shouldStopProcessing(Pair<String, String> data, List<CSARArchive.CSARError> 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<CSARArchive.CSARError> errors, - int lineNumber, - Pair<String, String> data) { - - String paramName = data.getKey(); - String value = data.getValue(); - - switch (paramName) { - case PRODUCT_NAME: - metadata.setProductName(value); - break; - case PROVIDER_ID: - metadata.setProviderId(value); - break; - case VERSION: - metadata.setPackageVersion(value); - break; - case RELEASE_DATE_TIME: - metadata.setReleaseDateTime(value); - break; - default: - errors.add(new PnfCSARErrorInvalidEntry( - paramName, - this.fileName, - lineNumber)); - break; - } + Pair<List<SourcesParser.Source>, List<CSARArchive.CSARError>> fetchSourcesSection() { + return this.sourcesParser.parse(this.lines); } - private void handleNonManoArtifactLine( - Map<String, Map<String, List<String>>> nonManoArtifacts, - String attributeName, - Pair<String, String> data) { - - String key = data.getKey(); - String value = data.getValue(); - Map<String, List<String>> attributeWithValues = nonManoArtifacts.getOrDefault(attributeName, new HashMap<>()); - List<String> values = attributeWithValues.getOrDefault(key, new ArrayList<>()); - values.add(value); - attributeWithValues.put(key, values); - nonManoArtifacts.put(attributeName, attributeWithValues); - } - - private boolean isSectionSupported(String key) { - return Lists.newArrayList( - METADATA_SECTION_TAG_SECTION, - SOURCE_TAG_SECTION, - NON_MANO_ARTIFACT_SETS_TAG_SECTION).contains(key.toLowerCase()); + Optional<Pair<Map<String, Map<String, List<String>>>, List<CSARArchive.CSARError>>> fetchNonManoArtifacts() { + return this.nonManoArtifactsParser.parse(this.lines); } - private boolean isNewSection(Pair<String, String> data) { - String key = data.getKey().trim(); - String value = data.getValue().trim(); - return key.matches("[a-zA-z_0-9]+") && (value.isEmpty() || startsWith(value,"#")); + Pair<String, List<CSARArchive.CSARError>> fetchCMS() { + return this.cmsParser.parse(this.lines); } - - private Pair<String, String> parseLine(String line) { - String[] elements = line.split(": "); - if (elements.length == 2) - return Pair.of(elements[0], elements[1]); - - if (line.endsWith(":")) - return Pair.of(line.substring(0, line.length() - 1), ""); - else - return Pair.of(line, ""); - - - } } |