Merge "Upgrade Liquibase to Version 4.14.0"
[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.CmHandleQueryServiceParameters;
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 cmHandleQueryServiceParameters name of data to be validated
40      */
41     public static void validateCmHandleQueryParameters(
42             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
43         cmHandleQueryServiceParameters.getCmHandleQueryParameters().forEach(
44                 conditionApiProperty -> {
45                     if (Strings.isNullOrEmpty(conditionApiProperty.getConditionName())) {
46                         throwDataValidationException("Missing 'conditionName' - please supply a valid name.");
47                     }
48                     if (!VALID_PROPERTY_NAMES.contains(conditionApiProperty.getConditionName())) {
49                         throwDataValidationException(
50                                 String.format("Wrong 'conditionName': %s - please supply a valid name.",
51                                 conditionApiProperty.getConditionName()));
52                     }
53                     if (conditionApiProperty.getConditionParameters().isEmpty()) {
54                         throwDataValidationException(
55                                 "Empty 'conditionsParameters' - please supply a valid condition parameter.");
56                     }
57                     conditionApiProperty.getConditionParameters().forEach(
58                             CmHandleQueryRestParametersValidator::validateConditionParameter
59                     );
60                 }
61         );
62     }
63
64     private static void validateConditionParameter(final Map<String, String> conditionParameter) {
65         if (conditionParameter.isEmpty()) {
66             throwDataValidationException(
67                     "Empty 'conditionsParameter' - please supply a valid condition parameter.");
68         }
69         if (conditionParameter.size() > 1) {
70             throwDataValidationException("Too many name in one 'conditionsParameter' -"
71                     + " please supply one name in one condition parameter.");
72         }
73         conditionParameter.forEach((key, value) -> {
74             if (Strings.isNullOrEmpty(key)) {
75                 throwDataValidationException(
76                         "Missing 'conditionsParameterName' - please supply a valid name.");
77             }
78         });
79     }
80
81     /**
82      * Validate module name condition properties.
83      * @param conditionProperty name of data to be validated
84      */
85     public static void validateModuleNameConditionProperties(final Map<String, String> conditionProperty) {
86         if (conditionProperty.containsKey("moduleName") && !conditionProperty.get("moduleName").isEmpty()) {
87             return;
88         }
89         throwDataValidationException("Wrong module condition property. - please supply a valid condition property.");
90     }
91
92     private static void throwDataValidationException(final String details) {
93         throw new DataValidationException("Invalid Query Parameter.", details);
94     }
95
96 }