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 java.util.HashMap;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import lombok.AccessLevel;
30 import org.onap.cps.spi.exceptions.CpsPathException;
33 @Setter(AccessLevel.PRIVATE)
34 public class CpsPathQuery {
36 private CpsPathQueryType cpsPathQueryType;
37 private String xpathPrefix;
38 private String leafName;
39 private Object leafValue;
40 private String descendantName;
41 private Map<String, Object> leavesData;
43 private static final String NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS = "((?:\\/[^\\/]+){1,99})";
45 private static final String YANG_LEAF_VALUE_EQUALS_CONDITION =
46 "\\[\\s{0,9}@(\\S+?)\\s{0,9}=\\s{0,9}(.*?)\\s{0,9}\\]";
48 private static final Pattern QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN =
49 Pattern.compile(NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS + YANG_LEAF_VALUE_EQUALS_CONDITION);
51 private static final Pattern DESCENDANT_ANYWHERE_PATTERN = Pattern.compile("\\/\\/([^\\/].+)");
53 private static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+");
55 private static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]");
57 private static final String YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION = "\\[(.*?)\\s{0,9}]";
59 private static final Pattern DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN =
60 Pattern.compile(DESCENDANT_ANYWHERE_PATTERN + YANG_MULTIPLE_LEAF_VALUE_EQUALS_CONDITION);
62 private static final String INDIVIDUAL_LEAF_DETAIL_PATTERN = ("\\s{0,9}and\\s{0,9}");
64 private static final Pattern LEAF_VALUE_PATTERN = Pattern.compile("@(\\S+?)=(.*)");
67 * Returns a cps path query.
69 * @param cpsPath cps path
70 * @return a CpsPath object.
72 public static CpsPathQuery createFrom(final String cpsPath) {
73 Matcher matcher = QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN.matcher(cpsPath);
74 final CpsPathQuery cpsPathQuery = new CpsPathQuery();
75 if (matcher.matches()) {
76 return buildCpsPathQueryWithSingleLeafPattern(cpsPath, matcher, cpsPathQuery);
78 matcher = DESCENDANT_ANYWHERE_PATTERN_WITH_MULTIPLE_LEAF_PATTERN.matcher(cpsPath);
79 if (matcher.matches()) {
80 return buildCpsQueryForDescendentWithLeafPattern(cpsPath, matcher, cpsPathQuery);
82 matcher = DESCENDANT_ANYWHERE_PATTERN.matcher(cpsPath);
83 if (matcher.matches()) {
84 cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE);
85 cpsPathQuery.setDescendantName(matcher.group(1));
88 throw new CpsPathException("Invalid cps path.",
89 String.format("Cannot interpret or parse cps path '%s'.", cpsPath));
92 private static CpsPathQuery buildCpsPathQueryWithSingleLeafPattern(final String cpsPath, final Matcher matcher,
93 final CpsPathQuery cpsPathQuery) {
94 cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
95 cpsPathQuery.setXpathPrefix(matcher.group(1));
96 cpsPathQuery.setLeafName(matcher.group(2));
97 cpsPathQuery.setLeafValue(convertLeafValueToCorrectType(matcher.group(3), cpsPath));
101 private static CpsPathQuery buildCpsQueryForDescendentWithLeafPattern(final String cpsPath, final Matcher matcher,
102 final CpsPathQuery cpsPathQuery) {
103 cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES);
104 cpsPathQuery.setDescendantName(matcher.group(1));
105 final Map<String, Object> leafData = new HashMap<>();
106 for (final String leafValuePair : matcher.group(2).split(INDIVIDUAL_LEAF_DETAIL_PATTERN)) {
107 final Matcher descendentMatcher = LEAF_VALUE_PATTERN.matcher(leafValuePair);
108 if (descendentMatcher.matches()) {
109 leafData.put(descendentMatcher.group(1),
110 convertLeafValueToCorrectType(descendentMatcher.group(2), cpsPath));
112 throw new CpsPathException("Invalid cps path.",
113 String.format("Cannot interpret or parse attributes in cps path '%s'.", cpsPath));
116 cpsPathQuery.setLeavesData(leafData);
120 private static Object convertLeafValueToCorrectType(final String leafValueString, final String cpsPath) {
121 final Matcher stringValueWithQuotesMatcher = LEAF_STRING_VALUE_PATTERN.matcher(leafValueString);
122 if (stringValueWithQuotesMatcher.matches()) {
123 return stringValueWithQuotesMatcher.group(1);
125 final Matcher integerValueMatcher = LEAF_INTEGER_VALUE_PATTERN.matcher(leafValueString);
126 if (integerValueMatcher.matches()) {
127 return Integer.valueOf(leafValueString);
129 throw new CpsPathException("Unsupported leaf value.",
130 String.format("Unsupported leaf value '%s' in cps path '%s'.", leafValueString, cpsPath));