diff options
author | AvinashS <avinash.s@huawei.com> | 2017-09-14 13:37:28 +0000 |
---|---|---|
committer | Avinash S <avinash.s@huawei.com> | 2017-09-14 14:01:06 +0000 |
commit | 13d2f6721f1ef051cca2c22317363be963271635 (patch) | |
tree | 242ed441c6c61dd085a8bbdd0fef74f3f465a48e /csarvalidation/src/main/java/org | |
parent | 543d805f02b7b2c133e34ea5a481a46e23eff0d3 (diff) |
Add csar.meta and TOSCA.meta validation code.
IssueId: VNFSDK-83
Change-Id: I5bae38239c5dcd3c235396eeb5f8f81da69d298c
Signed-off-by: AvinashS <avinash.s@huawei.com>
Diffstat (limited to 'csarvalidation/src/main/java/org')
4 files changed, 98 insertions, 77 deletions
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 b36bb62..be709ad 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java @@ -27,8 +27,6 @@ public class CommonConstants { // host image progress - public static final String TOSCA_METADATA = "TOSCA-Metadata"; - public static final String CSAR_VERSION_META = "version"; public static final String CSAR_TYPE_META = "type"; @@ -39,6 +37,10 @@ public class CommonConstants { public static final String CSAR_META = "csar.meta"; + public static final String TOSCA_METADATA = "TOSCA-Metadata"; + + public static final String TOSCA_META = "TOSCA.meta"; + public static final String CSAR_SUFFIX = ".csar"; public static final String HTTP_HEADER_CONTENT_RANGE = "Content-Range"; diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java index 5448268..62e8492 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java @@ -18,30 +18,65 @@ package org.onap.validation.csar; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; +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.Date; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Enumeration; -import java.io.File; -import java.io.FileOutputStream; +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<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 final CsarUtil cUtil = new CsarUtil(); + /* + * pubic static boolean validateCsar(String filePath) { + * + * csarExtract(filePath); + * + * validateCsarMeta(); + * + * + * } + */ + + + 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 csarExtract(String filePath) { try { String tempfolder = CsarUtil.getUnzipDir(filePath); - CsarUtil.csarFiles = CsarUtil.unzip(filePath, tempfolder); + csarFiles = CsarUtil.unzip(filePath, tempfolder); } catch (IOException e1) { LOG.error("CSAR extraction error ! " + e1.getMessage()); return false; @@ -49,11 +84,10 @@ public class CsarParser { return true; } - public static boolean validateCsarMeta() { - for (String cfile : CsarUtil.csarFiles) { - if (cfile.endsWith(CommonConstants.CSAR_META)) { + String cfile = csarFiles.get(CommonConstants.CSAR_META); + if (!cfile.isEmpty()) { File file = new File(cfile); BufferedReader reader = null; @@ -82,9 +116,10 @@ public class CsarParser { return false; } } - } + } reader.close(); + } } catch (IOException e2) { e2.printStackTrace(); return false; @@ -96,12 +131,46 @@ public class CsarParser { LOG.error("close reader failed ! " + e1.getMessage()); } } + return true; } - return true; - } - } return false; } + + + public static boolean validateToscaMeta() { + + String cfile = csarFiles.get(CommonConstants.TOSCA_META); + try { + if (!cfile.isEmpty() && cfile.contains("/" + CommonConstants.TOSCA_METADATA + "/" + 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<String> 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()); + } + } + 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 53169fb..3708dbf 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.Enumeration; import java.util.zip.ZipEntry; @@ -40,7 +41,7 @@ public class CsarUtil { private static final Logger logger = LoggerFactory.getLogger(CsarUtil.class); - public static ArrayList<String> csarFiles = null; + public static String getUnzipDir(String dirName) { File tmpDir = new File(File.separator + dirName); @@ -54,13 +55,13 @@ public class CsarUtil { * file name to zip * @param extPlace * extPlace - * @return unzip file name + * @return unzip file names in zip * @throws IOException * e1 */ - public static ArrayList<String> unzip(String zipFileName, String extPlace) throws IOException { + public static HashMap<String, String> unzip(String zipFileName, String extPlace) throws IOException { ZipFile zipFile = null; - ArrayList<String> unzipFileNams = new ArrayList<String>(); + HashMap<String, String> unzipFileNames = new HashMap<String, String>(); try { zipFile = new ZipFile(zipFileName); @@ -90,7 +91,7 @@ public class CsarUtil { } bos.write(buffer, 0, length); } - unzipFileNams.add(file.getAbsolutePath()); + unzipFileNames.put(file.getName(), file.getAbsolutePath()); } finally { closeOutputStream(bos); closeInputStream(input); @@ -99,7 +100,7 @@ public class CsarUtil { } finally { FileUtil.closeZipFile(zipFile); } - return unzipFileNams; + return unzipFileNames; } /** diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java b/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java index 3a9d9e8..82f6ff4 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java @@ -93,57 +93,6 @@ public final class FileUtil { /** - * unzip zip file. - * @param zipFileName file name to zip - * @param extPlace extPlace - * @return unzip file name - * @throws IOException e1 - */ - public static ArrayList<String> unzip(String zipFileName, String extPlace) throws IOException { - ZipFile zipFile = null; - ArrayList<String> unzipFileNams = new ArrayList<String>(); - - try { - zipFile = new ZipFile(zipFileName); - Enumeration<?> fileEn = zipFile.entries(); - byte[] buffer = new byte[CommonConstants.BUFFER_SIZE]; - - while (fileEn.hasMoreElements()) { - InputStream input = null; - BufferedOutputStream bos = null; - try { - ZipEntry entry = (ZipEntry) fileEn.nextElement(); - if (entry.isDirectory()) { - continue; - } - - input = zipFile.getInputStream(entry); - File file = new File(extPlace, entry.getName()); - if (!file.getParentFile().exists()) { - createDirectory(file.getParentFile().getAbsolutePath()); - } - - bos = new BufferedOutputStream(new FileOutputStream(file)); - while (true) { - int length = input.read(buffer); - if (length == -1) { - break; - } - bos.write(buffer, 0, length); - } - unzipFileNams.add(file.getAbsolutePath()); - } finally { - closeOutputStream(bos); - closeInputStream(input); - } - } - } finally { - closeZipFile(zipFile); - } - return unzipFileNams; - } - - /** * close InputStream. * * @param inputStream the inputstream to close |