From fc645e5bbdaba22c52b08121f754dbb6ee0ed1e6 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Thu, 7 Mar 2019 13:22:15 +0100 Subject: Create pnf scar archive module Change-Id: Iad552af5d4e9f446de9abecdded317ee2065edc4 Issue-ID: VNFSDK-377 Signed-off-by: Zebek Bogumil --- .../main/java/org/onap/cvc/csar/CSARArchive.java | 8 +- .../java/org/onap/cvc/csar/PnfCSARArchive.java | 48 +++++ .../java/org/onap/cvc/csar/PnfManifestParser.java | 220 +++++++++++++++++++++ 3 files changed, 272 insertions(+), 4 deletions(-) create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/PnfCSARArchive.java create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java (limited to 'csarvalidation/src/main/java/org/onap') 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 cfea2b4..fdc8713 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/CSARArchive.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/CSARArchive.java @@ -783,7 +783,7 @@ public class CSARArchive { private Manifest.Metadata metadata = new Metadata(); - private Map> nonMano = new HashMap<>(); + private Map>> nonMano = new HashMap<>(); public Manifest.Metadata getMetadata() { return metadata; @@ -793,11 +793,11 @@ public class CSARArchive { this.metadata = metadata; } - public Map> getNonMano() { + public Map>> getNonMano() { return nonMano; } - public void setNonMano(Map> nonMano) { + public void setNonMano(Map>> nonMano) { this.nonMano = nonMano; } } @@ -923,7 +923,7 @@ public class CSARArchive { } } - private void parseManifest() throws IOException { + void parseManifest() throws IOException { //manifest is optional, so check for it if (this.manifestMfFile == null) { return; diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/PnfCSARArchive.java b/csarvalidation/src/main/java/org/onap/cvc/csar/PnfCSARArchive.java new file mode 100644 index 0000000..bb0a723 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/PnfCSARArchive.java @@ -0,0 +1,48 @@ +/** + * Copyright 2019 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.cvc.csar; + +import org.apache.commons.lang3.tuple.Pair; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class PnfCSARArchive extends CSARArchive { + + @Override + void parseManifest() throws IOException { + + //manifest is optional, so check for it + if (this.getManifestMfFile() == null) { + return; + } + + PnfManifestParser pnfManifestParser = PnfManifestParser.getInstance( + this.getManifestMfFile().getAbsolutePath() + ); + + Pair> metadataData = pnfManifestParser.fetchMetadata(); + Pair>>, List> nonManoArtifactsData = pnfManifestParser.fetchNonManoArtifacts(); + + Manifest manifest = this.getManifest(); + manifest.setMetadata(metadataData.getKey()); + manifest.setNonMano(nonManoArtifactsData.getKey()); + this.getErrors().addAll(metadataData.getValue()); + this.getErrors().addAll(nonManoArtifactsData.getValue()); + } + +} diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java new file mode 100644 index 0000000..a92ebd4 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/PnfManifestParser.java @@ -0,0 +1,220 @@ +/** + * Copyright 2019 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.cvc.csar; + +import com.google.common.collect.Lists; +import org.apache.commons.lang3.tuple.Pair; + +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.stream.Collectors; +import java.util.stream.Stream; + +public class PnfManifestParser { + + + public static final String METADATA_SECTION = "metadata"; + public static final String NON_MANO_ARTIFACT_SETS_SECTION = "non_mano_artifact_sets"; + + private final List lines; + private final String fileName; + + PnfManifestParser(List lines, String fileName) { + this.lines = lines; + this.fileName = fileName; + } + + public static PnfManifestParser getInstance(String fileName) throws IOException { + try (Stream stream = Files.lines(Paths.get(fileName))) { + List lines = stream + .map(String::trim) + .collect(Collectors.toList()); + + return new PnfManifestParser(lines, fileName); + } + } + + public Pair> fetchMetadata() { + CSARArchive.Manifest.Metadata metadata = new CSARArchive.Manifest.Metadata(); + List errors = new ArrayList<>(); + + boolean isMetadataSectionAvailable = false; + int lineNumber = 0; + for (String line : lines) { + lineNumber++; + if (line.trim().isEmpty()){ + continue; + } else if (line.startsWith(METADATA_SECTION)) { + isMetadataSectionAvailable = true; + continue; + }else if (isMetadataSectionAvailable) { + Pair data = parseLine(line); + + if (isNewSection(data)) { + if(!isSectionSupported(data.getKey())) { + errors.add(new PnfCSARErrorWarning(data.getKey(), this.fileName, lineNumber)); + } + break; + } + + handleMetadataLine(metadata, errors, lineNumber, data); + } + } + + if (!isMetadataSectionAvailable) { + errors.add(new PnfCSARErrorEntryMissing(METADATA_SECTION, this.fileName, -1)); + } + + return Pair.of(metadata, errors); + + } + + public Pair>>, List> fetchNonManoArtifacts() { + Map>> nonManoArtifacts = new HashMap<>(); + List errors = new ArrayList<>(); + + boolean isNonManoArtifactsSectionAvailable = false; + String attributeName = null; + + for (String line : lines) { + + if (line.trim().isEmpty()) { + continue; + } else if (line.startsWith(NON_MANO_ARTIFACT_SETS_SECTION)) { + isNonManoArtifactsSectionAvailable = true; + continue; + } else if (isNonManoArtifactsSectionAvailable) { + Pair data = parseLine(line); + + if (isNewSection(data)) { + attributeName = data.getKey(); + continue; + } + + handleNonManoArtifactLine(nonManoArtifacts, attributeName, data); + } + } + + if (!isNonManoArtifactsSectionAvailable) { + errors.add(new PnfCSARErrorEntryMissing(NON_MANO_ARTIFACT_SETS_SECTION, this.fileName, -1)); + } + + return Pair.of(nonManoArtifacts, errors); + } + + private void handleMetadataLine( + CSARArchive.Manifest.Metadata metadata, + List errors, + int lineNumber, + Pair data) { + + String paramName = data.getKey(); + String value = data.getValue(); + + switch (paramName) { + case "pnf_product_name": + metadata.setProductName(value); + break; + case "pnf_provider_id": + metadata.setProviderId(value); + break; + case "pnf_package_version": + metadata.setPackageVersion(value); + break; + case "pnf_release_date_time": + metadata.setReleaseDateTime(value); + break; + default: + errors.add(new PnfCSARErrorInvalidEntry( + paramName, + this.fileName, + lineNumber)); + break; + } + } + + private void handleNonManoArtifactLine( + Map>> nonManoArtifacts, + String attributeName, + Pair data) { + + String key = data.getKey(); + String value = data.getValue(); + + Map> attributeWithValues = nonManoArtifacts.getOrDefault(attributeName, new HashMap<>()); + List 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, NON_MANO_ARTIFACT_SETS_SECTION).contains(key); + } + + + private boolean isNewSection(Pair data) { + String value = data.getValue().trim(); + return value.isEmpty() || value.startsWith("#"); + } + + + private Pair 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, ""); + + + } + + private static class PnfCSARError extends CSARArchive.CSARError { + public PnfCSARError(String errorCode, String message, int lineNumber, String file) { + super(errorCode); + this.message = message; + this.file = file; + this.lineNumber = lineNumber; + } + } + + public static class PnfCSARErrorInvalidEntry extends PnfCSARError { + public PnfCSARErrorInvalidEntry(String entry, String file, int lineNumber) { + super("0x2000", "Invalid. Entry [" + entry + "]", lineNumber, file); + } + } + + private static class PnfCSARErrorWarning extends PnfCSARError { + public PnfCSARErrorWarning(String entry, String file, int lineNumber) { + super("0x2001", "Warning. Entry [" + entry + "]", lineNumber, file); + } + } + + private static class PnfCSARErrorEntryMissing extends PnfCSARError { + public PnfCSARErrorEntryMissing(String entry, String file, int lineNumber) { + super("0x2002", "Missing. Entry [" + entry + "]", lineNumber, file); + } + } + +} -- cgit 1.2.3-korg