Merge "Cm Subscription: Predicates optional now"
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / utils / ZipFileSizeValidator.java
1 /*
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.utils;
22
23 import lombok.Getter;
24 import lombok.Setter;
25 import org.onap.cps.spi.exceptions.ModelValidationException;
26
27 @Setter
28 @Getter
29 public class ZipFileSizeValidator {
30
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.";
35
36     private int totalUncompressedSizeOfYangFilesInArchive = 0;
37     private int totalYangFileEntriesInArchive = 0;
38     private long compressedSize = 0;
39
40     /**
41      * Increment the totalEntryInArchive by 1.
42      */
43     public void incrementTotalYangFileEntryCountInArchive() {
44         totalYangFileEntriesInArchive++;
45     }
46
47     /**
48      * Update the totalSizeArchive by numberOfBytesRead.
49      *
50      * @param numberOfBytesRead the number of bytes of each entry
51      */
52     public void updateTotalUncompressedSizeOfYangFilesInArchive(final int numberOfBytesRead) {
53         totalUncompressedSizeOfYangFilesInArchive += numberOfBytesRead;
54     }
55
56     /**
57      * Validate the total Compression size of the zip.
58      *
59      * @param totalEntrySize the size of the unzipped entry.
60      */
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));
67         }
68     }
69
70     /**
71      * Validate the total Size and number of entries in the zip.
72      */
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.",
77                         thresholdSize));
78         }
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.",
82                     THRESHOLD_ENTRIES));
83         }
84     }
85 }