summaryrefslogtreecommitdiffstats
path: root/cps-rest/src/main
diff options
context:
space:
mode:
authorRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-01-19 17:57:00 +0200
committerRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-01-21 09:41:37 +0200
commit2006f67db860e755b599acda36cfc85a49d8c8f3 (patch)
tree0e548b2124df1d7f1c8121f3d6375c3eba2faaf8 /cps-rest/src/main
parentdce4e316da66ede214977491c712b0a4ab457644 (diff)
ZIP archive support for multiple YANG files delivery on Schema Set creation using REST
Issue-ID: CPS-180 Change-Id: I7e78a595593b170b981746e9aed1a7e5a45b202a Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-rest/src/main')
-rw-r--r--cps-rest/src/main/java/org/onap/cps/rest/utils/MultipartFileUtil.java88
1 files changed, 79 insertions, 9 deletions
diff --git a/cps-rest/src/main/java/org/onap/cps/rest/utils/MultipartFileUtil.java b/cps-rest/src/main/java/org/onap/cps/rest/utils/MultipartFileUtil.java
index c53d1a42a6..532a0ca848 100644
--- a/cps-rest/src/main/java/org/onap/cps/rest/utils/MultipartFileUtil.java
+++ b/cps-rest/src/main/java/org/onap/cps/rest/utils/MultipartFileUtil.java
@@ -19,13 +19,18 @@
package org.onap.cps.rest.utils;
-import static com.google.common.base.Preconditions.checkNotNull;
import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION;
import com.google.common.collect.ImmutableMap;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Locale;
import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.onap.cps.spi.exceptions.CpsException;
@@ -35,26 +40,81 @@ import org.springframework.web.multipart.MultipartFile;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MultipartFileUtil {
+ private static final String ZIP_FILE_EXTENSION = ".zip";
+ private static final String YANG_FILE_EXTENSION = RFC6020_YANG_FILE_EXTENSION;
+ private static final int READ_BUFFER_SIZE = 1024;
+
/**
* Extracts yang resources from multipart file instance.
*
* @param multipartFile the yang file uploaded
* @return yang resources as {map} where the key is original file name, and the value is file content
- * @throws ModelValidationException if the file name extension is not '.yang'
+ * @throws ModelValidationException if the file name extension is not '.yang' or '.zip'
+ * or if zip archive contain no yang files
* @throws CpsException if the file content cannot be read
*/
public static Map<String, String> extractYangResourcesMap(final MultipartFile multipartFile) {
- return ImmutableMap.of(extractYangResourceName(multipartFile), extractYangResourceContent(multipartFile));
+ final String originalFileName = multipartFile.getOriginalFilename();
+ if (resourceNameEndsWithExtension(originalFileName, YANG_FILE_EXTENSION)) {
+ return ImmutableMap.of(originalFileName, extractYangResourceContent(multipartFile));
+ }
+ if (resourceNameEndsWithExtension(originalFileName, ZIP_FILE_EXTENSION)) {
+ return extractYangResourcesMapFromZipArchive(multipartFile);
+ }
+ throw new ModelValidationException("Unsupported file type.",
+ String.format("Filename %s matches none of expected extensions: %s", originalFileName,
+ Arrays.asList(YANG_FILE_EXTENSION, ZIP_FILE_EXTENSION)));
+ }
+
+ private static Map<String, String> extractYangResourcesMapFromZipArchive(final MultipartFile multipartFile) {
+ final ImmutableMap.Builder<String, String> yangResourceMapBuilder = ImmutableMap.builder();
+
+ try (
+ final InputStream inputStream = multipartFile.getInputStream();
+ final ZipInputStream zipInputStream = new ZipInputStream(inputStream);
+ ) {
+ ZipEntry zipEntry;
+ while ((zipEntry = zipInputStream.getNextEntry()) != null) {
+ extractZipEntryToMapIfApplicable(yangResourceMapBuilder, zipEntry, zipInputStream);
+ }
+ zipInputStream.closeEntry();
+
+ } catch (final IOException e) {
+ throw new CpsException("Cannot extract resources from zip archive.", e.getMessage(), e);
+ }
+
+ try {
+ final Map<String, String> yangResourceMap = yangResourceMapBuilder.build();
+ if (yangResourceMap.isEmpty()) {
+ throw new ModelValidationException("Archive contains no YANG resources.",
+ String.format("Archive contains no files having %s extension.", YANG_FILE_EXTENSION));
+ }
+ return yangResourceMap;
+
+ } catch (final IllegalArgumentException e) {
+ throw new ModelValidationException("Invalid ZIP archive content.",
+ "Multiple resources with same name detected.", e);
+ }
}
- private static String extractYangResourceName(final MultipartFile multipartFile) {
- final String fileName = checkNotNull(multipartFile.getOriginalFilename(), "Missing filename.");
- if (!fileName.endsWith(RFC6020_YANG_FILE_EXTENSION)) {
- throw new ModelValidationException("Unsupported file type.",
- String.format("Filename %s does not end with '%s'", fileName, RFC6020_YANG_FILE_EXTENSION));
+ private static void extractZipEntryToMapIfApplicable(
+ final ImmutableMap.Builder<String, String> yangResourceMapBuilder, final ZipEntry zipEntry,
+ final ZipInputStream zipInputStream) throws IOException {
+
+ final String yangResourceName = extractResourceNameFromPath(zipEntry.getName());
+ if (zipEntry.isDirectory() || !resourceNameEndsWithExtension(yangResourceName, YANG_FILE_EXTENSION)) {
+ return;
}
- return fileName;
+ yangResourceMapBuilder.put(yangResourceName, extractYangResourceContent(zipInputStream));
+ }
+
+ private static boolean resourceNameEndsWithExtension(final String resourceName, final String extension) {
+ return resourceName != null && resourceName.toLowerCase(Locale.ENGLISH).endsWith(extension);
+ }
+
+ private static String extractResourceNameFromPath(final String path) {
+ return path == null ? "" : path.replaceAll("^.*[\\\\/]", "");
}
private static String extractYangResourceContent(final MultipartFile multipartFile) {
@@ -65,4 +125,14 @@ public class MultipartFileUtil {
}
}
+ private static String extractYangResourceContent(final ZipInputStream zipInputStream) throws IOException {
+ try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
+ final byte[] buffer = new byte[READ_BUFFER_SIZE];
+ int numberOfBytesRead;
+ while ((numberOfBytesRead = zipInputStream.read(buffer, 0, READ_BUFFER_SIZE)) > 0) {
+ byteArrayOutputStream.write(buffer, 0, numberOfBytesRead);
+ }
+ return byteArrayOutputStream.toString(StandardCharsets.UTF_8);
+ }
+ }
}