From 55401bf89a8bc1c51f76554faac278186560724a Mon Sep 17 00:00:00 2001 From: franciscovila Date: Fri, 4 Feb 2022 10:47:33 +0000 Subject: Create onboarding validator for ASD VSPs Create a new validator in SDC onboarding that will be used for ASD VSPs Issue-ID: SDC-3859 Signed-off-by: franciscovila Change-Id: Ida192122a95414f7d18855fe2a993b269c55a91d --- .../sdc/tosca/csar/AbstractOnboardingManifest.java | 2 +- .../sdc/tosca/csar/AsdManifestOnboarding.java | 104 +++++++++++++ .../openecomp/sdc/tosca/csar/AsdPackageHelper.java | 57 ++++++++ .../openecomp/sdc/tosca/csar/CSARConstants.java | 5 + .../openecomp/sdc/tosca/csar/ManifestBuilder.java | 161 +++++++++++++++++++++ .../sdc/tosca/csar/ManifestTokenType.java | 22 ++- .../openecomp/sdc/tosca/csar/ManifestUtils.java | 138 ++++++++++++++++++ .../sdc/tosca/csar/SOL004ManifestOnboarding.java | 8 +- .../sdc/tosca/csar/ToscaMetaEntryAsd.java | 51 +++++++ 9 files changed, 541 insertions(+), 7 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/main') 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 5b64c8e0af..1aab56c7ed 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 @@ -38,7 +38,7 @@ import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -abstract class AbstractOnboardingManifest implements Manifest { +public abstract class AbstractOnboardingManifest implements Manifest { protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractOnboardingManifest.class); protected static final int MAX_ALLOWED_MANIFEST_META_ENTRIES = 4; diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java new file mode 100644 index 0000000000..7f32893798 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java @@ -0,0 +1,104 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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 org.apache.commons.lang3.StringUtils; +import org.openecomp.sdc.common.errors.Messages; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ASD_MANIFEST_META_ENTRIES; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; + +/** + * Processes a ASD Manifest. + */ +public class AsdManifestOnboarding extends SOL004ManifestOnboarding { + + @Override + protected boolean isMetadataEntry(final String metadataEntry) { + final Optional manifestTokenType = ManifestTokenType.parse(metadataEntry); + return manifestTokenType.map(ManifestTokenType::isMetadataAsdEntry).orElse(false); + } + + /** + * Processes {@link ManifestTokenType#SOURCE} entries in {@link ManifestTokenType#NON_MANO_ARTIFACT_SETS}. + * + * @return A list of sources paths + */ + protected List readNonManoSourceList() { + final List nonManoSourceList = new ArrayList<>(); + while (getCurrentLine().isPresent()) { + final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); + if (!(manifestTokenType == ManifestTokenType.SOURCE || manifestTokenType == ManifestTokenType.VENDOR_NAME || manifestTokenType == ManifestTokenType.ARTIFACT_TYPE)) { + break; + } + if (manifestTokenType == ManifestTokenType.SOURCE) { + 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; + } + + /** + * Validates the manifest metadata content, reporting errors found. + * + * @return {@code true} if the metadata content is valid, {@code false} otherwise. + */ + protected boolean validateMetadata() { + if (metadata.isEmpty()) { + reportError(Messages.MANIFEST_NO_METADATA); + return false; + } + String key = metadata.keySet().stream().filter(k -> !COMPATIBLE_SPECIFICATION_VERSIONS.getToken().equals(k)).findFirst().orElse(null); + final ManifestTokenType firstManifestEntryTokenType = ManifestTokenType.parse(key).orElse(null); + if (firstManifestEntryTokenType == null) { + reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, key); + return false; + } + for (final Map.Entry manifestEntry : metadata.entrySet()) { + final ManifestTokenType manifestEntryTokenType = ManifestTokenType.parse(manifestEntry.getKey()).orElse(null); + if (manifestEntryTokenType == null || !manifestEntryTokenType.isMetadataAsdEntry()) { + reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, manifestEntry.getKey()); + return false; + } + } + if (metadata.entrySet().size() != getMaxAllowedManifestMetaEntries()) { + reportError(Messages.MANIFEST_METADATA_DOES_NOT_MATCH_LIMIT, getMaxAllowedManifestMetaEntries()); + return false; + } + return true; + } + + protected int getMaxAllowedManifestMetaEntries() { + return ASD_MANIFEST_META_ENTRIES; + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java new file mode 100644 index 0000000000..0b29826831 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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 org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +import java.io.IOException; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ASD_DEFINITION_TYPE; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; + +/** + * Helper class for ASD packages. + */ +public class AsdPackageHelper { + + protected static final Logger LOGGER = LoggerFactory.getLogger(AsdPackageHelper.class); + + private final ManifestUtils manifestUtils; + + public AsdPackageHelper(ManifestUtils manifestUtils) { + this.manifestUtils = manifestUtils; + } + + public boolean isAsdPackage(final FileContentHandler fileContentHandler) { + try { + final Manifest manifest = manifestUtils.loadManifest(fileContentHandler, new AsdManifestOnboarding()); + return null != manifest && manifest.getMetadata().entrySet().stream() + .anyMatch(manifestEntry -> ENTRY_DEFINITION_TYPE.getToken().equalsIgnoreCase(manifestEntry.getKey()) + && ASD_DEFINITION_TYPE.equalsIgnoreCase(manifestEntry.getValue())); + } + catch (IOException ioe) { + LOGGER.warn("There was a problem loading the manifest: ", ioe); + return false; + } + } +} 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 cd83cc816b..f99f326d00 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,10 +18,12 @@ package org.openecomp.sdc.tosca.csar; import static com.google.common.collect.ImmutableSet.of; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; 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.RELEASE_DATE_TIME; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNFD_ID; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNFM_INFO; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNF_PACKAGE_VERSION; @@ -48,6 +50,7 @@ public class CSARConstants { public static final ImmutableSet MANIFEST_VNF_METADATA_VERSION_3 = of(COMPATIBLE_SPECIFICATION_VERSIONS.getToken(), VNFD_ID.getToken(), VNF_PROVIDER_ID.getToken(), VNF_PRODUCT_NAME.getToken(), VNF_RELEASE_DATE_TIME.getToken(), VNF_PACKAGE_VERSION.getToken(), VNF_SOFTWARE_VERSION.getToken(), VNFM_INFO.getToken()); + public static final ImmutableSet MANIFEST_ASD_METADATA = of(ENTRY_DEFINITION_TYPE.getToken(), RELEASE_DATE_TIME.getToken()); public static final int MANIFEST_METADATA_LIMIT = 4; public static final int MANIFEST_VNF_METADATA_LIMIT_VERSION_3 = 8; public static final int MANIFEST_PNF_METADATA_LIMIT_VERSION_3 = 5; @@ -60,6 +63,8 @@ public class CSARConstants { public static final String TOSCA_TYPE_PNF = "pnf"; public static final String TOSCA_TYPE_VNF = "vnf"; public static final String TOSCA_MANIFEST_FILE_EXT = "mf"; + public static final String ASD_DEFINITION_TYPE = "asd"; + public static final int ASD_MANIFEST_META_ENTRIES = 4; private CSARConstants() { } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java new file mode 100644 index 0000000000..275bed5a0f --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java @@ -0,0 +1,161 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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 static org.openecomp.sdc.tosca.csar.ManifestTokenType.ALGORITHM; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.HASH; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.METADATA; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.NON_MANO_ARTIFACT_SETS; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.SOURCE; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +/** + * Builds SOL0004 manifest file as a String. + */ +public class ManifestBuilder { + + private static final String PROPERTY_FORMAT = "%s: %s%n"; + private static final String SECTION_FORMAT = "%s:%n"; + private final Map> sourceWithPropertiesMap = new TreeMap<>(); + private final Map> nonManoArtifactMap = new TreeMap<>(); + private final Map metadataMap = new TreeMap<>(); + + /** + * Adds a metadata property. + * + * @param metadataProperty the property name + * @param value the property value + * @return a reference to this object. + */ + public ManifestBuilder withMetaData(final String metadataProperty, final String value) { + metadataMap.put(metadataProperty, value); + return this; + } + + /** + * Adds a manifest source path. + * + * @param sourcePath The source path + * @return a reference to this object. + */ + public ManifestBuilder withSource(final String sourcePath) { + sourceWithPropertiesMap.put(sourcePath, null); + return this; + } + + /** + * Adds a manifest source path with the source sign. + * + * @param sourcePath The source path + * @param hashAlgorithm The hash algorithm + * @param hash The hash representing the sign + * @return a reference to this object. + */ + public ManifestBuilder withSignedSource(final String sourcePath, final String hashAlgorithm, final String hash) { + TreeMap sourcePropertiesMap = new TreeMap<>(); + sourcePropertiesMap.put(ALGORITHM.getToken(), hashAlgorithm); + sourcePropertiesMap.put(HASH.getToken(), hash); + sourceWithPropertiesMap.put(sourcePath, sourcePropertiesMap); + return this; + } + + /** + * Adds a non mano artifact. + * + * @param artifactType the artifact type + * @param sourcePath the artifact source path + * @return a reference to this object. + */ + public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) { + nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>()); + nonManoArtifactMap.get(artifactType).add(sourcePath); + return this; + } + + /** + * Builds the String representing the manifest file. + * + * @return The manifest file as String + */ + public String build() { + final StringBuilder stringBuilder = new StringBuilder(); + if (!metadataMap.isEmpty()) { + stringBuilder.append(buildMetadata()); + } + if (!sourceWithPropertiesMap.isEmpty()) { + stringBuilder.append(buildSource()); + } + if (!nonManoArtifactMap.isEmpty()) { + stringBuilder.append(buildNonManoArtifact()); + } + return stringBuilder.toString(); + } + + private String buildMetadata() { + final StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(String.format(SECTION_FORMAT, METADATA.getToken())); + for (Entry metadataAndValue : metadataMap.entrySet()) { + stringBuilder.append("\t"); + stringBuilder.append(String.format(PROPERTY_FORMAT, metadataAndValue.getKey(), metadataAndValue.getValue())); + } + stringBuilder.append("\n"); + return stringBuilder.toString(); + } + + private String buildSource() { + final StringBuilder stringBuilder = new StringBuilder(); + for (final Entry> signedSourceMap : sourceWithPropertiesMap.entrySet()) { + stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), signedSourceMap.getKey())); + final Map propertiesMap = signedSourceMap.getValue(); + if (propertiesMap != null && !propertiesMap.isEmpty()) { + final String algorithm = propertiesMap.get(ALGORITHM.getToken()); + if (algorithm != null) { + stringBuilder.append(String.format(PROPERTY_FORMAT, ALGORITHM.getToken(), algorithm)); + } + final String hash = propertiesMap.get(HASH.getToken()); + if (hash != null) { + stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash)); + } + } + } + stringBuilder.append("\n"); + return stringBuilder.toString(); + } + + private String buildNonManoArtifact() { + final StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(String.format(SECTION_FORMAT, NON_MANO_ARTIFACT_SETS.getToken())); + for (Entry> artifactTypeAndSourcesEntry : nonManoArtifactMap.entrySet()) { + stringBuilder.append("\t"); + stringBuilder.append(String.format(SECTION_FORMAT, artifactTypeAndSourcesEntry.getKey())); + for (String source : artifactTypeAndSourcesEntry.getValue()) { + stringBuilder.append("\t\t"); + stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), source)); + } + } + return stringBuilder.toString(); + } +} 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 index 9b6b34f755..d9a2143d24 100644 --- 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 @@ -45,7 +45,13 @@ public enum ManifestTokenType { PNFD_RELEASE_DATE_TIME("pnfd_release_date_time"), SIGNATURE("Signature"), CERTIFICATE("Certificate"), - COMPATIBLE_SPECIFICATION_VERSIONS("compatible_specification_versions"); + COMPATIBLE_SPECIFICATION_VERSIONS("compatible_specification_versions"), + APPLICATION_NAME("application_name"), + APPLICATION_PROVIDER("application_provider"), + RELEASE_DATE_TIME("release_date_time"), + ENTRY_DEFINITION_TYPE("entry_definition_type"), + VENDOR_NAME("vendor_name"), + ARTIFACT_TYPE("artifact_type"); // @formatter:on private final String token; @@ -55,7 +61,7 @@ public enum ManifestTokenType { } public static Optional parse(final String token) { - return Arrays.stream(values()).filter(it -> it.getToken() != null && it.getToken().equals(token)).findFirst(); + return Arrays.stream(values()).filter(it -> it.getToken() != null && it.getToken().equalsIgnoreCase(token)).findFirst(); } public String getToken() { @@ -94,4 +100,16 @@ public enum ManifestTokenType { return false; } } + + public boolean isMetadataAsdEntry() { + switch (this) { + case APPLICATION_NAME: + case APPLICATION_PROVIDER: + case RELEASE_DATE_TIME: + case ENTRY_DEFINITION_TYPE: + return true; + default: + return false; + } + } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java new file mode 100644 index 0000000000..4ebae5027f --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java @@ -0,0 +1,138 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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 org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; + +/** + * Offers method utils dealing with the manifest + */ +public class ManifestUtils { + + public ManifestUtils() { + } + + private static final Logger LOGGER = LoggerFactory.getLogger(ManifestUtils.class); + + /** + * Loads a manifest given the file handler and the type to manifest to load. + * + * @param fileContentHandler The package file handler + * @param manifestHandler The type of abstract manifest to load + * @return The loaded Manifest. + */ + public Manifest loadManifest(final FileContentHandler fileContentHandler, final T manifestHandler) throws IOException { + final Manifest manifest; + try { + manifest = getManifest(fileContentHandler, manifestHandler); + } catch (final IOException ex) { + LOGGER.error("An error occurred while getting the manifest file", ex); + throw ex; + } + return manifest; + } + + /** + * Retrieves the manifest file from the CSAR + * + * @param fileContentHandler contains csar artifacts + * @param manifestHandler The type of abstract manifest to load + * @return The retrieved Manifest + * @throws IOException when TOSCA.meta file or manifest file is invalid + */ + public Manifest getManifest(FileContentHandler fileContentHandler, T manifestHandler) throws IOException { + ToscaMetadata metadata = getMetadata(fileContentHandler); + return getManifest(fileContentHandler, getEntryManifestLocation(metadata), manifestHandler); + } + + /** + * Retrieves the metadata from the CSAR + * + * @param fileContentHandler contains csar artifacts + * @return The retrieved metadata + * @throws IOException when TOSCA.meta file or manifest file is invalid + */ + public ToscaMetadata getMetadata(FileContentHandler fileContentHandler) throws IOException { + ToscaMetadata metadata; + if (fileContentHandler.containsFile(TOSCA_META_PATH_FILE_NAME)) { + metadata = OnboardingToscaMetadata.parseToscaMetadataFile(fileContentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)); + } else if (fileContentHandler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { + metadata = OnboardingToscaMetadata.parseToscaMetadataFile(fileContentHandler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); + } else { + throw new IOException("TOSCA.meta file not found!"); + } + return metadata; + } + + /** + * Retrieves the manifest location present in the metadata within the CSAR + * + * @param metadata the CSAR metadata + * @return The path of the location of the manifest within the CSAR + */ + public String getEntryManifestLocation(final ToscaMetadata metadata) { + return metadata.getMetaEntries().containsKey(ETSI_ENTRY_MANIFEST.getName()) ? + metadata.getMetaEntries().get(ETSI_ENTRY_MANIFEST.getName()): + metadata.getMetaEntries().get(ENTRY_MANIFEST.getName()); + } + + /** + * Retrieves the manifest given the file handler, the manifest location within the CSAR, + * and the type to manifest to load. + * + * @param fileContentHandler The package file handler + * @param manifestLocation The path of the location of the manifest within the CSAR + * @param manifestHandler The type of abstract manifest to load + * @return The loaded Manifest. + */ + public Manifest getManifest(FileContentHandler fileContentHandler, + String manifestLocation, T manifestHandler) throws IOException { + try (InputStream manifestInputStream = getManifestInputStream(fileContentHandler, manifestLocation)) { + manifestHandler.parse(manifestInputStream); + return manifestHandler; + } + } + + private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException { + InputStream io; + if (manifestLocation == null || !handler.containsFile(manifestLocation)) { + io = handler.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME); + } else { + io = handler.getFileContentAsStream(manifestLocation); + } + if (io == null) { + throw new IOException("Manifest file not found!"); + } + return io; + } +} 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 390fb3eaab..88e7915188 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 @@ -147,7 +147,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { * * @return A list of sources paths */ - private List readNonManoSourceList() { + protected List readNonManoSourceList() { final List nonManoSourceList = new ArrayList<>(); while (getCurrentLine().isPresent()) { final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); @@ -204,7 +204,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { * * @return the current line manifest token. */ - private Optional detectLineEntry() { + protected Optional detectLineEntry() { final Optional currentLine = getCurrentLine(); if (currentLine.isPresent()) { final String line = currentLine.get(); @@ -223,7 +223,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { * * @return {@code true} if the metadata content is valid, {@code false} otherwise. */ - private boolean validateMetadata() { + protected boolean validateMetadata() { if (metadata.isEmpty()) { reportError(Messages.MANIFEST_NO_METADATA); return false; @@ -362,7 +362,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { readNextNonEmptyLine(); } - private int getMaxAllowedManifestMetaEntries() { + protected int getMaxAllowedManifestMetaEntries() { if (maxAllowedMetaEntries == 0) { boolean isVersion3 = metadata.containsKey(COMPATIBLE_SPECIFICATION_VERSIONS.getToken()) && !getHighestCompatibleVersion().isLowerThan(ETSI_VERSION_2_7_1); diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java new file mode 100644 index 0000000000..73b3b912ac --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java @@ -0,0 +1,51 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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; + +import java.util.Arrays; +import java.util.Optional; + +/** + * Represents entry keys in a TOSCA.meta file. + */ +@AllArgsConstructor +@Getter +public enum ToscaMetaEntryAsd implements ToscaMetaEntry { + TOSCA_META_FILE_VERSION_ENTRY("TOSCA-Meta-File-Version"), + CSAR_VERSION_ENTRY("CSAR-Version"), + CREATED_BY_ENTRY("Created-By"), + ENTRY_DEFINITIONS("Entry-Definitions"), + ETSI_ENTRY_MANIFEST("ETSI-Entry-Manifest"), + ETSI_ENTRY_CHANGE_LOG("ETSI-Entry-Change-Log"), + ETSI_ENTRY_TESTS("ETSI-Entry-Tests"), + ETSI_ENTRY_LICENSES("ETSI-Entry-Licenses"), + ETSI_ENTRY_CERTIFICATE("ETSI-Entry-Certificate"), + OTHER_DEFINITIONS("Other-Definitions"); + + private final String name; + + public static Optional parse(final String name) { + return Arrays.stream(values()).filter(toscaMetaEntry -> toscaMetaEntry.getName().equals(name)).findFirst(); + } +} -- cgit 1.2.3-korg