Merge "Fix upload size to be greater than 1MB"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / query / CpsPathQuery.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
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.spi.query;
22
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import lombok.AccessLevel;
26 import lombok.Getter;
27 import lombok.Setter;
28 import org.onap.cps.spi.exceptions.CpsPathException;
29
30 @Getter
31 @Setter(AccessLevel.PRIVATE)
32 public class CpsPathQuery {
33
34     private CpsPathQueryType cpsPathQueryType;
35     private String xpathPrefix;
36     private String leafName;
37     private Object leafValue;
38     private String endsWith;
39
40     private static final String NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS = "((?:\\/[^\\/]+){1,99})";
41
42     private static final String YANG_LEAF_VALUE_EQUALS_CONDITION =
43         "\\[\\s{0,9}@(\\S+?)\\s{0,9}=\\s{0,9}(.*?)\\s{0,9}\\]";
44
45     private static final Pattern QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN =
46         Pattern.compile(NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS + YANG_LEAF_VALUE_EQUALS_CONDITION);
47
48     private static final Pattern QUERY_CPS_PATH_ENDS_WITH_PATTERN = Pattern.compile("\\/\\/(.+)");
49
50     private static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+");
51
52     private static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]");
53
54     /**
55      * Returns a cps path query.
56      *
57      * @param cpsPath cps path
58      * @return a CpsPath object.
59      */
60     public static CpsPathQuery createFrom(final String cpsPath) {
61         Matcher matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
62         final CpsPathQuery cpsPathQuery = new CpsPathQuery();
63         if (matcher.matches()) {
64             cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
65             cpsPathQuery.setXpathPrefix(matcher.group(1));
66             cpsPathQuery.setLeafName(matcher.group(2));
67             cpsPathQuery.setLeafValue(convertLeafValueToCorrectType(matcher.group(3), cpsPath));
68             return cpsPathQuery;
69         }
70         matcher = QUERY_CPS_PATH_ENDS_WITH_PATTERN.matcher(cpsPath);
71         if (matcher.matches()) {
72             cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_ENDS_WITH);
73             cpsPathQuery.setEndsWith(matcher.group(1));
74             return cpsPathQuery;
75         }
76         throw new CpsPathException("Invalid cps path.",
77             String.format("Cannot interpret or parse cps path '%s'.", cpsPath));
78     }
79
80     private static Object convertLeafValueToCorrectType(final String leafValueString, final String cpsPath) {
81         final Matcher stringValueWithQuotesMatcher = LEAF_STRING_VALUE_PATTERN.matcher(leafValueString);
82         if (stringValueWithQuotesMatcher.matches()) {
83             return stringValueWithQuotesMatcher.group(1);
84         }
85         final Matcher integerValueMatcher = LEAF_INTEGER_VALUE_PATTERN.matcher(leafValueString);
86         if (integerValueMatcher.matches()) {
87             return Integer.valueOf(leafValueString);
88         }
89         throw new CpsPathException("Unsupported leaf value.",
90             String.format("Unsupported leaf value '%s' in cps path '%s'.", leafValueString, cpsPath));
91     }
92 }