Merge 2 'query' end points in NCMP
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / CmHandleQueryRestParametersValidator.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
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  *
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.utils;
22
23 import com.google.common.base.Strings;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Map;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
29 import org.onap.cps.spi.exceptions.DataValidationException;
30 import org.onap.cps.spi.model.CmHandleQueryParameters;
31
32 @NoArgsConstructor(access = AccessLevel.PRIVATE)
33 public class CmHandleQueryRestParametersValidator {
34
35     private static final List<String> VALID_PROPERTY_NAMES = Arrays.asList("hasAllProperties", "hasAllModules");
36
37     /**
38      * Validate cm handle query parameters.
39      * @param cmHandleQueryParameters name of data to be validated
40      */
41     public static void validateCmHandleQueryParameters(final CmHandleQueryParameters cmHandleQueryParameters) {
42         cmHandleQueryParameters.getCmHandleQueryParameters().forEach(
43                 conditionApiProperty -> {
44                     if (Strings.isNullOrEmpty(conditionApiProperty.getConditionName())) {
45                         throwDataValidationException("Missing 'conditionName' - please supply a valid name.");
46                     }
47                     if (!VALID_PROPERTY_NAMES.contains(conditionApiProperty.getConditionName())) {
48                         throwDataValidationException(
49                                 String.format("Wrong 'conditionName': %s - please supply a valid name.",
50                                 conditionApiProperty.getConditionName()));
51                     }
52                     if (conditionApiProperty.getConditionParameters().isEmpty()) {
53                         throwDataValidationException(
54                                 "Empty 'conditionsParameters' - please supply a valid condition parameter.");
55                     }
56                     conditionApiProperty.getConditionParameters().forEach(
57                             conditionParameter -> {
58                                 if (conditionParameter.isEmpty()) {
59                                     throwDataValidationException(
60                                             "Empty 'conditionsParameter' - please supply a valid condition parameter.");
61                                 }
62                                 if (conditionParameter.size() > 1) {
63                                     throwDataValidationException("Too many name in one 'conditionsParameter' -"
64                                             + " please supply one name in one condition parameter.");
65                                 }
66                                 conditionParameter.forEach((key, value) -> {
67                                     if (Strings.isNullOrEmpty(key)) {
68                                         throwDataValidationException(
69                                                 "Missing 'conditionsParameterName' - please supply a valid name.");
70                                     }
71                                 });
72                             }
73                     );
74                 }
75         );
76     }
77
78     /**
79      * Validate module name condition properties.
80      * @param conditionProperty name of data to be validated
81      */
82     public static void validateModuleNameConditionProperties(final Map<String, String> conditionProperty) {
83         if (conditionProperty.containsKey("moduleName") && !conditionProperty.get("moduleName").isEmpty()) {
84             return;
85         }
86         throwDataValidationException("Wrong module condition property. - please supply a valid condition property.");
87     }
88
89     private static void throwDataValidationException(final String details) {
90         throw new DataValidationException("Invalid Query Parameter.", details);
91     }
92
93 }