diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/main')
8 files changed, 741 insertions, 220 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java index a0c346c677..7cc11e564d 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java @@ -19,10 +19,6 @@ package org.openecomp.sdc.tosca.csar; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.common.errors.Messages; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -35,21 +31,27 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Optional; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang.StringUtils; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; -import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; -import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA; -import static org.openecomp.sdc.tosca.csar.CSARConstants.METADATA_MF_ATTRIBUTE; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE; - - abstract class AbstractOnboardingManifest implements Manifest{ +abstract class AbstractOnboardingManifest implements Manifest { - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractOnboardingManifest.class); - private static final int MAX_ALLOWED_MANIFEST_META_ENTRIES = 4; + protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractOnboardingManifest.class); + protected static final int MAX_ALLOWED_MANIFEST_META_ENTRIES = 4; protected Map<String, String> metadata; protected List<String> sources; - protected List<String> errors; protected Map<String, List<String>> nonManoSources; - protected ResourceTypeEnum type; + protected Map<String, AlgorithmDigest> sourceAndChecksumMap = new HashMap<>(); + protected String cmsSignature; + protected List<String> errors; + protected boolean continueToProcess; + protected String currentLine; + protected Iterator<String> linesIterator; + protected int currentLineNumber; protected AbstractOnboardingManifest() { errors = new ArrayList<>(); @@ -59,122 +61,327 @@ import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE; } @Override - public Optional<ResourceTypeEnum> getType(){ - if(errors.isEmpty() && !metadata.isEmpty() && metadata.size() == MAX_ALLOWED_MANIFEST_META_ENTRIES) { - for (String key : metadata.keySet()) { - if (MANIFEST_PNF_METADATA.stream().anyMatch(key::equals)) { - return Optional.of(ResourceTypeEnum.PNF); - } - return Optional.of(ResourceTypeEnum.VF); - } + public Optional<ResourceTypeEnum> getType() { + if (!isValid()) { + return Optional.empty(); } - return Optional.empty(); + final String firstKey = metadata.keySet().iterator().next(); + final ManifestTokenType manifestTokenType = ManifestTokenType.parse(firstKey).orElse(null); + if (manifestTokenType == null) { + return Optional.empty(); + } + if (manifestTokenType.isMetadataPnfEntry()) { + return Optional.of(ResourceTypeEnum.PNF); + } + return Optional.of(ResourceTypeEnum.VF); } @Override - public void parse(InputStream is) { + public void parse(final InputStream manifestAsStream) { try { - ImmutableList<String> lines = readAllLines(is); + final ImmutableList<String> lines = readAllLines(manifestAsStream); + continueToProcess = true; + currentLineNumber = 0; processManifest(lines); - } catch (IOException e){ - LOGGER.error(e.getMessage(),e); + } catch (IOException e) { + LOGGER.error(e.getMessage(), e); errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage()); } } - protected void processManifest(ImmutableList<String> lines) { - if (isEmptyManifest(lines)){ + /** + * Process the manifest lines, reporting an error when detected. + * + * @param lines the manifest lines + */ + protected void processManifest(final ImmutableList<String> lines) { + if (isEmptyManifest(lines)) { return; } - Iterator<String> iterator = lines.iterator(); - //SOL004 #4.3.2: The manifest file shall start with the package metadata - String line = iterator.next(); - if (!isMetadata(line)) { + linesIterator = lines.iterator(); + readNextNonEmptyLine(); + if (!getCurrentLine().isPresent()) { + errors.add(Messages.MANIFEST_EMPTY.getErrorMessage()); return; } - //handle metadata - processMetadata(iterator); - if (errors.isEmpty() && metadata.isEmpty()) { - errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage()); - } - } - - protected abstract void processMetadata(Iterator<String> iterator); - - protected boolean isEmptyLine(Iterator<String> iterator, String line) { - if(line.isEmpty()){ - processMetadata(iterator); - return true; - } - return false; - } - - protected boolean isInvalidLine(String line, String[] metaSplit) { - if (metaSplit.length < 2){ - reportError(line); - return true; - } - return false; - } - - protected boolean isMetadata(String line) { - if(line.trim().equals(METADATA_MF_ATTRIBUTE + SEPARATOR_MF_ATTRIBUTE)){ - return true; - } - reportError(line); - return false; - } - - protected boolean isEmptyManifest(ImmutableList<String> lines) { - if(lines == null || lines.isEmpty()){ - errors.add(Messages.MANIFEST_EMPTY.getErrorMessage()); - return true; - } - return false; - } - - protected void reportError(String line) { - errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line)); - } - - protected ImmutableList<String> readAllLines(InputStream is) throws IOException { - if(is == null){ - throw new IOException("Input Stream cannot be null!"); - } - ImmutableList.Builder<String> builder = ImmutableList.<String> builder(); - try (BufferedReader bufferedReader = new BufferedReader( - new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder()))) { + + processMetadata(); + processBody(); + } + + /** + * Process the metadata part of the Manifest file. + */ + protected abstract void processMetadata(); + + /** + * Process the other parts from manifest different than metadata. + */ + protected abstract void processBody(); + + /** + * Read the manifest as a list of lines. + * + * @param manifestAsStream The manifest file input stream + * @return The manifest as a list of string + * @throws IOException when the input stream is null or a read problem happened. + */ + protected ImmutableList<String> readAllLines(final InputStream manifestAsStream) throws IOException { + if (manifestAsStream == null) { + throw new IOException("Manifest Input Stream cannot be null."); + } + final ImmutableList.Builder<String> builder = ImmutableList.builder(); + try (final BufferedReader bufferedReader = new BufferedReader( + new InputStreamReader(manifestAsStream, StandardCharsets.UTF_8.newDecoder()))) { bufferedReader.lines().forEach(builder::add); } return builder.build(); } + /** + * Checks if the line is a {@link ManifestTokenType#METADATA} entry. + * + * @param line The line to check + * @return {@code true} if the line is a 'metadata' entry, {@code false} otherwise. + */ + protected boolean isMetadata(final String line) { + return line.trim() + .equals(ManifestTokenType.METADATA.getToken() + ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR.getToken()); + } + + /** + * Checks if the the entry is a valid metadata entry. + * + * @param metadataEntry the entry to be evaluated + * @return {@code true} if the entry is a valid metadata entry, {@code false} otherwise. + */ + protected boolean isMetadataEntry(final String metadataEntry) { + final Optional<ManifestTokenType> manifestTokenType = ManifestTokenType.parse(metadataEntry); + return manifestTokenType.map(ManifestTokenType::isMetadataEntry).orElse(false); + } + + /** + * Checks if the manifest is empty + * + * @param lines the manifest parsed as a string lines list + * @return {@code true} if the manifest is empty, {@code false} otherwise. + */ + protected boolean isEmptyManifest(final ImmutableList<String> lines) { + if (lines == null || lines.isEmpty()) { + errors.add(Messages.MANIFEST_EMPTY.getErrorMessage()); + return true; + } + return false; + } + + /** + * Reports a manifest invalid line error occurred in the current line. + */ + protected void reportInvalidLine() { + reportInvalidLine(currentLineNumber, getCurrentLine().orElse("")); + } + + /** + * Reports a manifest invalid line error. + * + * @param lineNumber the line number + * @param line the line + */ + protected void reportInvalidLine(final int lineNumber, final String line) { + errors.add(Messages.MANIFEST_INVALID_LINE.formatMessage(lineNumber, line)); + } + + /** + * Reports a manifest error occurred in the current line. + * + * @param message The error message + * @param params The message params + */ + protected void reportError(final Messages message, final Object... params) { + reportError(currentLineNumber, getCurrentLine().orElse(""), message, params); + } + + /** + * Reports a manifest error occurred in the specified line. + * + * @param lineNumber The line number + * @param line The line + * @param message The error message + * @param params The message params + */ + protected void reportError(final int lineNumber, final String line, final Messages message, + final Object... params) { + errors.add(Messages.MANIFEST_ERROR_WITH_LINE.formatMessage(message.formatMessage(params), lineNumber, line)); + } + + /** + * Checks if the manifest is valid. + * + * @return {@code true} if the manifest is valid, {@code false} otherwise. + */ + public boolean isValid() { + return errors.isEmpty(); + } + + /** + * Reads the next non empty line in the manifest. Updates the current line and line number. + * + * @return the next non empty line. If there is no more lines, an empty value. + */ + protected Optional<String> readNextNonEmptyLine() { + while (linesIterator.hasNext()) { + final String line = linesIterator.next().trim(); + currentLineNumber++; + if (!line.isEmpty()) { + currentLine = line; + return getCurrentLine(); + } + currentLine = null; + } + + if (getCurrentLine().isPresent()) { + currentLineNumber++; + currentLine = null; + } + + return getCurrentLine(); + } + + /** + * Gets the current line. + * + * @return the current line. + */ + protected Optional<String> getCurrentLine() { + return Optional.ofNullable(currentLine); + } + + /** + * Reads the current line entry name. The entry name and value must be separated by {@link + * ManifestTokenType#ATTRIBUTE_VALUE_SEPARATOR}. + * + * @return the entry value + */ + protected Optional<String> readCurrentEntryName() { + final Optional<String> line = getCurrentLine(); + if (line.isPresent()) { + return readEntryName(line.get()); + } + + return Optional.empty(); + } + + /** + * Read a entry name. The entry name and value must be separated by {@link ManifestTokenType#ATTRIBUTE_VALUE_SEPARATOR}. + * + * @param line the entry line + * @return returns the entry name + */ + protected Optional<String> readEntryName(final String line) { + if (StringUtils.isEmpty(line)) { + return Optional.empty(); + } + if (!line.contains(ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR.getToken())) { + return Optional.empty(); + } + final String attribute = line.substring(0, line.indexOf(ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR.getToken())).trim(); + if (StringUtils.isEmpty(attribute)) { + return Optional.empty(); + } + + return Optional.of(attribute); + } + + /** + * Reads the current line entry value. The entry name and value must be separated by {@link + * ManifestTokenType#ATTRIBUTE_VALUE_SEPARATOR}. + * + * @return the entry value + */ + protected Optional<String> readCurrentEntryValue() { + final Optional<String> line = getCurrentLine(); + if (line.isPresent()) { + return readEntryValue(line.get()); + } + + return Optional.empty(); + } + + /** + * Reads a entry value. The entry name and value must be separated by {@link ManifestTokenType#ATTRIBUTE_VALUE_SEPARATOR}. + * + * @param line the entry line + * @return the entry value + */ + protected Optional<String> readEntryValue(final String line) { + if (StringUtils.isEmpty(line)) { + return Optional.empty(); + } + if (!line.contains(ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR.getToken())) { + return Optional.empty(); + } + final String value = line.substring(line.indexOf(ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR.getToken()) + 1).trim(); + if (StringUtils.isEmpty(value)) { + return Optional.empty(); + } + + return Optional.of(value); + } + + /** + * Adds a entry to the metadata map. Only accepts new entries. If the entry is duplicated a manifest error is + * reported. + * + * @param entry the metadata entry + * @param value the entry value + * @return {@code true} if the entry was added, {@code false} otherwise. + */ + protected boolean addToMetadata(final String entry, final String value) { + if (metadata.containsKey(entry)) { + reportError(Messages.MANIFEST_METADATA_DUPLICATED_ENTRY, entry); + return false; + } + + metadata.put(entry, value); + return true; + } + + public List<String> getErrors() { + return ImmutableList.copyOf(errors); + } + public Map<String, String> getMetadata() { - if (!isValid()){ + if (!isValid()) { return Collections.emptyMap(); } return ImmutableMap.copyOf(metadata); } public List<String> getSources() { - if (!isValid()){ + if (!isValid()) { return Collections.emptyList(); } return ImmutableList.copyOf(sources); } - public List<String> getErrors() { - return ImmutableList.copyOf(errors); + public Map<String, List<String>> getNonManoSources() { + if (!isValid()) { + return Collections.emptyMap(); + } + return ImmutableMap.copyOf(nonManoSources); } - public boolean isValid() { - return errors.isEmpty(); + @Override + public Optional<String> getCmsSignature() { + return Optional.ofNullable(cmsSignature); } - public Map<String, List<String>> getNonManoSources() { - if (!isValid()){ - return Collections.emptyMap(); + @Override + public Optional<Map<String, AlgorithmDigest>> getSourceAndChecksumMap() { + if (MapUtils.isEmpty(sourceAndChecksumMap)) { + return Optional.empty(); } - return ImmutableMap.copyOf(nonManoSources); + + return Optional.of(ImmutableMap.copyOf(sourceAndChecksumMap)); } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AlgorithmDigest.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AlgorithmDigest.java new file mode 100644 index 0000000000..8ce0fb650b --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AlgorithmDigest.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.tosca.csar; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * Represents a manifest Hash algorithm and Digest + */ +@Getter +@AllArgsConstructor +public class AlgorithmDigest { + private final String hashAlgorithm; + private final String digest; +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java index 7a42758322..1de91f9549 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java @@ -18,6 +18,14 @@ package org.openecomp.sdc.tosca.csar; import static com.google.common.collect.ImmutableSet.of; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_ARCHIVE_VERSION; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_NAME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_PROVIDER; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_RELEASE_DATE_TIME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNF_PACKAGE_VERSION; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNF_PRODUCT_NAME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNF_PROVIDER_ID; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNF_RELEASE_DATE_TIME; import com.google.common.collect.ImmutableSet; @@ -34,37 +42,21 @@ public class CSARConstants { public static final String TOSCA_META_CSAR_VERSION_ENTRY = "CSAR-Version"; public static final String TOSCA_META_CREATED_BY_ENTRY = "Created-By"; public static final String TOSCA_META_ENTRY_DEFINITIONS ="Entry-Definitions"; - public static final String TOSCA_META_ETSI_ENTRY_MANIFEST ="ETSI-Entry-Manifest"; - public static final String TOSCA_META_ETSI_ENTRY_CHANGE_LOG ="ETSI-Entry-Change-Log"; + public static final String TOSCA_META_ETSI_ENTRY_MANIFEST = "ETSI-Entry-Manifest"; + public static final String TOSCA_META_ETSI_ENTRY_CHANGE_LOG = "ETSI-Entry-Change-Log"; public static final String TOSCA_META_ETSI_ENTRY_TESTS = "ETSI-Entry-Tests"; public static final String TOSCA_META_ETSI_ENTRY_LICENSES = "ETSI-Entry-Licenses"; public static final String TOSCA_META_ETSI_ENTRY_CERTIFICATE = "ETSI-Entry-Certificate"; public static final ImmutableSet<String> ELIGIBLE_FILES = of(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME,MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME); - public static final String PNFD_PROVIDER = "pnfd_provider"; - public static final String PNFD_NAME = "pnfd_name"; - public static final String PNFD_RELEASE_DATE_TIME = "pnfd_release_date_time"; - public static final String PNFD_ARCHIVE_VERSION = "pnfd_archive_version"; public static final ImmutableSet<String> MANIFEST_PNF_METADATA = - of(PNFD_PROVIDER, PNFD_NAME, PNFD_RELEASE_DATE_TIME, PNFD_ARCHIVE_VERSION); - - public static final String VNF_PROVIDER_ID = "vnf_provider_id"; - public static final String VNF_PRODUCT_NAME = "vnf_product_name"; - public static final String VNF_RELEASE_DATE_TIME = "vnf_release_date_time"; - public static final String VNF_PACKAGE_VERSION = "vnf_package_version"; + of(PNFD_PROVIDER.getToken(), PNFD_NAME.getToken(), PNFD_RELEASE_DATE_TIME.getToken(), + PNFD_ARCHIVE_VERSION.getToken()); public static final ImmutableSet<String> MANIFEST_VNF_METADATA = - of(VNF_PROVIDER_ID, VNF_PRODUCT_NAME, VNF_RELEASE_DATE_TIME, VNF_PACKAGE_VERSION); - + of(VNF_PROVIDER_ID.getToken(), VNF_PRODUCT_NAME.getToken(), VNF_RELEASE_DATE_TIME.getToken(), + VNF_PACKAGE_VERSION.getToken()); public static final int MANIFEST_METADATA_LIMIT = 4; - public static final String METADATA_MF_ATTRIBUTE = "metadata"; - public static final String SOURCE_MF_ATTRIBUTE = "Source"; - public static final String ALGORITHM_MF_ATTRIBUTE = "Algorithm"; - public static final String HASH_MF_ATTRIBUTE = "Hash"; - public static final String CMS_BEGIN = "----BEGIN CMS-----"; - public static final String CMD_END = "----END CMS-----"; - public static final String SEPARATOR_MF_ATTRIBUTE = ":"; - public static final String NON_MANO_MF_ATTRIBUTE = "non_mano_artifact_sets"; public static final String TOSCA_META_ORIG_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta.original"; public static final String TOSCA_META_FILE_VERSION = "1.0"; diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/Manifest.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/Manifest.java index c11f4a3ddc..c0ccbbc14d 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/Manifest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/Manifest.java @@ -20,11 +20,11 @@ package org.openecomp.sdc.tosca.csar; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.Optional; +import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; public interface Manifest { @@ -70,4 +70,18 @@ public interface Manifest { * @return enum for type values */ Optional<ResourceTypeEnum> getType(); + + /** + * Gets the CMS manifest signature if present in manifest + * @return + * the CMS manifest signature. + */ + Optional<String> getCmsSignature(); + + /** + * Gets the Map of source path and it correspondent checksum. + * @return + * The source and checksum map. + */ + Optional<Map<String, AlgorithmDigest>> getSourceAndChecksumMap(); } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestTokenType.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestTokenType.java new file mode 100644 index 0000000000..68ad91d29a --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestTokenType.java @@ -0,0 +1,84 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.tosca.csar; + +import java.util.Arrays; +import java.util.Optional; + +public enum ManifestTokenType { + ALGORITHM("Algorithm"), + ATTRIBUTE_VALUE_SEPARATOR(":"), + CMS_BEGIN("-----BEGIN CMS-----"), + CMS_END("-----END CMS-----"), + HASH("Hash"), + METADATA("metadata"), + NON_MANO_ARTIFACT_SETS("non_mano_artifact_sets"), + SOURCE("Source"), + VNF_PRODUCT_NAME("vnf_product_name"), + VNF_PROVIDER_ID("vnf_provider_id"), + VNF_PACKAGE_VERSION("vnf_package_version"), + VNF_RELEASE_DATE_TIME("vnf_release_date_time"), + PNFD_NAME("pnfd_name"), + PNFD_PROVIDER("pnfd_provider"), + PNFD_ARCHIVE_VERSION("pnfd_archive_version"), + PNFD_RELEASE_DATE_TIME("pnfd_release_date_time"); + + private final String token; + + ManifestTokenType(final String token) { + this.token = token; + } + + public String getToken() { + return token; + } + + public static Optional<ManifestTokenType> parse(final String token) { + return Arrays.stream(values()).filter(it -> it.getToken() != null && it.getToken().equals(token)).findFirst(); + } + + public boolean isMetadataEntry() { + return isMetadataVnfEntry() || isMetadataPnfEntry(); + } + + public boolean isMetadataVnfEntry() { + switch (this) { + case VNF_PRODUCT_NAME: + case VNF_PROVIDER_ID: + case VNF_PACKAGE_VERSION: + case VNF_RELEASE_DATE_TIME: + return true; + default: + return false; + } + } + + public boolean isMetadataPnfEntry() { + switch (this) { + case PNFD_NAME: + case PNFD_PROVIDER: + case PNFD_ARCHIVE_VERSION: + case PNFD_RELEASE_DATE_TIME: + return true; + default: + return false; + } + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ONAPManifestOnboarding.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ONAPManifestOnboarding.java index 2c4cc3a81f..02ee12db13 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ONAPManifestOnboarding.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ONAPManifestOnboarding.java @@ -21,44 +21,54 @@ package org.openecomp.sdc.tosca.csar; import com.google.common.collect.ImmutableList; +import java.util.Optional; import org.openecomp.sdc.common.errors.Messages; -import java.util.Iterator; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SOURCE_MF_ATTRIBUTE; - -public class ONAPManifestOnboarding extends AbstractOnboardingManifest implements Manifest { +public class ONAPManifestOnboarding extends AbstractOnboardingManifest { @Override protected void processManifest(ImmutableList<String> lines) { super.processManifest(lines); if (errors.isEmpty() && sources.isEmpty()) { - errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage()); + errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage()); } } @Override - protected void processMetadata(Iterator<String> iterator) { - if(!iterator.hasNext()){ - return; - } - String line = iterator.next(); - if(isEmptyLine(iterator, line)) { - return; - } - String[] metaSplit = line.split(SEPARATOR_MF_ATTRIBUTE); - if (isInvalidLine(line, metaSplit)) { + protected void processMetadata() { + Optional<String> currentLine = getCurrentLine(); + if (!currentLine.isPresent() || !isMetadata(currentLine.get())) { + reportError(Messages.MANIFEST_START_METADATA); + continueToProcess = false; return; } - if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){ - String value = line.substring((metaSplit[0] + SEPARATOR_MF_ATTRIBUTE).length()).trim(); - metadata.put(metaSplit[0].trim(),value.trim()); - processMetadata(iterator); - }else if(metaSplit[0].startsWith(SOURCE_MF_ATTRIBUTE)){ - String value = line.substring((metaSplit[0] + SEPARATOR_MF_ATTRIBUTE).length()).trim(); - sources.add(value); - processMetadata(iterator); + currentLine = readNextNonEmptyLine(); + + while (currentLine.isPresent() && continueToProcess) { + final String line = currentLine.get(); + final String entry = readEntryName(line).orElse(null); + if (entry == null) { + reportInvalidLine(); + } + final String value = readEntryValue(line).orElse(null); + if (value == null) { + reportInvalidLine(); + } + + final ManifestTokenType tokenType = ManifestTokenType.parse(entry).orElse(null); + if (tokenType == ManifestTokenType.SOURCE) { + sources.add(value); + } else { + addToMetadata(entry, value); + continueToProcess = isValid(); + } + currentLine = readNextNonEmptyLine(); } } + @Override + protected void processBody() { + //no implementation + } + } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/OnboardingToscaMetadata.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/OnboardingToscaMetadata.java index 8d603cccb3..3fc55adb51 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/OnboardingToscaMetadata.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/OnboardingToscaMetadata.java @@ -36,10 +36,10 @@ import java.util.List; import java.util.Map; import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE; import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR; -public class OnboardingToscaMetadata implements ToscaMetadata{ +public class OnboardingToscaMetadata implements ToscaMetadata { private Map<String, String> metaEntries; private List<ErrorMessage> errors; @@ -66,7 +66,7 @@ public class OnboardingToscaMetadata implements ToscaMetadata{ if (line.isEmpty()) { return meta; } - String[] entry = line.split(SEPARATOR_MF_ATTRIBUTE); + String[] entry = line.split(ATTRIBUTE_VALUE_SEPARATOR.getToken()); //No empty keys allowed, no empty values allowed if (entry.length < 2 || entry[0].isEmpty()) { meta.errors.add(new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters( diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java index b5b3d9cdce..bb0b07a63f 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java @@ -21,114 +21,295 @@ package org.openecomp.sdc.tosca.csar; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; +import java.util.Map.Entry; +import java.util.Optional; +import org.apache.commons.lang.StringUtils; +import org.openecomp.sdc.common.errors.Messages; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ALGORITHM_MF_ATTRIBUTE; -import static org.openecomp.sdc.tosca.csar.CSARConstants.CMD_END; -import static org.openecomp.sdc.tosca.csar.CSARConstants.CMS_BEGIN; -import static org.openecomp.sdc.tosca.csar.CSARConstants.HASH_MF_ATTRIBUTE; -import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_MANO_MF_ATTRIBUTE; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPARATOR_MF_ATTRIBUTE; -import static org.openecomp.sdc.tosca.csar.CSARConstants.SOURCE_MF_ATTRIBUTE; - +/** + * Processes a SOL004 Manifest. + */ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { @Override - protected void processMetadata(Iterator<String> iterator) { - if(!iterator.hasNext()){ + protected void processMetadata() { + Optional<String> currentLine = getCurrentLine(); + //SOL004 #4.3.2: The manifest file shall start with the package metadata + if (!currentLine.isPresent() || !isMetadata(currentLine.get())) { + reportError(Messages.MANIFEST_START_METADATA); + continueToProcess = false; return; } - String line = iterator.next(); - if(isEmptyLine(iterator, line)){ + while (continueToProcess) { + currentLine = readNextNonEmptyLine(); + if (!currentLine.isPresent()) { + continueToProcess = validateMetadata(); + return; + } + final String metadataLine = currentLine.get(); + final String metadataEntry = readEntryName(metadataLine).orElse(null); + if (!isMetadataEntry(metadataEntry)) { + if (metadata.size() < MAX_ALLOWED_MANIFEST_META_ENTRIES) { + reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, metadataLine); + continueToProcess = false; + return; + } + continueToProcess = validateMetadata(); + return; + } + final String metadataValue = readEntryValue(metadataLine).orElse(null); + addToMetadata(metadataEntry, metadataValue); + continueToProcess = isValid(); + } + readNextNonEmptyLine(); + } + + @Override + protected void processBody() { + while (continueToProcess) { + final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); + if (manifestTokenType == null) { + getCurrentLine().ifPresent(line -> reportInvalidLine()); + break; + } + + switch (manifestTokenType) { + case CMS_BEGIN: + readCmsSignature(); + break; + case NON_MANO_ARTIFACT_SETS: + processNonManoArtifactEntry(); + continueToProcess = false; + break; + case SOURCE: + processSource(); + break; + default: + getCurrentLine().ifPresent(line -> reportInvalidLine()); + continueToProcess = false; + break; + } + } + } + + /** + * Processes the {@link ManifestTokenType#NON_MANO_ARTIFACT_SETS} entry. + */ + private void processNonManoArtifactEntry() { + Optional<String> currentLine = readNextNonEmptyLine(); + while (currentLine.isPresent()) { + final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); + if (manifestTokenType != null) { + reportError(Messages.MANIFEST_INVALID_NON_MANO_KEY, manifestTokenType.getToken()); + continueToProcess = false; + return; + } + final String nonManoKey = readCurrentEntryName().orElse(null); + if (nonManoKey == null) { + reportError(Messages.MANIFEST_INVALID_NON_MANO_KEY, currentLine.get()); + continueToProcess = false; + return; + } + readNextNonEmptyLine(); + final List<String> nonManoSourceList = readNonManoSourceList(); + if (!isValid()) { + continueToProcess = false; + return; + } + if (nonManoSourceList.isEmpty()) { + reportError(Messages.MANIFEST_EMPTY_NON_MANO_KEY, nonManoKey); + continueToProcess = false; + return; + } + if (nonManoSources.get(nonManoKey) == null) { + nonManoSources.put(nonManoKey, nonManoSourceList); + } else { + nonManoSources.get(nonManoKey).addAll(nonManoSourceList); + } + currentLine = getCurrentLine(); + } + } + + /** + * Processes {@link ManifestTokenType#SOURCE} entries in {@link ManifestTokenType#NON_MANO_ARTIFACT_SETS}. + * + * @return A list of sources paths + */ + private List<String> readNonManoSourceList() { + final List<String> nonManoSourceList = new ArrayList<>(); + while (getCurrentLine().isPresent()) { + final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); + if (manifestTokenType != ManifestTokenType.SOURCE) { + break; + } + + final String value = readCurrentEntryValue().orElse(null); + if (!StringUtils.isEmpty(value)) { + nonManoSourceList.add(value); + } else { + reportError(Messages.MANIFEST_EMPTY_NON_MANO_SOURCE); + break; + } + + readNextNonEmptyLine(); + } + return nonManoSourceList; + } + + /** + * Reads a manifest CMS signature. + */ + private void readCmsSignature() { + if (cmsSignature != null) { + reportError(Messages.MANIFEST_DUPLICATED_CMS_SIGNATURE); + continueToProcess = false; return; } - String[] metaSplit = line.split(SEPARATOR_MF_ATTRIBUTE); - if (isInvalidLine(line, metaSplit)) { + Optional<String> currentLine = readNextNonEmptyLine(); + if(!getCurrentLine().isPresent()) { return; } - if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE) && !metaSplit[0].equals(NON_MANO_MF_ATTRIBUTE)){ - String value = line.substring((metaSplit[0] + SEPARATOR_MF_ATTRIBUTE).length()).trim(); - metadata.put(metaSplit[0].trim(),value.trim()); - processMetadata(iterator); - } else { - processSourcesAndNonManoSources(iterator, line); + StringBuilder stringBuilder = new StringBuilder(); + while (currentLine.isPresent() && detectLineEntry().orElse(null) != ManifestTokenType.CMS_END) { + stringBuilder.append(currentLine.get()); + stringBuilder.append("\n"); + currentLine = readNextNonEmptyLine(); + } + + if (currentLine.isPresent()) { + cmsSignature = stringBuilder.toString(); + readNextNonEmptyLine(); } } - private void processSourcesAndNonManoSources(Iterator<String> iterator, String prevLine) { - if(prevLine.isEmpty()){ - if(iterator.hasNext()){ - processSourcesAndNonManoSources(iterator, iterator.next()); + /** + * Detects the current line manifest token. + * + * @return the current line manifest token. + */ + private Optional<ManifestTokenType> detectLineEntry() { + final Optional<String> currentLine = getCurrentLine(); + if (currentLine.isPresent()) { + final String line = currentLine.get(); + final String entry = readEntryName(line).orElse(null); + if (entry == null) { + return ManifestTokenType.parse(line); + } else { + return ManifestTokenType.parse(entry); } - } else if(prevLine.startsWith(SOURCE_MF_ATTRIBUTE+ SEPARATOR_MF_ATTRIBUTE)){ - processSource(iterator, prevLine); - } - else if(prevLine.startsWith(ALGORITHM_MF_ATTRIBUTE + SEPARATOR_MF_ATTRIBUTE) || - prevLine.startsWith(HASH_MF_ATTRIBUTE + SEPARATOR_MF_ATTRIBUTE)){ - processSourcesAndNonManoSources(iterator, iterator.next()); - }else if(prevLine.startsWith(CMS_BEGIN)){ - String line = iterator.next(); - while(iterator.hasNext() && !line.contains(CMD_END)){ - line = iterator.next(); + } + return Optional.empty(); + } + + /** + * Validates the manifest metadata content, reporting errors found. + * + * @return {@code true} if the metadata content is valid, {@code false} otherwise. + */ + private boolean validateMetadata() { + if (metadata.isEmpty()) { + reportError(Messages.MANIFEST_NO_METADATA); + return false; + } + + final Entry<String, String> firstManifestEntry = metadata.entrySet().iterator().next(); + final ManifestTokenType firstManifestEntryTokenType = + ManifestTokenType.parse(firstManifestEntry.getKey()).orElse(null); + if (firstManifestEntryTokenType == null) { + reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, firstManifestEntry.getKey()); + return false; + } + for (final Entry<String, String> manifestEntry : metadata.entrySet()) { + final ManifestTokenType manifestEntryTokenType = ManifestTokenType.parse(manifestEntry.getKey()) + .orElse(null); + if (manifestEntryTokenType == null) { + reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, manifestEntry.getKey()); + return false; + } + if ((firstManifestEntryTokenType.isMetadataVnfEntry() && !manifestEntryTokenType.isMetadataVnfEntry()) + || (firstManifestEntryTokenType.isMetadataPnfEntry() && !manifestEntryTokenType.isMetadataPnfEntry())) { + reportError(Messages.MANIFEST_METADATA_UNEXPECTED_ENTRY_TYPE); + return false; } - processSourcesAndNonManoSources(iterator, iterator.next()); } - else if(prevLine.startsWith(NON_MANO_MF_ATTRIBUTE+ SEPARATOR_MF_ATTRIBUTE)){ - //non mano should be the last bit in manifest file, - // all sources after non mano will be placed to the last non mano - // key, if any other structure met error reported - processNonManoInputs(iterator, iterator.next()); - }else{ - reportError(prevLine); + + if (metadata.entrySet().size() != MAX_ALLOWED_MANIFEST_META_ENTRIES) { + reportError(Messages.MANIFEST_METADATA_DOES_NOT_MATCH_LIMIT, MAX_ALLOWED_MANIFEST_META_ENTRIES); + return false; } + + return true; } - private void processSource(Iterator<String> iterator, String prevLine) { - String value = prevLine.substring((SOURCE_MF_ATTRIBUTE + SEPARATOR_MF_ATTRIBUTE).length()).trim(); - sources.add(value); - if(iterator.hasNext()) { - processSourcesAndNonManoSources(iterator, iterator.next()); + /** + * Processes a Manifest {@link ManifestTokenType#SOURCE} entry. + */ + private void processSource() { + final Optional<String> currentLine = getCurrentLine(); + if (!currentLine.isPresent()) { + return; + } + final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); + if (manifestTokenType != ManifestTokenType.SOURCE) { + return; + } + + final String sourceLine = currentLine.get(); + final String sourcePath = readEntryValue(sourceLine).orElse(null); + + if (sourcePath == null) { + reportError(Messages.MANIFEST_EXPECTED_SOURCE_PATH); + return; } + sources.add(sourcePath); + readAlgorithmEntry(sourcePath); } - private void processNonManoInputs(Iterator<String> iterator, String prevLine) { - if(prevLine.trim().equals(SOURCE_MF_ATTRIBUTE + SEPARATOR_MF_ATTRIBUTE)){ - reportError(prevLine); + /** + * Processes entries {@link ManifestTokenType#ALGORITHM} and {@link ManifestTokenType#HASH} of a {@link + * ManifestTokenType#SOURCE} entry. + * + * @param sourcePath the source path related to the algorithm entry. + */ + private void readAlgorithmEntry(final String sourcePath) { + Optional<String> currentLine = readNextNonEmptyLine(); + if (!currentLine.isPresent()) { return; } - if(!prevLine.contains(SEPARATOR_MF_ATTRIBUTE)){ - reportError(prevLine); + final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); + if (manifestTokenType == ManifestTokenType.HASH) { + reportError(Messages.MANIFEST_EXPECTED_ALGORITHM_BEFORE_HASH); + continueToProcess = false; return; } - - String[] metaSplit = prevLine.trim().split(SEPARATOR_MF_ATTRIBUTE); - if (metaSplit.length > 1){ - reportError(prevLine); + if (manifestTokenType != ManifestTokenType.ALGORITHM) { return; } - int index = prevLine.indexOf(':'); - if(index > 0){ - prevLine = prevLine.substring(0, index); + final String algorithmLine = currentLine.get(); + final String algorithmType = readEntryValue(algorithmLine).orElse(null); + if (algorithmType == null) { + reportError(Messages.MANIFEST_EXPECTED_ALGORITHM_VALUE); + continueToProcess = false; + return; } - processNonManoSource(iterator, prevLine, new ArrayList<>()); - } + currentLine = readNextNonEmptyLine(); + if (!currentLine.isPresent() || detectLineEntry().orElse(null) != ManifestTokenType.HASH) { + reportError(Messages.MANIFEST_EXPECTED_HASH_ENTRY); + continueToProcess = false; + return; + } - private void processNonManoSource(Iterator<String> iterator, String key, List<String> sources) { - if(!iterator.hasNext()){ + final String hashLine = currentLine.get(); + final String hash = readEntryValue(hashLine).orElse(null); + if (hash == null) { + reportError(Messages.MANIFEST_EXPECTED_HASH_VALUE); + continueToProcess = false; return; } - String line = iterator.next(); - if(line.isEmpty()){ - processNonManoSource(iterator, key, sources); - }else if(line.trim().startsWith(SOURCE_MF_ATTRIBUTE + SEPARATOR_MF_ATTRIBUTE)){ - String value = line.replace(SOURCE_MF_ATTRIBUTE + SEPARATOR_MF_ATTRIBUTE, "").trim(); - sources.add(value); - processNonManoSource(iterator, key, sources); - }else { - processNonManoInputs(iterator, line); - } - nonManoSources.put(key.trim(), sources); + sourceAndChecksumMap.put(sourcePath, new AlgorithmDigest(algorithmType, hash)); + readNextNonEmptyLine(); } + }
\ No newline at end of file |