Adding Possessive Quantifier to Regex to prevent backtracking
[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.
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 static org.springframework.util.StringUtils.isEmpty;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import lombok.AccessLevel;
30 import lombok.Getter;
31 import lombok.Setter;
32 import org.onap.cps.spi.exceptions.CpsPathException;
33
34 @Getter
35 @Setter(AccessLevel.PRIVATE)
36 public class CpsPathQuery {
37
38     private CpsPathQueryType cpsPathQueryType;
39     private String xpathPrefix;
40     private String leafName;
41     private Object leafValue;
42     private String descendantName;
43     private Map<String, Object> leavesData;
44     private String ancestorSchemaNodeIdentifier;
45
46     private static final String NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS = "((?:\\/[^\\/]+){1,99})";
47
48     private static final String YANG_LEAF_VALUE_EQUALS_CONDITION =
49         "\\[\\s{0,9}@(\\S+?)\\s{0,9}=\\s{0,9}(.*?)\\s{0,9}\\]";
50
51     private static final Pattern QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN =
52         Pattern.compile(NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS + YANG_LEAF_VALUE_EQUALS_CONDITION);
53
54     private static final Pattern DESCENDANT_ANYWHERE_PATTERN = Pattern.compile("\\/\\/([^\\/][^:]+)");
55
56     private static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+");
57
58     private static final Pattern LEAF_STRING_VALUE_IN_SINGLE_QUOTES_PATTERN = Pattern.compile("'(.*)'");
59     private static final Pattern LEAF_STRING_VALUE_IN_DOUBLE_QUOTES_PATTERN = Pattern.compile("\"(.*)\"");
60
61     private static final String YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION = "\\[(.*?)\\s{0,9}]";
62
63     private static final Pattern DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN =
64         Pattern.compile(DESCENDANT_ANYWHERE_PATTERN + YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION);
65
66     private static final String INDIVIDUAL_LEAF_DETAIL_PATTERN = ("\\s{1,9}and\\s{1,9}");
67
68     private static final Pattern LEAF_VALUE_PATTERN = Pattern.compile("@(\\S+?)=(.*+)");
69
70     private static final Pattern ANCESTOR_AXIS_PATTERN = Pattern.compile("(\\S+)\\/ancestor::\\/?(\\S++)");
71
72     /**
73      * Returns a cps path query.
74      *
75      * @param cpsPathSource cps path
76      * @return a CpsPath object.
77      */
78     public static CpsPathQuery createFrom(final String cpsPathSource) {
79         var cpsPath = cpsPathSource;
80         final var cpsPathQuery = new CpsPathQuery();
81         var matcher = ANCESTOR_AXIS_PATTERN.matcher(cpsPath);
82         if (matcher.matches()) {
83             cpsPath = matcher.group(1);
84             cpsPathQuery.setAncestorSchemaNodeIdentifier(matcher.group(2));
85         }
86         matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
87         if (matcher.matches()) {
88             cpsPathQuery.setParametersForSingleLeafValue(cpsPath, matcher);
89             return cpsPathQuery;
90         }
91         matcher = DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN.matcher(cpsPath);
92         if (matcher.matches()) {
93             cpsPathQuery.setParametersForDescendantWithLeafValues(cpsPath, matcher);
94             return cpsPathQuery;
95         }
96         matcher = DESCENDANT_ANYWHERE_PATTERN.matcher(cpsPath);
97         if (matcher.matches()) {
98             cpsPathQuery.setParametersForDescendantAnywhere(matcher);
99             return cpsPathQuery;
100         }
101         throw new CpsPathException("Invalid cps path.",
102             String.format("Cannot interpret or parse cps path '%s'.", cpsPath));
103     }
104
105     /**
106      * Has ancestor axis been populated.
107      *
108      * @return boolean value.
109      */
110     public boolean hasAncestorAxis() {
111         return !(isEmpty(ancestorSchemaNodeIdentifier));
112     }
113
114     private void setParametersForSingleLeafValue(final String cpsPath, final Matcher matcher) {
115         setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
116         setXpathPrefix(matcher.group(1));
117         setLeafName(matcher.group(2));
118         setLeafValue(convertLeafValueToCorrectType(matcher.group(3), cpsPath));
119     }
120
121     private void setParametersForDescendantWithLeafValues(final String cpsPath, final Matcher matcher) {
122         setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES);
123         setDescendantName(matcher.group(1));
124         final Map<String, Object> leafData = new HashMap<>();
125         for (final String leafValuePair : matcher.group(2).split(INDIVIDUAL_LEAF_DETAIL_PATTERN)) {
126             final var descendentMatcher = LEAF_VALUE_PATTERN.matcher(leafValuePair);
127             if (descendentMatcher.matches()) {
128                 leafData.put(descendentMatcher.group(1),
129                     convertLeafValueToCorrectType(descendentMatcher.group(2), cpsPath));
130             } else {
131                 throw new CpsPathException("Invalid cps path.",
132                     String.format("Cannot interpret or parse attributes in cps path '%s'.", cpsPath));
133             }
134         }
135         setLeavesData(leafData);
136     }
137
138     private void setParametersForDescendantAnywhere(final Matcher matcher) {
139         setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE);
140         setDescendantName(matcher.group(1));
141     }
142
143     private static Object convertLeafValueToCorrectType(final String leafValueString, final String cpsPath) {
144         final var stringValueWithSingleQuotesMatcher =
145                 LEAF_STRING_VALUE_IN_SINGLE_QUOTES_PATTERN.matcher(leafValueString);
146         if (stringValueWithSingleQuotesMatcher.matches()) {
147             return stringValueWithSingleQuotesMatcher.group(1);
148         }
149         final var stringValueWithDoubleQuotesMatcher =
150                 LEAF_STRING_VALUE_IN_DOUBLE_QUOTES_PATTERN.matcher(leafValueString);
151         if (stringValueWithDoubleQuotesMatcher.matches()) {
152             return stringValueWithDoubleQuotesMatcher.group(1);
153         }
154         final var integerValueMatcher = LEAF_INTEGER_VALUE_PATTERN.matcher(leafValueString);
155         if (integerValueMatcher.matches()) {
156             return Integer.valueOf(leafValueString);
157         }
158         throw new CpsPathException("Unsupported leaf value.",
159             String.format("Unsupported leaf value '%s' in cps path '%s'.", leafValueString, cpsPath));
160     }
161 }