diff options
author | franciscovila <javier.paradela.vila@est.tech> | 2022-02-04 10:47:33 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-03-28 08:40:07 +0000 |
commit | 55401bf89a8bc1c51f76554faac278186560724a (patch) | |
tree | 39abefaee3df7109fe3924816bec7c513f0256b0 /openecomp-be/lib/openecomp-tosca-lib | |
parent | 4307dbb5f0dc5da60e55f3473259cfd059d14770 (diff) |
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 <javier.paradela.vila@est.tech>
Change-Id: Ida192122a95414f7d18855fe2a993b269c55a91d
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib')
10 files changed, 684 insertions, 7 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 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 = 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<String> readNonManoSourceList() { + final List<String> 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<String, String> 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<String> 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<String> 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<String, Map<String, String>> sourceWithPropertiesMap = new TreeMap<>(); + private final Map<String, List<String>> nonManoArtifactMap = new TreeMap<>(); + private final Map<String, String> 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<String, String> 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<String, String> 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<String, Map<String, String>> signedSourceMap : sourceWithPropertiesMap.entrySet()) { + stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), signedSourceMap.getKey())); + final Map<String, String> 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<String, List<String>> 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<ManifestTokenType> 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 <T extends AbstractOnboardingManifest> 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 <T extends AbstractOnboardingManifest> 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 <T extends AbstractOnboardingManifest> 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<String> readNonManoSourceList() { + protected List<String> readNonManoSourceList() { final List<String> 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<ManifestTokenType> detectLineEntry() { + protected Optional<ManifestTokenType> detectLineEntry() { final Optional<String> 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<ToscaMetaEntryAsd> parse(final String name) { + return Arrays.stream(values()).filter(toscaMetaEntry -> toscaMetaEntry.getName().equals(name)).findFirst(); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java new file mode 100644 index 0000000000..19f3def510 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java @@ -0,0 +1,143 @@ +/* + * ============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.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openecomp.core.utilities.file.FileContentHandler; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.APPLICATION_NAME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.APPLICATION_PROVIDER; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.RELEASE_DATE_TIME; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.CREATED_BY_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.CSAR_VERSION_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_CHANGE_LOG; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.TOSCA_META_FILE_VERSION_ENTRY; + +class AsdPackageHelperTest { + + public static final String TOSCA_DEFINITION_FILEPATH = "Definitions/MainServiceTemplate.yaml"; + public static final String TOSCA_MANIFEST_FILEPATH = "Definitions/MainServiceTemplate.mf"; + public static final String TOSCA_CHANGELOG_FILEPATH = "Artifacts/changeLog.text"; + public static final String TOSCA_META_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta"; + + private FileContentHandler handler; + private StringBuilder metaFileBuilder; + + @BeforeEach + void setUp() { + handler = new FileContentHandler(); + metaFileBuilder = getMetaFileBuilder(); + } + + protected StringBuilder getMetaFileBuilder() { + return new StringBuilder() + .append(TOSCA_META_FILE_VERSION_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.0").append("\n") + .append(CSAR_VERSION_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.1").append("\n") + .append(CREATED_BY_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" Vendor").append("\n") + .append(ENTRY_DEFINITIONS.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_DEFINITION_FILEPATH).append("\n") + .append(ETSI_ENTRY_MANIFEST.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_MANIFEST_FILEPATH).append("\n") + .append(ETSI_ENTRY_CHANGE_LOG.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_CHANGELOG_FILEPATH).append("\n"); + } + + @Test + public void givenRightAsdHandlerItReturnsTrueAsAsdPackage() { + final ManifestBuilder manifestBuilder = getAsdManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + AsdPackageHelper asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); + assertTrue(asdPackageHelper.isAsdPackage(handler)); + + } + + @Test + public void givenWrongAsdHandlerItReturnsFalseAsAsdPackage() { + final ManifestBuilder manifestBuilder = getWrongAsdManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + AsdPackageHelper asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); + assertFalse(asdPackageHelper.isAsdPackage(handler)); + + } + + @Test + public void givenNonAsdHandlerItReturnsFalseAsAsdPackage() throws IOException { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + AsdPackageHelper asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); + assertFalse(asdPackageHelper.isAsdPackage(handler)); + + } + + protected ManifestBuilder getAsdManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(APPLICATION_NAME.getToken(), "RadioNode") + .withMetaData(APPLICATION_PROVIDER.getToken(), "Ericsson") + .withMetaData(ENTRY_DEFINITION_TYPE.getToken(), "asd") + .withMetaData(RELEASE_DATE_TIME.getToken(), "2022-02-01T11:25:00+00:00"); + } + + protected ManifestBuilder getWrongAsdManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(APPLICATION_NAME.getToken(), "RadioNode") + .withMetaData(APPLICATION_PROVIDER.getToken(), "Ericsson") + .withMetaData(ENTRY_DEFINITION_TYPE.getToken(), " Invalid") + .withMetaData(RELEASE_DATE_TIME.getToken(), "2022-02-01T11:25:00+00:00"); + } + + protected ManifestBuilder getVnfManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(ManifestTokenType.VNF_PRODUCT_NAME.getToken(), "RadioNode") + .withMetaData(ManifestTokenType.VNF_PROVIDER_ID.getToken(), "ACME") + .withMetaData(ManifestTokenType.VNF_PACKAGE_VERSION.getToken(), "1.0") + .withMetaData(ManifestTokenType.VNF_RELEASE_DATE_TIME.getToken(), "2019-03-11T11:25:00+00:00"); + } + +}
\ No newline at end of file |