From a04f31a2b6aa49cc0646a35bf6f7f7566bb1c331 Mon Sep 17 00:00:00 2001 From: AvinashS Date: Wed, 20 Sep 2017 10:13:15 +0000 Subject: Updated validation for CSIT Lib interface for CSIT with marketplace Change-Id: I73c68b2f8f7a3940cb175cce2397abd4a4deaff4 IssueId: VNFSDK-90 Signed-off-by: AvinashS --- csarvalidation/pom.xml | 5 + .../org/onap/validation/csar/CommonConstants.java | 6 +- .../java/org/onap/validation/csar/CsarParser.java | 199 ---------------- .../java/org/onap/validation/csar/CsarUtil.java | 13 ++ .../org/onap/validation/csar/CsarValidator.java | 259 +++++++++++++++++++++ .../csarvalidationtest/CsarParserTest.java | 62 ----- .../csarvalidationtest/CsarValidatorTest.java | 64 +++++ 7 files changed, 346 insertions(+), 262 deletions(-) delete mode 100644 csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java delete mode 100644 csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarParserTest.java create mode 100644 csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java diff --git a/csarvalidation/pom.xml b/csarvalidation/pom.xml index cc38996..6bf50c2 100644 --- a/csarvalidation/pom.xml +++ b/csarvalidation/pom.xml @@ -54,6 +54,11 @@ 1.6.4 test + + org.yaml + snakeyaml + 1.18 + com.eclipsesource.jaxrs consumer diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java b/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java index be709ad..03cc36a 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java @@ -41,6 +41,10 @@ public class CommonConstants { public static final String TOSCA_META = "TOSCA.meta"; + public static final String MAINSERV_MANIFEST = "MainServiceTemplate.mf"; + + public static final String MAINSERV_TEMPLATE = "MainServiceTemplate.yaml"; + public static final String CSAR_SUFFIX = ".csar"; public static final String HTTP_HEADER_CONTENT_RANGE = "Content-Range"; @@ -61,7 +65,7 @@ public class CommonConstants { public static final int BUFFER_SIZE = 2 * 1024 * 1024; - private CommonConstants() { + private CommonConstants() { // Cannot create instance of the class } diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java deleted file mode 100644 index 76a7b11..0000000 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java +++ /dev/null @@ -1,199 +0,0 @@ -/** - * Copyright 2017 Huawei Technologies Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onap.validation.csar; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; - -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.text.SimpleDateFormat; - -import java.util.*; - -import static java.nio.charset.StandardCharsets.*; - - -public class CsarParser { - - private static final Logger LOG = LoggerFactory.getLogger(CsarParser.class); - - // Map of CSAR file and un-zipped file indices - private static HashMap csarFiles; - - // Map of packageId and CSAR files - private static HashMap> csar = new HashMap>(); - - private static final CsarUtil cUtil = new CsarUtil(); - - public CsarParser(String csarWithPath) { - - try { - FileInputStream is = new FileInputStream(csarWithPath); - } catch (FileNotFoundException e2) { - LOG.error("CSAR %s is not found! ", e2); - } - try { - boolean ret = csarExtract(csarWithPath); - if(ret == true) { - LOG.debug("CSAR extracted sucessfully."); - } - } catch (Exception e1) { - LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); - } - } - /* - * pubic static boolean validateCsar(String filePath) { - * - * csarExtract(filePath); - * - * validateCsarMeta(); - * - * validateToscaMeta(); - * - * validateManifest(); - * } - */ - - - public static boolean validateCsarIntegrity(String csarWithPath) { - - try { - RandomAccessFile raf = new RandomAccessFile(csarWithPath, "r"); - long n = raf.readInt(); - raf.close(); - - // Check for the CSAR's integrity - if (n != 0x504B0304) { - LOG.error("CSAR %s is not a valid CSAR/ZIP file! "); - return false; - } - return true; - } catch (IOException e1) { - LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); - return false; - } - } - - private static boolean csarExtract(String filePath) { - - try { - String tempfolder = CsarUtil.getUnzipDir(filePath); - csarFiles = CsarUtil.unzip(filePath, tempfolder); - - } catch (IOException e1) { - LOG.error("CSAR extraction error ! " + e1.getMessage()); - - return false; - } - return true; - } - - public static boolean validateCsarMeta() { - - String cfile = csarFiles.get(CommonConstants.CSAR_META); - if (!cfile.isEmpty()) { - File file = new File(cfile); - BufferedReader reader = null; - - try { - reader = new BufferedReader(new FileReader(file)); - String tempString = null; - while ((tempString = reader.readLine()) != null) { - if (!tempString.equals("")) { - int count1 = tempString.indexOf(":"); - String meta = tempString.substring(0, count1).trim(); - if (meta.equalsIgnoreCase(CommonConstants.CSAR_TYPE_META)) { - int count = tempString.indexOf(":") + 1; - if (tempString.substring(count).trim().isEmpty()) { - return false; - } - } - if (meta.equalsIgnoreCase(CommonConstants.CSAR_PROVIDER_META)) { - int count = tempString.indexOf(":") + 1; - if (tempString.substring(count).trim().isEmpty()) { - return false; - } - } - if (meta.equalsIgnoreCase(CommonConstants.CSAR_VERSION_META)) { - int count = tempString.indexOf(":") + 1; - if (tempString.substring(count).trim().isEmpty()) { - return false; - } - } - } - reader.close(); - return true; - } - } catch (IOException e2) { - LOG.error("Exception cought while validateCsarMeta ! " + e2.getMessage()); - //e2.printStackTrace(); - - } finally { - if (reader != null) { - try { - reader.close(); - - } catch (IOException e1) { - LOG.error("close reader failed ! " + e1.getMessage()); - } - } - - } - } - - return false; - } - - - public static boolean validateToscaMeta() { - - String cfile = csarFiles.get(CommonConstants.TOSCA_META); - try { - if (!cfile.isEmpty() && cfile.contains( System.getProperty("file.separator")+ CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") + CommonConstants.TOSCA_META)) { - - String value = checkEntryFor("Entry-Definitions:", cfile); - if (value == null) { - return false; - //Check if Entry-Defintions pointed file exists in CSAR - } else if (csarFiles.get(value) != null) { - return true; - } - } - } catch (IOException e) { - LOG.error("Could not read file %s ! " + e.getMessage(), cfile); - } - - return false; - } - - - private static String checkEntryFor(String attribute, String fileWithPath) throws IOException { - - List lines = Files.readAllLines(Paths.get(fileWithPath), UTF_8); - - for(String strLine : lines) { - if (!attribute.isEmpty() && strLine.contains(attribute)) { - return strLine.substring(attribute.length(), strLine.length()).trim(); - } - } - return null; - } -} - 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 3708dbf..ee0110c 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java @@ -33,6 +33,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Enumeration; +import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -135,4 +136,16 @@ public class CsarUtil { } } + public static HashMap csarExtract(String filePath) { + + try { + String tempfolder = CsarUtil.getUnzipDir(filePath); + return CsarUtil.unzip(filePath, tempfolder); + + } catch (IOException e1) { + logger.error("CSAR extraction error ! " + e1.getMessage()); + } + return null; + } + } diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java new file mode 100644 index 0000000..7d252e1 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java @@ -0,0 +1,259 @@ +/** + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.validation.csar; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; + +import java.nio.file.Files; +import java.nio.file.Paths; +import static java.nio.charset.StandardCharsets.*; + +import java.util.*; +import java.util.stream.Collectors; + + +import org.yaml.snakeyaml.Yaml; + + +public class CsarValidator { + + private static final Logger LOG = LoggerFactory.getLogger(CsarValidator.class); + + // Map of CSAR file and un-zipped file indices + static HashMap csarFiles; + + // Map of packageId and CSAR files + private static HashMap> csar = new HashMap>(); + + private static final CsarUtil cUtil = new CsarUtil(); + + public CsarValidator(String packageId, String csarWithPath) { + + try { + FileInputStream is = new FileInputStream(csarWithPath); + } catch (FileNotFoundException e2) { + LOG.error("CSAR %s is not found! ", e2); + } + try { + csarFiles = CsarUtil.csarExtract(csarWithPath); + if(!csarFiles.isEmpty()) { + csar.put(packageId, getCsarFiles()); + LOG.debug("CSAR extracted sucessfully."); + } + } catch (Exception e1) { + LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); + } + + } + + public static boolean validateCsar() { + + validateCsarMeta(); + + validateToscaMeta(); + + validateMainService(); + + //In future return the status handler object instead. + return true; + } + + public static boolean validateCsarIntegrity(String csarWithPath) { + + try { + RandomAccessFile raf = new RandomAccessFile(csarWithPath, "r"); + long n = raf.readInt(); + raf.close(); + + // Check for the CSAR's integrity + if (n != 0x504B0304) { + LOG.error("CSAR %s is not a valid CSAR/ZIP file! "); + return false; + } + return true; + } catch (IOException e1) { + LOG.error("CSAR %s is not a valid CSAR/ZIP file! ", e1); + return false; + } + } + + + + public static boolean validateCsarMeta() { + + String cfile = csarFiles.get(CommonConstants.CSAR_META); + if (!cfile.isEmpty()) { + File file = new File(cfile); + BufferedReader reader = null; + + try { + reader = new BufferedReader(new FileReader(file)); + String tempString = null; + while ((tempString = reader.readLine()) != null) { + if (!tempString.equals("")) { + int count1 = tempString.indexOf(":"); + String meta = tempString.substring(0, count1).trim(); + if (meta.equalsIgnoreCase(CommonConstants.CSAR_TYPE_META)) { + int count = tempString.indexOf(":") + 1; + if (tempString.substring(count).trim().isEmpty()) { + return false; + } + } + if (meta.equalsIgnoreCase(CommonConstants.CSAR_PROVIDER_META)) { + int count = tempString.indexOf(":") + 1; + if (tempString.substring(count).trim().isEmpty()) { + return false; + } + } + if (meta.equalsIgnoreCase(CommonConstants.CSAR_VERSION_META)) { + int count = tempString.indexOf(":") + 1; + if (tempString.substring(count).trim().isEmpty()) { + return false; + } + } + } + reader.close(); + return true; + } + } catch (IOException e2) { + LOG.error("Exception caught while validateCsarMeta ! " + e2.getMessage()); + } finally { + if (reader != null) { + try { + reader.close(); + + } catch (IOException e1) { + LOG.error("close reader failed ! " + e1.getMessage()); + } + } + + } + } + + return false; + } + + + public static boolean validateToscaMeta() { + + String cfile = csarFiles.get(CommonConstants.TOSCA_META); + try { + if (!cfile.isEmpty() && cfile.contains( System.getProperty("file.separator")+ CommonConstants.TOSCA_METADATA + System.getProperty("file.separator") + CommonConstants.TOSCA_META)) { + + String value = checkEntryFor(cfile, "Entry-Definitions:"); + if (value == null) { + return false; + //Check if Entry-Defintions pointed file exists in CSAR + } else if (csarFiles.get(value) != null) { + return true; + } + } + } catch (IOException e) { + LOG.error("Could not read file %s ! " + e.getMessage(), cfile); + } + + return false; + } + + private static boolean validateMainService() { + String key = "metadata"; + + // Infuture load from the respective file template/schema + List 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 tListMetadata = Arrays.asList("vendor", "csarVersion", + "csarProvider","id", "version", "csarType", "name", "vnfdVersion", + "vnfmType"); + boolean tResult = checkEntryFor(CommonConstants.MAINSERV_TEMPLATE, tListMetadata, key); + + if (tResult && mfResult) { + return true; + } + else { + return false; + } + } + + private static String checkEntryFor(String fileWithPath, String attribute) throws IOException { + + List lines = Files.readAllLines(Paths.get(fileWithPath), UTF_8); + + for(String strLine : lines) { + if (!attribute.isEmpty() && strLine.contains(attribute)) { + return strLine.substring(attribute.length(), strLine.length()).trim(); + } + } + return null; + } + + private static boolean checkEntryFor(String cFile, List attributes, String key) { + String tFileWithPath = csarFiles.get(cFile); + + Yaml yaml = new Yaml(); + Map values = null; + try { + values = (Map) yaml.load(new FileInputStream(new File(tFileWithPath))); + } catch (FileNotFoundException e) { + return false; + } + + Map subValues = (Map) values.get(key); + + //1. Check for empty values in map and if number of mandatory attributes presence + Map mResult = subValues.entrySet() + .stream() + .filter(e -> e.getValue() != null) + .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())); + if (mResult.size() != attributes.size()) + { + return false; + } + + //2. Validate the exact mandatory attributes with expected attributes list + List lResult = subValues.values().stream() + .filter(attributes::contains) + .collect(Collectors.toList()); + + // System.out.println(result); + if (lResult.size() != attributes.size()) { + return false; + } + return true; + + } + + public static HashMap> getCsar() { + return csar; + } + + public static void setCsar(HashMap> csar) { + CsarValidator.csar = csar; + } + + public static HashMap getCsarFiles() { + return csarFiles; + } + + public static void setCsarFiles(HashMap csarFiles) { + CsarValidator.csarFiles = csarFiles; + } +} + diff --git a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarParserTest.java b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarParserTest.java deleted file mode 100644 index ce9ba5b..0000000 --- a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarParserTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright 2017 Huawei Technologies Co., Ltd. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onap.validation.csarvalidationtest; - -import org.junit.Test; -import org.onap.validation.csar.CsarParser; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class CsarParserTest { - - String regex = "^\\/[a-zA-Z]\\:\\/"; - ClassLoader classLoader = getClass().getClassLoader(); - Pattern pattern = Pattern.compile(regex); - private String configFile = classLoader.getResource("enterprise2DC.csar").getFile(); - Matcher matcher = pattern.matcher(configFile); - String dir2 = "/"+configFile.substring(1); - - CsarParser csarParser = new CsarParser(dir2); - - @Test - public void testValidateCsarMeta() { - boolean result = CsarParser.validateCsarMeta(); - assertEquals(true, result == true); - System.out.println("inside testValidateCsarMeta : " + result); - } - - @Test - public void testValidateCsarIntegrity() { - boolean result = csarParser.validateCsarIntegrity(dir2); - assertEquals(true, result == true); - System.out.println("inside testValidateCsarIntegrity : " + result); - } - - @Test - public void testValidateToscaMeta() { - boolean result = csarParser.validateToscaMeta(); - assertEquals(true, result == true); - System.out.println("inside testValidateToscaMeta : " + result); - } - -} - - - diff --git a/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java new file mode 100644 index 0000000..46d7beb --- /dev/null +++ b/csarvalidation/src/test/java/org/onap/validation/csarvalidationtest/CsarValidatorTest.java @@ -0,0 +1,64 @@ +/** + * Copyright 2017 Huawei Technologies Co., Ltd. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.validation.csarvalidationtest; + +import org.junit.Test; +import org.onap.validation.csar.CsarValidator; + +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class CsarValidatorTest { + + String regex = "^\\/[a-zA-Z]\\:\\/"; + ClassLoader classLoader = getClass().getClassLoader(); + 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 packageId = UUID.randomUUID().toString(); + + CsarValidator csarValidator = new CsarValidator(packageId, dir2); + + @Test + public void testValidateCsarMeta() { + boolean result = CsarValidator.validateCsarMeta(); + assertEquals(true, result == true); + System.out.println("inside testValidateCsarMeta : " + result); + } + + @Test + public void testValidateCsarIntegrity() { + boolean result = csarValidator.validateCsarIntegrity(dir2); + assertEquals(true, result == true); + System.out.println("inside testValidateCsarIntegrity : " + result); + } + + @Test + public void testValidateToscaMeta() { + boolean result = csarValidator.validateToscaMeta(); + assertEquals(true, result == true); + System.out.println("inside testValidateToscaMeta : " + result); + } + +} + + + -- cgit 1.2.3-korg