From 0a1d82ac04a8ef78bfdcbcced4f5096c050edcfe Mon Sep 17 00:00:00 2001 From: "bilal.iqbal" Date: Sun, 10 Mar 2019 00:47:55 +0000 Subject: CSAR Package validation Change-Id: I11af8d93f5a2cd0566a5caf0dad0519d70bd57d7 Issue-ID: SDC-2147 Issue-ID: SDC-2148 Issue-ID: SDC-2149 Issue-ID: SDC-2150 Signed-off-by: bilal.iqbal --- .../services/impl/etsi/ETSIService.java | 5 ++- .../services/impl/etsi/ETSIServiceImpl.java | 37 ++++++++++++++-------- .../services/impl/etsi/ETSIServiceImplTest.java | 8 ++--- 3 files changed, 32 insertions(+), 18 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-vendor-software-product-lib') 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 69ce97efc5..d5dab46533 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 @@ -22,16 +22,19 @@ package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.sdc.tosca.csar.Manifest; +import java.io.IOException; public interface ETSIService { + /** * Checks package structure is CSAR with TOSCA-Metadata directory according to SOL004 v2.5.1 * and contains mandatory Entries in Tosca.meta * @param handler contains csar artifacts * @return true if all condition matched, false otherwise + * @throws IOException when TOSCA.meta file is invalid */ - boolean isSol004WithToscaMetaDirectory(FileContentHandler handler); + boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException; /** * Update file structure. Moves non mano files to Artifacts/Deployment/non mano key location 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 b3002abf6e..9984df1ccc 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 @@ -30,11 +30,16 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; -import java.util.Optional; import org.openecomp.sdc.tosca.csar.Manifest; +import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata; +import org.openecomp.sdc.tosca.csar.ToscaMetadata; -import static org.openecomp.sdc.tosca.csar.CSARConstants.*; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_CHANGE_LOG; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS; +import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_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; public class ETSIServiceImpl implements ETSIService { @@ -55,9 +60,9 @@ public class ETSIServiceImpl implements ETSIService { } @Override - public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) { + public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException { Map templates = handler.getFiles(); - return isMetaFilePresent(templates) && hasMetaMandatoryEntries(templates); + return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler)); } @Override @@ -69,6 +74,15 @@ public class ETSIServiceImpl implements ETSIService { } } + private InputStream getMetadata(FileContentHandler contentHandler) throws IOException{ + if(contentHandler.containsFile(TOSCA_META_PATH_FILE_NAME)){ + return contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME); + }else if(contentHandler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)){ + return contentHandler.getFileContent(TOSCA_META_ORIG_PATH_FILE_NAME); + } + throw new IOException("TOSCA.meta file does not exist"); + } + private void updateNonManoLocation(FileContentHandler handler, String nonManoKey, List sources) { Map files = handler.getFiles(); for (String key : sources) { @@ -98,15 +112,12 @@ public class ETSIServiceImpl implements ETSIService { return key.substring(key.lastIndexOf('/') + 1); } - private boolean hasMetaMandatoryEntries(Map templates) { - Optional meta = templates.entrySet().stream().filter(e -> e.getKey().equals(TOSCA_META_PATH_FILE_NAME) - || e.getKey().equals(TOSCA_META_ORIG_PATH_FILE_NAME)).findFirst().map(Map.Entry::getValue); - if (!meta.isPresent()) { - return false; - } - String metaContent = new String(meta.get(), StandardCharsets.UTF_8); - return metaContent.contains(TOSCA_META_ENTRY_DEFINITIONS) && metaContent.contains(TOSCA_META_ENTRY_MANIFEST) - && metaContent.contains(TOSCA_META_ENTRY_CHANGE_LOG); + private boolean hasMetaMandatoryEntries(InputStream metadataInputStream) throws IOException { + + ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(metadataInputStream); + Map metaDataEntries = toscaMetadata.getMetaEntries(); + return metaDataEntries.containsKey(TOSCA_META_ENTRY_DEFINITIONS) && metaDataEntries.containsKey(TOSCA_META_ENTRY_MANIFEST) + && metaDataEntries.containsKey(TOSCA_META_ENTRY_CHANGE_LOG); } private boolean isMetaFilePresent(Map handler) { 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 ae69415f1d..2dc37f17e4 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 @@ -49,28 +49,28 @@ public class ETSIServiceImplTest { } @Test - public void testIsSol004TrueOrigin() { + public void testIsSol004TrueOrigin() throws IOException { FileContentHandler fileContentHandler = new FileContentHandler(); fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", sol004MetaFile.getBytes(StandardCharsets.UTF_8)); assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); } @Test - public void testIsSol004True() { + 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() { + 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() { + public void testIsSol004FalseWithNull() throws IOException { FileContentHandler fileContentHandler = new FileContentHandler(); assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); } -- cgit 1.2.3-korg