diff options
author | AvinashS <avinash.s@huawei.com> | 2017-09-07 13:46:54 +0000 |
---|---|---|
committer | Avinash S <avinash.s@huawei.com> | 2017-09-07 14:07:16 +0000 |
commit | 3c4d26e6442babc0b09bc74082bd68108484d275 (patch) | |
tree | c1eea9c391915ee7593025065239887920a41658 /csarvalidation | |
parent | f66e77c8571f851cdd31428809710cff4020b503 (diff) |
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 <avinash.s@huawei.com>
Diffstat (limited to 'csarvalidation')
5 files changed, 812 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved. + 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. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <!-- <parent> <groupId>org.onap.csarvalidation</groupId> <artifactId>csarvalidation</artifactId> + <version>1.0.0-SNAPSHOT</version> </parent> --> + + <parent> + <groupId>org.onap.oparent</groupId> + <artifactId>oparent</artifactId> + <version>1.0.0-SNAPSHOT</version> + <relativePath>oparent</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <groupId>org.onap.vnfsdk.validation</groupId> + <artifactId>validation-csar</artifactId> + <version>1.0.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>csarvalidation/validation</name> + + <repositories> + <!-- <repository> <id>open-o</id> <name>OPEN-O repository</name> <url>https://nexus.open-o.org/content/repositories/public/</url> + </repository> --> + <repository> + <id>google-collections</id> + <name>Google collections repository</name> + <url>https://mvnrepository.com/artifact/com.google.collections/google-collections</url> + </repository> + <repository> + <id>onap</id> + <name>ONAP repository</name> + <url>https://nexus.onap.org/content/repositories/public/</url> + </repository> + </repositories> + + + <dependencies> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.6.4</version> + </dependency> + <dependency> + <groupId>com.eclipsesource.jaxrs</groupId> + <artifactId>consumer</artifactId> + <exclusions> + <exclusion> + <groupId>com.eclipsesource.jaxrs</groupId> + <artifactId>jersey-all</artifactId> + </exclusion> + </exclusions> + <version>5.0</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-core</artifactId> + <version>2.5.4</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.5.4</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-annotations</artifactId> + <version>2.5.4</version> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.2.4</version> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>11.0.2</version> + </dependency> + <dependency> + <groupId>com.google.collections</groupId> + <artifactId>google-collections</artifactId> + <version>1.0-rc1</version> + </dependency> + </dependencies> + + + <profiles> + <profile> + <id>all-tests</id> + <properties> + <build.profile.id>all-tests</build.profile.id> + <skip.unit.tests>true</skip.unit.tests> + </properties> + </profile> + </profiles> + + + <build> + <finalName>${project.artifactId}-${project.version}</finalName> + + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>3.0.2</version> + <configuration> + <archive> + <manifest> + <addDefaultImplementationEntries>true</addDefaultImplementationEntries> + <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> + </manifest> + </archive> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + </plugin> + + <plugin> + <artifactId>maven-war-plugin</artifactId> + <configuration> + <failOnMissingWebXml>false</failOnMissingWebXml> + </configuration> + </plugin> + </plugins> + </build> +</project> 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<String> 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<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()) { + 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<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 + */ + 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 <T> Object readJsonDatafFromFile(String fileAbsPath, Class<T> 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(); + } +} |