[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-common-lib / src / main / java / org / openecomp / sdc / common / utils / CommonUtil.java
index 8ffddc4..07322c6 100644 (file)
 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);
+  }
 }