From 55401bf89a8bc1c51f76554faac278186560724a Mon Sep 17 00:00:00 2001 From: franciscovila Date: Fri, 4 Feb 2022 10:47:33 +0000 Subject: Create onboarding validator for ASD VSPs Create a new validator in SDC onboarding that will be used for ASD VSPs Issue-ID: SDC-3859 Signed-off-by: franciscovila Change-Id: Ida192122a95414f7d18855fe2a993b269c55a91d --- .../services/impl/etsi/ETSIServiceImpl.java | 124 +++++----------- .../services/impl/etsi/ETSIServiceImplTest.java | 67 +++++---- .../sdc/tosca/csar/AbstractOnboardingManifest.java | 2 +- .../sdc/tosca/csar/AsdManifestOnboarding.java | 104 +++++++++++++ .../openecomp/sdc/tosca/csar/AsdPackageHelper.java | 57 ++++++++ .../openecomp/sdc/tosca/csar/CSARConstants.java | 5 + .../openecomp/sdc/tosca/csar/ManifestBuilder.java | 161 +++++++++++++++++++++ .../sdc/tosca/csar/ManifestTokenType.java | 22 ++- .../openecomp/sdc/tosca/csar/ManifestUtils.java | 138 ++++++++++++++++++ .../sdc/tosca/csar/SOL004ManifestOnboarding.java | 8 +- .../sdc/tosca/csar/ToscaMetaEntryAsd.java | 51 +++++++ .../sdc/tosca/csar/AsdPackageHelperTest.java | 143 ++++++++++++++++++ 12 files changed, 762 insertions(+), 120 deletions(-) create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java create mode 100644 openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java (limited to 'openecomp-be/lib') diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java index a0ab46e589..dbec8f7473 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java @@ -19,31 +19,7 @@ */ package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ARTIFACTS_FOLDER; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; -import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME; -import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; -import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ENTRY_DEFINITIONS; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_CHANGE_LOG; -import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; -import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; - import com.vdurmont.semver4j.Semver; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; import org.onap.sdc.tosca.services.YamlUtil; @@ -57,29 +33,56 @@ import org.openecomp.sdc.common.CommonConfigurationManager; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.csar.ManifestUtils; import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata; import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; import org.openecomp.sdc.tosca.csar.ToscaMetadata; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ARTIFACTS_FOLDER; +import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; +import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_CHANGE_LOG; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; + public class ETSIServiceImpl implements ETSIService { private static final Logger LOGGER = LoggerFactory.getLogger(ETSIServiceImpl.class); private final NonManoConfiguration nonManoConfiguration; - private final String ONAP_CSAR = "onap_csar"; + private final ManifestUtils manifestUtils; + private static final String ONAP_CSAR = "onap_csar"; public ETSIServiceImpl() { nonManoConfiguration = NonManoConfigurationManager.getInstance().getNonManoConfiguration(); + manifestUtils = new ManifestUtils(); } - public ETSIServiceImpl(final NonManoConfiguration nonManoConfiguration) { + public ETSIServiceImpl(final NonManoConfiguration nonManoConfiguration, + final ManifestUtils manifestUtils) { this.nonManoConfiguration = nonManoConfiguration; + this.manifestUtils = manifestUtils; } @Override public boolean hasEtsiSol261Metadata(FileContentHandler handler) throws IOException { final Map templates = handler.getFiles(); - return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler)); + return isMetaFilePresent(templates) && hasMetaMandatoryEntries(manifestUtils.getMetadata(handler)); } @Override @@ -88,6 +91,8 @@ public class ETSIServiceImpl implements ETSIService { && !ONAP_CSAR.equalsIgnoreCase(getDefaultCsarFormat()); } + + private boolean hasOnapCsarMetadata(final FileContentHandler fileContentHandler) throws IOException { if (fileContentHandler.containsFile(TOSCA_META_PATH_FILE_NAME)){ final ToscaMetadata metadata = @@ -103,7 +108,7 @@ public class ETSIServiceImpl implements ETSIService { @Override public Optional> moveNonManoFileToArtifactFolder(final FileContentHandler handler) throws IOException { - final Manifest manifest = loadManifest(handler); + final Manifest manifest = manifestUtils.loadManifest(handler, new SOL004ManifestOnboarding()); final Path originalManifestPath; try { originalManifestPath = getOriginalManifestPath(handler); @@ -223,32 +228,19 @@ public class ETSIServiceImpl implements ETSIService { @Override public boolean hasCnfEnhancements(final FileContentHandler fileContentHandler) throws IOException { - final Manifest manifest = loadManifest(fileContentHandler); + final Manifest manifest = manifestUtils.loadManifest(fileContentHandler, new SOL004ManifestOnboarding()); return manifest.getNonManoSources().entrySet().stream() .filter(manifestNonManoSourceEntry -> NonManoArtifactType.ONAP_CNF_HELM.getType().equalsIgnoreCase(manifestNonManoSourceEntry.getKey())) .findFirst().isPresent(); } - private Manifest loadManifest(final FileContentHandler handler) throws IOException { - final Manifest manifest; - try { - manifest = getManifest(handler); - } catch (final IOException ex) { - if (LOGGER.isErrorEnabled()) { - LOGGER.error("An error occurred while getting the manifest file", ex); - } - throw ex; - } - return manifest; - } - private boolean isMetaFilePresent(Map handler) { return handler.containsKey(TOSCA_META_PATH_FILE_NAME) || handler.containsKey(TOSCA_META_ORIG_PATH_FILE_NAME); } public ResourceTypeEnum getResourceType(FileContentHandler handler) throws IOException { - ToscaMetadata metadata = getMetadata(handler); - Manifest manifest = getManifest(handler, getEntryManifestLocation(metadata)); + ToscaMetadata metadata = manifestUtils.getMetadata(handler); + Manifest manifest = manifestUtils.getManifest(handler, manifestUtils.getEntryManifestLocation(metadata), new SOL004ManifestOnboarding()); return getResourceType(manifest); } @@ -265,43 +257,16 @@ public class ETSIServiceImpl implements ETSIService { } public Manifest getManifest(FileContentHandler handler) throws IOException { - ToscaMetadata metadata = getMetadata(handler); - return getManifest(handler, getEntryManifestLocation(metadata)); - } - - private String getEntryManifestLocation(final ToscaMetadata metadata) { - return metadata.getMetaEntries().containsKey(ETSI_ENTRY_MANIFEST.getName()) ? - metadata.getMetaEntries().get(ETSI_ENTRY_MANIFEST.getName()): - metadata.getMetaEntries().get(ENTRY_MANIFEST.getName()); - } - - private Manifest getManifest(FileContentHandler handler, String manifestLocation) throws IOException { - try (InputStream manifestInputStream = getManifestInputStream(handler, manifestLocation)) { - Manifest onboardingManifest = new SOL004ManifestOnboarding(); - onboardingManifest.parse(manifestInputStream); - return onboardingManifest; - } + return manifestUtils.getManifest(handler, new SOL004ManifestOnboarding()); } public Path getOriginalManifestPath(final FileContentHandler handler) throws IOException { final ToscaMetadata metadata = getOriginalMetadata(handler); - final String originalMetadataPath = getEntryManifestLocation(metadata); + final String originalMetadataPath = manifestUtils.getEntryManifestLocation(metadata); final Path path = Paths.get(originalMetadataPath); return path.getParent() == null ? Paths.get("") : path.getParent(); } - private ToscaMetadata getMetadata(FileContentHandler handler) throws IOException { - ToscaMetadata metadata; - if (handler.containsFile(TOSCA_META_PATH_FILE_NAME)) { - metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)); - } else if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { - metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); - } else { - throw new IOException("TOSCA.meta file not found!"); - } - return metadata; - } - private ToscaMetadata getOriginalMetadata(final FileContentHandler handler) throws IOException { if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { return OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); @@ -310,19 +275,6 @@ public class ETSIServiceImpl implements ETSIService { } } - private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException { - InputStream io; - if (manifestLocation == null || !handler.containsFile(manifestLocation)) { - io = handler.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME); - } else { - io = handler.getFileContentAsStream(manifestLocation); - } - if (io == null) { - throw new IOException("Manifest file not found!"); - } - return io; - } - public NonManoConfiguration getConfiguration() { return nonManoConfiguration; } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java index 314db8f323..e7a5d56b3e 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java @@ -20,17 +20,20 @@ package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; +import com.vdurmont.semver4j.Semver; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.be.config.NonManoConfiguration; +import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.csar.ManifestUtils; +import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.yaml.snakeyaml.Yaml; import java.io.File; import java.io.IOException; @@ -47,23 +50,23 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.onap.sdc.tosca.services.YamlUtil; -import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.sdc.be.config.NonManoConfiguration; -import org.openecomp.sdc.tosca.csar.Manifest; -import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; -import org.yaml.snakeyaml.Yaml; - -import com.vdurmont.semver4j.Semver; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.tosca.csar.CSARConstants.ETSI_VERSION_2_6_1; public class ETSIServiceImplTest { private ETSIService etsiService; + private ManifestUtils manifestUtils; private String sol004MetaFile = "TOSCA-Meta-Version: 1.0\n" + "CSAR-Version: 1.0\n" + "Created-By: Kuku\n" + @@ -77,14 +80,15 @@ public class ETSIServiceImplTest { private String finalNonManoLocation = "Deployment/VES_EVENTS/test.xml"; - @Before + @BeforeEach public void setUp() throws IOException { final String fullFileName = Paths.get("src", "test", "resources", "nonManoConfig.yaml").toString(); final NonManoConfiguration configuration = convert(fullFileName, NonManoConfiguration.class); - etsiService = Mockito.spy(new ETSIServiceImpl(configuration)); + manifestUtils = Mockito.spy(new ManifestUtils()); + etsiService = Mockito.spy(new ETSIServiceImpl(configuration, manifestUtils)); } - @After + @AfterEach public void tearDown() { etsiService = null; } @@ -127,8 +131,10 @@ public class ETSIServiceImplTest { fileContentHandler.addFile("Some/test.xml", new byte[1]); fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", new byte[1]); fileContentHandler.addFile("MainServiceTemplate.mf", new byte[1]); + doReturn(manifest).when(manifestUtils).loadManifest(fileContentHandler, new SOL004ManifestOnboarding()); doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation)); } @@ -144,6 +150,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation)); } @@ -157,7 +164,9 @@ public class ETSIServiceImplTest { FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile("test.xml", new byte[1]); Manifest manifest = mock(Manifest.class); + //Manifest manifest = etsiService.getManifest(fileContentHandler); doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); @@ -177,6 +186,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); } @@ -193,6 +203,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); } @@ -216,6 +227,7 @@ public class ETSIServiceImplTest { doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("two/lvlFolder")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); //when files are non mano moved etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Should contain moved file", fileContentHandler.getFileList(), @@ -249,6 +261,7 @@ public class ETSIServiceImplTest { when(manifest.getNonManoSources()).thenReturn(nonManoSourceMap); doReturn(manifest).when(etsiService).getManifest(fileContentHandler); doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); + doReturn(manifest).when(manifestUtils).loadManifest(any(FileContentHandler.class), any(SOL004ManifestOnboarding.class)); final Optional> fromToPathMap = etsiService .moveNonManoFileToArtifactFolder(fileContentHandler); assertThat("Files should be moved", fromToPathMap.isPresent(), is(true)); diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java index 5b64c8e0af..1aab56c7ed 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AbstractOnboardingManifest.java @@ -38,7 +38,7 @@ import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; -abstract class AbstractOnboardingManifest implements Manifest { +public abstract class AbstractOnboardingManifest implements Manifest { protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractOnboardingManifest.class); protected static final int MAX_ALLOWED_MANIFEST_META_ENTRIES = 4; diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java new file mode 100644 index 0000000000..7f32893798 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdManifestOnboarding.java @@ -0,0 +1,104 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ +package org.openecomp.sdc.tosca.csar; + +import org.apache.commons.lang3.StringUtils; +import org.openecomp.sdc.common.errors.Messages; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ASD_MANIFEST_META_ENTRIES; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; + +/** + * Processes a ASD Manifest. + */ +public class AsdManifestOnboarding extends SOL004ManifestOnboarding { + + @Override + protected boolean isMetadataEntry(final String metadataEntry) { + final Optional manifestTokenType = ManifestTokenType.parse(metadataEntry); + return manifestTokenType.map(ManifestTokenType::isMetadataAsdEntry).orElse(false); + } + + /** + * Processes {@link ManifestTokenType#SOURCE} entries in {@link ManifestTokenType#NON_MANO_ARTIFACT_SETS}. + * + * @return A list of sources paths + */ + protected List readNonManoSourceList() { + final List nonManoSourceList = new ArrayList<>(); + while (getCurrentLine().isPresent()) { + final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); + if (!(manifestTokenType == ManifestTokenType.SOURCE || manifestTokenType == ManifestTokenType.VENDOR_NAME || manifestTokenType == ManifestTokenType.ARTIFACT_TYPE)) { + break; + } + if (manifestTokenType == ManifestTokenType.SOURCE) { + final String value = readCurrentEntryValue().orElse(null); + if (!StringUtils.isEmpty(value)) { + nonManoSourceList.add(value); + } else { + reportError(Messages.MANIFEST_EMPTY_NON_MANO_SOURCE); + break; + } + } + readNextNonEmptyLine(); + } + return nonManoSourceList; + } + + /** + * Validates the manifest metadata content, reporting errors found. + * + * @return {@code true} if the metadata content is valid, {@code false} otherwise. + */ + protected boolean validateMetadata() { + if (metadata.isEmpty()) { + reportError(Messages.MANIFEST_NO_METADATA); + return false; + } + String key = metadata.keySet().stream().filter(k -> !COMPATIBLE_SPECIFICATION_VERSIONS.getToken().equals(k)).findFirst().orElse(null); + final ManifestTokenType firstManifestEntryTokenType = ManifestTokenType.parse(key).orElse(null); + if (firstManifestEntryTokenType == null) { + reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, key); + return false; + } + for (final Map.Entry manifestEntry : metadata.entrySet()) { + final ManifestTokenType manifestEntryTokenType = ManifestTokenType.parse(manifestEntry.getKey()).orElse(null); + if (manifestEntryTokenType == null || !manifestEntryTokenType.isMetadataAsdEntry()) { + reportError(Messages.MANIFEST_METADATA_INVALID_ENTRY1, manifestEntry.getKey()); + return false; + } + } + if (metadata.entrySet().size() != getMaxAllowedManifestMetaEntries()) { + reportError(Messages.MANIFEST_METADATA_DOES_NOT_MATCH_LIMIT, getMaxAllowedManifestMetaEntries()); + return false; + } + return true; + } + + protected int getMaxAllowedManifestMetaEntries() { + return ASD_MANIFEST_META_ENTRIES; + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java new file mode 100644 index 0000000000..0b29826831 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/AsdPackageHelper.java @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ +package org.openecomp.sdc.tosca.csar; + +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +import java.io.IOException; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.ASD_DEFINITION_TYPE; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; + +/** + * Helper class for ASD packages. + */ +public class AsdPackageHelper { + + protected static final Logger LOGGER = LoggerFactory.getLogger(AsdPackageHelper.class); + + private final ManifestUtils manifestUtils; + + public AsdPackageHelper(ManifestUtils manifestUtils) { + this.manifestUtils = manifestUtils; + } + + public boolean isAsdPackage(final FileContentHandler fileContentHandler) { + try { + final Manifest manifest = manifestUtils.loadManifest(fileContentHandler, new AsdManifestOnboarding()); + return null != manifest && manifest.getMetadata().entrySet().stream() + .anyMatch(manifestEntry -> ENTRY_DEFINITION_TYPE.getToken().equalsIgnoreCase(manifestEntry.getKey()) + && ASD_DEFINITION_TYPE.equalsIgnoreCase(manifestEntry.getValue())); + } + catch (IOException ioe) { + LOGGER.warn("There was a problem loading the manifest: ", ioe); + return false; + } + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java index cd83cc816b..f99f326d00 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/CSARConstants.java @@ -18,10 +18,12 @@ package org.openecomp.sdc.tosca.csar; import static com.google.common.collect.ImmutableSet.of; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.COMPATIBLE_SPECIFICATION_VERSIONS; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_ARCHIVE_VERSION; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_NAME; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_PROVIDER; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_RELEASE_DATE_TIME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.RELEASE_DATE_TIME; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNFD_ID; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNFM_INFO; import static org.openecomp.sdc.tosca.csar.ManifestTokenType.VNF_PACKAGE_VERSION; @@ -48,6 +50,7 @@ public class CSARConstants { public static final ImmutableSet MANIFEST_VNF_METADATA_VERSION_3 = of(COMPATIBLE_SPECIFICATION_VERSIONS.getToken(), VNFD_ID.getToken(), VNF_PROVIDER_ID.getToken(), VNF_PRODUCT_NAME.getToken(), VNF_RELEASE_DATE_TIME.getToken(), VNF_PACKAGE_VERSION.getToken(), VNF_SOFTWARE_VERSION.getToken(), VNFM_INFO.getToken()); + public static final ImmutableSet MANIFEST_ASD_METADATA = of(ENTRY_DEFINITION_TYPE.getToken(), RELEASE_DATE_TIME.getToken()); public static final int MANIFEST_METADATA_LIMIT = 4; public static final int MANIFEST_VNF_METADATA_LIMIT_VERSION_3 = 8; public static final int MANIFEST_PNF_METADATA_LIMIT_VERSION_3 = 5; @@ -60,6 +63,8 @@ public class CSARConstants { public static final String TOSCA_TYPE_PNF = "pnf"; public static final String TOSCA_TYPE_VNF = "vnf"; public static final String TOSCA_MANIFEST_FILE_EXT = "mf"; + public static final String ASD_DEFINITION_TYPE = "asd"; + public static final int ASD_MANIFEST_META_ENTRIES = 4; private CSARConstants() { } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java new file mode 100644 index 0000000000..275bed5a0f --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestBuilder.java @@ -0,0 +1,161 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ +package org.openecomp.sdc.tosca.csar; + +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ALGORITHM; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.HASH; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.METADATA; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.NON_MANO_ARTIFACT_SETS; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.SOURCE; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +/** + * Builds SOL0004 manifest file as a String. + */ +public class ManifestBuilder { + + private static final String PROPERTY_FORMAT = "%s: %s%n"; + private static final String SECTION_FORMAT = "%s:%n"; + private final Map> sourceWithPropertiesMap = new TreeMap<>(); + private final Map> nonManoArtifactMap = new TreeMap<>(); + private final Map metadataMap = new TreeMap<>(); + + /** + * Adds a metadata property. + * + * @param metadataProperty the property name + * @param value the property value + * @return a reference to this object. + */ + public ManifestBuilder withMetaData(final String metadataProperty, final String value) { + metadataMap.put(metadataProperty, value); + return this; + } + + /** + * Adds a manifest source path. + * + * @param sourcePath The source path + * @return a reference to this object. + */ + public ManifestBuilder withSource(final String sourcePath) { + sourceWithPropertiesMap.put(sourcePath, null); + return this; + } + + /** + * Adds a manifest source path with the source sign. + * + * @param sourcePath The source path + * @param hashAlgorithm The hash algorithm + * @param hash The hash representing the sign + * @return a reference to this object. + */ + public ManifestBuilder withSignedSource(final String sourcePath, final String hashAlgorithm, final String hash) { + TreeMap sourcePropertiesMap = new TreeMap<>(); + sourcePropertiesMap.put(ALGORITHM.getToken(), hashAlgorithm); + sourcePropertiesMap.put(HASH.getToken(), hash); + sourceWithPropertiesMap.put(sourcePath, sourcePropertiesMap); + return this; + } + + /** + * Adds a non mano artifact. + * + * @param artifactType the artifact type + * @param sourcePath the artifact source path + * @return a reference to this object. + */ + public ManifestBuilder withNonManoArtifact(final String artifactType, final String sourcePath) { + nonManoArtifactMap.putIfAbsent(artifactType, new ArrayList<>()); + nonManoArtifactMap.get(artifactType).add(sourcePath); + return this; + } + + /** + * Builds the String representing the manifest file. + * + * @return The manifest file as String + */ + public String build() { + final StringBuilder stringBuilder = new StringBuilder(); + if (!metadataMap.isEmpty()) { + stringBuilder.append(buildMetadata()); + } + if (!sourceWithPropertiesMap.isEmpty()) { + stringBuilder.append(buildSource()); + } + if (!nonManoArtifactMap.isEmpty()) { + stringBuilder.append(buildNonManoArtifact()); + } + return stringBuilder.toString(); + } + + private String buildMetadata() { + final StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(String.format(SECTION_FORMAT, METADATA.getToken())); + for (Entry metadataAndValue : metadataMap.entrySet()) { + stringBuilder.append("\t"); + stringBuilder.append(String.format(PROPERTY_FORMAT, metadataAndValue.getKey(), metadataAndValue.getValue())); + } + stringBuilder.append("\n"); + return stringBuilder.toString(); + } + + private String buildSource() { + final StringBuilder stringBuilder = new StringBuilder(); + for (final Entry> signedSourceMap : sourceWithPropertiesMap.entrySet()) { + stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), signedSourceMap.getKey())); + final Map propertiesMap = signedSourceMap.getValue(); + if (propertiesMap != null && !propertiesMap.isEmpty()) { + final String algorithm = propertiesMap.get(ALGORITHM.getToken()); + if (algorithm != null) { + stringBuilder.append(String.format(PROPERTY_FORMAT, ALGORITHM.getToken(), algorithm)); + } + final String hash = propertiesMap.get(HASH.getToken()); + if (hash != null) { + stringBuilder.append(String.format(PROPERTY_FORMAT, HASH.getToken(), hash)); + } + } + } + stringBuilder.append("\n"); + return stringBuilder.toString(); + } + + private String buildNonManoArtifact() { + final StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(String.format(SECTION_FORMAT, NON_MANO_ARTIFACT_SETS.getToken())); + for (Entry> artifactTypeAndSourcesEntry : nonManoArtifactMap.entrySet()) { + stringBuilder.append("\t"); + stringBuilder.append(String.format(SECTION_FORMAT, artifactTypeAndSourcesEntry.getKey())); + for (String source : artifactTypeAndSourcesEntry.getValue()) { + stringBuilder.append("\t\t"); + stringBuilder.append(String.format(PROPERTY_FORMAT, SOURCE.getToken(), source)); + } + } + return stringBuilder.toString(); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestTokenType.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestTokenType.java index 9b6b34f755..d9a2143d24 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestTokenType.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestTokenType.java @@ -45,7 +45,13 @@ public enum ManifestTokenType { PNFD_RELEASE_DATE_TIME("pnfd_release_date_time"), SIGNATURE("Signature"), CERTIFICATE("Certificate"), - COMPATIBLE_SPECIFICATION_VERSIONS("compatible_specification_versions"); + COMPATIBLE_SPECIFICATION_VERSIONS("compatible_specification_versions"), + APPLICATION_NAME("application_name"), + APPLICATION_PROVIDER("application_provider"), + RELEASE_DATE_TIME("release_date_time"), + ENTRY_DEFINITION_TYPE("entry_definition_type"), + VENDOR_NAME("vendor_name"), + ARTIFACT_TYPE("artifact_type"); // @formatter:on private final String token; @@ -55,7 +61,7 @@ public enum ManifestTokenType { } public static Optional parse(final String token) { - return Arrays.stream(values()).filter(it -> it.getToken() != null && it.getToken().equals(token)).findFirst(); + return Arrays.stream(values()).filter(it -> it.getToken() != null && it.getToken().equalsIgnoreCase(token)).findFirst(); } public String getToken() { @@ -94,4 +100,16 @@ public enum ManifestTokenType { return false; } } + + public boolean isMetadataAsdEntry() { + switch (this) { + case APPLICATION_NAME: + case APPLICATION_PROVIDER: + case RELEASE_DATE_TIME: + case ENTRY_DEFINITION_TYPE: + return true; + default: + return false; + } + } } diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java new file mode 100644 index 0000000000..4ebae5027f --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ManifestUtils.java @@ -0,0 +1,138 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ + +package org.openecomp.sdc.tosca.csar; + +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME; + +/** + * Offers method utils dealing with the manifest + */ +public class ManifestUtils { + + public ManifestUtils() { + } + + private static final Logger LOGGER = LoggerFactory.getLogger(ManifestUtils.class); + + /** + * Loads a manifest given the file handler and the type to manifest to load. + * + * @param fileContentHandler The package file handler + * @param manifestHandler The type of abstract manifest to load + * @return The loaded Manifest. + */ + public Manifest loadManifest(final FileContentHandler fileContentHandler, final T manifestHandler) throws IOException { + final Manifest manifest; + try { + manifest = getManifest(fileContentHandler, manifestHandler); + } catch (final IOException ex) { + LOGGER.error("An error occurred while getting the manifest file", ex); + throw ex; + } + return manifest; + } + + /** + * Retrieves the manifest file from the CSAR + * + * @param fileContentHandler contains csar artifacts + * @param manifestHandler The type of abstract manifest to load + * @return The retrieved Manifest + * @throws IOException when TOSCA.meta file or manifest file is invalid + */ + public Manifest getManifest(FileContentHandler fileContentHandler, T manifestHandler) throws IOException { + ToscaMetadata metadata = getMetadata(fileContentHandler); + return getManifest(fileContentHandler, getEntryManifestLocation(metadata), manifestHandler); + } + + /** + * Retrieves the metadata from the CSAR + * + * @param fileContentHandler contains csar artifacts + * @return The retrieved metadata + * @throws IOException when TOSCA.meta file or manifest file is invalid + */ + public ToscaMetadata getMetadata(FileContentHandler fileContentHandler) throws IOException { + ToscaMetadata metadata; + if (fileContentHandler.containsFile(TOSCA_META_PATH_FILE_NAME)) { + metadata = OnboardingToscaMetadata.parseToscaMetadataFile(fileContentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME)); + } else if (fileContentHandler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { + metadata = OnboardingToscaMetadata.parseToscaMetadataFile(fileContentHandler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME)); + } else { + throw new IOException("TOSCA.meta file not found!"); + } + return metadata; + } + + /** + * Retrieves the manifest location present in the metadata within the CSAR + * + * @param metadata the CSAR metadata + * @return The path of the location of the manifest within the CSAR + */ + public String getEntryManifestLocation(final ToscaMetadata metadata) { + return metadata.getMetaEntries().containsKey(ETSI_ENTRY_MANIFEST.getName()) ? + metadata.getMetaEntries().get(ETSI_ENTRY_MANIFEST.getName()): + metadata.getMetaEntries().get(ENTRY_MANIFEST.getName()); + } + + /** + * Retrieves the manifest given the file handler, the manifest location within the CSAR, + * and the type to manifest to load. + * + * @param fileContentHandler The package file handler + * @param manifestLocation The path of the location of the manifest within the CSAR + * @param manifestHandler The type of abstract manifest to load + * @return The loaded Manifest. + */ + public Manifest getManifest(FileContentHandler fileContentHandler, + String manifestLocation, T manifestHandler) throws IOException { + try (InputStream manifestInputStream = getManifestInputStream(fileContentHandler, manifestLocation)) { + manifestHandler.parse(manifestInputStream); + return manifestHandler; + } + } + + private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException { + InputStream io; + if (manifestLocation == null || !handler.containsFile(manifestLocation)) { + io = handler.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME); + } else { + io = handler.getFileContentAsStream(manifestLocation); + } + if (io == null) { + throw new IOException("Manifest file not found!"); + } + return io; + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java index 390fb3eaab..88e7915188 100644 --- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/SOL004ManifestOnboarding.java @@ -147,7 +147,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { * * @return A list of sources paths */ - private List readNonManoSourceList() { + protected List readNonManoSourceList() { final List nonManoSourceList = new ArrayList<>(); while (getCurrentLine().isPresent()) { final ManifestTokenType manifestTokenType = detectLineEntry().orElse(null); @@ -204,7 +204,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { * * @return the current line manifest token. */ - private Optional detectLineEntry() { + protected Optional detectLineEntry() { final Optional currentLine = getCurrentLine(); if (currentLine.isPresent()) { final String line = currentLine.get(); @@ -223,7 +223,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { * * @return {@code true} if the metadata content is valid, {@code false} otherwise. */ - private boolean validateMetadata() { + protected boolean validateMetadata() { if (metadata.isEmpty()) { reportError(Messages.MANIFEST_NO_METADATA); return false; @@ -362,7 +362,7 @@ public class SOL004ManifestOnboarding extends AbstractOnboardingManifest { readNextNonEmptyLine(); } - private int getMaxAllowedManifestMetaEntries() { + protected int getMaxAllowedManifestMetaEntries() { if (maxAllowedMetaEntries == 0) { boolean isVersion3 = metadata.containsKey(COMPATIBLE_SPECIFICATION_VERSIONS.getToken()) && !getHighestCompatibleVersion().isLowerThan(ETSI_VERSION_2_7_1); diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java new file mode 100644 index 0000000000..73b3b912ac --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/csar/ToscaMetaEntryAsd.java @@ -0,0 +1,51 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ +package org.openecomp.sdc.tosca.csar; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Arrays; +import java.util.Optional; + +/** + * Represents entry keys in a TOSCA.meta file. + */ +@AllArgsConstructor +@Getter +public enum ToscaMetaEntryAsd implements ToscaMetaEntry { + TOSCA_META_FILE_VERSION_ENTRY("TOSCA-Meta-File-Version"), + CSAR_VERSION_ENTRY("CSAR-Version"), + CREATED_BY_ENTRY("Created-By"), + ENTRY_DEFINITIONS("Entry-Definitions"), + ETSI_ENTRY_MANIFEST("ETSI-Entry-Manifest"), + ETSI_ENTRY_CHANGE_LOG("ETSI-Entry-Change-Log"), + ETSI_ENTRY_TESTS("ETSI-Entry-Tests"), + ETSI_ENTRY_LICENSES("ETSI-Entry-Licenses"), + ETSI_ENTRY_CERTIFICATE("ETSI-Entry-Certificate"), + OTHER_DEFINITIONS("Other-Definitions"); + + private final String name; + + public static Optional parse(final String name) { + return Arrays.stream(values()).filter(toscaMetaEntry -> toscaMetaEntry.getName().equals(name)).findFirst(); + } +} diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java new file mode 100644 index 0000000000..19f3def510 --- /dev/null +++ b/openecomp-be/lib/openecomp-tosca-lib/src/test/java/org/openecomp/sdc/tosca/csar/AsdPackageHelperTest.java @@ -0,0 +1,143 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + * + * + */ + +package org.openecomp.sdc.tosca.csar; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.openecomp.core.utilities.file.FileContentHandler; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.APPLICATION_NAME; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.APPLICATION_PROVIDER; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ATTRIBUTE_VALUE_SEPARATOR; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.ENTRY_DEFINITION_TYPE; +import static org.openecomp.sdc.tosca.csar.ManifestTokenType.RELEASE_DATE_TIME; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.CREATED_BY_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.CSAR_VERSION_ENTRY; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_CHANGE_LOG; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.TOSCA_META_FILE_VERSION_ENTRY; + +class AsdPackageHelperTest { + + public static final String TOSCA_DEFINITION_FILEPATH = "Definitions/MainServiceTemplate.yaml"; + public static final String TOSCA_MANIFEST_FILEPATH = "Definitions/MainServiceTemplate.mf"; + public static final String TOSCA_CHANGELOG_FILEPATH = "Artifacts/changeLog.text"; + public static final String TOSCA_META_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta"; + + private FileContentHandler handler; + private StringBuilder metaFileBuilder; + + @BeforeEach + void setUp() { + handler = new FileContentHandler(); + metaFileBuilder = getMetaFileBuilder(); + } + + protected StringBuilder getMetaFileBuilder() { + return new StringBuilder() + .append(TOSCA_META_FILE_VERSION_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.0").append("\n") + .append(CSAR_VERSION_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" 1.1").append("\n") + .append(CREATED_BY_ENTRY.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" Vendor").append("\n") + .append(ENTRY_DEFINITIONS.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_DEFINITION_FILEPATH).append("\n") + .append(ETSI_ENTRY_MANIFEST.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_MANIFEST_FILEPATH).append("\n") + .append(ETSI_ENTRY_CHANGE_LOG.getName()) + .append(ATTRIBUTE_VALUE_SEPARATOR.getToken()).append(" ").append(TOSCA_CHANGELOG_FILEPATH).append("\n"); + } + + @Test + public void givenRightAsdHandlerItReturnsTrueAsAsdPackage() { + final ManifestBuilder manifestBuilder = getAsdManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + AsdPackageHelper asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); + assertTrue(asdPackageHelper.isAsdPackage(handler)); + + } + + @Test + public void givenWrongAsdHandlerItReturnsFalseAsAsdPackage() { + final ManifestBuilder manifestBuilder = getWrongAsdManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + AsdPackageHelper asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); + assertFalse(asdPackageHelper.isAsdPackage(handler)); + + } + + @Test + public void givenNonAsdHandlerItReturnsFalseAsAsdPackage() throws IOException { + final ManifestBuilder manifestBuilder = getVnfManifestSampleBuilder(); + + handler.addFile(TOSCA_META_PATH_FILE_NAME, metaFileBuilder.toString().getBytes(StandardCharsets.UTF_8)); + manifestBuilder.withSource(TOSCA_META_PATH_FILE_NAME); + + manifestBuilder.withSource(TOSCA_MANIFEST_FILEPATH); + handler.addFile(TOSCA_MANIFEST_FILEPATH, manifestBuilder.build().getBytes(StandardCharsets.UTF_8)); + AsdPackageHelper asdPackageHelper = new AsdPackageHelper(new ManifestUtils()); + assertFalse(asdPackageHelper.isAsdPackage(handler)); + + } + + protected ManifestBuilder getAsdManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(APPLICATION_NAME.getToken(), "RadioNode") + .withMetaData(APPLICATION_PROVIDER.getToken(), "Ericsson") + .withMetaData(ENTRY_DEFINITION_TYPE.getToken(), "asd") + .withMetaData(RELEASE_DATE_TIME.getToken(), "2022-02-01T11:25:00+00:00"); + } + + protected ManifestBuilder getWrongAsdManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(APPLICATION_NAME.getToken(), "RadioNode") + .withMetaData(APPLICATION_PROVIDER.getToken(), "Ericsson") + .withMetaData(ENTRY_DEFINITION_TYPE.getToken(), " Invalid") + .withMetaData(RELEASE_DATE_TIME.getToken(), "2022-02-01T11:25:00+00:00"); + } + + protected ManifestBuilder getVnfManifestSampleBuilder() { + return new ManifestBuilder() + .withMetaData(ManifestTokenType.VNF_PRODUCT_NAME.getToken(), "RadioNode") + .withMetaData(ManifestTokenType.VNF_PROVIDER_ID.getToken(), "ACME") + .withMetaData(ManifestTokenType.VNF_PACKAGE_VERSION.getToken(), "1.0") + .withMetaData(ManifestTokenType.VNF_RELEASE_DATE_TIME.getToken(), "2019-03-11T11:25:00+00:00"); + } + +} \ No newline at end of file -- cgit 1.2.3-korg