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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.spi.query;
23 import static org.springframework.util.StringUtils.isEmpty;
25 import java.util.HashMap;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import lombok.AccessLevel;
32 import org.onap.cps.spi.exceptions.CpsPathException;
35 @Setter(AccessLevel.PRIVATE)
36 public class CpsPathQuery {
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;
46 private static final String NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS = "((?:\\/[^\\/]+){1,99})";
48 private static final String YANG_LEAF_VALUE_EQUALS_CONDITION =
49 "\\[\\s{0,9}@(\\S+?)\\s{0,9}=\\s{0,9}(.*?)\\s{0,9}\\]";
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);
54 private static final Pattern DESCENDANT_ANYWHERE_PATTERN = Pattern.compile("\\/\\/([^\\/][^:]+)");
56 private static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+");
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("\"(.*)\"");
61 private static final String YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION = "\\[(.*?)\\s{0,9}]";
63 private static final Pattern DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN =
64 Pattern.compile(DESCENDANT_ANYWHERE_PATTERN + YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION);
66 private static final String INDIVIDUAL_LEAF_DETAIL_PATTERN = ("\\s{1,9}and\\s{1,9}");
68 private static final Pattern LEAF_VALUE_PATTERN = Pattern.compile("@(\\S+?)=(.*+)");
70 private static final Pattern ANCESTOR_AXIS_PATTERN = Pattern.compile("(\\S+)\\/ancestor::\\/?(\\S++)");
73 * Returns a cps path query.
75 * @param cpsPathSource cps path
76 * @return a CpsPath object.
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));
86 matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
87 if (matcher.matches()) {
88 cpsPathQuery.setParametersForSingleLeafValue(cpsPath, matcher);
91 matcher = DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN.matcher(cpsPath);
92 if (matcher.matches()) {
93 cpsPathQuery.setParametersForDescendantWithLeafValues(cpsPath, matcher);
96 matcher = DESCENDANT_ANYWHERE_PATTERN.matcher(cpsPath);
97 if (matcher.matches()) {
98 cpsPathQuery.setParametersForDescendantAnywhere(matcher);
101 throw new CpsPathException("Invalid cps path.",
102 String.format("Cannot interpret or parse cps path '%s'.", cpsPath));
106 * Has ancestor axis been populated.
108 * @return boolean value.
110 public boolean hasAncestorAxis() {
111 return !(isEmpty(ancestorSchemaNodeIdentifier));
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));
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));
131 throw new CpsPathException("Invalid cps path.",
132 String.format("Cannot interpret or parse attributes in cps path '%s'.", cpsPath));
135 setLeavesData(leafData);
138 private void setParametersForDescendantAnywhere(final Matcher matcher) {
139 setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE);
140 setDescendantName(matcher.group(1));
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);
149 final var stringValueWithDoubleQuotesMatcher =
150 LEAF_STRING_VALUE_IN_DOUBLE_QUOTES_PATTERN.matcher(leafValueString);
151 if (stringValueWithDoubleQuotesMatcher.matches()) {
152 return stringValueWithDoubleQuotesMatcher.group(1);
154 final var integerValueMatcher = LEAF_INTEGER_VALUE_PATTERN.matcher(leafValueString);
155 if (integerValueMatcher.matches()) {
156 return Integer.valueOf(leafValueString);
158 throw new CpsPathException("Unsupported leaf value.",
159 String.format("Unsupported leaf value '%s' in cps path '%s'.", leafValueString, cpsPath));