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 --- .../csar/validation/AsdValidator.java | 122 ++++++++++ .../validation/EtsiSol004Version251Validator.java | 15 +- .../csar/validation/ManifestBuilder.java | 159 ------------- .../validation/SOL004MetaDirectoryValidator.java | 12 +- .../csar/validation/ValidatorFactory.java | 24 +- .../OrchestrationTemplateProcessCsarHandler.java | 21 +- .../csar/validation/AsdCsarValidatorTest.java | 247 +++++++++++++++++++++ .../csar/validation/ManifestBuilderTest.java | 1 + .../SOL004MetaDirectoryValidatorTest.java | 1 + .../SOL004Version3MetaDirectoryValidatorTest.java | 1 + .../SOL004Version4MetaDirectoryValidatorTest.java | 1 + .../csar/validation/ValidatorFactoryTest.java | 47 +++- .../services/impl/etsi/ETSIServiceImpl.java | 124 ++++------- .../services/impl/etsi/ETSIServiceImplTest.java | 67 +++--- .../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 +++++ .../sdc/tosca/csar/AsdPackageHelperTest.java | 143 ++++++++++++ 24 files changed, 1223 insertions(+), 310 deletions(-) create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdValidator.java delete mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilder.java create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdCsarValidatorTest.java 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 create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java (limited to 'openecomp-be') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdValidator.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdValidator.java new file mode 100644 index 0000000000..71254552a8 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdValidator.java @@ -0,0 +1,122 @@ +/* + * ============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.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import com.google.common.collect.ImmutableSet; +import lombok.NoArgsConstructor; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.tosca.csar.AbstractOnboardingManifest; +import org.openecomp.sdc.tosca.csar.AsdManifestOnboarding; +import org.openecomp.sdc.tosca.csar.ToscaMetaEntryAsd; + +import java.util.Map; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ASD_DEFINITION_TYPE; +import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_ASD_METADATA; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST; + +/** + * Validates the contents of the package to ensure it complies with the ASD specifications + */ +@NoArgsConstructor +public class AsdValidator extends SOL004MetaDirectoryValidator { + + private static final Logger LOGGER = LoggerFactory.getLogger(AsdValidator.class); + + protected T getOnboardingManifest() { + return (T) new AsdManifestOnboarding(); + } + + @Override + protected void handleEntry(final Map.Entry entry) { + final String key = entry.getKey(); + final var toscaMetaEntry = ToscaMetaEntryAsd.parse(entry.getKey()).orElse(null); + // allows any other unknown entry + if (toscaMetaEntry == null) { + return; + } + final String value = entry.getValue(); + switch (toscaMetaEntry) { + case TOSCA_META_FILE_VERSION_ENTRY: + case CSAR_VERSION_ENTRY: + case CREATED_BY_ENTRY: + verifyMetadataEntryVersions(key, value); + break; + case ENTRY_DEFINITIONS: + validateDefinitionFile(value); + break; + case ETSI_ENTRY_MANIFEST: + validateManifestFile(value); + break; + case ETSI_ENTRY_CHANGE_LOG: + validateChangeLog(value); + break; + case ETSI_ENTRY_TESTS: + case ETSI_ENTRY_LICENSES: + validateOtherEntries(entry); + break; + case ETSI_ENTRY_CERTIFICATE: + validateCertificate(value); + break; + default: + handleOtherEntry(entry); + break; + } + } + + @Override + protected String getManifestFilePath() { + return getToscaMetadata().getMetaEntries().get(ENTRY_MANIFEST.getName()); + } + + + protected boolean isValidEntryDefinitionType(final String key, final String value) { + if (!ASD_DEFINITION_TYPE.equalsIgnoreCase(value)) { + reportMetadataInvalidValue(key, value); + return false; + } + return true; + } + + private void reportMetadataInvalidValue(final String key, final String value) { + reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), key, value)); + LOGGER.error("{}: key {} - value {} ", Messages.MANIFEST_INVALID_LINE.getErrorMessage(), key, value); + } + + @Override + protected ImmutableSet getManifestMetadata(final Map metadata) { + return MANIFEST_ASD_METADATA; + } + + @Override + protected void handleMetadataEntries(final Map metadata) { + getManifestMetadata(metadata).stream().filter(requiredEntry -> !metadata.containsKey(requiredEntry) || + (ENTRY_DEFINITION_TYPE.getToken().equalsIgnoreCase(requiredEntry) + && !isValidEntryDefinitionType(requiredEntry, metadata.get(requiredEntry)))).forEach( + requiredEntry -> reportError(ErrorLevel.ERROR, + String.format(Messages.MANIFEST_METADATA_MISSING_ENTRY.getErrorMessage(), requiredEntry))); + } + +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/EtsiSol004Version251Validator.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/EtsiSol004Version251Validator.java index 3e4edd6e87..5eae5eb33e 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/EtsiSol004Version251Validator.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/EtsiSol004Version251Validator.java @@ -20,18 +20,19 @@ */ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_MANIFEST_FILE_EXT; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_CERTIFICATE; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_DEFINITIONS; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST; - -import java.util.Map; -import java.util.Optional; import lombok.NoArgsConstructor; import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251; +import java.util.Map; +import java.util.Optional; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_MANIFEST_FILE_EXT; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_CERTIFICATE; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST; + /** * Validates the contents of the package to ensure it complies with the "CSAR with TOSCA-Metadata directory" structure as defined in ETSI GS NFV-SOL * 004 v2.5.1. diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilder.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilder.java deleted file mode 100644 index ee708b4176..0000000000 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilder.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * ============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.vendorsoftwareproduct.impl.orchestration.csar.validation; - -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/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java index fde0565a3e..b992d22ea6 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java @@ -69,6 +69,7 @@ import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.datatypes.error.ErrorMessage; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.tosca.csar.AbstractOnboardingManifest; import org.openecomp.sdc.tosca.csar.Manifest; import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata; import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; @@ -230,6 +231,10 @@ public class SOL004MetaDirectoryValidator implements Validator { toscaMetadata.getMetaEntries().entrySet().parallelStream().forEach(this::handleEntry); } + protected T getOnboardingManifest() { + return (T) new SOL004ManifestOnboarding(); + } + protected void handleEntry(final Map.Entry entry) { final String key = entry.getKey(); final var toscaMetaEntry = ToscaMetaEntryVersion261.parse(entry.getKey()).orElse(null); @@ -321,7 +326,7 @@ public class SOL004MetaDirectoryValidator implements Validator { protected void validateManifestFile(final String filePath) { final Set existingFiles = contentHandler.getFileList(); if (verifyFileExists(existingFiles, filePath)) { - final Manifest onboardingManifest = new SOL004ManifestOnboarding(); + final Manifest onboardingManifest = getOnboardingManifest(); onboardingManifest.parse(contentHandler.getFileContentAsStream(filePath)); if (onboardingManifest.isValid()) { try { @@ -356,9 +361,10 @@ public class SOL004MetaDirectoryValidator implements Validator { return TOSCA_TYPE_PNF.equals(expectedMetadataType); } - private void handleMetadataEntries(final Map metadata) { + protected void handleMetadataEntries(final Map metadata) { getManifestMetadata(metadata).stream().filter(requiredEntry -> !metadata.containsKey(requiredEntry)).forEach( - requiredEntry -> reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_METADATA_MISSING_ENTRY.getErrorMessage(), requiredEntry))); + requiredEntry -> reportError(ErrorLevel.ERROR, + String.format(Messages.MANIFEST_METADATA_MISSING_ENTRY.getErrorMessage(), requiredEntry))); } /** diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java index d3ac455a56..6f5cdeeb5a 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactory.java @@ -19,7 +19,11 @@ */ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_7_1; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.tosca.csar.AsdPackageHelper; +import org.openecomp.sdc.tosca.csar.ManifestUtils; +import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService; +import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl; import java.io.IOException; import java.util.Comparator; @@ -27,16 +31,17 @@ import java.util.List; import java.util.ServiceLoader; import java.util.ServiceLoader.Provider; import java.util.stream.Collectors; -import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService; -import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_7_1; public class ValidatorFactory { private final ServiceLoader validatorLoader; + private final AsdPackageHelper asdPackageHelper; public ValidatorFactory() { this.validatorLoader = ServiceLoader.load(Validator.class); + this.asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); } /** @@ -47,9 +52,16 @@ public class ValidatorFactory { * @throws IOException when metafile is invalid */ public Validator getValidator(final FileContentHandler fileContentHandler) throws IOException { - final ETSIService etsiService = new ETSIServiceImpl(null); + final ETSIService etsiService = new ETSIServiceImpl(null, new ManifestUtils()); + if (asdPackageHelper.isAsdPackage(fileContentHandler)) { + return new AsdValidator(); + } if (!etsiService.hasEtsiSol261Metadata(fileContentHandler)) { - return etsiService.isEtsiPackage(fileContentHandler) ? new EtsiSol004Version251Validator() : new ONAPCsarValidator(); + if (etsiService.isEtsiPackage(fileContentHandler)) { + + return new EtsiSol004Version251Validator(); + } + return new ONAPCsarValidator(); } if (!etsiService.getHighestCompatibleSpecificationVersion(fileContentHandler).isLowerThan(ETSI_VERSION_2_7_1)) { if (etsiService.hasCnfEnhancements(fileContentHandler)) { diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java index 08b4b26846..817d212fc7 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java @@ -15,13 +15,6 @@ */ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.openecomp.core.impl.AbstractToscaSolConverter; @@ -43,6 +36,8 @@ import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList; import org.openecomp.sdc.heat.services.tree.ToscaTreeManager; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.tosca.csar.AsdPackageHelper; +import org.openecomp.sdc.tosca.csar.ManifestUtils; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; @@ -54,6 +49,14 @@ import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImp import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse; import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTemplateProcessHandler { private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessCsarHandler.class); @@ -62,9 +65,11 @@ public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTem private final CandidateService candidateService = CandidateServiceFactory.getInstance().createInterface(); private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager(); private final ETSIService etsiService; + private final AsdPackageHelper asdPackageHelper; public OrchestrationTemplateProcessCsarHandler() { etsiService = new ETSIServiceImpl(); + this.asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); } @Override @@ -124,7 +129,7 @@ public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTem if (CollectionUtils.isNotEmpty(modelList)) { return handleToscaModelConversion(modelList, fileContentHandler, candidateData); } - if (etsiService.isEtsiPackage(fileContentHandler)) { + if (etsiService.isEtsiPackage(fileContentHandler) || asdPackageHelper.isAsdPackage(fileContentHandler)) { return getToscaServiceModelSol004(fileContentHandler, candidateData); } return new ToscaConverterImpl().convert(fileContentHandler); diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdCsarValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdCsarValidatorTest.java new file mode 100644 index 0000000000..5cd871ae45 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/AsdCsarValidatorTest.java @@ -0,0 +1,247 @@ +/* + * ============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.vendorsoftwareproduct.impl.orchestration.csar.validation; + +import org.apache.commons.collections.CollectionUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.tosca.csar.ManifestBuilder; +import org.openecomp.sdc.tosca.csar.ManifestTokenType; +import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.OnboardingPackageContentHandler; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.openecomp.sdc.be.test.util.TestResourcesHandler.getResourceBytesOrFail; +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_LICENSES; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_TESTS; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.TOSCA_META_FILE_VERSION_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_DEFINITION_FILE_PATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_DEFINITION_IMPORT_FILE_PATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_SOURCE; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_CHANGELOG_FILEPATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_DEFINITION_FILEPATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_MANIFEST_FILEPATH; + + +class AsdCsarValidatorTest { + + private AsdValidator asdValidator; + protected OnboardingPackageContentHandler handler; + protected StringBuilder metaFileBuilder; + private ValidatorFactory validatorFactory; + + @BeforeEach + void setUp() { + validatorFactory = new ValidatorFactory(); + asdValidator = new AsdValidator(); + handler = new OnboardingPackageContentHandler(); + metaFileBuilder = getMetaFileBuilder(); + } + + protected StringBuilder getMetaFileBuilder() { + return new StringBuilder() + .append(ENTRY_DEFINITION_TYPE.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" asd").append("\n") + .append(APPLICATION_PROVIDER.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" Ericsson").append("\n") + .append(APPLICATION_NAME.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" vCU").append("\n") + .append(RELEASE_DATE_TIME.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 2022-02-07T11:30:00+05:00").append("\n") + .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 + void testGivenASDTOSCAMeta_withAllSupportedEntries_thenNoErrorsReturned() { + + final String entryTestFilePath = "Files/Tests"; + final String entryLicenseFilePath = "Files/Licenses"; + + handler.addFolder("Files/Tests/"); + handler.addFolder("Files/Licenses/"); + metaFileBuilder + .append(ETSI_ENTRY_TESTS.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(entryTestFilePath).append("\n") + .append(ETSI_ENTRY_LICENSES.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(entryLicenseFilePath).append("\n"); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH)); + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + + handler.addFile(SAMPLE_SOURCE, "".getBytes()); + handler.addFile(SAMPLE_DEFINITION_IMPORT_FILE_PATH, "".getBytes()); + handler.addFile(entryTestFilePath, "".getBytes()); + handler.addFile(entryLicenseFilePath, "".getBytes()); + + final ManifestBuilder manifestBuilder = getAsdManifestSampleBuilder() + .withSource(TOSCA_META_PATH_FILE_NAME) + .withSource(TOSCA_DEFINITION_FILEPATH) + .withSource(TOSCA_CHANGELOG_FILEPATH) + .withSource(TOSCA_MANIFEST_FILEPATH).withSource(SAMPLE_SOURCE) + .withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH) + .withSource(entryTestFilePath) + .withSource(entryLicenseFilePath); + + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final ValidationResult validationResult = asdValidator.validate(handler); + + assertTrue(validationResult.getErrors().isEmpty()); + } + + @Test + void testGivenASDTOSCAMetaFile_withInvalidOranEntryDefinitionType_thenErrorIsReturned() { + final StringBuilder metaFileBuilder = 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); + final ManifestBuilder manifestBuilder = getWrongAsdManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + final ValidationResult validationResult = asdValidator.validate(handler); + assertExpectedErrors("Invalid value invalid in TOSCA.meta file", validationResult.getErrors(), 2); + } + + @Test + void testGivenASDTOSCAMetaFile_withNoReleaseDateTime_thenNoAsdValidatorIsReturned() throws IOException { + final StringBuilder metaFileBuilder = new StringBuilder() + .append(ENTRY_DEFINITION_TYPE.getToken()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" asd").append("\n") + .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); + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + handler.addFile(TOSCA_DEFINITION_FILEPATH, getResourceBytesOrFail(SAMPLE_DEFINITION_FILE_PATH)); + manifestBuilder.withSource(TOSCA_DEFINITION_FILEPATH); + + handler.addFile(TOSCA_CHANGELOG_FILEPATH, "".getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_CHANGELOG_FILEPATH); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + assertNotEquals(AsdValidator.class, validatorFactory.getValidator(handler).getClass()); + } + + 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"); + } + + protected void assertExpectedErrors(final String testCase, final List errorMessages, final int expectedErrors){ + printErrorMessages(errorMessages); + if (expectedErrors > 0) { + assertEquals(expectedErrors, errorMessages.size(), testCase); + } else { + assertTrue(errorMessages.isEmpty(), testCase); + } + } + + private void printErrorMessages(final List errorMessages) { + if (CollectionUtils.isNotEmpty(errorMessages)) { + errorMessages.forEach(errorMessage -> + System.out.printf("%s: %s%n", errorMessage.getLevel(), errorMessage.getMessage()) + ); + } + } +} diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilderTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilderTest.java index e966a12b65..5672e065dd 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilderTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilderTest.java @@ -39,6 +39,7 @@ import java.util.TreeMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.csar.ManifestBuilder; import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; public class ManifestBuilderTest { diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java index d45031f789..ed1955577a 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidatorTest.java @@ -77,6 +77,7 @@ import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.common.utils.SdcCommon; import org.openecomp.sdc.datatypes.error.ErrorLevel; import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.tosca.csar.ManifestBuilder; import org.openecomp.sdc.tosca.csar.ManifestTokenType; import org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.OnboardingPackageContentHandler; import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager; diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version3MetaDirectoryValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version3MetaDirectoryValidatorTest.java index 9235f363eb..470c9cad27 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version3MetaDirectoryValidatorTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version3MetaDirectoryValidatorTest.java @@ -22,6 +22,7 @@ import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEP import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.OTHER_DEFINITIONS; import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_DEFINITION_FILEPATH; +import org.openecomp.sdc.tosca.csar.ManifestBuilder; import org.openecomp.sdc.tosca.csar.ManifestTokenType; import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager; diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java index fade5d18ad..bfea275d58 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004Version4MetaDirectoryValidatorTest.java @@ -36,6 +36,7 @@ import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.va import java.io.IOException; import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; +import org.openecomp.sdc.tosca.csar.ManifestBuilder; import org.openecomp.sdc.tosca.csar.ManifestTokenType; import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManager; diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java index c603b7fa6d..1eefb2e211 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ValidatorFactoryTest.java @@ -20,6 +20,16 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.tosca.csar.ManifestBuilder; +import org.openecomp.sdc.tosca.csar.ManifestTokenType; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -33,17 +43,12 @@ import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_C import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.TOSCA_META_FILE_VERSION_ENTRY; import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_DEFINITION_IMPORT_FILE_PATH; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.SAMPLE_SOURCE; import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_CHANGELOG_FILEPATH; import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_DEFINITION_FILEPATH; import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.TestConstants.TOSCA_MANIFEST_FILEPATH; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.openecomp.core.utilities.file.FileContentHandler; - class ValidatorFactoryTest { private String metaFile; @@ -105,6 +110,26 @@ class ValidatorFactoryTest { assertEquals(SOL004MetaDirectoryValidator.class, validatorFactory.getValidator(handler).getClass()); } + @Test + void testGivenAsdCompliantMetafile_thenAsdCsarValidatorIsReturned() throws IOException { + metaFile = metaFile + + ENTRY_DEFINITIONS.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_DEFINITION_FILEPATH + "\n" + + ETSI_ENTRY_MANIFEST.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_MANIFEST_FILEPATH + "\n" + + ETSI_ENTRY_CHANGE_LOG.getName() + ATTRIBUTE_VALUE_SEPARATOR.getToken() + TOSCA_CHANGELOG_FILEPATH + "\n"; + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFile.getBytes(StandardCharsets.UTF_8)); + + final ManifestBuilder manifestBuilder = getAsdManifestSampleBuilder() + .withSource(TOSCA_META_PATH_FILE_NAME) + .withSource(TOSCA_DEFINITION_FILEPATH) + .withSource(TOSCA_CHANGELOG_FILEPATH) + .withSource(TOSCA_MANIFEST_FILEPATH).withSource(SAMPLE_SOURCE) + .withSource(SAMPLE_DEFINITION_IMPORT_FILE_PATH); + + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + + assertEquals(AsdValidator.class, validatorFactory.getValidator(handler).getClass()); + } + @Test void testGivenMultiBlockMetadataWithSOL00CompliantMetaFile_thenSOL004MetaDirectoryValidatorReturned() throws IOException { @@ -130,4 +155,12 @@ class ValidatorFactoryTest { assertTrue(validatorList1.isEmpty()); } + protected ManifestBuilder getAsdManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(ManifestTokenType.APPLICATION_NAME.getToken(), "RadioNode") + .withMetaData(ManifestTokenType.APPLICATION_PROVIDER.getToken(), "Ericsson") + .withMetaData(ManifestTokenType.ENTRY_DEFINITION_TYPE.getToken(), "asd") + .withMetaData(ManifestTokenType.RELEASE_DATE_TIME.getToken(), "2022-02-01T11:25:00+00:00"); + } + } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java index a0ab46e589..dbec8f7473 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java @@ -19,31 +19,7 @@ */ package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ARTIFACTS_FOLDER; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; -import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME; -import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; -import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST; -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.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; - import com.vdurmont.semver4j.Semver; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; import org.onap.sdc.tosca.services.YamlUtil; @@ -57,29 +33,56 @@ import org.openecomp.sdc.common.CommonConfigurationManager; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.csar.ManifestUtils; import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata; import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; import org.openecomp.sdc.tosca.csar.ToscaMetadata; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ARTIFACTS_FOLDER; +import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; +import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; +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.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; + public class ETSIServiceImpl implements ETSIService { private static final Logger LOGGER = LoggerFactory.getLogger(ETSIServiceImpl.class); private final NonManoConfiguration nonManoConfiguration; - private final String ONAP_CSAR = "onap_csar"; + private final ManifestUtils manifestUtils; + private static final String ONAP_CSAR = "onap_csar"; public ETSIServiceImpl() { nonManoConfiguration = NonManoConfigurationManager.getInstance().getNonManoConfiguration(); + manifestUtils = new ManifestUtils(); } - public ETSIServiceImpl(final NonManoConfiguration nonManoConfiguration) { + public ETSIServiceImpl(final NonManoConfiguration nonManoConfiguration, + final ManifestUtils manifestUtils) { this.nonManoConfiguration = nonManoConfiguration; + this.manifestUtils = manifestUtils; } @Override public boolean hasEtsiSol261Metadata(FileContentHandler handler) throws IOException { final Map templates = handler.getFiles(); - return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler)); + return isMetaFilePresent(templates) && hasMetaMandatoryEntries(manifestUtils.getMetadata(handler)); } @Override @@ -88,6 +91,8 @@ public class ETSIServiceImpl implements ETSIService { && !ONAP_CSAR.equalsIgnoreCase(getDefaultCsarFormat()); } + + private boolean hasOnapCsarMetadata(final FileContentHandler fileContentHandler) throws IOException { if (fileContentHandler.containsFile(TOSCA_META_PATH_FILE_NAME)){ final ToscaMetadata metadata = @@ -103,7 +108,7 @@ public class ETSIServiceImpl implements ETSIService { @Override public Optional> moveNonManoFileToArtifactFolder(final FileContentHandler handler) throws IOException { - final Manifest manifest = loadManifest(handler); + final Manifest manifest = manifestUtils.loadManifest(handler, new SOL004ManifestOnboarding()); final Path originalManifestPath; try { originalManifestPath = getOriginalManifestPath(handler); @@ -223,32 +228,19 @@ public class ETSIServiceImpl implements ETSIService { @Override public boolean hasCnfEnhancements(final FileContentHandler fileContentHandler) throws IOException { - final Manifest manifest = loadManifest(fileContentHandler); + final Manifest manifest = manifestUtils.loadManifest(fileContentHandler, new SOL004ManifestOnboarding()); return manifest.getNonManoSources().entrySet().stream() .filter(manifestNonManoSourceEntry -> NonManoArtifactType.ONAP_CNF_HELM.getType().equalsIgnoreCase(manifestNonManoSourceEntry.getKey())) .findFirst().isPresent(); } - private Manifest loadManifest(final FileContentHandler handler) throws IOException { - final Manifest manifest; - try { - manifest = getManifest(handler); - } catch (final IOException ex) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error("An error occurred while getting the manifest file", ex); - } - throw ex; - } - return manifest; - } - private boolean isMetaFilePresent(Map handler) { return handler.containsKey(TOSCA_META_PATH_FILE_NAME) || handler.containsKey(TOSCA_META_ORIG_PATH_FILE_NAME); } public ResourceTypeEnum getResourceType(FileContentHandler handler) throws IOException { - ToscaMetadata metadata = getMetadata(handler); - Manifest manifest = getManifest(handler, getEntryManifestLocation(metadata)); + ToscaMetadata metadata = manifestUtils.getMetadata(handler); + Manifest manifest = manifestUtils.getManifest(handler, manifestUtils.getEntryManifestLocation(metadata), new SOL004ManifestOnboarding()); return getResourceType(manifest); } @@ -265,43 +257,16 @@ public class ETSIServiceImpl implements ETSIService { } public Manifest getManifest(FileContentHandler handler) throws IOException { - ToscaMetadata metadata = getMetadata(handler); - return getManifest(handler, getEntryManifestLocation(metadata)); - } - - private 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()); - } - - private Manifest getManifest(FileContentHandler handler, String manifestLocation) throws IOException { - try (InputStream manifestInputStream = getManifestInputStream(handler, manifestLocation)) { - Manifest onboardingManifest = new SOL004ManifestOnboarding(); - onboardingManifest.parse(manifestInputStream); - return onboardingManifest; - } + return manifestUtils.getManifest(handler, new SOL004ManifestOnboarding()); } public Path getOriginalManifestPath(final FileContentHandler handler) throws IOException { final ToscaMetadata metadata = getOriginalMetadata(handler); - final String originalMetadataPath = getEntryManifestLocation(metadata); + final String originalMetadataPath = manifestUtils.getEntryManifestLocation(metadata); final Path path = Paths.get(originalMetadataPath); return path.getParent() == null ? Paths.get("") : path.getParent(); } - private ToscaMetadata getMetadata(FileContentHandler handler) throws IOException { - ToscaMetadata metadata; - if (handler.containsFile(TOSCA_META_PATH_FILE_NAME)) { - metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)); - } else if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { - metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); - } else { - throw new IOException("TOSCA.meta file not found!"); - } - return metadata; - } - private ToscaMetadata getOriginalMetadata(final FileContentHandler handler) throws IOException { if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { return OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); @@ -310,19 +275,6 @@ public class ETSIServiceImpl implements ETSIService { } } - 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; - } - public NonManoConfiguration getConfiguration() { return nonManoConfiguration; } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java index 314db8f323..e7a5d56b3e 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java @@ -20,17 +20,20 @@ package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; +import com.vdurmont.semver4j.Semver; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.be.config.NonManoConfiguration; +import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.csar.ManifestUtils; +import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.yaml.snakeyaml.Yaml; import java.io.File; import java.io.IOException; @@ -47,23 +50,23 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.onap.sdc.tosca.services.YamlUtil; -import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.sdc.be.config.NonManoConfiguration; -import org.openecomp.sdc.tosca.csar.Manifest; -import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; -import org.yaml.snakeyaml.Yaml; - -import com.vdurmont.semver4j.Semver; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; public class ETSIServiceImplTest { private ETSIService etsiService; + private ManifestUtils manifestUtils; private String sol004MetaFile = "TOSCA-Meta-Version: 1.0\n" + "CSAR-Version: 1.0\n" + "Created-By: Kuku\n" + @@ -77,14 +80,15 @@ public class ETSIServiceImplTest { private String finalNonManoLocation = "Deployment/VES_EVENTS/test.xml"; - @Before + @BeforeEach public void setUp() throws IOException { final String fullFileName = Paths.get("src", "test", "resources", "nonManoConfig.yaml").toString(); final NonManoConfiguration configuration = convert(fullFileName, NonManoConfiguration.class); - etsiService = Mockito.spy(new ETSIServiceImpl(configuration)); + manifestUtils = Mockito.spy(new ManifestUtils()); + etsiService = Mockito.spy(new ETSIServiceImpl(configuration, manifestUtils)); } - @After + @AfterEach public void tearDown() { etsiService = null; } @@ -127,8 +131,10 @@ public class ETSIServiceImplTest { fileContentHandler.addFile("Some/test.xml", new byte[1]); fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", new byte[1]); fileContentHandler.addFile("MainServiceTemplate.mf", new byte[1]); + doReturn(manifest).when(manifestUtils).loadManifest(fileContentHandler, new SOL004ManifestOnboarding()); doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation)); } @@ -144,6 +150,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation)); } @@ -157,7 +164,9 @@ public class ETSIServiceImplTest { FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile("test.xml", new byte[1]); Manifest manifest = mock(Manifest.class); + //Manifest manifest = etsiService.getManifest(fileContentHandler); doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); @@ -177,6 +186,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); } @@ -193,6 +203,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); } @@ -216,6 +227,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("two/lvlFolder")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); //when files are non mano moved etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Should contain moved file", fileContentHandler.getFileList(), @@ -249,6 +261,7 @@ public class ETSIServiceImplTest { when(manifest.getNonManoSources()).thenReturn(nonManoSourceMap); doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); final Optional> fromToPathMap = etsiService .moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Files should be moved", fromToPathMap.isPresent(), is(true)); 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(); + } +} 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 -- cgit 1.2.3-korg