diff options
Diffstat (limited to 'csarvalidation')
6 files changed, 102 insertions, 54 deletions
diff --git a/csarvalidation/pom.xml b/csarvalidation/pom.xml index 3deae96..1a91677 100644 --- a/csarvalidation/pom.xml +++ b/csarvalidation/pom.xml @@ -111,7 +111,16 @@ <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> - + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-io</artifactId> + <version>1.3.2</version> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.5</version> + </dependency> </dependencies> diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java index dbead03..f915168 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java @@ -78,6 +78,13 @@ public class CsarUtil { input = zipFile.getInputStream(entry); File file = new File(extPlace, entry.getName()); + + //Currently it does not support xml based VNF descriptors. + //So skip and proceed to YAML defined files validation only. + if (file.getAbsolutePath().contains("xml"+System.getProperty("file.separator"))) { + continue; + } + if (!file.getParentFile().exists()) { FileUtil.createDirectory(file.getParentFile().getAbsolutePath()); } @@ -90,7 +97,9 @@ public class CsarUtil { } bos.write(buffer, 0, length); } + unzipFileNames.put(file.getName(), file.getAbsolutePath()); + } finally { closeOutputStream(bos); closeInputStream(input); diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java index 46200a7..57870be 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java @@ -16,10 +16,12 @@ package org.onap.validation.csar; +import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import static java.nio.charset.StandardCharsets.*; import java.util.*; @@ -31,13 +33,17 @@ public class CsarValidator { private static final Logger LOG = LoggerFactory.getLogger(CsarValidator.class); + //Schema files + // static private ValidatorSchemaLoader vsl; + // Map of CSAR file and un-zipped file indices - static HashMap<String, String> csarFiles; + static private HashMap<String, String> csarFiles; // Map of packageId and CSAR files private static HashMap<String, HashMap<String, String>> csar = new HashMap<String, HashMap<String, String>>(); + private static String MAINSERV_TEMPLATE; - public CsarValidator(String packageId, String csarWithPath) { + public CsarValidator(String packageId, String csarWithPath) { try { FileInputStream is = new FileInputStream(csarWithPath); @@ -45,6 +51,7 @@ public class CsarValidator { LOG.error("CSAR %s is not found! " +ErrorCodes.RESOURCE_MISSING); throw new ValidationException(ErrorCodes.RESOURCE_MISSING); } + try { csarFiles = CsarUtil.csarExtract(csarWithPath); if(!csarFiles.isEmpty()) { @@ -57,7 +64,13 @@ public class CsarValidator { LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); } - } + + try { + // vsl = new ValidatorSchemaLoader(); + } catch (Exception e) { + e.printStackTrace(); + } + } public static boolean validateCsar() { @@ -94,8 +107,6 @@ public class CsarValidator { } } - - public static boolean validateCsarMeta() { String cfile = csarFiles.get(CommonConstants.CSAR_META); @@ -139,7 +150,6 @@ public class CsarValidator { } } - return false; } @@ -151,20 +161,29 @@ public class CsarValidator { return false; } try { - if (!cfile.isEmpty() && cfile.contains(System.getProperty("file.separator") + + if (cfile.contains(System.getProperty("file.separator") + CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") + CommonConstants.TOSCA_META)) { - String value = checkEntryFor(cfile, "Entry-Definitions:"); + String value = CheckEntryFor(cfile, "Entry-Definitions:"); String[] splitPath = value.split("/"); String subValue = splitPath[splitPath.length - 1]; if (value.isEmpty() || subValue.isEmpty()) { return false; - //Check if Entry-Defintions pointed file exists in CSAR - } else if (!(null == csarFiles.get(value)) || - !(null == csarFiles.get(subValue))) { - return true; + //Check if Entry-Defintions pointed file exists in CSAR + } else { + if (!(null == csarFiles.get(value))) { + MAINSERV_TEMPLATE = csarFiles.get(value); + return true; + } + else if (!(null == csarFiles.get(subValue))) { + MAINSERV_TEMPLATE = csarFiles.get(subValue); + return true; + } + else { + MAINSERV_TEMPLATE = CommonConstants.MAINSERV_TEMPLATE; + } } } } catch (IOException | NullPointerException e) { @@ -181,21 +200,19 @@ public class CsarValidator { // Infuture load from the respective file template/schema List<String> mListMetadata = Arrays.asList("vnf_product_name", "vnf_provider_id", "vnf_package_version", "vnf_release_data_time"); - boolean mfResult = checkEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key); - - List<String> tListMetadata = Arrays.asList("vendor", "csarVersion", - "csarProvider", "id", "version", "csarType", "name", "vnfdVersion", - "vnfmType"); - boolean tResult = checkEntryFor(CommonConstants.MAINSERV_TEMPLATE, tListMetadata, key); - if (!tResult || !mfResult) { - return false; - } else { - return true; + boolean mfResult = CheckEntryFor(CommonConstants.MAINSERV_MANIFEST, mListMetadata, key); + String mrfFile = MAINSERV_TEMPLATE; + if(!Paths.get(mrfFile).isAbsolute()){ + mrfFile = csarFiles.get(FilenameUtils.getName(mrfFile)); + } + if(StringUtils.isEmpty(mrfFile)){ + return false; } + return true; } - private static String checkEntryFor(String fileWithPath, String attribute) throws IOException { + private static String CheckEntryFor(String fileWithPath, String attribute) throws IOException { List<String> lines = Files.readAllLines(Paths.get(fileWithPath), UTF_8); @@ -207,17 +224,21 @@ public class CsarValidator { return null; } - private static boolean checkEntryFor(String cFile, List<String> attributes, String key) { - String tFileWithPath = csarFiles.get(cFile); + private static boolean CheckEntryFor(String cFile, List<String> attributes, String key) { + String tFileWithPath; + + if (! Paths.get(cFile).isAbsolute()) { + cFile = csarFiles.get(FilenameUtils.getName(cFile)); + } - if(StringUtils.isEmpty(tFileWithPath)) { + if(StringUtils.isEmpty(cFile)) { return false; } Yaml yaml = new Yaml(); Map<String, ?> values; try { - values = (Map<String, ?>) yaml.load(new FileInputStream(new File(tFileWithPath))); + values = (Map<String, ?>) yaml.load(new FileInputStream(new File(cFile))); } catch (FileNotFoundException e) { LOG.error("FILE_NOT_FOUND" + ":" + "Exception caught while trying to find the file ! " + e.getMessage(), e); return false; @@ -231,19 +252,26 @@ public class CsarValidator { .map(Object::toString) .collect(Collectors.toList()); + // If no attributes provided, take it easy + // there are no mandatory fields to be + // validated. + if (attributes == null) { + return true; + } if (subValues.size() != attributes.size() && lResultNonNull.size() != attributes.size()) { return false; } - //2. Validate the exact mandatory attributes with expected attributes list + //Validate the exact mandatory(not defined in SOL004 yet) + // attributes with expected attributes list List<? super String> lResult = subValues.keySet().stream() .filter(attributes::contains) .collect(Collectors.toList()); // System.out.println(result); - if (lResult.size() != attributes.size()) { + if (lResult.size() == 0) { return false; } return true; diff --git a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java index 9e0500d..19422b9 100644 --- a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java @@ -41,10 +41,10 @@ public class CsarValidatorTest { Pattern pattern = Pattern.compile(regex); private String csarFile = classLoader.getResource("enterprise2DC.csar").getFile(); Matcher matcher = pattern.matcher(csarFile); - String dir2 = System.getProperty("file.separator") + csarFile.substring(1); + String sample1 = System.getProperty("file.separator") + csarFile.substring(1); String packageId = UUID.randomUUID().toString(); - private String csarFile2 = classLoader.getResource("sample2.csar").getFile(); + private String csarFile2 = classLoader.getResource("vEPC_NS.csar").getFile(); String sample2 = System.getProperty("file.separator") + csarFile2.substring(1); String packageId2 = UUID.randomUUID().toString(); /* @@ -52,25 +52,24 @@ public class CsarValidatorTest { String sample3 = System.getProperty("file.separator") + csarFile3.substring(1); String packageId3 = UUID.randomUUID().toString(); */ @Test - public void testAll() { - CsarValidator csarValidator = new CsarValidator(packageId, dir2); + public void testAll() throws IOException, InterruptedException { + CsarValidator csarValidator = new CsarValidator(packageId, sample1); testValidateCsar(csarValidator); CsarValidator csarValidator2 = new CsarValidator(packageId2, sample2); // CsarValidator csarValidator3 = new CsarValidator(packageId3, sample3); testValidateCsar(csarValidator2); - String dir3 = dir2.replace(".csar", ""); - String dir4 = sample2.replace(".csar", ""); - // String dir5 = sample3.replace(".csar", ""); - boolean result = FileUtil.deleteDirectory(dir3); - boolean result1 = FileUtil.deleteDirectory(dir4); - //boolean result2 = FileUtil.deleteDirectory(dir5); + String sample1Dir = sample1.replace(".csar", ""); + String sample2Dir = sample2.replace(".csar", ""); + + boolean result = FileUtil.deleteDirectory(sample1Dir); + boolean result1 = FileUtil.deleteDirectory(sample2Dir); assertEquals(true, result == true && result1 ==true); } @Test - public void testIndividual() { - CsarValidator csarValidator = new CsarValidator(packageId, dir2); + public void testIndividual() throws IOException, InterruptedException { + CsarValidator csarValidator = new CsarValidator(packageId, sample1); testValidateCsarMeta(csarValidator); testValidateCsarIntegrity(csarValidator); testValidateToscaMeta(csarValidator); @@ -78,9 +77,9 @@ public class CsarValidatorTest { CsarValidator csarValidator2 = new CsarValidator(packageId2, sample2); testValidateCsarIntegrity(csarValidator2); testValidateToscaMeta(csarValidator2); - testValidateMainService(csarValidator2); + testValidateMainService(csarValidator2); //Rel1 specific test case - String dir3 = dir2.replace(".csar", ""); + String dir3 = sample1.replace(".csar", ""); String dir4 = sample2.replace(".csar", ""); boolean result = FileUtil.deleteDirectory(dir3); boolean result1 = FileUtil.deleteDirectory(dir4); @@ -92,31 +91,31 @@ public class CsarValidatorTest { FileUtil.closeInputStream(dir); assertTrue(true); } - + @Test(expected = IllegalArgumentException.class) public void testReadJsonDatafFromFile() { - FileUtil.readJsonDatafFromFile(dir2, null); + FileUtil.readJsonDatafFromFile(sample1, null); } @Test public void testCloseZipFile() throws ZipException, IOException { - File file = new File(dir2); + File file = new File(sample1); ZipFile dir1 = new ZipFile(file); FileUtil.closeZipFile(dir1); assertTrue(true); } @Test public void testCloseFileStream() throws FileNotFoundException { - FileInputStream dir3 = new FileInputStream(dir2); + FileInputStream dir3 = new FileInputStream(sample1); FileUtil.closeFileStream(dir3 ); } @Test public void testCloseOutptutStream() { OutputStream dir4 = new OutputStream() { - + @Override public void write(int b) throws IOException { - + } }; FileUtil.closeOutputStream(dir4); @@ -138,7 +137,7 @@ public class CsarValidatorTest { private void testValidateCsarIntegrity(CsarValidator cv) { - boolean result = cv.validateCsarIntegrity(dir2); + boolean result = cv.validateCsarIntegrity(sample1); assertEquals(true, result == true); } diff --git a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/ValidationExceptionTest.java b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/ValidationExceptionTest.java index dfbd32d..9855288 100644 --- a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/ValidationExceptionTest.java +++ b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/ValidationExceptionTest.java @@ -27,22 +27,25 @@ import static org.junit.Assert.assertTrue; public class ValidationExceptionTest { ValidationException validationException = new ValidationException(); + @Test public void testWrappedInfoThrowableErrorCodes() { Throwable ex = new IOException(); - ValidationException result = validationException.wrappedInfo(ex , ErrorCodes.FILE_IO); + ValidationException result = validationException.wrappedInfo(ex, ErrorCodes.FILE_IO); assertTrue(true); } + @Test public void testWrappedInfoThrowableErrorCodes1() { Throwable ex = new ValidationException(); - ValidationException result = validationException.wrappedInfo(ex , ErrorCodes.FILE_IO); + ValidationException result = validationException.wrappedInfo(ex, ErrorCodes.FILE_IO); assertTrue(true); } + @Test public void testWrappedInfoThrowableErrorCodes2() { Throwable ex = new ValidationException(); - ValidationException result = validationException.wrappedInfo(ex , null); + ValidationException result = validationException.wrappedInfo(ex, null); assertTrue(true); } } diff --git a/csarvalidation/src/test/resources/vEPC_NS.csar b/csarvalidation/src/test/resources/vEPC_NS.csar Binary files differnew file mode 100644 index 0000000..67ab75a --- /dev/null +++ b/csarvalidation/src/test/resources/vEPC_NS.csar |