2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Bell Canada.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
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;
25 import org.onap.cps.spi.exceptions.ModelValidationException;
29 public class ZipFileSizeValidator {
31 private static final int THRESHOLD_ENTRIES = 10000;
32 private static int thresholdSize = 100000000;
33 private static final double THRESHOLD_RATIO = 40;
34 private static final String INVALID_ZIP = "Invalid ZIP archive content.";
36 private int totalUncompressedSizeOfYangFilesInArchive = 0;
37 private int totalYangFileEntriesInArchive = 0;
38 private long compressedSize = 0;
41 * Increment the totalEntryInArchive by 1.
43 public void incrementTotalYangFileEntryCountInArchive() {
44 totalYangFileEntriesInArchive++;
48 * Update the totalSizeArchive by numberOfBytesRead.
50 * @param numberOfBytesRead the number of bytes of each entry
52 public void updateTotalUncompressedSizeOfYangFilesInArchive(final int numberOfBytesRead) {
53 totalUncompressedSizeOfYangFilesInArchive += numberOfBytesRead;
57 * Validate the total Compression size of the zip.
59 * @param totalEntrySize the size of the unzipped entry.
61 public void validateCompresssionRatio(final int totalEntrySize) {
62 final double compressionRatio = (double) totalEntrySize / compressedSize;
63 if (compressionRatio > THRESHOLD_RATIO) {
64 throw new ModelValidationException(INVALID_ZIP,
65 String.format("Ratio between compressed and uncompressed data exceeds the CPS limit"
66 + " %s.", THRESHOLD_RATIO));
71 * Validate the total Size and number of entries in the zip.
73 public void validateSizeAndEntries() {
74 if (totalUncompressedSizeOfYangFilesInArchive > thresholdSize) {
75 throw new ModelValidationException(INVALID_ZIP,
76 String.format("The total size of uncompressed yang files exceeds the CPS limit of %s bytes.",
79 if (totalYangFileEntriesInArchive > THRESHOLD_ENTRIES) {
80 throw new ModelValidationException(INVALID_ZIP,
81 String.format("The number of yang file entries in the archive exceeds the CPS limit %s.",