From 55401bf89a8bc1c51f76554faac278186560724a Mon Sep 17 00:00:00 2001 From: franciscovila Date: Fri, 4 Feb 2022 10:47:33 +0000 Subject: [PATCH] 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 --- .../templates/default/BE-configuration.yaml.erb | 1 + .../data/providers/OnboardingDataProviders.java | 6 + .../ci/tests/datatypes/enums/PackageTypeEnum.java | 3 +- .../onap/sdc/backend/ci/tests/sanity/Onboard.java | 6 + .../ci/tests/utils/general/FileHandling.java | 5 +- .../src/test/resources/Files/ASDs/asd.csar | Bin 0 -> 11330 bytes .../csar/validation/AsdValidator.java | 122 ++++++++++ .../validation/EtsiSol004Version251Validator.java | 15 +- .../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 | 6 +- .../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 ++++++++++++ 29 files changed, 1085 insertions(+), 155 deletions(-) create mode 100644 integration-tests/src/test/resources/Files/ASDs/asd.csar 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 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 rename openecomp-be/{backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation => lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar}/ManifestBuilder.java (98%) 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 diff --git a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/templates/default/BE-configuration.yaml.erb b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/templates/default/BE-configuration.yaml.erb index 325cba022e..aa4c8c4f6a 100644 --- a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/templates/default/BE-configuration.yaml.erb +++ b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/templates/default/BE-configuration.yaml.erb @@ -1258,6 +1258,7 @@ cadiFilterParams: definedResourceNamespace: - org.openecomp.resource. - org.onap.policy.clamp.acm. + - tosca.nodes. # This configuration entry lists all Directives values that shall be allowed on SDC. directives: diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/data/providers/OnboardingDataProviders.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/data/providers/OnboardingDataProviders.java index 10d7b38899..e84256fbc6 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/data/providers/OnboardingDataProviders.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/data/providers/OnboardingDataProviders.java @@ -72,6 +72,12 @@ public final class OnboardingDataProviders { FileHandling.getPackageRepositoryPath(PackageTypeEnum.PNF)); } + @DataProvider(name = "ASD_List", parallel = true) + private static Object[][] asdList() { + return provideData(OnboardingUtils.getXnfNamesFileList(PackageTypeEnum.ASD), + FileHandling.getPackageRepositoryPath(PackageTypeEnum.ASD)); + } + @DataProvider(name = "CNF_List", parallel = true) private static Object[][] cnfList() { final List fileNamesFromFolder = OnboardingUtils.getXnfNamesFileList(PackageTypeEnum.CNF); diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/datatypes/enums/PackageTypeEnum.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/datatypes/enums/PackageTypeEnum.java index 0e67cdaeb9..99c2670161 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/datatypes/enums/PackageTypeEnum.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/datatypes/enums/PackageTypeEnum.java @@ -32,7 +32,8 @@ public enum PackageTypeEnum { ETSI("ETSI"), PNF("PNF"), VFC("VFC"), - VNF("VNF"); + VNF("VNF"), + ASD("ASD"); private String value; } diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/sanity/Onboard.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/sanity/Onboard.java index a3323b98d3..a0600f9995 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/sanity/Onboard.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/sanity/Onboard.java @@ -81,6 +81,12 @@ public class Onboard extends ComponentBaseTest { runOnboardToDistributionFlow(filePath, pnfFile, ResourceTypeEnum.PNF); } + @Test(dataProviderClass = OnboardingDataProviders.class, dataProvider = "ASD_List") + public void onboardASDFlow(String filePath, String asdFile) throws Exception { + setLog(asdFile); + runOnboardToDistributionFlow(filePath, asdFile, ResourceTypeEnum.VF); + } + @Test(dataProviderClass = OnboardingDataProviders.class, dataProvider = "CNF_List") public void onboardCNFFlow(String filePath, String cnfFile) throws Exception { setLog(cnfFile); diff --git a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/utils/general/FileHandling.java b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/utils/general/FileHandling.java index 81ca398813..3cc8e26e15 100644 --- a/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/utils/general/FileHandling.java +++ b/integration-tests/src/test/java/org/onap/sdc/backend/ci/tests/utils/general/FileHandling.java @@ -138,7 +138,8 @@ public class FileHandling { PackageTypeEnum.CNF_HELM, getCnfRepositoryForHelmValidatorPath(), PackageTypeEnum.VNF, getVnfRepositoryPath(), PackageTypeEnum.ETSI, getEtsiRepositoryPath(), - PackageTypeEnum.VFC, getVfcRepositoryPath() + PackageTypeEnum.VFC, getVfcRepositoryPath(), + PackageTypeEnum.ASD, getAsdRepositoryPath() )); public static String getVnfRepositoryPath() { @@ -159,6 +160,8 @@ public class FileHandling { private static String getEtsiRepositoryPath() { return getFilePath("ETSI"); } + private static String getAsdRepositoryPath() { return getFilePath("ASDs"); } + private static String getVfcRepositoryPath() { return getFilePath("VFCs"); } public static String getPackageRepositoryPath(PackageTypeEnum packageTypeEnum) { diff --git a/integration-tests/src/test/resources/Files/ASDs/asd.csar b/integration-tests/src/test/resources/Files/ASDs/asd.csar new file mode 100644 index 0000000000000000000000000000000000000000..3f928b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khZ8`FjWD~$EKY=N5Dp^q>Wg*dl%iaZp6#~OZSbC*)Ko$X*D~{b zGfTVV8-VvPM_U*wXE)3=U+^hXoyiE#J3~0GPkDXQJ2pqAugZ(Y?f`-+0|vQSIDxI8rUF|#zFr3hbD}>#y};A?lAf1BUo9Fd`vMdz z$jGHlyt6KMJKugXB{X_>zJes+2JeqIM=y_-1Vg0$WiZ%$hf=Le`>GFKteAonRz#*P zyHA;^yn6Dy==H)mZqecu(GGcNj~XOO3?Xwq^;z9)%=RsDc69Qb&nY8c!oBjOdoPk4 zc_USyy+#pIjo3+Xm3Y*-L2}BZ(gP|G2s$=1wRg9MXaV!R@D2qJ3F1qG!SGv2Q$q+H zKBeTqdpOlv2!Px-C{VWYX7}tNcfmpiYu@zK$NTF2TtoV#!0&PKg8DjA4}(}RbUgB* zLJSoxRZZzOk)2?BGEXP$d)zR$)hCb5Jk!Yfs6z1sn{7bL!M(9XfaxvTjpK`Io zX5OQ*AMc?wRq)w`Hf^NJGB8<+##?VUn#jOOSRCx=fdo|T1($LCKX0J;MveB$O+RA% z^jZnH|6YCwRUhh#(1NzWpIg6*5&zlePepG(ul&`g8`}J-1n!>*f2vjbji3N6gZvl5 zKPs1gKXZR3wtwMJLL18`e?KVyi{}0f@Mr$|C%`Y~?*0$J{|}q}PuxE-&cAUjp#=P| zC-~pE|A&A6C+MFj;-8?uA`CLrpua%>L>T|Z`7?p>8>b%XzQ1t(NN4;8`W5p2%~$*e zLWc&bUqJs4_To1r0Pv^0@e}fwL6&I$hWvLH`QLH)-{JCS|6ij-P5(E-pMvLaO@7_n uzoXr6oXY>;{P)=RPqaV9h~H@a&^HqQHDdgoA20wIfEqXe;3ebFj{gJk?(ORU literal 0 HcmV?d00001 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/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/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilder.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java similarity index 98% rename from openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/ManifestBuilder.java rename to openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java index ee708b4176..275bed5a0f 100644 --- 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/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation + * 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. @@ -15,8 +15,10 @@ * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= + * + * */ -package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation; +package org.openecomp.sdc.tosca.csar; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ALGORITHM; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.HASH; 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 -- 2.16.6