From b67f6263e48bfcf51c157a9cd7fe774353cc5f38 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Wed, 3 Jul 2019 13:48:25 +0200 Subject: Option 1 - pnf only Change-Id: I9af3bb9b0682d7babed16042c5cac948db5dd822 Issue-ID: VNFSDK-396 Signed-off-by: Zebek Bogumil --- .../java/org/onap/cvc/csar/parser/CmsParser.java | 89 +++++++++++ .../org/onap/cvc/csar/parser/ManifestConsts.java | 38 +++++ .../org/onap/cvc/csar/parser/ManifestLine.java | 57 +++++++ .../org/onap/cvc/csar/parser/MetadataParser.java | 135 +++++++++++++++++ .../cvc/csar/parser/NonManoArtifactsParser.java | 85 +++++++++++ .../org/onap/cvc/csar/parser/SourcesParser.java | 165 +++++++++++++++++++++ 6 files changed, 569 insertions(+) create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/parser/CmsParser.java create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestLine.java create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/parser/MetadataParser.java create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/parser/NonManoArtifactsParser.java create mode 100644 csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java (limited to 'csarvalidation/src/main/java/org/onap/cvc/csar/parser') diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/CmsParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/CmsParser.java new file mode 100644 index 0000000..b1bf4b4 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/CmsParser.java @@ -0,0 +1,89 @@ +/* + * 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.parser; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.cvc.csar.CSARArchive; +import org.onap.cvc.csar.PnfCSARError; + +import java.util.ArrayList; +import java.util.List; + +import static org.onap.cvc.csar.parser.ManifestConsts.*; + + +public class CmsParser { + + + private final String fileName; + + public CmsParser(String fileName) { + this.fileName = fileName; + } + + public Pair> parse(List lines){ + StringBuilder buf = new StringBuilder(); + List errors = new ArrayList<>(); + + boolean isSpecialTagReached = false; + boolean cmsSectionParsing = false; + boolean endCmsMarkerReached = false; + boolean atEndFile = true; + + + for (String line : lines) { + ManifestLine manifestLine = ManifestLine.of(line); + if (cmsSectionParsing && (manifestLine.startsWith(METADATA_SECTION_TAG_SECTION) + || manifestLine.startsWith(NON_MANO_ARTIFACT_SETS_TAG_SECTION) + || manifestLine.startsWith(SOURCE_TAG_SECTION))) { + isSpecialTagReached = true; + } else if (!isSpecialTagReached && line.contains(BEGIN_CMS_SECTION)) { + cmsSectionParsing = true; + } else if (!isSpecialTagReached && line.contains(END_CMS_SECTION)) { + if(!cmsSectionParsing){ + errors.add(new PnfCSARError.PnfCSARErrorInvalidEntry("Unable to find BEGIN CMS marker!", this.fileName, -1)); + break; + } + cmsSectionParsing = false; + endCmsMarkerReached = true; + } else if (cmsSectionParsing){ + buf.append(line); + } else if(endCmsMarkerReached) { + atEndFile = false; + } + } + + if(!atEndFile){ + errors.add(new PnfCSARError.PnfCSARErrorInvalidEntry("CMS section is not at the end of file!", this.fileName, -1)); + } + + return constructResponse(buf, errors, cmsSectionParsing, endCmsMarkerReached); + } + + private Pair> constructResponse(StringBuilder buf, List errors, boolean cmsSectionParsing, boolean endCmsMarkerReached) { + if(endCmsMarkerReached) { + return Pair.of(buf.toString(), errors); + } else if(cmsSectionParsing) { + errors.add(new PnfCSARError.PnfCSARErrorInvalidEntry("Unable to find END CMS marker!", this.fileName, -1)); + return Pair.of("",errors); + } else { + return Pair.of("",errors); + } + } + +} diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java new file mode 100644 index 0000000..da17317 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestConsts.java @@ -0,0 +1,38 @@ +/* + * 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.parser; + + +final class ManifestConsts { + + private ManifestConsts(){} + + static final String METADATA_SECTION_TAG_SECTION = "metadata"; + static final String SOURCE_TAG_SECTION = "source"; + static final String ALGORITHM = "algorithm"; + static final String HASH = "hash"; + static final String NON_MANO_ARTIFACT_SETS_TAG_SECTION = "non_mano_artifact_sets"; + static final String PRODUCT_NAME = "pnfd_name"; + static final String PROVIDER_ID = "pnfd_provider"; + static final String VERSION = "pnfd_archive_version"; + static final String RELEASE_DATE_TIME = "pnfd_release_date_time"; + static final String CMS = "CMS"; + static final String BEGIN_CMS_SECTION = "BEGIN CMS"; + static final String END_CMS_SECTION = "END CMS"; + +} diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestLine.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestLine.java new file mode 100644 index 0000000..390c534 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/ManifestLine.java @@ -0,0 +1,57 @@ +/* + * 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.parser; + +import org.apache.commons.lang3.tuple.Pair; + +public class ManifestLine { + + private final String line; + + private ManifestLine(String line) { + this.line = line; + } + + public static ManifestLine of(String line) { + return new ManifestLine(line); + } + + public Pair parse() { + 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, ""); + } + + + } + + boolean startsWith(String word) { + return line.trim().toLowerCase().startsWith(word); + } + + boolean isEmpty() { + return line.trim().isEmpty(); + } + +} diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/MetadataParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/MetadataParser.java new file mode 100644 index 0000000..b0c06ee --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/MetadataParser.java @@ -0,0 +1,135 @@ +/* + * 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.parser; + +import com.google.common.collect.Lists; +import org.apache.commons.lang3.tuple.Pair; +import org.onap.cvc.csar.CSARArchive; +import org.onap.cvc.csar.PnfCSARError; + +import java.util.ArrayList; +import java.util.List; + +import static org.onap.cvc.csar.parser.ManifestConsts.*; + +public class MetadataParser { + + private final String fileName; + + public MetadataParser(String fileName) { + this.fileName = fileName; + } + + public Pair> parse(List lines) { + CSARArchive.Manifest.Metadata metadata = new CSARArchive.Manifest.Metadata(); + List errors = new ArrayList<>(); + + boolean isMetadataSectionAvailable = false; + + for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) { + String line = lines.get(lineNumber); + ManifestLine manifestLine = ManifestLine.of(line); + Pair data = manifestLine.parse(); + + if (data.getKey().equalsIgnoreCase(METADATA_SECTION_TAG_SECTION)) { + isMetadataSectionAvailable = true; + } else if (isMetadataSectionAvailable && !isLineExcluded(manifestLine)) { + + if (shouldStopProcessing(data, errors, lineNumber)) { + break; + } + + handleMetadataLine(metadata, errors, lineNumber, data); + } + } + + if (!isMetadataSectionAvailable) { + errors.add(new PnfCSARError.PnfCSARErrorEntryMissing(METADATA_SECTION_TAG_SECTION, this.fileName, -1)); + } + + return Pair.of(metadata, errors); + + } + + private boolean isLineExcluded(ManifestLine line) { + return line.isEmpty() + || line.startsWith("#") + || line.startsWith(SOURCE_TAG_SECTION); + } + + private boolean shouldStopProcessing(Pair data, List errors, int lineNumber) { + if (isNewSection(data) || isSourceSection(data)) { + if (!isSectionSupported(data.getKey())) { + errors.add(new PnfCSARError.PnfCSARErrorWarning(data.getKey(), this.fileName, lineNumber)); + } + return true; + } + return false; + } + + 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() || ManifestLine.of(value).startsWith("#")); + } + + private boolean isSourceSection(Pair data) { + return data.getKey().equalsIgnoreCase(SOURCE_TAG_SECTION) + || data.getKey().equalsIgnoreCase(ALGORITHM) + || data.getKey().equalsIgnoreCase(HASH); + } + + private boolean isSectionSupported(String key) { + return Lists.newArrayList( + METADATA_SECTION_TAG_SECTION, + SOURCE_TAG_SECTION, ALGORITHM, HASH, + NON_MANO_ARTIFACT_SETS_TAG_SECTION).contains(key.toLowerCase()); + } + + private void handleMetadataLine( + CSARArchive.Manifest.Metadata metadata, + List errors, + int lineNumber, + Pair 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 PnfCSARError.PnfCSARErrorInvalidEntry( + paramName, + this.fileName, + lineNumber)); + break; + } + } + +} diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/NonManoArtifactsParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/NonManoArtifactsParser.java new file mode 100644 index 0000000..d27ef68 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/NonManoArtifactsParser.java @@ -0,0 +1,85 @@ +/* + * 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.parser; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.cvc.csar.CSARArchive; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.onap.cvc.csar.parser.ManifestConsts.NON_MANO_ARTIFACT_SETS_TAG_SECTION; + +public class NonManoArtifactsParser { + + public Optional>>, List>> parse(List lines) { + Map>> nonManoArtifacts = new HashMap<>(); + List errors = new ArrayList<>(); + + boolean isNonManoArtifactsSectionAvailable = false; + String attributeName = null; + + for (String line : lines) { + ManifestLine manifestLine = ManifestLine.of(line); + if (manifestLine.startsWith(NON_MANO_ARTIFACT_SETS_TAG_SECTION)) { + isNonManoArtifactsSectionAvailable = true; + } else if (isNonManoArtifactsSectionAvailable) { + Pair data = manifestLine.parse(); + + 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 isNewSection(Pair data) { + String key = data.getKey().trim(); + String value = data.getValue().trim(); + return key.matches("[a-zA-z_0-9]+") && (value.isEmpty() || ManifestLine.of(value).startsWith("#")); + } + + 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); + } + +} diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java new file mode 100644 index 0000000..5f3f0d7 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/parser/SourcesParser.java @@ -0,0 +1,165 @@ +/* + * 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.parser; + +import org.apache.commons.lang3.tuple.Pair; +import org.onap.cvc.csar.CSARArchive; +import org.onap.cvc.csar.PnfCSARError; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static org.onap.cvc.csar.parser.ManifestConsts.*; + +public class SourcesParser { + + private final String fileName; + + public SourcesParser(String fileName) { + this.fileName = fileName; + } + + public Pair, List> parse(List lines) { + List sources = new ArrayList<>(); + List errors = new ArrayList<>(); + boolean isSpecialTagReached = false; + boolean sourceSectionParsing = false; + Source source = null; + + for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) { + String line = lines.get(lineNumber); + ManifestLine manifestLine = ManifestLine.of(line); + if (sourceSectionParsing && (manifestLine.startsWith(METADATA_SECTION_TAG_SECTION) + || manifestLine.startsWith(NON_MANO_ARTIFACT_SETS_TAG_SECTION) + || line.contains(CMS))) { + isSpecialTagReached = true; + } else if (!isSpecialTagReached && manifestLine.startsWith(SOURCE_TAG_SECTION)) { + sourceSectionParsing = true; + source = handleSourceLine(sources, errors, lineNumber, manifestLine); + } else if (!isSpecialTagReached && manifestLine.startsWith(ALGORITHM)) { + handleAlgorithmLine(errors, source, lineNumber, manifestLine); + } else if (!isSpecialTagReached && manifestLine.startsWith(HASH)) { + handleHashLine(errors, source, lineNumber, manifestLine); + } + } + + return Pair.of(sources, errors); + } + + private Source handleSourceLine(List sources, List errors, int lineNumber, ManifestLine manifestLine) { + Source source; + String value = parseSourceSectionLine(manifestLine, lineNumber, errors); + if (!value.isEmpty()) { + source = new Source(value); + sources.add(source); + } else { + source = null; + } + return source; + } + + private void handleAlgorithmLine(List errors, Source source, int lineNumber, ManifestLine manifestLine) { + String algorithm = parseSourceSectionLine(manifestLine, lineNumber, errors); + if (source != null) + source.setAlgorithm(algorithm); + } + + private void handleHashLine(List errors, Source source, int lineNumber, ManifestLine manifestLine) { + String hash = parseSourceSectionLine(manifestLine, lineNumber, errors); + if (source != null) + source.setHash(hash); + } + + private String parseSourceSectionLine(ManifestLine line, int lineNumber, List errors) { + String retVal = ""; + Pair data = line.parse(); + + String value = data.getValue(); + if (value.isEmpty()) { + errors.add(new PnfCSARError.PnfCSARErrorWarning(data.getKey(), this.fileName, lineNumber)); + } else { + retVal = value; + } + + return retVal; + } + + public static class Source { + + + private final String value; + private String algorithm; + private String hash; + + public Source(String value, String algorithm, String hash) { + + this.value = value; + this.algorithm = algorithm; + this.hash = hash; + } + + public Source(String source) { + this(source, "", ""); + } + + public String getValue() { + return value; + } + + public String getAlgorithm() { + return algorithm; + } + + public void setAlgorithm(String algorithm) { + this.algorithm = algorithm; + } + + public String getHash() { + return hash; + } + + public void setHash(String hash) { + this.hash = hash; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Source source1 = (Source) o; + return Objects.equals(value, source1.value) && + Objects.equals(algorithm, source1.algorithm) && + Objects.equals(hash, source1.hash); + } + + @Override + public int hashCode() { + return Objects.hash(value, algorithm, hash); + } + + @Override + public String toString() { + return "Source{" + + "value='" + value + '\'' + + ", algorithm='" + algorithm + '\'' + + ", hash='" + hash + '\'' + + '}'; + } + } +} -- cgit 1.2.3-korg