2 * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
20 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
21 import org.opendaylight.restconf.nb.rfc8040.utils.parser.builder.ParserBuilderConstants.Deserializer;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 public final class ParserFieldsParameter {
29 private static final char COLON = ':';
30 private static final char SEMICOLON = ';';
31 private static final char SLASH = '/';
32 private static final char STARTING_PARENTHESIS = '(';
33 private static final char CLOSING_PARENTHESIS = ')';
35 private ParserFieldsParameter() {
40 * Parse fields parameter and return complete list of child nodes organized into levels.
41 * @param identifier identifier context created from request URI
42 * @param input input value of fields parameter
43 * @return {@link List}
46 public static List<Set<QName>> parseFieldsParameter(@Nonnull final InstanceIdentifierContext<?> identifier,
47 @Nonnull final String input) {
48 final List<Set<QName>> parsed = new ArrayList<>();
49 final SchemaContext context = identifier.getSchemaContext();
50 final QNameModule startQNameModule = identifier.getSchemaNode().getQName().getModule();
51 final DataSchemaContextNode<?> startNode = DataSchemaContextNode.fromDataSchemaNode(
52 (DataSchemaNode) identifier.getSchemaNode());
54 if (startNode == null) {
55 throw new RestconfDocumentedException(
56 "Start node missing in " + input, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
59 parseInput(input, startQNameModule, startNode, parsed, context);
64 * Parse input value of fields parameter and create list of sets. Each set represents one level of child nodes.
65 * @param input input value of fields parameter
66 * @param startQNameModule starting qname module
67 * @param startNode starting node
68 * @param parsed list of results
69 * @param context schema context
71 private static void parseInput(@Nonnull final String input,
72 @Nonnull final QNameModule startQNameModule,
73 @Nonnull final DataSchemaContextNode<?> startNode,
74 @Nonnull final List<Set<QName>> parsed,
75 @Nonnull final SchemaContext context) {
76 int currentPosition = 0;
77 int startPosition = 0;
78 DataSchemaContextNode<?> currentNode = startNode;
79 QNameModule currentQNameModule = startQNameModule;
81 Set<QName> currentLevel = new HashSet<>();
82 parsed.add(currentLevel);
84 while (currentPosition < input.length()) {
85 final char currentChar = input.charAt(currentPosition);
87 if (Deserializer.IDENTIFIER.matches(currentChar) || currentChar == '/') {
88 if (currentChar == SLASH) {
89 // add parsed identifier to results for current level
90 currentNode = addChildToResult(
92 input.substring(startPosition, currentPosition), currentQNameModule, currentLevel);
94 currentLevel = new HashSet<>();
95 parsed.add(currentLevel);
98 startPosition = currentPosition;
106 switch (currentChar) {
108 // new namespace and revision found
109 currentQNameModule = context.findModules(
110 input.substring(startPosition, currentPosition)).iterator().next().getQNameModule();
113 case STARTING_PARENTHESIS:
114 // add current child to parsed results for current level
115 final DataSchemaContextNode<?> child = addChildToResult(
117 input.substring(startPosition, currentPosition), currentQNameModule, currentLevel);
118 // call with child node as new start node for one level down
119 final int closingParenthesis = currentPosition
120 + findClosingParenthesis(input.substring(currentPosition + 1));
122 input.substring(currentPosition + 1, closingParenthesis),
128 // closing parenthesis must be at the end of input or separator and one more character is expected
129 currentPosition = closingParenthesis + 1;
130 if (currentPosition != input.length()) {
131 if (currentPosition + 1 < input.length()) {
132 if (input.charAt(currentPosition) == SEMICOLON) {
135 throw new RestconfDocumentedException(
136 "Missing semicolon character after "
137 + child.getIdentifier().getNodeType().getLocalName()
139 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
142 throw new RestconfDocumentedException(
143 "Unexpected character '"
144 + input.charAt(currentPosition)
145 + "' found in fields parameter value",
146 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
152 // complete identifier found
155 input.substring(startPosition, currentPosition), currentQNameModule, currentLevel);
159 throw new RestconfDocumentedException(
160 "Unexpected character '" + currentChar + "' found in fields parameter value",
161 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
164 startPosition = currentPosition;
167 // parse input to end
168 if (startPosition < input.length()) {
169 addChildToResult(currentNode, input.substring(startPosition), currentQNameModule, currentLevel);
174 * Add parsed child of current node to result for current level.
175 * @param currentNode current node
176 * @param identifier parsed identifier of child node
177 * @param currentQNameModule current namespace and revision in {@link QNameModule}
178 * @param level current nodes level
179 * @return {@link DataSchemaContextNode}
182 private static DataSchemaContextNode<?> addChildToResult(
183 @Nonnull final DataSchemaContextNode<?> currentNode,
184 @Nonnull final String identifier,
185 @Nonnull final QNameModule currentQNameModule,
186 @Nonnull final Set<QName> level) {
187 final QName childQName = QName.create(currentQNameModule, identifier);
189 // resolve parent node
190 final DataSchemaContextNode<?> parentNode = resolveMixinNode(
191 currentNode, level, currentNode.getIdentifier().getNodeType());
192 if (parentNode == null) {
193 throw new RestconfDocumentedException(
194 "Not-mixin node missing in " + currentNode.getIdentifier().getNodeType().getLocalName(),
195 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
198 // resolve child node
199 final DataSchemaContextNode<?> childNode = resolveMixinNode(
200 parentNode.getChild(childQName), level, childQName);
201 if (childNode == null) {
202 throw new RestconfDocumentedException(
203 "Child " + identifier + " node missing in "
204 + currentNode.getIdentifier().getNodeType().getLocalName(),
205 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
208 // add final childNode node to level nodes
209 level.add(childNode.getIdentifier().getNodeType());
214 * Resolve mixin node by searching for inner nodes until not mixin node or null is found.
215 * All nodes expect of not mixin node are added to current level nodes.
216 * @param node initial mixin or not-mixin node
217 * @param level current nodes level
218 * @param qualifiedName qname of initial node
219 * @return {@link DataSchemaContextNode}
222 private static DataSchemaContextNode<?> resolveMixinNode(@Nullable final DataSchemaContextNode<?> node,
223 @Nonnull final Set<QName> level,
224 @Nonnull final QName qualifiedName) {
225 DataSchemaContextNode<?> currentNode = node;
226 while (currentNode != null && currentNode.isMixin()) {
227 level.add(qualifiedName);
228 currentNode = currentNode.getChild(qualifiedName);
235 * Find position of matching parenthesis increased by one, but at most equals to input size.
236 * @param input input where to find for closing parenthesis
237 * @return int position of closing parenthesis increased by one
239 private static int findClosingParenthesis(@Nonnull final String input) {
243 while (position < input.length()) {
244 final char currentChar = input.charAt(position);
246 if (currentChar == STARTING_PARENTHESIS) {
250 if (currentChar == CLOSING_PARENTHESIS) {
261 // closing parenthesis was not found
262 if (position >= input.length()) {
263 throw new RestconfDocumentedException("Missing closing parenthesis in fields parameter",
264 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);