From f745139f2baa56bce2d0b0ce05810a1061ff3268 Mon Sep 17 00:00:00 2001 From: AvinashS Date: Tue, 3 Oct 2017 18:13:29 +0530 Subject: Add exception handling Error codes and exception handling. Change-Id: Ia44a8ed2f206ff5fdca3f029f2ba043af769366a IssueId: VNFSDK-104 Signed-off-by: AvinashS --- .../java/org/onap/validation/csar/CsarUtil.java | 14 +++- .../org/onap/validation/csar/CsarValidator.java | 13 ++-- .../java/org/onap/validation/csar/ErrorCodes.java | 49 ++++++++++++ .../java/org/onap/validation/csar/FileUtil.java | 32 ++++++-- .../onap/validation/csar/ValidationException.java | 87 ++++++++++++++++++---- 5 files changed, 162 insertions(+), 33 deletions(-) create mode 100644 csarvalidation/src/main/java/org/onap/validation/csar/ErrorCodes.java (limited to 'csarvalidation/src') 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 745d7cc..dbead03 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarUtil.java @@ -57,6 +57,7 @@ public class CsarUtil { * @return unzip file names in zip * @throws IOException * e1 + * @throws ValidationException */ public static HashMap unzip(String zipFileName, String extPlace) throws IOException { HashMap unzipFileNames = new HashMap<>(); @@ -104,6 +105,7 @@ public class CsarUtil { * * @param inputStream * the inputstream to close + * @throws ValidationException */ public static void closeInputStream(InputStream inputStream) { try { @@ -111,7 +113,8 @@ public class CsarUtil { inputStream.close(); } } catch (Exception e1) { - logger.error("FILE_IO" + ":" + "close InputStream error! " + e1.getMessage(), e1); + logger.error("FILE_IO" + ":" + "close InputStream error! " +ErrorCodes.FILE_IO+" "+ e1.getMessage(), e1); + throw new ValidationException(ErrorCodes.FILE_IO); } } @@ -120,6 +123,7 @@ public class CsarUtil { * * @param outputStream * the output stream to close + * @throws ValidationException */ public static void closeOutputStream(OutputStream outputStream) { try { @@ -127,7 +131,9 @@ public class CsarUtil { outputStream.close(); } } catch (Exception e1) { - logger.error("FILE_IO" + ":" + "close OutputStream error! " + e1.getMessage(), e1); + logger.error("FILE_IO" + ":" + "close OutputStream error! " +ErrorCodes.FILE_IO); + throw new ValidationException(ErrorCodes.FILE_IO); + } } @@ -138,9 +144,9 @@ public class CsarUtil { return CsarUtil.unzip(filePath, tempfolder); } catch (IOException e1) { - logger.error("CSAR_EXTRACTION" + ":" + "CSAR extraction error ! " + e1.getMessage(), e1); + logger.error("CSAR_EXTRACTION" + ":" + "CSAR extraction error ! " +ErrorCodes.FILE_IO+" "+ e1.getMessage(), e1); + throw new ValidationException(ErrorCodes.FILE_IO); } - 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 index 14aa6a4..46200a7 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/CsarValidator.java @@ -35,16 +35,15 @@ public class CsarValidator { static HashMap csarFiles; // Map of packageId and CSAR files - private static HashMap> csar = new HashMap>(); - - private static final CsarUtil cUtil = new CsarUtil(); + private static HashMap> csar = new HashMap>(); public CsarValidator(String packageId, String csarWithPath) { try { FileInputStream is = new FileInputStream(csarWithPath); } catch (FileNotFoundException e2) { - LOG.error("CSAR %s is not found! ", e2); + LOG.error("CSAR %s is not found! " +ErrorCodes.RESOURCE_MISSING); + throw new ValidationException(ErrorCodes.RESOURCE_MISSING); } try { csarFiles = CsarUtil.csarExtract(csarWithPath); @@ -135,7 +134,8 @@ public class CsarValidator { return true; } } catch (IOException e2) { - LOG.error("CSAR_META_VALIDATION" + ":" + "Exception caught while validateCsarMeta ! " + e2.getMessage(), e2); + LOG.error("CSAR_META_VALIDATION" + ":" + "Exception caught while validateCsarMeta ! " +ErrorCodes.FILE_IO); + throw new ValidationException(ErrorCodes.FILE_IO); } } @@ -168,7 +168,8 @@ public class CsarValidator { } } } catch (IOException | NullPointerException e) { - LOG.error("CSAR_TOSCA_VALIDATION" + ":" + "Could not read file %s ! " + e.getMessage(), e, cfile); + LOG.error("CSAR_TOSCA_VALIDATION" + ":" + "Could not read file %s ! " +ErrorCodes.FILE_IO+ " " +ErrorCodes.RESOURCE_MISSING); + throw new ValidationException(ErrorCodes.RESOURCE_MISSING); } return false; } diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/ErrorCodes.java b/csarvalidation/src/main/java/org/onap/validation/csar/ErrorCodes.java new file mode 100644 index 0000000..162f61d --- /dev/null +++ b/csarvalidation/src/main/java/org/onap/validation/csar/ErrorCodes.java @@ -0,0 +1,49 @@ +/** + * 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 enum ErrorCodes { + + VALUE_REQUIRED(201), + INVALID_FORMAT(202), + VALUE_TOO_SHORT(203), + VALUE_TOO_LONGS(204), + VALIDATION_FAILED(205), + INVALID_CSAR(206), + INVALID_CSAR_CONTENT(207), + INVALID_CSAR_TOSCA_CONTENT(208), + INVALID_CSAR_META_CONTENT(209), + FILE_IO(210), + DIR_IO(211), + ZIP_IO(212), + RESOURCE_MISSING(213), + PARSE_ERROR(214), + JSON_MAPPING_FAILED(215), + JSON_GENERATION_ERROR(216); + + private final int number; + + private ErrorCodes(int number) { + this.number = number; + } + + + public int getNumber() { + return number; + } + + } + 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 3faaa2a..c1b10fa 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/FileUtil.java @@ -87,6 +87,7 @@ public final class FileUtil { } } else { logger.info("fail to delete " + hintInfo + file.getAbsolutePath()); + } return isFileDeleted; } @@ -96,6 +97,7 @@ public final class FileUtil { * close InputStream. * * @param inputStream the inputstream to close + * @throws ValidationException */ public static void closeInputStream(InputStream inputStream) { try { @@ -103,7 +105,8 @@ public final class FileUtil { inputStream.close(); } } catch (Exception e1) { - logger.error("FILE_IO" + ":" + "close InputStream error! " + e1.getMessage(), e1); + logger.error("FILE_IO" + ":" + "close InputStream error! "+ErrorCodes.FILE_IO+ " " + e1.getMessage(), e1); + throw new ValidationException(ErrorCodes.FILE_IO); } } @@ -111,6 +114,7 @@ public final class FileUtil { * close OutputStream. * * @param outputStream the output stream to close + * @throws ValidationException */ public static void closeOutputStream(OutputStream outputStream) { try { @@ -118,7 +122,8 @@ public final class FileUtil { outputStream.close(); } } catch (Exception e1) { - logger.error("FILE_IO" + ":" + "close OutputStream error! " + e1.getMessage(), e1); + logger.error("FILE_IO" + ":" + "close OutputStream error! "+ErrorCodes.FILE_IO+ " " + e1.getMessage(), e1); + throw new ValidationException(ErrorCodes.FILE_IO); } } @@ -128,7 +133,8 @@ public final class FileUtil { ifs.close(); } } catch (Exception e1) { - logger.error("FILE_IO" + ":" + "close OutputStream error! " + e1.getMessage(), e1); + logger.error("FILE_IO" + ":" + "close OutputStream error! "+ErrorCodes.FILE_IO+ " " + e1.getMessage(), e1); + throw new ValidationException(ErrorCodes.FILE_IO); } } @@ -136,6 +142,7 @@ public final class FileUtil { * close zipFile. * * @param zipFile the zipFile to close + * @throws ValidationException */ public static void closeZipFile(ZipFile zipFile) { try { @@ -143,7 +150,8 @@ public final class FileUtil { zipFile.close(); } } catch (IOException e1) { - logger.error("CLOSE_ZIPFILE" + ":" + "close ZipFile error! " + e1.getMessage(), e1); + logger.error("CLOSE_ZIPFILE" + ":" + "close ZipFile error! "+ErrorCodes.FILE_IO+ " " + e1.getMessage(), e1); + throw new ValidationException(ErrorCodes.FILE_IO); } } @@ -177,16 +185,18 @@ public final class FileUtil { } catch (JsonGenerationException e) { - logger.error("JSON_GENERATION" + ":" + "JsonGenerationException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " + e.getMessage(), e); - + logger.error("JSON_GENERATION" + ":" + "JsonGenerationException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " +ErrorCodes.JSON_GENERATION_ERROR+" " + e.getMessage(), e); + throw new ValidationException(ErrorCodes.JSON_GENERATION_ERROR); } catch (JsonMappingException e) { - logger.error("JSON_MAPPING" + ":" + "JsonMappingException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " + e.getMessage(), e); + logger.error("JSON_MAPPING" + ":" + "JsonMappingException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " +ErrorCodes.JSON_MAPPING_FAILED+" " + e.getMessage(), e); + throw new ValidationException(ErrorCodes.JSON_MAPPING_FAILED); } catch (IOException e) { - logger.error("FILE_IO" + ":" + "IOException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " + e.getMessage(), e); + logger.error("FILE_IO" + ":" + "IOException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " +ErrorCodes.FILE_IO+" " + e.getMessage(), e); + throw new ValidationException(ErrorCodes.FILE_IO); } return bResult; } @@ -211,14 +221,20 @@ public final class FileUtil { catch (JsonParseException e1) { logger.error("JSON_PARSING" + ":" + "JsonParseException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " + e1.getMessage(), e1); + logger.error("CSAR extraction error ! " +ErrorCodes.PARSE_ERROR); + throw new ValidationException(ErrorCodes.PARSE_ERROR); } catch (JsonMappingException e1) { logger.error("JSON_MAPPING" + ":" + "JsonMappingException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " + e1.getMessage(), e1); + logger.error("CSAR extraction error ! " +ErrorCodes.JSON_MAPPING_FAILED); + throw new ValidationException(ErrorCodes.JSON_MAPPING_FAILED); } catch (IOException e1) { logger.error("FILE_IO" + ":" + "IOException Exception: writeJsonDatatoFile-->"+fileAbsPath+" : " + e1.getMessage(), e1); + logger.error("CSAR extraction error ! " +ErrorCodes.FILE_IO); + throw new ValidationException(ErrorCodes.FILE_IO); } return obj; } diff --git a/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java b/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java index a587f32..804b632 100644 --- a/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java +++ b/csarvalidation/src/main/java/org/onap/validation/csar/ValidationException.java @@ -14,32 +14,89 @@ * limitations under the License. */ package org.onap.validation.csar; -class ValidationException extends Exception{ - private String errCode = "Validation_Exception"; - private String errorMessage = "Error Message"; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.util.Map; +import java.util.TreeMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +public class ValidationException extends RuntimeException { + public static final Logger logger = LoggerFactory.getLogger(ValidationException.class); + private String errorMessage; public ValidationException(){ super(); } - public ValidationException(String message){ + + public ValidationException(ErrorCodes errCode, String message) { super(message); } - - public ValidationException(String message, Throwable cause){ - super(message,cause); + + public String toString(){ + return ("Exception Number = "+errorMessage) ; } + private static final long serialVersionUID = 1L; - public ValidationException(String errCode, String message, Throwable cause){ - super(message,cause); - this.errCode = errCode; + public static ValidationException wrappedInfo(Throwable exception, ErrorCodes errorCode) { + if (exception instanceof ValidationException) { + ValidationException se = (ValidationException)exception; + if (errorCode != null) { + return new ValidationException(exception.getMessage(), exception, errorCode); + } + return se; + } else { + return new ValidationException(exception.getMessage(), exception, errorCode); + } + } + + public static ValidationException wrappedInfo(Throwable exception) { + return wrappedInfo(exception, null); } + + private ErrorCodes errorCode; + private final Map properties = new TreeMap(); + + public ValidationException(ErrorCodes fileIo) { + this.errorCode = fileIo; + } + + public ValidationException(String message, ErrorCodes errorCode) { + super(message); + this.errorCode = errorCode; + } - public String getErrCode() { - return this.errCode; + public ValidationException(Throwable cause, ErrorCodes errorCode) { + super(cause); + this.errorCode = errorCode; + } + + public ValidationException(String message, Throwable cause, ErrorCodes errorCode) { + super(message, cause); + this.errorCode = errorCode; + } + + public ErrorCodes getErrorCode() { + return errorCode; } - public String toString(){ - return ("Exception Number = "+errorMessage) ; + + public ValidationException setErrorCode(ErrorCodes errorCode) { + this.errorCode = errorCode; + return this; + } + + public Map getProperties() { + return properties; + } + + @SuppressWarnings("unchecked") + public T get(String name) { + return (T)properties.get(name); + } + + public ValidationException set(String name, Object value) { + properties.put(name, value); + return this; } - } -- cgit 1.2.3-korg