Add validation of manifest for helm packages.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / onboarding / validation / CnfPackageValidator.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nokia
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation;
21
22 import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_MISSING;
23 import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET;
24 import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_NOT_UNIQUE;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
30
31 public class CnfPackageValidator {
32
33     public List<String> validateHelmPackage(List<FileData> modules) {
34         List<String> messages = Collections.emptyList();
35
36         if (modules != null && !modules.isEmpty()) {
37             Stats stats = calculateStats(modules);
38             messages = createErrorMessages(stats);
39         }
40
41         return messages;
42     }
43
44     private Stats calculateStats(List<FileData> modules) {
45         Stats stats = new Stats();
46         for (FileData mod : modules) {
47             if (mod.getBase() == null) {
48                 stats.without++;
49             } else if (mod.getBase()) {
50                 stats.base++;
51             }
52         }
53         return stats;
54     }
55
56     private List<String> createErrorMessages(Stats stats) {
57         List<String> messages = new ArrayList<>();
58
59         if (stats.without > 0) {
60             messages.add(MANIFEST_VALIDATION_HELM_IS_BASE_MISSING.formatMessage(stats.without));
61         }
62
63         if (stats.base == 0) {
64             messages.add(MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET.getErrorMessage());
65         } else if (stats.base > 1) {
66             messages.add(MANIFEST_VALIDATION_HELM_IS_BASE_NOT_UNIQUE.getErrorMessage());
67         }
68
69         return messages;
70     }
71
72     private static class Stats {
73
74         private int base = 0;
75         private int without = 0;
76     }
77 }