From 96908856dea9061161b2c324aa7b0eeb5d6e4fd5 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Thu, 29 Aug 2019 16:31:53 +0000 Subject: Fix artifacts references in main TOSCA descriptor During package conversion, artifacts can have their path changed. References for those artifacts inside the main TOSCA descriptor must be updated accordingly. Change-Id: Idcffb0724c20875ff1decebfec7c47605bfe20dc Issue-ID: SDC-2540 Signed-off-by: andre.schmid --- .../impl/VendorSoftwareProductManagerImpl.java | 76 ++-- .../impl/VendorSoftwareProductManagerImplTest.java | 475 ++++++--------------- .../descriptor/MainServiceTemplate.yaml | 97 +++++ .../manifest/ValidNonManoToscaPNF.mf | 7 - .../manifest/ValidNonManoToscaPNFWithNonMano.mf | 11 + .../manifest/ValidNonManoToscaPnfWithoutNonMano.mf | 7 + .../services/impl/etsi/ETSIService.java | 27 +- .../services/impl/etsi/ETSIServiceImpl.java | 214 +++++++--- .../services/impl/etsi/ETSIServiceImplTest.java | 215 ++++++++-- .../src/test/resources/nonManoConfig.yaml | 5 +- .../vnf1/Definitions/MainServiceTemplate.yaml | 97 +++++ .../openecomp/sdc/tosca/csar/CSARConstants.java | 7 +- 12 files changed, 752 insertions(+), 486 deletions(-) create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/descriptor/MainServiceTemplate.yaml delete mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNF.mf create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNFWithNonMano.mf create mode 100644 openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPnfWithoutNonMano.mf create mode 100644 openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/vnfPackage/vnf1/Definitions/MainServiceTemplate.yaml diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java index 5d2fd518fa..4a44076e46 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java @@ -16,6 +16,31 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.candidateDataNotProcessedOrAbortedErrorBuilder; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.invalidProcessedCandidate; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.vspMissingDeploymentFlavorErrorBuilder; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -108,31 +133,6 @@ import org.openecomp.sdc.versioning.VersioningManagerFactory; import org.openecomp.sdc.versioning.VersioningUtil; import org.openecomp.sdc.versioning.dao.types.Version; -import java.io.BufferedOutputStream; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.zip.ZipInputStream; -import java.util.zip.ZipOutputStream; - -import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.candidateDataNotProcessedOrAbortedErrorBuilder; -import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.invalidProcessedCandidate; -import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.vspMissingDeploymentFlavorErrorBuilder; - public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager { private VspMergeDao vspMergeDao; @@ -610,26 +610,28 @@ public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductMa } @Override - public PackageInfo createPackage(String vspId, Version version) throws IOException { - ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version); - VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version)); - Version vlmVersion = vspDetails.getVlmVersion(); + public PackageInfo createPackage(final String vspId, final Version version) throws IOException { + final ToscaServiceModel toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version); + final VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version)); + final Version vlmVersion = vspDetails.getVlmVersion(); if (vlmVersion != null) { populateVersionsForVlm(vspDetails.getVendorId(), vlmVersion); } - PackageInfo packageInfo = createPackageInfo(vspDetails); + final PackageInfo packageInfo = createPackageInfo(vspDetails); - ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar = + final ToscaFileOutputServiceCsarImpl toscaServiceTemplateServiceCsar = new ToscaFileOutputServiceCsarImpl(); - FileContentHandler licenseArtifacts = licenseArtifactsService + final FileContentHandler licenseArtifacts = licenseArtifactsService .createLicenseArtifacts(vspDetails.getId(), vspDetails.getVendorId(), vlmVersion, vspDetails.getFeatureGroups()); - ETSIService etsiService = new ETSIServiceImpl(); + final ETSIService etsiService = new ETSIServiceImpl(); if (etsiService.isSol004WithToscaMetaDirectory(toscaServiceModel.getArtifactFiles())) { - FileContentHandler handler = toscaServiceModel.getArtifactFiles(); - Manifest manifest = etsiService.getManifest(handler); - etsiService.moveNonManoFileToArtifactFolder(handler, manifest); - packageInfo.setResourceType(etsiService.getResourceType(manifest).name()); + final FileContentHandler handler = toscaServiceModel.getArtifactFiles(); + final Manifest manifest = etsiService.getManifest(handler); + final Optional> fromToMovedPaths = etsiService.moveNonManoFileToArtifactFolder(handler); + fromToMovedPaths + .ifPresent(it -> etsiService.updateMainDescriptorPaths(toscaServiceModel, it)); + packageInfo.setResourceType(etsiService.getResourceType(manifest).name()); } packageInfo.setTranslatedFile(ByteBuffer.wrap( toscaServiceTemplateServiceCsar.createOutputFile(toscaServiceModel, licenseArtifacts))); diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java index 61732a8faa..fb1ab0306e 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java @@ -16,6 +16,32 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyObject; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +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.CSARConstants.TOSCA_META_PATH_FILE_NAME; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Optional; import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.Assert; @@ -27,19 +53,17 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.Spy; -import org.openecomp.core.enrichment.factory.EnrichmentManagerFactory; -import org.openecomp.core.factory.impl.AbstractFactoryBase; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.core.model.dao.EnrichedServiceModelDao; import org.openecomp.core.model.dao.ServiceModelDao; import org.openecomp.core.model.types.ServiceElement; import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; -import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.healing.api.HealingManager; import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade; @@ -56,12 +80,9 @@ import org.openecomp.sdc.vendorsoftwareproduct.dao.PackageInfoDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; -import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; -import org.openecomp.sdc.vendorsoftwareproduct.impl.mock.EnrichmentManagerFactoryImpl; import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator; -import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse; import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor; import org.openecomp.sdc.versioning.ActionVersioningManager; @@ -70,36 +91,6 @@ import org.openecomp.sdc.versioning.dao.types.VersionStatus; import org.openecomp.sdc.versioning.types.VersionInfo; import org.openecomp.sdc.versioning.types.VersionableEntityAction; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Optional; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyObject; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME; - - public class VendorSoftwareProductManagerImplTest { @@ -135,12 +126,9 @@ public class VendorSoftwareProductManagerImplTest { private ManualVspToscaManager manualVspToscaManager; @Mock private DeploymentFlavorDao deploymentFlavorDaoMock; - - @Spy @InjectMocks private VendorSoftwareProductManagerImpl vendorSoftwareProductManager; - @Mock private OrchestrationTemplateCandidateManager orchestrationTemplateCandidateManagerMock; @Mock @@ -161,30 +149,34 @@ public class VendorSoftwareProductManagerImplTest { vendorSoftwareProductManager = null; } - @Test - public void testCreatePackageEtsiVNF(){ - try(InputStream metadataInput = getClass().getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta"); - InputStream manifestInput = getClass().getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoTosca.mf")) { - - FileContentHandler handler = new FileContentHandler(); - handler.addFile(TOSCA_META_PATH_FILE_NAME, IOUtils.toByteArray(metadataInput)); - handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput)); - ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), ""); - when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata ); - VspDetails vsp = - createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon", - "category", "subCategory", "123", null); - //want to avoid triggering populateVersionsForVlm method - vsp.setVlmVersion(null); - - when(vspInfoDaoMock.get(any())).thenReturn(vsp); - when(licenseArtifactsServiceMock.createLicenseArtifacts(any(),any(), any(), any())).thenReturn(new FileContentHandler()); - PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version()); - assertEquals(packageInfo.getResourceType(), ResourceTypeEnum.VF.name()); - } catch (IOException e) { - fail(); + @Test + public void testCreatePackageEtsiVNF() throws IOException { + try ( + final InputStream metadataInput = getClass() + .getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta"); + final InputStream manifestInput = getClass() + .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoTosca.mf")) { + + final FileContentHandler handler = new FileContentHandler(); + final byte[] metadataInputBytes = IOUtils.toByteArray(metadataInput); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metadataInputBytes); + handler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, metadataInputBytes); + handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput)); + final ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), ""); + when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata); + final VspDetails vsp = + createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon", + "category", "subCategory", "123", null); + //want to avoid triggering populateVersionsForVlm method + vsp.setVlmVersion(null); + + when(vspInfoDaoMock.get(any())).thenReturn(vsp); + when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any())) + .thenReturn(new FileContentHandler()); + final PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version()); + assertEquals(packageInfo.getResourceType(), ResourceTypeEnum.VF.name()); + } } - } @Test(expected = IOException.class) public void testCreatePackageEtsiNoManifest() throws IOException { @@ -207,30 +199,82 @@ public class VendorSoftwareProductManagerImplTest { } } - @Test - public void testCreatePackageEtsiPNF(){ - try(InputStream metadataInput = getClass().getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta"); - InputStream manifestInput = getClass().getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoToscaPNF.mf")) { - - FileContentHandler handler = new FileContentHandler(); - handler.addFile(TOSCA_META_PATH_FILE_NAME, IOUtils.toByteArray(metadataInput)); - handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput)); - ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), ""); - when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata ); - VspDetails vsp = - createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon", - "category", "subCategory", "123", null); - //want to avoid triggering populateVersionsForVlm method - vsp.setVlmVersion(null); + @Test + public void testCreatePackageEtsiPnfWithoutNonMano() throws IOException { + try ( + final InputStream metadataInput = getClass() + .getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta"); + final InputStream manifestInput = getClass() + .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoToscaPnfWithoutNonMano.mf")) { + + final FileContentHandler handler = new FileContentHandler(); + final byte[] metadataInputBytes = IOUtils.toByteArray(metadataInput); + handler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, metadataInputBytes); + handler.addFile(TOSCA_META_PATH_FILE_NAME, metadataInputBytes); + handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput)); + final ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, new HashMap<>(), ""); + when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata); + final VspDetails vsp = + createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon", + "category", "subCategory", "123", null); + //want to avoid triggering populateVersionsForVlm method + vsp.setVlmVersion(null); + + when(vspInfoDaoMock.get(any())).thenReturn(vsp); + when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any())) + .thenReturn(new FileContentHandler()); + final PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version()); + assertEquals(packageInfo.getResourceType(), ResourceTypeEnum.PNF.name()); + } + } - when(vspInfoDaoMock.get(any())).thenReturn(vsp); - when(licenseArtifactsServiceMock.createLicenseArtifacts(any(),any(), any(), any())).thenReturn(new FileContentHandler()); - PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version()); - assertEquals(packageInfo.getResourceType(), ResourceTypeEnum.PNF.name()); - } catch (IOException e) { - fail(); + @Test + public void testCreatePackageEtsiPnfWithNonManoArtifacts() throws IOException { + try ( + final InputStream metadataInput = getClass() + .getResourceAsStream("/vspmanager.csar/metadata/ValidETSItosca.meta"); + final InputStream manifestInput = getClass() + .getResourceAsStream("/vspmanager.csar/manifest/ValidNonManoToscaPNFWithNonMano.mf"); + final InputStream mainServiceTemplateYamlFile = getClass() + .getResourceAsStream("/vspmanager.csar/descriptor/MainServiceTemplate.yaml")) { + + final FileContentHandler handler = new FileContentHandler(); + handler.addFile(TOSCA_META_ORIG_PATH_FILE_NAME, IOUtils.toByteArray(metadataInput)); + handler.addFile(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME, IOUtils.toByteArray(manifestInput)); + handler.addFile("Deployment/ANOTHER/authorized_keys", "".getBytes()); + + final ServiceTemplate mainServiceTemplate = new YamlUtil() + .yamlToObject(mainServiceTemplateYamlFile, ServiceTemplate.class); + final String mainServiceTemplateName = "MainServiceTemplate.yaml"; + final HashMap serviceTemplateMap = new HashMap<>(); + serviceTemplateMap.put(mainServiceTemplateName, mainServiceTemplate); + + final ToscaServiceModel toscaMetadata = new ToscaServiceModel(handler, serviceTemplateMap, + mainServiceTemplateName); + when(enrichedServiceModelDaoMock.getServiceModel(any(), any())).thenReturn(toscaMetadata); + final VspDetails vsp = + createVspDetails("0", new Version(), "Vsp_PNF", "Test-vsp-pnf", "vendorName", "esy", "icon", + "category", "subCategory", "123", null); + //want to avoid triggering populateVersionsForVlm method + vsp.setVlmVersion(null); + + when(vspInfoDaoMock.get(any())).thenReturn(vsp); + when(licenseArtifactsServiceMock.createLicenseArtifacts(any(), any(), any(), any())) + .thenReturn(new FileContentHandler()); + final PackageInfo packageInfo = vendorSoftwareProductManager.createPackage("0", new Version()); + assertThat("Package Info should contain resource type", packageInfo.getResourceType(), + equalTo(ResourceTypeEnum.PNF.name())); + assertThat("Should not contain moved artifact", toscaMetadata.getArtifactFiles().getFileList(), + not(hasItem("Deployment/ANOTHER/authorized_keys"))); + assertThat("Should contain moved artifact", toscaMetadata.getArtifactFiles().getFileList(), + hasItem("Informational/OTHER/authorized_keys")); + final String serviceTemplateAsYaml = new YamlUtil().objectToYaml(toscaMetadata.getServiceTemplates()); + assertThat("Descriptor should not contain reference to file", serviceTemplateAsYaml, + not(containsString("Artifacts/Deployment/ANOTHER/authorized_keys"))); + assertThat("Descriptor should contain reference to file", serviceTemplateAsYaml, + containsString("Artifacts/Informational/OTHER/authorized_keys")); + } } - } @Test public void testCreate() { @@ -399,120 +443,6 @@ public class VendorSoftwareProductManagerImplTest { assertVspsEquals(actualVsp, expectedVsp); } -/* - @Test - public void testSubmitWithMissingData() throws IOException { - VersionInfo versionInfo = new VersionInfo(); - versionInfo.setActiveVersion(VERSION01); - - doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo( - VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, - VSP_ID, USER1, VersionableEntityAction.Read); - - VspDetails vsp = new VspDetails(VSP_ID, VERSION01); - vsp.setOnboardingMethod("Manual"); - doReturn(vsp).when(vspInfoDaoMock).get(anyObject()); - - VspQuestionnaireEntity vspQuestionnaire = new VspQuestionnaireEntity(VSP_ID, VERSION01); - vspQuestionnaire.setQuestionnaireData("{}"); - doReturn(vspQuestionnaire).when(vspInfoDaoMock).getQuestionnaire(VSP_ID, VERSION01); - - ComponentEntity comp1 = new ComponentEntity(VSP_ID, VERSION01, "comp1"); - comp1.setQuestionnaireData("{}"); - doReturn(Collections.singleton(comp1)).when(vendorSoftwareProductDaoMock) - .listComponentsCompositionAndQuestionnaire(VSP_ID, VERSION01); - - NicEntity nic1 = new NicEntity(VSP_ID, VERSION01, "comp1", "nic1"); - nic1.setQuestionnaireData("{}"); - doReturn(Collections.singleton(nic1)) - .when(vendorSoftwareProductDaoMock).listNicsByVsp(VSP_ID, VERSION01); - - ValidationResponse validationResponse = vendorSoftwareProductManager.submit(VSP_ID, USER1); - Assert.assertNotNull(validationResponse); - Assert.assertFalse(validationResponse.isValid()); - List errorIds = validationResponse.getVspErrors().stream().map(ErrorCode::id).distinct() - .collect(Collectors.toList()); - Assert.assertTrue(errorIds.contains(ValidationErrorBuilder.FIELD_VALIDATION_ERROR_ERR_ID)); - Assert.assertTrue(errorIds.contains(VendorSoftwareProductErrorCodes.VSP_INVALID)); - - verify(versioningManagerMock, never()) - .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, - USER1, null); - verify(activityLogManagerMock, never()).addActionLog(any(ActivityLogEntity.class), eq(USER1)); - } - - */ - - // TODO: 3/15/2017 fix and enable - //@Test - public void testSubmitWithInvalidLicensingData() throws IOException { - VersionInfo versionInfo = new VersionInfo(); - versionInfo.setActiveVersion(VERSION01); - doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo( - VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, - VSP_ID, USER1, VersionableEntityAction.Read); - - VspDetails vsp = - createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon", - "category", "subCategory", "licenseAgreementId", - Collections.singletonList("featureGroupId")); - doReturn(vsp).when(vspInfoDaoMock).get(anyObject()); - OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity(VSP_ID, VERSION01); - uploadData.setContentData( - ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition")))); - doReturn(uploadData).when(orchestrationTemplateDataDaoMock) - .get(anyObject(), anyObject()); - doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), - "MainServiceTemplate.yaml")) - .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01); - - ValidationResponse validationResponse = - vendorSoftwareProductManager.validate(vsp); - Assert.assertNotNull(validationResponse); - Assert.assertFalse(validationResponse.isValid()); - Assert.assertNull(validationResponse.getVspErrors()); - Assert.assertEquals(validationResponse.getLicensingDataErrors().size(), 1); - - verify(versioningManagerMock, never()) - .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, - USER1, null); - } - - // TODO: 3/15/2017 fix and enable - //@Test - public void testSubmit() throws IOException { - mockVersioning(VersionableEntityAction.Read); - - EnrichmentManagerFactory.getInstance(); - AbstractFactoryBase - .registerFactory(EnrichmentManagerFactory.class, EnrichmentManagerFactoryImpl.class); - - VspDetails vsp = - createVspDetails(VSP_ID, VERSION01, "Vsp1", "Test-vsp", "vendorName", "vlm1Id", "icon", - "category", "subCategory", "123", Collections.singletonList("fg1")); - doReturn(vsp).when(vspInfoDaoMock).get(anyObject()); - OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity(VSP_ID, VERSION01); - uploadData.setContentData( - ByteBuffer.wrap(FileUtils.toByteArray(getFileInputStream("/emptyComposition")))); - doReturn(uploadData).when(orchestrationTemplateDataDaoMock) - .get(anyObject(), anyObject()); - doReturn(new ToscaServiceModel(new FileContentHandler(), new HashMap<>(), - "MainServiceTemplate.yaml")) - .when(serviceModelDaoMock).getServiceModel(VSP_ID, VERSION01); - - ValidationResponse validationResponse = - vendorSoftwareProductManager.validate(vsp); - Assert.assertTrue(validationResponse.isValid()); - -/* Assert.assertEquals(vsp2.getVersionInfo().getVersion(), VERSION10); - Assert.assertEquals(vsp2.getVersionInfo().getStatus(), VersionStatus.Certified); - Assert.assertNull(vsp2.getVersionInfo().getLockingUser());*/ - - verify(versioningManagerMock) - .submit(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, - USER1, null); - } - @Test public void testCreatePackage() throws IOException { /*VspDetails vspDetailsMock = new VspDetails("vspId", new Version(1, 0)); @@ -540,122 +470,6 @@ public class VendorSoftwareProductManagerImplTest { Assert.assertNotNull(packageInfo.getVspId()); } - // TODO: 3/15/2017 fix and enable - //@Test(dependsOnMethods = {"testListFinals"}) - public void testUploadFileMissingFile() throws IOException { - try (InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip")) { - - UploadFileResponse uploadFileResponse = - candidateManager.upload(VSP_ID, VERSION01, zis, "zip", "file"); - - Assert.assertEquals(uploadFileResponse.getErrors().size(), 0); - } - } - - // TODO: 3/15/2017 fix and enable - //@Test(dependsOnMethods = {"testUploadFileMissingFile"}) - public void testUploadNotZipFile() { - URL url = this.getClass().getResource("/notZipFile"); - - try { - candidateManager.upload(VSP_ID, VERSION01, url.openStream(), "zip", "file"); - candidateManager.process(VSP_ID, VERSION01); - } catch (Exception ce) { - Assert.assertEquals(ce.getMessage(), Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage()); - } - } - - private List getWantedFileNamesFromCsar(String pathInCsar) - throws IOException { - File translatedFile = vendorSoftwareProductManager.getTranslatedFile(VSP_ID, VERSION10); - - return getFileNamesFromFolderInCsar(translatedFile, - pathInCsar); - } - - private List getFileNamesFromFolderInCsar(File csar, String folderName) - throws IOException { - List fileNames = new ArrayList<>(); - - try (ZipInputStream zip = new ZipInputStream(new FileInputStream(csar))) { - ZipEntry ze; - - while ((ze = zip.getNextEntry()) != null) { - String name = ze.getName(); - if (name.contains(folderName)) { - fileNames.add(name); - } - } - } - - return fileNames; - } - /* - //Disabled for sonar null pointer issue for componentEntities - private Pair uploadMib(String vspId, String user, String filePath, - String fileName) { - List componentEntities = null; - //(List) vendorSoftwareProductManager.listComponents(vspId, null, user); - monitoringUploadsManager.upload(getFileInputStream(filePath), - fileName, vspId, -<<<<<<< HEAD - VERSION01, componentEntities.get(0).getId(), ArtifactType.SNMP_POLL); - //TODO: add validate of logActivity() func call -======= - VERSION01, componentEntities.get(0).getId(), MonitoringUploadType.SNMP_POLL, user); - //TODO: add validate of addActionLog() func call ->>>>>>> feature/Amdocs-ASDC-1710 - - return new ImmutablePair<>(componentEntities.get(0).getId(), - componentEntities.get(0).getComponentCompositionData() - .getDisplayName()); - }*/ - - // TODO: 3/15/2017 fix and enable -/* - - public void testUpdatedVSPShouldBeInBeginningOfList() { - vendorSoftwareProductManager.updateVsp(new VspDetails(), USER3); - assertVSPInWantedLocationInVSPList(id006, 0, USER3); - - InputStream zis = getFileInputStream("/vspmanager/zips/fullComposition.zip"); - candidateManager.upload(id007, VERSION01, zis, USER3); - candidateManager.process(id007, VERSION01, USER3); - assertVSPInWantedLocationInVSPList(id007, 0, USER3); - } - - @Test(dependsOnMethods = {"testUpdatedVSPShouldBeInBeginningOfList"}) - public void testVSPInBeginningOfListAfterCheckin() { - vendorSoftwareProductManager.checkin(id006, USER3); - assertVSPInWantedLocationInVSPList(id006, 0, USER3); - - vendorSoftwareProductManager.checkin(id007, USER3); - assertVSPInWantedLocationInVSPList(id007, 0, USER3); - } - - @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckin"}) - public void testVSPInBeginningOfListAfterCheckout() { - vendorSoftwareProductManager.checkout(id006, USER3); - assertVSPInWantedLocationInVSPList(id006, 0, USER3); - } - - @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterCheckout"}) - public void testVSPInBeginningOfListAfterUndoCheckout() { - vendorSoftwareProductManager.checkout(id007, USER3); - assertVSPInWantedLocationInVSPList(id007, 0, USER3); - - vendorSoftwareProductManager.undoCheckout(id006, USER3); - assertVSPInWantedLocationInVSPList(id006, 0, USER3); - } - - @Test(dependsOnMethods = {"testVSPInBeginningOfListAfterUndoCheckout"}) - public void testVSPInBeginningOfListAfterSubmit() throws IOException { - vendorSoftwareProductManager.checkin(id007, USER3); - vendorSoftwareProductManager.submit(id007, USER3); - - assertVSPInWantedLocationInVSPList(id007, 0, USER3); - } -*/ @Test public void testValidateWithCandidateDataNotProcessed() throws IOException { VspDetails vsp = @@ -705,26 +519,6 @@ public class VendorSoftwareProductManagerImplTest { Assert.assertEquals(validationResponse.getVspErrors().size(), 1); } - private void testLegalUpload(String vspId, Version version, InputStream upload, String user) { - candidateManager.upload(vspId, VERSION01, upload, "zip", "file"); - candidateManager.process(vspId, VERSION01); - - OrchestrationTemplateEntity uploadData = - orchestrationTemplateDataDaoMock.get(vspId, version); - Assert.assertNotNull(uploadData); - } - - - private InputStream getFileInputStream(String fileName) { - URL url = this.getClass().getResource(fileName); - try { - return url.openStream(); - } catch (IOException exception) { - exception.printStackTrace(); - return null; - } - } - private static VspDetails createVspDetails(String id, Version version, String name, String desc, String vendorName, String vlm, String icon, String category, String subCategory, @@ -758,15 +552,4 @@ public class VendorSoftwareProductManagerImplTest { Assert.assertEquals(actual.getFeatureGroups(), expected.getFeatureGroups()); } - - // todo ********************** move to common ************************************** - - private void mockVersioning(VersionableEntityAction action) { - VersionInfo versionInfo = new VersionInfo(); - versionInfo.setActiveVersion(VERSION01); - doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo( - VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1, - action); - } - } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/descriptor/MainServiceTemplate.yaml b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/descriptor/MainServiceTemplate.yaml new file mode 100644 index 0000000000..cd1a2688d3 --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/descriptor/MainServiceTemplate.yaml @@ -0,0 +1,97 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 +metadata: + VNFD_SCHEMA_VERSION: 2.5.1+1 + template_name: Main + template_version: '1.0' + template_author: onap +topology_template: + node_templates: + VDU_vgw_0: + type: tosca.nodes.nfv.Vdu.Compute + properties: + name: + get_input: vgw_name_0 + description: vgw + vdu_profile: + min_number_of_instances: 1 + max_number_of_instances: 1 + watchdog: none + inject_files: + source_path: ../Artifacts/Deployment/ANOTHER/authorized_keys + dest_path: /home/ubuntu/.ssh/authorized_keys + meta_data: + vnf_id: + get_input: vnf_id + vf_module_id: + get_input: vf_module_id + mux_gw_private_net_ipaddr: + get_input: vgw_private_ip_0 + oam_ipaddr: + get_input: vgw_private_ip_1 + oam_cidr: + get_input: onap_private_net_cidr + cpe_public_net_cidr: + get_input: cpe_public_net_cidr + mux_gw_private_net_cidr: + get_input: mux_gw_private_net_cidr + mux_ip_addr: + get_input: mux_ip_addr + vg_vgmux_tunnel_vni: + get_input: vg_vgmux_tunnel_vni + install_script_version: + get_input: install_script_version + cloud_env: + get_input: cloud_env + nexus_artifact_repo: + get_input: nexus_artifact_repo + boot_data: | + #!/bin/bash + METADATA=`curl -s http://169.254.169.254/openstack/2012-08-10/meta_data.json` + apt-get -y install jq + + get_metadata () { + echo $METADATA | jq -r ".meta.$1" + } + + # Create configuration files + mkdir /opt/config + echo "$(get_metadata oam_ipaddr)" > /opt/config/oam_ipaddr.txt + echo "$(get_metadata oam_cidr)" > /opt/config/oam_cidr.txt + echo "$(get_metadata cpe_public_net_cidr)" > /opt/config/cpe_public_net_cidr.txt + echo "$(get_metadata mux_gw_private_net_ipaddr)" > /opt/config/mux_gw_private_net_ipaddr.txt + echo "$(get_metadata mux_gw_private_net_cidr)" > /opt/config/mux_gw_private_net_cidr.txt + echo "$(get_metadata install_script_version)" > /opt/config/install_script_version.txt + echo "$(get_metadata cloud_env)" > /opt/config/cloud_env.txt + echo "$(get_metadata mux_ip_addr)" > /opt/config/mux_ip_addr.txt + echo "$(get_metadata vg_vgmux_tunnel_vni)" > /opt/config/vg_vgmux_tunnel_vni.txt + echo "$(get_metadata nexus_artifact_repo)" > /opt/config/nexus_artifact_repo.txt + + # Download and run install script + apt-get -y install unzip + INSTALL_SCRIPT_VERSION=$(get_metadata install_script_version) + NEXUS_ARTIFACT_REPO=$(get_metadata nexus_artifact_repo) + if [[ "${INSTALL_SCRIPT_VERSION}" =~ "SNAPSHOT" ]]; then REPO=snapshots; else REPO=releases; fi + curl -k -L "${NEXUS_ARTIFACT_REPO}/service/local/artifact/maven/redirect?r=${REPO}&g=org.onap.demo.vnf.vcpe&a=vcpe-scripts&e=zip&v=${INSTALL_SCRIPT_VERSION}" -o /opt/vcpe-scripts-${INSTALL_SCRIPT_VERSION}.zip + unzip -j /opt/vcpe-scripts-${INSTALL_SCRIPT_VERSION}.zip -d /opt v_gw_install.sh + cd /opt + chmod +x v_gw_install.sh + ./v_gw_install.sh + capabilities: + virtual_compute: + properties: + virtual_memory: + virtual_mem_size: 4096 MB + vdu_mem_requirements: + memoryPageSize: '{"schemaVersion": "0", "schemaSelector": "", "hardwarePlatform": + "generic", "mandatory": "true", "configurationValue": "2 MB"}' + numberOfPages: '{"schemaVersion": "0","schemaSelector": "","hardwarePlatform": + "generic","mandatory": "true", "configurationValue": "1024"}' + virtual_cpu: + num_virtual_cpu: 2 + cpu_architecture: generic + virtual_local_storage: + - size_of_storage: 40 GB + artifacts: + sw_image: + type: tosca.artifacts.nfv.SwImage + file: ../Artifacts/Deployment/ANOTHER/image \ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNF.mf b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNF.mf deleted file mode 100644 index bda3264f5d..0000000000 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNF.mf +++ /dev/null @@ -1,7 +0,0 @@ -metadata: -pnfd_name: MRF -pnfd_provider: SunShineCompany -pnfd_archive_version: 1.0 -pnfd_release_date_time: 2017-01-01T10:00:00+03:00 - -Source: MainServiceTemplate.yaml \ No newline at end of file diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNFWithNonMano.mf b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNFWithNonMano.mf new file mode 100644 index 0000000000..02275e1bca --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPNFWithNonMano.mf @@ -0,0 +1,11 @@ +metadata: + pnfd_name: MRF + pnfd_provider: SunShineCompany + pnfd_archive_version: 1.0 + pnfd_release_date_time: 2017-01-01T10:00:00+03:00 + +Source: MainServiceTemplate.yaml + +non_mano_artifact_sets: + onap_others: + Source: Artifacts/Deployment/ANOTHER/authorized_keys diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPnfWithoutNonMano.mf b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPnfWithoutNonMano.mf new file mode 100644 index 0000000000..bda3264f5d --- /dev/null +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/resources/vspmanager.csar/manifest/ValidNonManoToscaPnfWithoutNonMano.mf @@ -0,0 +1,7 @@ +metadata: +pnfd_name: MRF +pnfd_provider: SunShineCompany +pnfd_archive_version: 1.0 +pnfd_release_date_time: 2017-01-01T10:00:00+03:00 + +Source: MainServiceTemplate.yaml \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIService.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIService.java index a6b58167f9..0a271c9d87 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIService.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-api/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIService.java @@ -20,10 +20,14 @@ package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Map; +import java.util.Optional; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.tosca.csar.Manifest; -import java.io.IOException; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; public interface ETSIService { @@ -38,11 +42,22 @@ public interface ETSIService { boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException; /** - * Update file structure. Moves non mano files to Artifacts/Deployment/non mano key location - * @param handler - * @param manifest + * Update file structure. Moves non mano files to the correct folder based on the manifest non mano type. + * + * @param handler The file handler containing the artifacts to move. + * @return A Map with pairs of from and to path of the moved artifacts. + */ + Optional> moveNonManoFileToArtifactFolder(final FileContentHandler handler) + throws IOException; + + /** + * Updates the main descriptor paths referring the artifacts that were moved. + * + * @param toscaServiceModel The tosca service model containing the main descriptor. + * @param fromToMovedArtifactMap A Map representing the from and to artifacts path changes. */ - void moveNonManoFileToArtifactFolder(FileContentHandler handler, Manifest manifest); + void updateMainDescriptorPaths(final ToscaServiceModel toscaServiceModel, + final Map fromToMovedArtifactMap); /** * Retrieves the manifest file from the CSAR @@ -64,4 +79,6 @@ public interface ETSIService { * @throws IOException when TOSCA.meta file or manifest file is invalid */ ResourceTypeEnum getResourceType(Manifest manifest) throws IOException; + + Path getOriginalManifestPath(final FileContentHandler handler) throws IOException; } 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 1b74b0094f..e727078daa 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 @@ -20,40 +20,54 @@ 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.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_ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CHANGE_LOG; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_MANIFEST; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +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.apache.commons.io.IOUtils; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter; +import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; +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.OnboardingToscaMetadata; import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding; import org.openecomp.sdc.tosca.csar.ToscaMetadata; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Map; - -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_ENTRY_DEFINITIONS; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CHANGE_LOG; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_MANIFEST; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME; -import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; public class ETSIServiceImpl implements ETSIService { + private static final Logger LOGGER = LoggerFactory.getLogger(ETSIServiceImpl.class); + private Configuration configuration; public ETSIServiceImpl() throws IOException { - InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml"); + final InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml"); if (io == null) { throw new IOException("Non Mano configuration not found"); } - String data = IOUtils.toString(io, StandardCharsets.UTF_8); - YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); + final String data = IOUtils.toString(io, StandardCharsets.UTF_8); + final YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); configuration = yamlToObjectConverter.convertFromString(data, Configuration.class); } @@ -63,52 +77,136 @@ public class ETSIServiceImpl implements ETSIService { @Override public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException { - Map templates = handler.getFiles(); + final Map templates = handler.getFiles(); return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler)); } @Override - public void moveNonManoFileToArtifactFolder(FileContentHandler handler, Manifest manifest) { - for (Map.Entry> entry : manifest.getNonManoSources().entrySet()) { - String e = entry.getKey(); - List k = entry.getValue(); - updateNonManoLocation(handler, e, k); + public Optional> moveNonManoFileToArtifactFolder(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; + } + final Path originalManifestPath; + try { + originalManifestPath = getOriginalManifestPath(handler); + } catch (final IOException ex) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error("An error occurred while getting the original manifest path", ex); + } + throw ex; } + final Map fromToPathMap = new HashMap<>(); + final Map nonManoKeyFolderMapping = configuration.getNonManoKeyFolderMapping(); + manifest.getNonManoSources().entrySet().stream() + .filter(manifestNonManoSourceEntry -> nonManoKeyFolderMapping.containsKey(manifestNonManoSourceEntry.getKey())) + .forEach(manifestNonManoSourceEntry -> { + final NonManoType nonManoType = nonManoKeyFolderMapping.get(manifestNonManoSourceEntry.getKey()); + final List nonManoFileList = manifestNonManoSourceEntry.getValue(); + final Map actualFromToPathMap = nonManoFileList.stream() + .map(nonManoFilePath -> { + final Path normalizedFilePath = resolveNonManoFilePath(originalManifestPath, nonManoFilePath); + final Optional changedPath = updateNonManoPathInHandler(handler, nonManoType, normalizedFilePath); + if (changedPath.isPresent()) { + final Map fromAndToPathMap = new HashMap<>(); + fromAndToPathMap.put(nonManoFilePath, Paths.get(ARTIFACTS_FOLDER).resolve(changedPath.get())); + return fromAndToPathMap; + } + return null; + }) + .filter(Objects::nonNull) + .collect(Collectors.toMap( + fromToPathEntry -> fromToPathEntry.keySet().iterator().next(), + fromToPathEntry -> fromToPathEntry.values().iterator().next() + )); + fromToPathMap.putAll(actualFromToPathMap); + }); + + return MapUtils.isEmpty(fromToPathMap) ? Optional.empty() : Optional.of(fromToPathMap); } + /** + * Resolves the non mano file path based on the original manifest path of the onboarded package. + * + * @param originalManifestPath The original path from the onboarded package manifest + * @param nonManoFilePath The non mano file path defined in the manifest + * @return The resolved and normalized non mano path. + */ + private Path resolveNonManoFilePath(final Path originalManifestPath, final String nonManoFilePath) { + return originalManifestPath.resolve(Paths.get(nonManoFilePath)).normalize(); + } - private void updateNonManoLocation(FileContentHandler handler, String nonManoKey, List sources) { - Map files = handler.getFiles(); - for (String key : sources) { - if (files.containsKey(key)) { - updateLocation(key, nonManoKey, files); + /** + * Updates the non mano file path in the package file handler based on the non mano type. + * + * @param handler The package file handler + * @param nonManoType The Non Mano type of the file to update + * @param nonManoOriginalFilePath The Non Mano file original path + * @return The new file path if it was updated in the package file handler, otherwise empty. + */ + private Optional updateNonManoPathInHandler(final FileContentHandler handler, final NonManoType nonManoType, + final Path nonManoOriginalFilePath) { + final Path fixedSourcePath = fixNonManoPath(nonManoOriginalFilePath); + final Map packageFileMap = handler.getFiles(); + if (packageFileMap.containsKey(fixedSourcePath.toString())) { + final Path newNonManoPath = Paths.get(nonManoType.getType(), nonManoType.getLocation() + , fixedSourcePath.getFileName().toString()); + if (!packageFileMap.containsKey(newNonManoPath.toString())) { + packageFileMap.put(newNonManoPath.toString(), packageFileMap.remove(fixedSourcePath.toString())); + return Optional.of(newNonManoPath); } } + + return Optional.empty(); } - private void updateLocation(String key, String nonManoKey, Map files) { - if (nonManoKey == null || nonManoKey.isEmpty()) { - return; + /** + * Fix the original non mano file path to the ONAP package file path. + * + * Non mano artifacts that were inside the {@link org.openecomp.sdc.tosca.csar.CSARConstants#ARTIFACTS_FOLDER} path + * are not moved when parsed to ONAP package, but the Manifest declaration can still have the {@link + * org.openecomp.sdc.tosca.csar.CSARConstants#ARTIFACTS_FOLDER} reference in it. If so, that reference is removed. + * + * @param nonManoOriginalFilePath The original non mano file path + * @return The non mano fixed path to ONAP package structure. + */ + private Path fixNonManoPath(final Path nonManoOriginalFilePath) { + final Path rootArtifactsPath = Paths.get("/", ARTIFACTS_FOLDER); + if (nonManoOriginalFilePath.startsWith(rootArtifactsPath)) { + return rootArtifactsPath.relativize(nonManoOriginalFilePath); } - Map map = configuration.getNonManoKeyFolderMapping(); - if (map.containsKey(nonManoKey)) { - NonManoType nonManoPair = map.get(nonManoKey); - String newLocation = nonManoPair.getType() + "/" + - nonManoPair.getLocation() + "/" + getFileName(key); - if (!files.containsKey(newLocation)) { - files.put(newLocation, files.remove(key)); - } + final Path relativeArtifactsPath = Paths.get(ARTIFACTS_FOLDER); + if (nonManoOriginalFilePath.startsWith(relativeArtifactsPath)) { + return relativeArtifactsPath.relativize(nonManoOriginalFilePath); } + + return nonManoOriginalFilePath; } - private String getFileName(String key) { - return key.substring(key.lastIndexOf('/') + 1); + @Override + public void updateMainDescriptorPaths(final ToscaServiceModel toscaServiceModel, + final Map fromToMovedArtifactMap) { + final ServiceTemplate entryDefinition = toscaServiceModel.getServiceTemplates() + .get(toscaServiceModel.getEntryDefinitionServiceTemplate()); + final YamlUtil yamlUtil = new YamlUtil(); + final String[] entryDefinitionYaml = {yamlUtil.objectToYaml(entryDefinition)}; + fromToMovedArtifactMap.forEach((fromPath, toPath) -> entryDefinitionYaml[0] = entryDefinitionYaml[0] + .replaceAll(fromPath, toPath.toString())); + + toscaServiceModel.addServiceTemplate(toscaServiceModel.getEntryDefinitionServiceTemplate() + , yamlUtil.yamlToObject(entryDefinitionYaml[0], ServiceTemplate.class)); } - private boolean hasMetaMandatoryEntries(ToscaMetadata toscaMetadata) { - Map metaDataEntries = toscaMetadata.getMetaEntries(); - return metaDataEntries.containsKey(TOSCA_META_ENTRY_DEFINITIONS) && metaDataEntries.containsKey(TOSCA_META_ETSI_ENTRY_MANIFEST) - && metaDataEntries.containsKey(TOSCA_META_ETSI_ENTRY_CHANGE_LOG); + private boolean hasMetaMandatoryEntries(final ToscaMetadata toscaMetadata) { + final Map metaDataEntries = toscaMetadata.getMetaEntries(); + return metaDataEntries.containsKey(TOSCA_META_ENTRY_DEFINITIONS) && metaDataEntries + .containsKey(TOSCA_META_ETSI_ENTRY_MANIFEST) + && metaDataEntries.containsKey(TOSCA_META_ETSI_ENTRY_CHANGE_LOG); } private boolean isMetaFilePresent(Map handler) { @@ -125,7 +223,7 @@ public class ETSIServiceImpl implements ETSIService { // Valid manifest should contain whether vnf or pnf related metadata data exclusively in SOL004 standard, // validation of manifest done during package upload stage if (manifest != null && !manifest.getMetadata().isEmpty() - && MANIFEST_PNF_METADATA.stream().anyMatch(e -> manifest.getMetadata().containsKey(e))) { + && MANIFEST_PNF_METADATA.stream().anyMatch(e -> manifest.getMetadata().containsKey(e))) { return ResourceTypeEnum.PNF; } // VNF is default resource type @@ -138,25 +236,43 @@ public class ETSIServiceImpl implements ETSIService { } private Manifest getManifest(FileContentHandler handler, String manifestLocation) throws IOException { - try(InputStream manifestInputStream = getManifestInputStream(handler, manifestLocation)) { + try (InputStream manifestInputStream = getManifestInputStream(handler, manifestLocation)) { Manifest onboardingManifest = new SOL004ManifestOnboarding(); onboardingManifest.parse(manifestInputStream); return onboardingManifest; } } + public Path getOriginalManifestPath(final FileContentHandler handler) throws IOException { + final ToscaMetadata metadata = getOriginalMetadata(handler); + final String originalMetadataPath = metadata.getMetaEntries().get(TOSCA_META_ETSI_ENTRY_MANIFEST); + 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.getFileContent(TOSCA_META_PATH_FILE_NAME)); + metadata = OnboardingToscaMetadata + .parseToscaMetadataFile(handler.getFileContent(TOSCA_META_PATH_FILE_NAME)); } else if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) { - metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContent(TOSCA_META_ORIG_PATH_FILE_NAME)); + metadata = OnboardingToscaMetadata + .parseToscaMetadataFile(handler.getFileContent(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.getFileContent(TOSCA_META_ORIG_PATH_FILE_NAME)); + } else { + throw new IOException(String.format("%s file not found", TOSCA_META_ORIG_PATH_FILE_NAME)); + } + } + private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException { InputStream io; if (manifestLocation == null || !handler.containsFile(manifestLocation)) { 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 dc456a1970..3d70948b2f 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,47 +20,62 @@ package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter; -import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.sdc.tosca.csar.Manifest; +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 java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +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.parser.utils.YamlToObjectConverter; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; public class ETSIServiceImplTest { + private ETSIService etsiService; private String sol004MetaFile = "TOSCA-Meta-Version: 1.0\n" + - "CSAR-Version: 1.0\n" + - "Created-By: Kuku\n" + - "Entry-Definitions: MainServiceTemplate.yaml\n" + - "ETSI-Entry-Manifest: MainServiceTemplate.mf\n" + - "ETSI-Entry-Change-Log: MainServiceTemplate.log"; + "CSAR-Version: 1.0\n" + + "Created-By: Kuku\n" + + "Entry-Definitions: MainServiceTemplate.yaml\n" + + "ETSI-Entry-Manifest: MainServiceTemplate.mf\n" + + "ETSI-Entry-Change-Log: MainServiceTemplate.log"; private String metaFile = "TOSCA-Meta-Version: 1.0\n" + - "CSAR-Version: 1.0\n" + - "Created-By: Kuku\n" + - "Entry-Definitions: MainServiceTemplate.yaml"; + "CSAR-Version: 1.0\n" + + "Created-By: Kuku\n" + + "Entry-Definitions: MainServiceTemplate.yaml"; private String finalNonManoLocation = "Deployment/VES_EVENTS/test.xml"; - private String finalOtherNonManoLocation = "Informational/OTHER/test.xml"; @Before public void setUp() throws IOException { YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); Configuration configuration = yamlToObjectConverter.convert("src/test/resources", Configuration.class, "nonManoConfig.yaml"); - etsiService = new ETSIServiceImpl(configuration); + etsiService = Mockito.spy(new ETSIServiceImpl(configuration)); } @After @@ -71,42 +86,60 @@ public class ETSIServiceImplTest { @Test public void testIsSol004TrueOrigin() throws IOException { FileContentHandler fileContentHandler = new FileContentHandler(); - fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", sol004MetaFile.getBytes(StandardCharsets.UTF_8)); + fileContentHandler + .addFile("TOSCA-Metadata/TOSCA.meta.original", sol004MetaFile.getBytes(StandardCharsets.UTF_8)); assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); } @Test - public void testIsSol004True() throws IOException { + public void testIsSol004True() throws IOException { FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta", sol004MetaFile.getBytes(StandardCharsets.UTF_8)); assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); } @Test - public void testIsSol004False() throws IOException { + public void testIsSol004False() throws IOException { FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", metaFile.getBytes(StandardCharsets.UTF_8)); assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); } @Test - public void testIsSol004FalseWithNull() throws IOException { + public void testIsSol004FalseWithNull() throws IOException { FileContentHandler fileContentHandler = new FileContentHandler(); assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); } @Test public void testMoveNonManoFileToArtifactFolder() throws IOException { - Map> nonManoSources = new HashMap<>(); - List sources = new ArrayList<>(); - sources.add("Some/test.xml"); - nonManoSources.put("Some", sources); - FileContentHandler fileContentHandler = new FileContentHandler(); + final Map> nonManoTypeAndSourceMapInManifest = new HashMap<>(); + nonManoTypeAndSourceMapInManifest.put("Some", Collections.singletonList("Some/test.xml")); + final Manifest manifest = mock(Manifest.class); + when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest); + final FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile("Some/test.xml", new byte[1]); - Manifest manifest = mock(Manifest.class); - when(manifest.getNonManoSources()).thenReturn(nonManoSources); - etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); - assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); + fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", new byte[1]); + fileContentHandler.addFile("MainServiceTemplate.mf", new byte[1]); + doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); + assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation)); + } + + + @Test + public void testMoveNonManoFileInArtifactFolderToNonManoOnapPath() throws IOException { + final Map> nonManoTypeAndSourceMapInManifest = new HashMap<>(); + nonManoTypeAndSourceMapInManifest.put("Some", Collections.singletonList("Artifacts/Some/test.xml")); + final FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("Some/test.xml", new byte[1]); + final Manifest manifest = mock(Manifest.class); + doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); + when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); + assertThat("Should contain moved file", fileContentHandler.getFileList(), hasItem(finalNonManoLocation)); } @Test @@ -118,8 +151,11 @@ public class ETSIServiceImplTest { FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile("test.xml", new byte[1]); Manifest manifest = mock(Manifest.class); + doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); - etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); + String finalOtherNonManoLocation = "Informational/OTHER/test.xml"; assertFalse(fileContentHandler.containsFile(finalOtherNonManoLocation)); } @@ -132,8 +168,10 @@ public class ETSIServiceImplTest { FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile(finalNonManoLocation, new byte[1]); Manifest manifest = mock(Manifest.class); + doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); - etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); } @@ -144,10 +182,111 @@ public class ETSIServiceImplTest { sources.add("Artifacts/Deployment/test.xml"); nonManoSources.put("Some", sources); FileContentHandler fileContentHandler = new FileContentHandler(); - fileContentHandler.addFile("Artifacts/Deployment/test.xml", new byte[1]); + fileContentHandler.addFile("Deployment/test.xml", new byte[1]); Manifest manifest = mock(Manifest.class); + doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); when(manifest.getNonManoSources()).thenReturn(nonManoSources); - etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); } + + @Test + public void givenManifestNotInRoot_moveNonManoFileToNonManoOnapFolder() throws IOException { + //given manifest non mano files under key "onap_other", inside and outside Artifacts folder, + // with relative and absolute paths. + final Map> nonManoTypeAndSourceMapInManifest = new HashMap<>(); + nonManoTypeAndSourceMapInManifest.put("onap_other", + Arrays.asList("../../Artifacts/Artifacts/Deployment/relativePathInsideSubArtifact.xml", + "../../Files/scriptInFilesPath.sh", + "/Artifacts/Deployment/absolutePathInsideArtifact.xml")); + //given ONAP package fileHandler + final FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("Artifacts/Deployment/relativePathInsideSubArtifact.xml", new byte[1]); + fileContentHandler.addFile("Deployment/absolutePathInsideArtifact.xml", new byte[1]); + fileContentHandler.addFile("Files/scriptInFilesPath.sh", new byte[1]); + //given onboarded manifest in two/lvlFolder folder + final Manifest manifest = mock(Manifest.class); + doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(Paths.get("two/lvlFolder")).when(etsiService).getOriginalManifestPath(fileContentHandler); + when(manifest.getNonManoSources()).thenReturn(nonManoTypeAndSourceMapInManifest); + //when files are non mano moved + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler); + assertThat("Should contain moved file", fileContentHandler.getFileList(), + hasItem("Deployment/OTHER/relativePathInsideSubArtifact.xml")); + assertThat("Should contain moved file", fileContentHandler.getFileList(), + hasItem("Deployment/OTHER/absolutePathInsideArtifact.xml")); + assertThat("Should contain moved file", fileContentHandler.getFileList(), + hasItem("Deployment/OTHER/scriptInFilesPath.sh")); + } + + @Test + public void givenManifestInRoot_moveNonManoFileToNonManoOnapFolder() throws IOException { + //given manifest non mano files under key "onap_other", inside and outside Artifacts folder + final Map> nonManoSourceMap = new HashMap<>(); + nonManoSourceMap.put("onap_other", + Arrays.asList("Artifacts/Deployment/ANOTHER/authorized_keys", + "Files/scriptInFilesPath.sh") + ); + //given manifest non mano file under key "Some" + nonManoSourceMap.put("Some", + Collections.singletonList("Files/willMoveToSome.sh") + ); + //given ONAP package fileHandler + final FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("Deployment/ANOTHER/authorized_keys", new byte[1]); + fileContentHandler.addFile("Files/scriptInFilesPath.sh", new byte[1]); + fileContentHandler.addFile("Files/willMoveToSome.sh", new byte[1]); + fileContentHandler.addFile("Deployment/willNotMove.xml", new byte[1]); + //given onboarded manifest in root folder + final Manifest manifest = mock(Manifest.class); + when(manifest.getNonManoSources()).thenReturn(nonManoSourceMap); + doReturn(manifest).when(etsiService).getManifest(fileContentHandler); + doReturn(Paths.get("")).when(etsiService).getOriginalManifestPath(fileContentHandler); + final Optional> fromToPathMap = etsiService + .moveNonManoFileToArtifactFolder(fileContentHandler); + assertThat("Files should be moved", fromToPathMap.isPresent(), is(true)); + assertThat("Should contain moved file", fileContentHandler.getFileList(), + hasItem("Deployment/OTHER/authorized_keys")); + assertThat("Should contain moved file", fileContentHandler.getFileList(), + hasItem("Deployment/OTHER/scriptInFilesPath.sh")); + assertThat("Should contain moved file", fileContentHandler.getFileList(), + hasItem("Deployment/VES_EVENTS/willMoveToSome.sh")); + assertThat("Should contain not moved file", fileContentHandler.getFileList(), + hasItem("Deployment/willNotMove.xml")); + } + + @Test + public void givenMovedFiles_updateDescriptorReferences() { + //given moved files + final Map fromToPathMap = new HashMap<>(); + final String file1OriginalPath = "Artifacts/Deployment/ANOTHER/authorized_keys"; + final Path file1Path = Paths.get("Artifacts", "Deployment", "OTHER", "authorized_keys"); + fromToPathMap.put(file1OriginalPath, file1Path); + final String file2OriginalPath = "Artifacts/Deployment/ANOTHER/image"; + final Path file2Path = Paths.get("Artifacts", "Deployment", "OTHER", "image"); + fromToPathMap.put(file2OriginalPath, file2Path); + //given main descriptor + final InputStream mainServiceTemplateYamlFile = getClass().getClassLoader() + .getResourceAsStream("vnfPackage/vnf1/Definitions/MainServiceTemplate.yaml"); + final ServiceTemplate mainServiceTemplate = new YamlUtil() + .yamlToObject(mainServiceTemplateYamlFile, ServiceTemplate.class); + final HashMap serviceTemplateMap = new HashMap<>(); + serviceTemplateMap.put("MainServiceTemplate.yaml", mainServiceTemplate); + final ToscaServiceModel toscaServiceModel = new ToscaServiceModel(null, serviceTemplateMap, + "MainServiceTemplate.yaml"); + //when descriptor is updated + etsiService.updateMainDescriptorPaths(toscaServiceModel, fromToPathMap); + //then + final String serviceTemplatesAsYaml = new YamlUtil().objectToYaml(toscaServiceModel.getServiceTemplates()); + assertThat("Descriptor should not contain reference to file", serviceTemplatesAsYaml, + not(containsString(file1OriginalPath))); + assertThat("Descriptor should not contain reference to file", serviceTemplatesAsYaml, + not(containsString(file2OriginalPath))); + assertThat("Descriptor should contain reference to file", serviceTemplatesAsYaml, + containsString(file1Path.toString())); + assertThat("Descriptor should contain reference to file", serviceTemplatesAsYaml, + containsString(file2Path.toString())); + } + } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml index 32c6b1ce57..914e9ff59d 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml @@ -4,4 +4,7 @@ nonManoKeyFolderMapping: type: Deployment Other: location: VES_EVENTS - type: Informational \ No newline at end of file + type: Informational + onap_other: + location: OTHER + type: Deployment \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/vnfPackage/vnf1/Definitions/MainServiceTemplate.yaml b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/vnfPackage/vnf1/Definitions/MainServiceTemplate.yaml new file mode 100644 index 0000000000..3ed5f5ce00 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/vnfPackage/vnf1/Definitions/MainServiceTemplate.yaml @@ -0,0 +1,97 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 +metadata: + VNFD_SCHEMA_VERSION: 2.5.1+1 + template_name: Main + template_version: '1.0' + template_author: onap +topology_template: + node_templates: + VDU_vgw_0: + type: tosca.nodes.nfv.Vdu.Compute + properties: + name: + get_input: vgw_name_0 + description: vgw + vdu_profile: + min_number_of_instances: 1 + max_number_of_instances: 1 + watchdog: none + inject_files: + source_path: ../Artifacts/Deployment/ANOTHER/authorized_keys + dest_path: /home/ubuntu/.ssh/authorized_keys + meta_data: + vnf_id: + get_input: vnf_id + vf_module_id: + get_input: vf_module_id + mux_gw_private_net_ipaddr: + get_input: vgw_private_ip_0 + oam_ipaddr: + get_input: vgw_private_ip_1 + oam_cidr: + get_input: onap_private_net_cidr + cpe_public_net_cidr: + get_input: cpe_public_net_cidr + mux_gw_private_net_cidr: + get_input: mux_gw_private_net_cidr + mux_ip_addr: + get_input: mux_ip_addr + vg_vgmux_tunnel_vni: + get_input: vg_vgmux_tunnel_vni + install_script_version: + get_input: install_script_version + cloud_env: + get_input: cloud_env + nexus_artifact_repo: + get_input: nexus_artifact_repo + boot_data: | + #!/bin/bash + METADATA=`curl -s http://169.254.169.254/openstack/2012-08-10/meta_data.json` + apt-get -y install jq + + get_metadata () { + echo $METADATA | jq -r ".meta.$1" + } + + # Create configuration files + mkdir /opt/config + echo "$(get_metadata oam_ipaddr)" > /opt/config/oam_ipaddr.txt + echo "$(get_metadata oam_cidr)" > /opt/config/oam_cidr.txt + echo "$(get_metadata cpe_public_net_cidr)" > /opt/config/cpe_public_net_cidr.txt + echo "$(get_metadata mux_gw_private_net_ipaddr)" > /opt/config/mux_gw_private_net_ipaddr.txt + echo "$(get_metadata mux_gw_private_net_cidr)" > /opt/config/mux_gw_private_net_cidr.txt + echo "$(get_metadata install_script_version)" > /opt/config/install_script_version.txt + echo "$(get_metadata cloud_env)" > /opt/config/cloud_env.txt + echo "$(get_metadata mux_ip_addr)" > /opt/config/mux_ip_addr.txt + echo "$(get_metadata vg_vgmux_tunnel_vni)" > /opt/config/vg_vgmux_tunnel_vni.txt + echo "$(get_metadata nexus_artifact_repo)" > /opt/config/nexus_artifact_repo.txt + + # Download and run install script + apt-get -y install unzip + INSTALL_SCRIPT_VERSION=$(get_metadata install_script_version) + NEXUS_ARTIFACT_REPO=$(get_metadata nexus_artifact_repo) + if [[ "${INSTALL_SCRIPT_VERSION}" =~ "SNAPSHOT" ]]; then REPO=snapshots; else REPO=releases; fi + curl -k -L "${NEXUS_ARTIFACT_REPO}/service/local/artifact/maven/redirect?r=${REPO}&g=org.onap.demo.vnf.vcpe&a=vcpe-scripts&e=zip&v=${INSTALL_SCRIPT_VERSION}" -o /opt/vcpe-scripts-${INSTALL_SCRIPT_VERSION}.zip + unzip -j /opt/vcpe-scripts-${INSTALL_SCRIPT_VERSION}.zip -d /opt v_gw_install.sh + cd /opt + chmod +x v_gw_install.sh + ./v_gw_install.sh + capabilities: + virtual_compute: + properties: + virtual_memory: + virtual_mem_size: 4096 MB + vdu_mem_requirements: + memoryPageSize: '{"schemaVersion": "0", "schemaSelector": "", "hardwarePlatform": + "generic", "mandatory": "true", "configurationValue": "2 MB"}' + numberOfPages: '{"schemaVersion": "0","schemaSelector": "","hardwarePlatform": + "generic","mandatory": "true", "configurationValue": "1024"}' + virtual_cpu: + num_virtual_cpu: 2 + cpu_architecture: generic + virtual_local_storage: + - size_of_storage: 40 GB + artifacts: + sw_image: + type: tosca.artifacts.nfv.SwImage + file: ../Artifacts/Deployment/ANOTHER/image \ No newline at end of file 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 06e2de1628..7a42758322 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 @@ -25,14 +25,15 @@ public class CSARConstants { public static final ImmutableSet ELIGBLE_FOLDERS = of("Artifacts/","Definitions/", "Licenses/", "TOSCA-Metadata/"); + public static final String ARTIFACTS_FOLDER = "Artifacts"; public static final String MAIN_SERVICE_TEMPLATE_MF_FILE_NAME = "MainServiceTemplate.mf"; public static final String MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME = "MainServiceTemplate.yaml"; - public static final String TOSCA_META_PATH_FILE_NAME="TOSCA-Metadata/TOSCA.meta"; + public static final String TOSCA_META_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta"; public static final String TOSCA_META_FILE_VERSION_ENTRY = "TOSCA-Meta-File-Version"; public static final String TOSCA_META_CSAR_VERSION_ENTRY = "CSAR-Version"; public static final String TOSCA_META_CREATED_BY_ENTRY = "Created-By"; - public static final String TOSCA_META_ENTRY_DEFINITIONS="Entry-Definitions"; + public static final String TOSCA_META_ENTRY_DEFINITIONS ="Entry-Definitions"; public static final String TOSCA_META_ETSI_ENTRY_MANIFEST ="ETSI-Entry-Manifest"; public static final String TOSCA_META_ETSI_ENTRY_CHANGE_LOG ="ETSI-Entry-Change-Log"; public static final String TOSCA_META_ETSI_ENTRY_TESTS = "ETSI-Entry-Tests"; @@ -64,7 +65,7 @@ public class CSARConstants { public static final String CMD_END = "----END CMS-----"; public static final String SEPARATOR_MF_ATTRIBUTE = ":"; public static final String NON_MANO_MF_ATTRIBUTE = "non_mano_artifact_sets"; - public static final String TOSCA_META_ORIG_PATH_FILE_NAME="TOSCA-Metadata/TOSCA.meta.original"; + public static final String TOSCA_META_ORIG_PATH_FILE_NAME = "TOSCA-Metadata/TOSCA.meta.original"; public static final String TOSCA_META_FILE_VERSION = "1.0"; public static final String CSAR_VERSION_1_0 = "1.0"; -- cgit 1.2.3-korg