diff options
author | Avi Ziv <avi.ziv@amdocs.com> | 2017-07-18 19:45:38 +0300 |
---|---|---|
committer | Avi Ziv <avi.ziv@amdocs.com> | 2017-07-18 19:45:38 +0300 |
commit | b8e2faf476202b6ffe61bc3a9a37df1304881d40 (patch) | |
tree | f78b8c0517d8e16c5ae610bf8b49f68ea8a312a1 /openecomp-be/lib/openecomp-common-lib/src | |
parent | 75aacbbe1acf78fa53378f07f0a8c7769449a17e (diff) |
[SDC] Onboarding 1710 rebase.
Change-Id: If3b6b81d221fde13908f1e8160db6f7d9433c535
Signed-off-by: Avi Ziv <avi.ziv@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-common-lib/src')
2 files changed, 48 insertions, 16 deletions
diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java index f850afbab8..9fff4bd749 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java @@ -32,6 +32,9 @@ public enum Messages { ZIP_NOT_EXIST("Zip file doesn't exist"), ZIP_SHOULD_NOT_CONTAIN_FOLDERS("Zip file should not contain folders"), + VES_ZIP_SHOULD_CONTAIN_YML_ONLY( + "Wrong VES EVENT Artifact was uploaded - all files contained in Artifact must be YAML files" + + " (using .yaml/.yml extensions)"), MANIFEST_NOT_EXIST("Manifest doesn't exist"), FILE_TYPE_NOT_LEGAL("File type not legal as data for other file"), MODULE_IN_MANIFEST_NO_YAML("Module '%s', has no yaml file reference"), @@ -47,6 +50,10 @@ public enum Messages { CANDIDATE_PROCESS_FAILED("Candidate zip file process failed"), FOUND_UNASSIGNED_FILES("cannot process zip since it has unassigned files"), + /* Monitor uploads related errors*/ + ILLEGAL_MONITORING_ARTIFACT_TYPE("Illegal monitoring artifact type for component id %s, vsp id " + + "%s"), + /* manifest errors*/ MISSING_FILE_IN_ZIP("Missing file in zip"), diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java index 8ffddc48d7..07322c626b 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/utils/CommonUtil.java @@ -21,37 +21,44 @@ package org.openecomp.sdc.common.utils; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.io.FilenameUtils; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCategory; import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.logging.types.LoggerConstants; import org.openecomp.sdc.logging.types.LoggerErrorDescription; -import org.openecomp.sdc.common.errors.Messages; import org.slf4j.MDC; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class CommonUtil { - public static String getMethodName() { - return Thread.currentThread().getStackTrace()[2].getMethodName(); + public static FileContentHandler validateAndUploadFileContent(byte[] uploadedFileData) + throws IOException { + return getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders(uploadedFileData); } - public static FileContentHandler loadUploadFileContent(byte[] uploadedFileData) - throws IOException { - return getFileContentMapFromOrchestrationCandidateZip(uploadedFileData); - } - - public static FileContentHandler getFileContentMapFromOrchestrationCandidateZip(byte[] uploadFileData) - throws IOException { + /** + * Gets files out of the zip AND validates zip is flat (no folders) + * + * @param uploadFileData zip file + * @return FileContentHandler if input is valid and has no folders + */ + private static FileContentHandler getFileContentMapFromOrchestrationCandidateZipAndValidateNoFolders( + byte[] uploadFileData) + throws IOException { ZipEntry zipEntry; List<String> folderList = new ArrayList<>(); FileContentHandler mapFileContent = new FileContentHandler(); @@ -73,22 +80,25 @@ public class CommonUtil { } else { mapFileContent.addFile(currentEntryName, fileByteContent); } - } } catch (RuntimeException exception) { throw new IOException(exception); } + validateNoFolders(folderList); + + return mapFileContent; + } + + private static void validateNoFolders(List<String> folderList) { if (CollectionUtils.isNotEmpty(folderList)) { MDC.put(LoggerConstants.ERROR_DESCRIPTION, LoggerErrorDescription.INVALID_ZIP); throw new CoreException((new ErrorCode.ErrorCodeBuilder()) - .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()) - .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()) - .withCategory(ErrorCategory.APPLICATION).build()); + .withMessage(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()) + .withId(Messages.ZIP_SHOULD_NOT_CONTAIN_FOLDERS.getErrorMessage()) + .withCategory(ErrorCategory.APPLICATION).build()); } - - return mapFileContent; } private static int lastIndexFileSeparatorIndex(String filePath) { @@ -103,4 +113,19 @@ public class CommonUtil { // if we've reached to the start of the string and didn't find file separator - return -1 return -1; } + + public static boolean validateFilesExtensions(Set<String> allowedExtensions, FileContentHandler + files) { + for (String fileName : files.getFileList()) { + if (!allowedExtensions.contains(FilenameUtils.getExtension(fileName))) { + return false; + } + } + return true; + } + + public static boolean validateAllFilesYml(FileContentHandler files) { + Set<String> allowedExtensions = new HashSet<>(Arrays.asList("yml", "yaml")); + return validateFilesExtensions(allowedExtensions, files); + } } |