2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * Modifications Copyright (C) 2021 Bell Canada.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.rest.utils;
23 import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION;
25 import com.google.common.collect.ImmutableMap;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.nio.charset.StandardCharsets;
29 import java.util.Arrays;
30 import java.util.Locale;
32 import java.util.zip.ZipEntry;
33 import java.util.zip.ZipInputStream;
34 import lombok.AccessLevel;
35 import lombok.NoArgsConstructor;
36 import org.onap.cps.spi.exceptions.CpsException;
37 import org.onap.cps.spi.exceptions.ModelValidationException;
38 import org.springframework.web.multipart.MultipartFile;
40 @NoArgsConstructor(access = AccessLevel.PRIVATE)
41 public class MultipartFileUtil {
43 private static final String ZIP_FILE_EXTENSION = ".zip";
44 private static final String YANG_FILE_EXTENSION = RFC6020_YANG_FILE_EXTENSION;
45 private static final int READ_BUFFER_SIZE = 1024;
48 * Extracts yang resources from multipart file instance.
50 * @param multipartFile the yang file uploaded
51 * @return yang resources as {map} where the key is original file name, and the value is file content
52 * @throws ModelValidationException if the file name extension is not '.yang' or '.zip'
53 * or if zip archive contain no yang files
54 * @throws CpsException if the file content cannot be read
57 public static Map<String, String> extractYangResourcesMap(final MultipartFile multipartFile) {
58 final String originalFileName = multipartFile.getOriginalFilename();
59 if (resourceNameEndsWithExtension(originalFileName, YANG_FILE_EXTENSION)) {
60 return Map.of(originalFileName, extractYangResourceContent(multipartFile));
62 if (resourceNameEndsWithExtension(originalFileName, ZIP_FILE_EXTENSION)) {
63 return extractYangResourcesMapFromZipArchive(multipartFile);
65 throw new ModelValidationException("Unsupported file type.",
66 String.format("Filename %s matches none of expected extensions: %s", originalFileName,
67 Arrays.asList(YANG_FILE_EXTENSION, ZIP_FILE_EXTENSION)));
70 private static Map<String, String> extractYangResourcesMapFromZipArchive(final MultipartFile multipartFile) {
71 final ImmutableMap.Builder<String, String> yangResourceMapBuilder = ImmutableMap.builder();
72 final var zipFileSizeValidator = new ZipFileSizeValidator();
74 final var inputStream = multipartFile.getInputStream();
75 final var zipInputStream = new ZipInputStream(inputStream);
78 while ((zipEntry = zipInputStream.getNextEntry()) != null) {
79 extractZipEntryToMapIfApplicable(yangResourceMapBuilder, zipEntry, zipInputStream,
80 zipFileSizeValidator);
82 zipInputStream.closeEntry();
84 } catch (final IOException e) {
85 throw new CpsException("Cannot extract resources from zip archive.", e.getMessage(), e);
87 zipFileSizeValidator.validateSizeAndEntries();
89 final Map<String, String> yangResourceMap = yangResourceMapBuilder.build();
90 if (yangResourceMap.isEmpty()) {
91 throw new ModelValidationException("Archive contains no YANG resources.",
92 String.format("Archive contains no files having %s extension.", YANG_FILE_EXTENSION));
94 return yangResourceMap;
96 } catch (final IllegalArgumentException e) {
97 throw new ModelValidationException("Invalid ZIP archive content.",
98 "Multiple resources with same name detected.", e);
102 private static void extractZipEntryToMapIfApplicable(
103 final ImmutableMap.Builder<String, String> yangResourceMapBuilder, final ZipEntry zipEntry,
104 final ZipInputStream zipInputStream, final ZipFileSizeValidator zipFileSizeValidator) throws IOException {
105 zipFileSizeValidator.setCompressedSize(zipEntry.getCompressedSize());
106 final String yangResourceName = extractResourceNameFromPath(zipEntry.getName());
107 if (zipEntry.isDirectory() || !resourceNameEndsWithExtension(yangResourceName, YANG_FILE_EXTENSION)) {
110 yangResourceMapBuilder.put(yangResourceName, extractYangResourceContent(zipInputStream, zipFileSizeValidator));
113 private static boolean resourceNameEndsWithExtension(final String resourceName, final String extension) {
114 return resourceName != null && resourceName.toLowerCase(Locale.ENGLISH).endsWith(extension);
117 private static String extractResourceNameFromPath(final String path) {
118 return path == null ? "" : path.replaceAll("^.*[\\\\/]", "");
121 private static String extractYangResourceContent(final MultipartFile multipartFile) {
123 return new String(multipartFile.getBytes(), StandardCharsets.UTF_8);
124 } catch (final IOException e) {
125 throw new CpsException("Cannot read the resource file.", e.getMessage(), e);
129 private static String extractYangResourceContent(final ZipInputStream zipInputStream,
130 final ZipFileSizeValidator zipFileSizeValidator) throws IOException {
131 try (final var byteArrayOutputStream = new ByteArrayOutputStream()) {
132 var totalSizeEntry = 0;
133 int numberOfBytesRead;
134 final var buffer = new byte[READ_BUFFER_SIZE];
135 zipFileSizeValidator.incrementTotalEntryInArchive();
136 while ((numberOfBytesRead = zipInputStream.read(buffer, 0, READ_BUFFER_SIZE)) > 0) {
137 byteArrayOutputStream.write(buffer, 0, numberOfBytesRead);
138 totalSizeEntry += numberOfBytesRead;
139 zipFileSizeValidator.updateTotalSizeArchive(totalSizeEntry);
140 zipFileSizeValidator.validateCompresssionRatio(totalSizeEntry);
142 return byteArrayOutputStream.toString(StandardCharsets.UTF_8);