From 3c4d26e6442babc0b09bc74082bd68108484d275 Mon Sep 17 00:00:00 2001 From: AvinashS Date: Thu, 7 Sep 2017 13:46:54 +0000 Subject: VNF Validation seed code Skeleton structure Reorganised code with tool as a jar for csar validation as an initial step. Change-Id: Ie5895c885872fb186f2acd8c8bb0f823210e906f IssueId: VNFSDK-76 Signed-off-by: AvinashS --- csarvalidation/pom.xml | 144 ++++++++++ .../org/onap/validation/csar/CommonConstants.java | 126 +++++++++ .../java/org/onap/validation/csar/CsarParser.java | 107 ++++++++ .../java/org/onap/validation/csar/CsarUtil.java | 137 ++++++++++ .../java/org/onap/validation/csar/FileUtil.java | 298 +++++++++++++++++++++ 5 files changed, 812 insertions(+) create mode 100644 csarvalidation/pom.xml create mode 100644 csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java create mode 100644 csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java (limited to 'csarvalidation') diff --git a/csarvalidation/pom.xml b/csarvalidation/pom.xml new file mode 100644 index 0000000..e606f31 --- /dev/null +++ b/csarvalidation/pom.xml @@ -0,0 +1,144 @@ + + + + + + + org.onap.oparent + oparent + 1.0.0-SNAPSHOT + oparent + + 4.0.0 + org.onap.vnfsdk.validation + validation-csar + 1.0.0-SNAPSHOT + jar + csarvalidation/validation + + + + + google-collections + Google collections repository + https://mvnrepository.com/artifact/com.google.collections/google-collections + + + onap + ONAP repository + https://nexus.onap.org/content/repositories/public/ + + + + + + + org.slf4j + slf4j-api + 1.6.4 + + + com.eclipsesource.jaxrs + consumer + + + com.eclipsesource.jaxrs + jersey-all + + + 5.0 + + + com.fasterxml.jackson.core + jackson-core + 2.5.4 + + + com.fasterxml.jackson.core + jackson-databind + 2.5.4 + + + com.fasterxml.jackson.core + jackson-annotations + 2.5.4 + + + com.google.code.gson + gson + 2.2.4 + + + com.google.guava + guava + 11.0.2 + + + com.google.collections + google-collections + 1.0-rc1 + + + + + + + all-tests + + all-tests + true + + + + + + + ${project.artifactId}-${project.version} + + + + org.apache.maven.plugins + maven-jar-plugin + 3.0.2 + + + + true + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-jar-plugin + + + + maven-war-plugin + + false + + + + + diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java b/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java new file mode 100644 index 0000000..b36bb62 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CommonConstants.java @@ -0,0 +1,126 @@ +/** + * 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; + +public class CommonConstants { + // Package Status + public static final String PACKAGE_STATUS_DELETING = "deleting"; + + public static final String PACKAGE_STATUS_DELETE_FAIL = "deleteFailed"; + + public static final String PACKAGE_XML_FORMAT = "xml"; + + public static final String PACKAGE_YAML_FORMAT = "yaml"; + + // 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"; + + public static final String CSAR_PROVIDER_META = "provider"; + + public static final String DEFINITIONS = "Definitions"; + + public static final String CSAR_META = "csar.meta"; + + public static final String CSAR_SUFFIX = ".csar"; + + public static final String HTTP_HEADER_CONTENT_RANGE = "Content-Range"; + + public static final String CATALOG_CSAR_DIR_NAME = "/csar"; + + public static final String REPORT_CSAR_DIR_NAME = "/reports"; + + public static final String COMETD_CHANNEL_PACKAGE_DELETE = "/package/delete"; + + public static final String SUCCESS_STR = "SUCCESS"; + + public static final int ONBOARDING_THREAD_COUNT = 1; + public static final int SUCESS = 0; + public static final int FAILED = -1; + + public static final String CATALOUGE_UPLOAD_URL = "/openoapi/catalog/v1/csars"; + + public static final int BUFFER_SIZE = 2 * 1024 * 1024; + + private CommonConstants() { + // Cannot create instance of the class + } + + public static class functionTest { + public static final String FUNCTEST_URL = "/openoapi/vnfsdk/v1/functest/"; + public static final String FUNCTEST_RESULT_URL = "/openoapi/vnfsdk/v1/functest/download/"; + public static final String FUNCTEST_OPERTYPE_ID = "functiontest"; + public static final String FUNCTEST_PACKAGE_EXISTS = "packageExists"; + public static final String FUNCTEST_EXEC = "functestexec"; + + private functionTest() { + } + } + + public static class LifeCycleTest { + public static final String LIFECYCLE_TEST_URL = "/openoapi/nslcm/v1/vnfpackage"; + public static final String LIFECYCLE_TEST_OPERTYPE_ID = "lifecycletest"; + public static final String LIFECYCLE_TEST_EXEC = "lifecycleTestexec"; + + private LifeCycleTest() { + } + } + + public static class HttpContext { + + public static final String CONTENT_TYPE = "Content-Type"; + + public static final String MEDIA_TYPE_JSON = "application/json;charset=UTF-8"; + + public static final String URL = "url"; + + public static final String METHOD_TYPE = "methodType"; + + private HttpContext() { + } + } + + public static class MethodType { + + public static final String POST = "post"; + + public static final String DELETE = "delete"; + + public static final String PUT = "put"; + + public static final String GET = "get"; + + private MethodType() { + } + } + + public static class MsbRegisterCode { + + public static final int MSDB_REGISTER_RETRIES = 12; + public static final int MSDB_REGISTER_RETRY_SLEEP = 10000; + + public static final int MSDB_REGISTER_FILE_NOT_EXISTS = 2; + public static final int MSDB_REGISTER_SUCESS = 0; + public static final int MSDB_REGISTER_FAILED = -1; + + private MsbRegisterCode() { + } + } +} diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java new file mode 100644 index 0000000..5448268 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarParser.java @@ -0,0 +1,107 @@ +/** + * 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.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +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; + +public class CsarParser { + + private static final Logger LOG = LoggerFactory.getLogger(CsarParser.class); + + + + public static boolean csarExtract(String filePath) { + + try { + String tempfolder = CsarUtil.getUnzipDir(filePath); + CsarUtil.csarFiles = CsarUtil.unzip(filePath, tempfolder); + } catch (IOException e1) { + LOG.error("CSAR extraction error ! " + e1.getMessage()); + return false; + } + return true; + } + + + public static boolean validateCsarMeta() { + + for (String cfile : CsarUtil.csarFiles) { + if (cfile.endsWith(CommonConstants.CSAR_META)) { + 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(); + } catch (IOException e2) { + e2.printStackTrace(); + return false; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + LOG.error("close reader failed ! " + e1.getMessage()); + } + } + } + return true; + } + + } + + return false; + } +} diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java new file mode 100644 index 0000000..53169fb --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java @@ -0,0 +1,137 @@ +/** + * 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.File; +import java.io.FileOutputStream; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Enumeration; + +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + + +public class CsarUtil { + + private static final Logger logger = LoggerFactory.getLogger(CsarUtil.class); + + public static ArrayList csarFiles = null; + + public static String getUnzipDir(String dirName) { + File tmpDir = new File(File.separator + dirName); + return tmpDir.getAbsolutePath().replace(".csar", ""); + } + + /** + * unzip zip file. + * + * @param zipFileName + * file name to zip + * @param extPlace + * extPlace + * @return unzip file name + * @throws IOException + * e1 + */ + public static ArrayList unzip(String zipFileName, String extPlace) throws IOException { + ZipFile zipFile = null; + ArrayList unzipFileNams = new ArrayList(); + + 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()) { + FileUtil.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 { + FileUtil.closeZipFile(zipFile); + } + return unzipFileNams; + } + + /** + * close InputStream. + * + * @param inputStream + * the inputstream to close + */ + public static void closeInputStream(InputStream inputStream) { + try { + if (inputStream != null) { + inputStream.close(); + } + } catch (Exception e1) { + logger.info("close InputStream error!"); + } + } + + /** + * close OutputStream. + * + * @param outputStream + * the output stream to close + */ + public static void closeOutputStream(OutputStream outputStream) { + try { + if (outputStream != null) { + outputStream.close(); + } + } catch (Exception e1) { + logger.info("close OutputStream error!"); + } + } + +} diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java b/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java new file mode 100644 index 0000000..3a9d9e8 --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java @@ -0,0 +1,298 @@ +/** + * 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 com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.io.Resources; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + + +public final class FileUtil { + + public static final Logger logger = LoggerFactory.getLogger(FileUtil.class); + + private static final int TRY_COUNT = 3; + + private FileUtil() { + + } + + + /** + * create dir. + * @param dir dir to create + * @return boolean + */ + public static boolean createDirectory(String dir) { + File folder = new File(dir); + int tryCount = 0; + while (tryCount < TRY_COUNT) { + tryCount++; + if (!folder.exists() && !folder.mkdirs()) { + continue; + } else { + return true; + } + } + + return folder.exists(); + } + + /** + * delete file. + * @param file the file to delete + * @return boolean + */ + public static boolean deleteFile(File file) { + String hintInfo = file.isDirectory() ? "dir " : "file "; + boolean isFileDeleted = file.delete(); + boolean isFileExist = file.exists(); + if (!isFileExist) { + if (isFileDeleted) { + logger.info("delete " + hintInfo + file.getAbsolutePath()); + } else { + isFileDeleted = true; + logger.info("file not exist. no need delete " + hintInfo + file.getAbsolutePath()); + } + } else { + logger.info("fail to delete " + hintInfo + file.getAbsolutePath()); + } + return isFileDeleted; + } + + + /** + * unzip zip file. + * @param zipFileName file name to zip + * @param extPlace extPlace + * @return unzip file name + * @throws IOException e1 + */ + public static ArrayList unzip(String zipFileName, String extPlace) throws IOException { + ZipFile zipFile = null; + ArrayList unzipFileNams = new ArrayList(); + + 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 + */ + public static void closeInputStream(InputStream inputStream) { + try { + if (inputStream != null) { + inputStream.close(); + } + } catch (Exception e1) { + logger.info("close InputStream error!"); + } + } + + /** + * close OutputStream. + * + * @param outputStream the output stream to close + */ + public static void closeOutputStream(OutputStream outputStream) { + try { + if (outputStream != null) { + outputStream.close(); + } + } catch (Exception e1) { + logger.info("close OutputStream error!"); + } + } + + public static void closeFileStream(FileInputStream ifs) { + try { + if (ifs != null) { + ifs.close(); + } + } catch (Exception e1) { + logger.info("close OutputStream error!"); + } + } + + /** + * close zipFile. + * + * @param zipFile the zipFile to close + */ + public static void closeZipFile(ZipFile zipFile) { + try { + if (zipFile != null) { + zipFile.close(); + zipFile = null; + } + } catch (IOException e1) { + logger.info("close ZipFile error!"); + } + } + + public static boolean checkFileExists(String filePath) + { + File file = new File(filePath); + return file.exists(); + } + + public static boolean deleteFile(String filePath) + { + File file = new File(filePath); + return deleteFile(file); + } + + public static boolean writeJsonDatatoFile(String fileAbsPath, Object obj) + { + logger.info("Write JsonData to file :"+fileAbsPath); + + boolean bResult = false; + if(checkFileExists(fileAbsPath)) + { + deleteFile(fileAbsPath); + } + + ObjectMapper mapper = new ObjectMapper(); + try + { + mapper.writeValue(new File(fileAbsPath), obj); + bResult = true; + } + catch (JsonGenerationException e) + { + logger.info("JsonGenerationException Exception: writeJsonDatatoFile-->"+fileAbsPath); + } + catch (JsonMappingException e) + { + logger.info("JsonMappingException Exception: writeJsonDatatoFile-->"+fileAbsPath); + } + catch (IOException e) + { + logger.info("IOException Exception: writeJsonDatatoFile-->"+fileAbsPath); + } + return bResult; + } + + public static Object readJsonDatafFromFile(String fileAbsPath, Class clazz) + { + if(!checkFileExists(fileAbsPath)) + { + logger.info("read JsonData from file , file not found :"+fileAbsPath); + return null; + } + + logger.info("read JsonData from file :"+fileAbsPath); + + T obj = null; + ObjectMapper mapper = new ObjectMapper(); + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + try + { + obj = mapper.readValue(new File(fileAbsPath), clazz); + } + catch (JsonParseException e1) + { + logger.info("JsonParseException Exception: writeJsonDatatoFile-->"+fileAbsPath); + } + catch (JsonMappingException e1) + { + logger.info("JsonMappingException Exception: writeJsonDatatoFile-->"+fileAbsPath); + } + catch (IOException e1) + { + logger.info("IOException Exception: writeJsonDatatoFile-->"+fileAbsPath); + } + return obj; + } + + public static boolean deleteDirectory(String path) + { + File file = new File(path); + return deleteDirectory(file); + } + + public static boolean deleteDirectory(File file) + { + if (!file.exists()) + { + return true; + } + if (file.isDirectory()) + { + for (File f : file.listFiles()) + { + deleteDirectory(f); + } + } + return file.delete(); + } +} -- cgit 1.2.3-korg