Query CmHandles using CPS path
[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.Map;
26 import lombok.AccessLevel;
27 import lombok.NoArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.spi.exceptions.DataValidationException;
30 import org.onap.cps.spi.model.CmHandleQueryServiceParameters;
31
32 @Slf4j
33 @NoArgsConstructor(access = AccessLevel.PRIVATE)
34 public class CmHandleQueryRestParametersValidator {
35
36     /**
37      * Validate cm handle query parameters.
38      * @param cmHandleQueryServiceParameters name of data to be validated
39      */
40     public static void validateCmHandleQueryParameters(
41             final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
42         cmHandleQueryServiceParameters.getCmHandleQueryParameters().forEach(
43                 conditionApiProperty -> {
44                     if (Strings.isNullOrEmpty(conditionApiProperty.getConditionName())) {
45                         throwDataValidationException("Missing 'conditionName' - please supply a valid name.");
46                     }
47                     if (Arrays.stream(ValidQueryProperties.values()).noneMatch(validQueryProperty ->
48                         validQueryProperty.getQueryProperty().equals(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     /**
93      * Validate CPS path condition properties.
94      * @param conditionProperty name of data to be validated
95      */
96     public static boolean validateCpsPathConditionProperties(final Map<String, String> conditionProperty) {
97         if (conditionProperty.isEmpty()) {
98             return true;
99         }
100         if (conditionProperty.size() > 1) {
101             throwDataValidationException("Only one condition property is allowed for the CPS path query.");
102         }
103         if (!conditionProperty.containsKey("cpsPath")) {
104             throwDataValidationException(
105                 "Wrong CPS path condition property. - expecting \"cpsPath\" as the condition property.");
106         }
107         final String cpsPath = conditionProperty.get("cpsPath");
108         if (cpsPath.isBlank()) {
109             throwDataValidationException(
110                 "Wrong CPS path. - please supply a valid CPS path.");
111         }
112         if (cpsPath.contains("/additional-properties")) {
113             log.debug("{} - Private metadata cannot be queried. Nothing to be returned",
114                 cpsPath);
115             return false;
116         }
117         return true;
118     }
119
120     private static void throwDataValidationException(final String details) {
121         throw new DataValidationException("Invalid Query Parameter.", details);
122     }
123
124 }