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
8 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.CharMatcher;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.Iterator;
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Optional;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.restconf.common.errors.RestconfError;
23 import org.opendaylight.restconf.common.util.RestUtil;
24 import org.opendaylight.restconf.common.util.RestconfSchemaUtil;
25 import org.opendaylight.restconf.common.validation.RestconfValidationUtils;
26 import org.opendaylight.restconf.nb.rfc8040.codecs.RestCodec;
27 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
28 import org.opendaylight.restconf.nb.rfc8040.utils.parser.builder.ParserBuilderConstants;
29 import org.opendaylight.yangtools.concepts.Codec;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
35 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
36 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
37 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
50 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
51 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
52 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
55 * Deserializer for {@link String} to {@link YangInstanceIdentifier} for
59 public final class YangInstanceIdentifierDeserializer {
61 private YangInstanceIdentifierDeserializer() {
62 throw new UnsupportedOperationException("Util class.");
66 * Method to create {@link Iterable} from {@link PathArgument} which are
67 * parsing from data by {@link SchemaContext}.
69 * @param schemaContext
70 * for validate of parsing path arguments
73 * @return {@link Iterable} of {@link PathArgument}
75 public static Iterable<PathArgument> create(final SchemaContext schemaContext, final String data) {
76 final List<PathArgument> path = new LinkedList<>();
77 final MainVarsWrapper variables = new YangInstanceIdentifierDeserializer.MainVarsWrapper(
78 data, DataSchemaContextTree.from(schemaContext).getRoot(),
79 YangInstanceIdentifierDeserializer.MainVarsWrapper.STARTING_OFFSET, schemaContext);
81 while (!variables.allCharsConsumed()) {
83 final QName qname = prepareQName(variables);
85 // this is the last identifier (input is consumed) or end of identifier (slash)
86 if (variables.allCharsConsumed() || variables.currentChar() == RestconfConstants.SLASH) {
87 prepareIdentifier(qname, path, variables);
88 if (variables.getCurrent() == null) {
89 path.add(NodeIdentifier.create(qname));
91 path.add(variables.getCurrent().getIdentifier());
93 } else if (variables.currentChar() == ParserBuilderConstants.Deserializer.EQUAL) {
94 if (nextContextNode(qname, path, variables).getDataSchemaNode() instanceof ListSchemaNode) {
95 prepareNodeWithPredicates(qname, path, variables,
96 (ListSchemaNode) variables.getCurrent().getDataSchemaNode());
98 prepareNodeWithValue(qname, path, variables);
101 throw new IllegalArgumentException(
102 "Bad char " + variables.currentChar() + " on position " + variables.getOffset() + ".");
106 return ImmutableList.copyOf(path);
109 private static void prepareNodeWithPredicates(final QName qname, final List<PathArgument> path,
110 final MainVarsWrapper variables, final ListSchemaNode listSchemaNode) {
111 variables.checkValid(listSchemaNode != null, "Data schema node is null");
113 final Iterator<QName> keys = listSchemaNode.getKeyDefinition().iterator();
114 final ImmutableMap.Builder<QName, Object> values = ImmutableMap.builder();
116 // skip already expected equal sign
117 variables.skipCurrentChar();
119 // read key value separated by comma
120 while (keys.hasNext() && !variables.allCharsConsumed() && variables.currentChar() != RestconfConstants.SLASH) {
123 if (variables.currentChar() == ParserBuilderConstants.Deserializer.COMMA) {
124 values.put(keys.next(), ParserBuilderConstants.Deserializer.EMPTY_STRING);
125 variables.skipCurrentChar();
129 // check if next value is parsable
130 RestconfValidationUtils.checkDocumentedError(
131 ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE.matches(variables.currentChar()),
132 RestconfError.ErrorType.PROTOCOL,
133 RestconfError.ErrorTag.MALFORMED_MESSAGE,
138 final QName key = keys.next();
139 Optional<DataSchemaNode> leafSchemaNode = listSchemaNode.findDataChildByName(key);
140 if (!leafSchemaNode.isPresent()) {
141 throw new RestconfDocumentedException("Schema not found for " + key,
142 RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.BAD_ELEMENT);
145 final String value = findAndParsePercentEncoded(variables.nextIdentifierFromNextSequence(
146 ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE));
147 final Object valueByType = prepareValueByType(leafSchemaNode.get(), value, variables);
148 values.put(key, valueByType);
152 if (keys.hasNext() && !variables.allCharsConsumed()
153 && variables.currentChar() == ParserBuilderConstants.Deserializer.COMMA) {
154 variables.skipCurrentChar();
158 // the last key is considered to be empty
159 if (keys.hasNext()) {
160 if (variables.allCharsConsumed() || variables.currentChar() == RestconfConstants.SLASH) {
161 values.put(keys.next(), ParserBuilderConstants.Deserializer.EMPTY_STRING);
164 // there should be no more missing keys
165 RestconfValidationUtils.checkDocumentedError(
167 RestconfError.ErrorType.PROTOCOL,
168 RestconfError.ErrorTag.MISSING_ATTRIBUTE,
169 "Key value missing for: " + qname
173 path.add(new YangInstanceIdentifier.NodeIdentifierWithPredicates(qname, values.build()));
176 private static Object prepareValueByType(final DataSchemaNode schemaNode, final String value,
177 final MainVarsWrapper vars) {
178 Object decoded = null;
180 TypeDefinition<? extends TypeDefinition<?>> typedef = null;
181 if (schemaNode instanceof LeafListSchemaNode) {
182 typedef = ((LeafListSchemaNode) schemaNode).getType();
184 typedef = ((LeafSchemaNode) schemaNode).getType();
186 final TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(typedef);
187 if (baseType instanceof LeafrefTypeDefinition) {
188 typedef = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) baseType, vars.getSchemaContext(),
191 final Codec<Object, Object> codec = RestCodec.from(typedef, null, vars.getSchemaContext());
192 decoded = codec.deserialize(value);
193 if (decoded == null) {
194 if (baseType instanceof IdentityrefTypeDefinition) {
195 decoded = toQName(value, schemaNode, vars.getSchemaContext());
201 private static Object toQName(final String value, final DataSchemaNode schemaNode,
202 final SchemaContext schemaContext) {
203 final String moduleName = toModuleName(value);
204 final String nodeName = toNodeName(value);
205 final Module module = schemaContext.findModules(moduleName).iterator().next();
206 for (final IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
207 final QName qName = identitySchemaNode.getQName();
208 if (qName.getLocalName().equals(nodeName)) {
212 return QName.create(schemaNode.getQName().getNamespace(), schemaNode.getQName().getRevision(), nodeName);
215 private static String toNodeName(final String str) {
216 final int idx = str.indexOf(':');
221 if (str.indexOf(':', idx + 1) != -1) {
225 return str.substring(idx + 1);
228 private static String toModuleName(final String str) {
229 final int idx = str.indexOf(':');
234 if (str.indexOf(':', idx + 1) != -1) {
238 return str.substring(0, idx);
241 private static QName prepareQName(final MainVarsWrapper variables) {
242 variables.checkValid(ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(variables.currentChar()),
243 "Identifier must start with character from set 'a-zA-Z_'");
244 final String preparedPrefix = variables.nextIdentifierFromNextSequence(
245 ParserBuilderConstants.Deserializer.IDENTIFIER);
247 final String localName;
249 if (variables.allCharsConsumed()) {
250 return getQNameOfDataSchemaNode(preparedPrefix, variables);
253 switch (variables.currentChar()) {
254 case RestconfConstants.SLASH:
255 case ParserBuilderConstants.Deserializer.EQUAL:
256 prefix = preparedPrefix;
257 return getQNameOfDataSchemaNode(prefix, variables);
258 case ParserBuilderConstants.Deserializer.COLON:
259 prefix = preparedPrefix;
260 variables.skipCurrentChar();
261 variables.checkValid(
262 ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(variables.currentChar()),
263 "Identifier must start with character from set 'a-zA-Z_'");
264 localName = variables.nextIdentifierFromNextSequence(ParserBuilderConstants.Deserializer.IDENTIFIER);
266 if (!variables.allCharsConsumed()
267 && variables.currentChar() == ParserBuilderConstants.Deserializer.EQUAL) {
268 return getQNameOfDataSchemaNode(localName, variables);
270 final Module module = moduleForPrefix(prefix, variables.getSchemaContext());
271 checkArgument(module != null, "Failed to lookup prefix %s", prefix);
272 return QName.create(module.getQNameModule(), localName);
275 throw new IllegalArgumentException("Failed build path.");
279 private static void prepareNodeWithValue(final QName qname, final List<PathArgument> path,
280 final MainVarsWrapper variables) {
281 variables.skipCurrentChar();
282 final String value = variables.nextIdentifierFromNextSequence(
283 ParserBuilderConstants.Deserializer.IDENTIFIER_PREDICATE);
285 // exception if value attribute is missing
286 RestconfValidationUtils.checkDocumentedError(
288 RestconfError.ErrorType.PROTOCOL,
289 RestconfError.ErrorTag.MISSING_ATTRIBUTE,
290 "Value missing for: " + qname
292 final DataSchemaNode dataSchemaNode = variables.getCurrent().getDataSchemaNode();
293 final Object valueByType = prepareValueByType(dataSchemaNode, findAndParsePercentEncoded(value), variables);
294 path.add(new YangInstanceIdentifier.NodeWithValue<>(qname, valueByType));
297 private static void prepareIdentifier(final QName qname, final List<PathArgument> path,
298 final MainVarsWrapper variables) {
299 final DataSchemaContextNode<?> currentNode = nextContextNode(qname, path, variables);
300 if (currentNode == null) {
303 variables.checkValid(!currentNode.isKeyedEntry(),
304 "Entry " + qname + " requires key or value predicate to be present");
307 @SuppressFBWarnings("NP_NULL_ON_SOME_PATH") // code does check for null 'current' but FB doesn't recognize it
308 private static DataSchemaContextNode<?> nextContextNode(final QName qname, final List<PathArgument> path,
309 final MainVarsWrapper variables) {
310 final DataSchemaContextNode<?> initialContext = variables.getCurrent();
311 final DataSchemaNode initialDataSchema = initialContext.getDataSchemaNode();
313 DataSchemaContextNode<?> current = initialContext.getChild(qname);
314 variables.setCurrent(current);
316 if (current == null) {
317 final Optional<Module> module = variables.getSchemaContext().findModule(qname.getModule());
318 if (module.isPresent()) {
319 for (final RpcDefinition rpcDefinition : module.get().getRpcs()) {
320 if (rpcDefinition.getQName().getLocalName().equals(qname.getLocalName())) {
325 if (findActionDefinition(initialDataSchema, qname.getLocalName()).isPresent()) {
329 variables.checkValid(current != null, qname + " is not correct schema node identifier.");
330 while (current.isMixin()) {
331 path.add(current.getIdentifier());
332 current = current.getChild(qname);
333 variables.setCurrent(current);
338 private static String findAndParsePercentEncoded(final String preparedPrefix) {
339 if (!preparedPrefix.contains(String.valueOf(ParserBuilderConstants.Deserializer.PERCENT_ENCODING))) {
340 return preparedPrefix;
343 final StringBuilder parsedPrefix = new StringBuilder(preparedPrefix);
344 final CharMatcher matcher = CharMatcher.is(ParserBuilderConstants.Deserializer.PERCENT_ENCODING);
346 while (matcher.matchesAnyOf(parsedPrefix)) {
347 final int percentCharPosition = matcher.indexIn(parsedPrefix);
348 parsedPrefix.replace(
350 percentCharPosition + ParserBuilderConstants.Deserializer.LAST_ENCODED_CHAR,
351 String.valueOf((char) Integer.parseInt(parsedPrefix.substring(
352 percentCharPosition + ParserBuilderConstants.Deserializer.FIRST_ENCODED_CHAR,
353 percentCharPosition + ParserBuilderConstants.Deserializer.LAST_ENCODED_CHAR),
354 ParserBuilderConstants.Deserializer.PERCENT_ENCODED_RADIX)));
357 return parsedPrefix.toString();
360 private static QName getQNameOfDataSchemaNode(final String nodeName, final MainVarsWrapper variables) {
361 final DataSchemaNode dataSchemaNode = variables.getCurrent().getDataSchemaNode();
362 if (dataSchemaNode instanceof ContainerSchemaNode) {
363 return getQNameOfDataSchemaNode((ContainerSchemaNode) dataSchemaNode, nodeName);
364 } else if (dataSchemaNode instanceof ListSchemaNode) {
365 return getQNameOfDataSchemaNode((ListSchemaNode) dataSchemaNode, nodeName);
368 throw new UnsupportedOperationException("Unsupported schema node " + dataSchemaNode);
371 private static <T extends DataNodeContainer & SchemaNode & ActionNodeContainer> QName getQNameOfDataSchemaNode(
372 final T parent, String nodeName) {
373 final Optional<ActionDefinition> actionDef = findActionDefinition(parent, nodeName);
374 final SchemaNode node;
375 if (actionDef.isPresent()) {
376 node = actionDef.get();
378 node = RestconfSchemaUtil.findSchemaNodeInCollection(parent.getChildNodes(), nodeName);
380 return node.getQName();
383 private static Module moduleForPrefix(final String prefix, final SchemaContext schemaContext) {
384 return schemaContext.findModules(prefix).stream().findFirst().orElse(null);
387 private static Optional<ActionDefinition> findActionDefinition(final SchemaNode dataSchemaNode,
388 final String nodeName) {
389 requireNonNull(dataSchemaNode, "DataSchema Node must not be null.");
390 if (dataSchemaNode instanceof ActionNodeContainer) {
391 return ((ActionNodeContainer) dataSchemaNode).getActions().stream()
392 .filter(actionDef -> actionDef.getQName().getLocalName().equals(nodeName)).findFirst();
394 return Optional.empty();
397 private static final class MainVarsWrapper {
398 private static final int STARTING_OFFSET = 0;
400 private final SchemaContext schemaContext;
401 private final String data;
403 private DataSchemaContextNode<?> current;
406 MainVarsWrapper(final String data, final DataSchemaContextNode<?> current, final int offset,
407 final SchemaContext schemaContext) {
409 this.current = current;
410 this.offset = offset;
411 this.schemaContext = schemaContext;
414 boolean allCharsConsumed() {
415 return offset == data.length();
418 void checkValid(final boolean condition, final String errorMsg) {
419 checkArgument(condition, "Could not parse Instance Identifier '%s'. Offset: %s : Reason: %s", data, offset,
424 return data.charAt(offset);
427 void skipCurrentChar() {
431 String nextIdentifierFromNextSequence(final CharMatcher matcher) {
432 final int start = offset;
433 while (!allCharsConsumed() && matcher.matches(currentChar())) {
436 return data.substring(start, offset);
440 // every identifier except of the first MUST start with slash
441 if (offset != MainVarsWrapper.STARTING_OFFSET) {
442 checkValid(RestconfConstants.SLASH == currentChar(), "Identifier must start with '/'.");
444 // skip consecutive slashes, users often assume restconf URLs behave just as HTTP does by squashing
445 // multiple slashes into a single one
446 while (!allCharsConsumed() && RestconfConstants.SLASH == currentChar()) {
450 // check if slash is not also the last char in identifier
451 checkValid(!allCharsConsumed(), "Identifier cannot end with '/'.");
455 public DataSchemaContextNode<?> getCurrent() {
459 public void setCurrent(final DataSchemaContextNode<?> current) {
460 this.current = current;
463 public int getOffset() {
467 public SchemaContext getSchemaContext() {
468 return schemaContext;