2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.api.query;
10 import com.google.common.collect.ImmutableList;
11 import java.text.ParseException;
12 import java.util.ArrayDeque;
13 import java.util.ArrayList;
14 import java.util.Deque;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.restconf.api.ApiPath.ApiIdentifier;
18 import org.opendaylight.restconf.api.query.FieldsParam.NodeSelector;
19 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
20 import org.opendaylight.yangtools.yang.common.YangNames;
23 * Stateful parser for {@link FieldsParam}. This is not as hard as IETF's ABNF would lead you to believe. The original
26 * fields-expr = path "(" fields-expr ")" / path ";" fields-expr / path
27 * path = api-identifier [ "/" path ]
29 * To make some sense of this, let's express the same constructs in a more powerful ANTLR4 grammar.
32 * {@code path} is a rather simple
34 * path = api-identifier ("/" api-identifier)*
36 * which is to say a {@code path} is "a sequence of one or more api-identifiers, separated by slashes". This boils in
37 * turn down to a list {@link ApiIdentifier}s, which is guaranteed to have at least one item.
40 * {@code fields-expr} can be rewritten as three distinct possibilities:
42 * fields-expr : path "(" fields-expr ")"
43 * | path ";" fields-expr
46 * which makes it clear it is a recursive structure, where the parentheses part is sub-filters and ';' serves as
47 * concatenation. So let's rewrite that by folding the common part and use optional elements and introducing proper
48 * names for constructs
50 * fields : node-selectors EOF
51 * node-selectors : node-selector (";" node-selector)*
52 * node-selector : path sub-selectors?
53 * sub-selectors : "(" node-selectors ")"
54 * path : api-identifier ("/" api-identifier)*
58 * That ANTLR4 grammar dictates the layout of {@link FieldsParam}. It also shows the parsing is recursive on
59 * {@code node-selectors}, which is what {@link #parse(String)} and
60 * {@link NodeSelectorParser#parseSubSelectors(String, int)} deal with.
62 final class FieldsParameterParser {
63 // Lazily instantiated queue for reuse of parser when we encounter sub-selectors. We could just rely on JIT/GC
64 // dealing with allocation rate, but we should be ready to see malicious inputs. One example of that is
65 // multiple nested sub-selectors like "a(b(c(d)));e(f(g(h)));i(j(k(l)))" With this cache we end allocating only four
66 // parsers instead of ten.
67 private Deque<NodeSelectorParser> parsers;
69 @NonNull FieldsParam parse(final String str) throws ParseException {
70 final var nodeSelectors = ImmutableList.<NodeSelector>builder();
73 final var parser = new NodeSelectorParser();
75 final int next = parser.fillFrom(str, idx);
76 nodeSelectors.add(parser.collectAndReset());
78 if (next == str.length()) {
79 // We have reached the end, we are done
80 return new FieldsParam(nodeSelectors.build());
83 final char ch = str.charAt(next);
85 throw new ParseException("Expecting ';', not '" + ch + "'", next);
91 private @NonNull NodeSelectorParser getParser() {
92 final var local = parsers;
94 final var existing = local.poll();
95 if (existing != null) {
99 return new NodeSelectorParser();
102 private void putParser(final NodeSelectorParser parser) {
105 // Let's be conservative with memory allocation
106 parsers = local = new ArrayDeque<>(2);
111 private static void expectIdentifierStart(final String str, final int offset) throws ParseException {
112 final char ch = charAt(str, offset);
113 if (!YangNames.IDENTIFIER_START.matches(ch)) {
114 throw new ParseException("Expecting [a-ZA-Z_], not '" + ch + "'", offset);
118 private static char charAt(final String str, final int offset) throws ParseException {
119 if (str.length() == offset) {
120 throw new ParseException("Unexpected end of input", offset);
122 return str.charAt(offset);
125 // A note here: we could store 'str' either in this object, or FieldsParameterParser, but that makes it a bit
126 // removed via indirection. We are opting for explicit argument passing to ensure JIT sees it as a local variable
127 // along with offset.
128 private final class NodeSelectorParser {
129 private final List<ApiIdentifier> path = new ArrayList<>(4);
131 // Not that common: lazily instantiated
132 private List<NodeSelector> selectors;
134 int fillFrom(final String str, final int offset) throws ParseException {
135 return parsePathStepFirst(str, offset);
138 @NonNull NodeSelector collectAndReset() {
139 final ImmutableList<ApiIdentifier> collectedPath = ImmutableList.copyOf(path);
142 final ImmutableList<NodeSelector> collectedSelectors;
143 if (selectors != null && !selectors.isEmpty()) {
144 collectedSelectors = ImmutableList.copyOf(selectors);
147 collectedSelectors = ImmutableList.of();
150 return new NodeSelector(collectedPath, collectedSelectors);
153 // We are at the start of a step in path. We are dealing with the first part of
154 // identifier (":" identifier)?
155 // but are mindful of the big picture
156 private int parsePathStepFirst(final String str, final int offset) throws ParseException {
157 expectIdentifierStart(str, offset);
159 int idx = offset + 1;
161 if (idx == str.length()) {
162 path.add(new ApiIdentifier(null, Unqualified.of(str.substring(offset))));
166 final char ch = str.charAt(idx);
167 if (!YangNames.NOT_IDENTIFIER_PART.matches(ch)) {
172 final String first = str.substring(offset, idx);
174 // We have complete first identifier, now switch to parsing the second identifier
175 return parsePathStepSecond(first, str, idx + 1);
177 path.add(new ApiIdentifier(null, Unqualified.of(first)));
180 case ';', ')' -> /* End of this selector, return */ idx;
181 case '/' -> /* Process next step */ parsePathStepFirst(str, idx + 1);
182 case '(' -> /* Process at least one sub-selector */ parseSubSelectors(str, idx + 1);
183 default -> throw new ParseException("Expecting [a-zA-Z_.-/(:;], not '" + ch + "'", idx);
188 // We are at the second identifier of a step in path, we already have the first identifier from
189 // identifier (":" identifier)?
190 // but are mindful of the big picture
191 private int parsePathStepSecond(final String module, final String str, final int offset) throws ParseException {
192 expectIdentifierStart(str, offset);
194 int idx = offset + 1;
196 if (idx == str.length()) {
197 path.add(new ApiIdentifier(module, Unqualified.of(str.substring(offset))));
201 final char ch = str.charAt(idx);
202 if (!YangNames.NOT_IDENTIFIER_PART.matches(ch)) {
206 path.add(new ApiIdentifier(module, Unqualified.of(str.substring(offset, idx))));
209 case ';', ')' -> /* End of this selector, return */ idx;
210 case '/' -> /* Process next step */ parsePathStepFirst(str, idx + 1);
211 case '(' -> /* Process at least one sub-selector */ parseSubSelectors(str, idx + 1);
212 default -> throw new ParseException("Expecting [a-zA-Z_.-/(:;], not '" + ch + "'", idx);
217 // We are dealing with sub-selectors here
218 private int parseSubSelectors(final String str, final int offset) throws ParseException {
219 var local = selectors;
221 selectors = local = new ArrayList<>(4);
225 final var parser = getParser();
227 final int next = parser.fillFrom(str, idx);
228 local.add(parser.collectAndReset());
230 final char ch = charAt(str, next);
233 // Another sub-selector
237 // End of these sub-selectors, return the parser for reuse
241 throw new ParseException("Expecting [;)], not '" + ch + "'", next);