50f1fd8d0606f41c7a2698cc03535cbcf436dcfa
[sdc.git] /
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 package org.openecomp.sdc.vendorsoftwareproduct.impl.onboarding.validation;
20
21 import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_MISSING;
22 import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET;
23 import static org.openecomp.sdc.common.errors.Messages.MANIFEST_VALIDATION_HELM_IS_BASE_NOT_UNIQUE;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
29
30 public class CnfPackageValidator {
31
32     public List<String> validateHelmPackage(List<FileData> modules) {
33         List<String> messages = Collections.emptyList();
34         if (modules != null && !modules.isEmpty()) {
35             Stats stats = calculateStats(modules);
36             messages = createErrorMessages(stats);
37         }
38         return messages;
39     }
40
41     private Stats calculateStats(List<FileData> modules) {
42         Stats stats = new Stats();
43         for (FileData mod : modules) {
44             if (mod.getBase() == null) {
45                 stats.without++;
46             } else if (mod.getBase()) {
47                 stats.base++;
48             }
49         }
50         return stats;
51     }
52
53     private List<String> createErrorMessages(Stats stats) {
54         List<String> messages = new ArrayList<>();
55         if (stats.without > 0) {
56             messages.add(MANIFEST_VALIDATION_HELM_IS_BASE_MISSING.formatMessage(stats.without));
57         }
58         if (stats.base == 0) {
59             messages.add(MANIFEST_VALIDATION_HELM_IS_BASE_NOT_SET.getErrorMessage());
60         } else if (stats.base > 1) {
61             messages.add(MANIFEST_VALIDATION_HELM_IS_BASE_NOT_UNIQUE.getErrorMessage());
62         }
63         return messages;
64     }
65
66     private static class Stats {
67
68         private int base = 0;
69         private int without = 0;
70     }
71 }