2 * Copyright (c) 2013 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.yangtools.yang.model.util;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Splitter;
14 import com.google.common.collect.Iterables;
15 import java.util.Arrays;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.List;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
33 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
34 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The Schema Context Util contains support methods for searching through Schema
46 * Context modules for specified schema nodes via Schema Path or Revision Aware
47 * XPath. The Schema Context Util is designed as mixin, so it is not
51 public final class SchemaContextUtil {
52 private static final Logger LOG = LoggerFactory.getLogger(SchemaContextUtil.class);
53 private static final Splitter COLON_SPLITTER = Splitter.on(':');
54 private static final Splitter SLASH_SPLITTER = Splitter.on('/');
56 private SchemaContextUtil() {
60 * Method attempts to find DataSchemaNode in Schema Context via specified
61 * Schema Path. The returned DataSchemaNode from method will be the node at
62 * the end of the SchemaPath. If the DataSchemaNode is not present in the
63 * Schema Context the method will return <code>null</code>. <br>
64 * In case that Schema Context or Schema Path are not specified correctly
65 * (i.e. contains <code>null</code> values) the method will return
66 * IllegalArgumentException.
68 * @throws IllegalArgumentException
73 * Schema Path to search for
74 * @return SchemaNode from the end of the Schema Path or <code>null</code>
75 * if the Node is not present.
77 public static SchemaNode findDataSchemaNode(final SchemaContext context, final SchemaPath schemaPath) {
78 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
79 Preconditions.checkArgument(schemaPath != null, "Schema Path reference cannot be NULL");
81 final Iterable<QName> prefixedPath = schemaPath.getPathFromRoot();
82 if (prefixedPath == null) {
83 LOG.debug("Schema path {} has null path", schemaPath);
87 LOG.trace("Looking for path {} in context {}", schemaPath, context);
88 return findNodeInSchemaContext(context, prefixedPath);
92 * Method attempts to find DataSchemaNode inside of provided Schema Context
93 * and Yang Module accordingly to Non-conditional Revision Aware XPath. The
94 * specified Module MUST be present in Schema Context otherwise the
95 * operation would fail and return <code>null</code>. <br>
96 * The Revision Aware XPath MUST be specified WITHOUT the conditional
97 * statement (i.e. without [cond]) in path, because in this state the Schema
98 * Context is completely unaware of data state and will be not able to
99 * properly resolve XPath. If the XPath contains condition the method will
100 * return IllegalArgumentException. <br>
101 * In case that Schema Context or Module or Revision Aware XPath contains
102 * <code>null</code> references the method will throw
103 * IllegalArgumentException <br>
104 * If the Revision Aware XPath is correct and desired Data Schema Node is
105 * present in Yang module or in depending module in Schema Context the
106 * method will return specified Data Schema Node, otherwise the operation
107 * will fail and method will return <code>null</code>.
109 * @throws IllegalArgumentException
115 * @param nonCondXPath
116 * Non Conditional Revision Aware XPath
117 * @return Returns Data Schema Node for specified Schema Context for given
118 * Non-conditional Revision Aware XPath, or <code>null</code> if the
119 * DataSchemaNode is not present in Schema Context.
121 public static SchemaNode findDataSchemaNode(final SchemaContext context, final Module module, final RevisionAwareXPath nonCondXPath) {
122 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
123 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
124 Preconditions.checkArgument(nonCondXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
126 final String strXPath = nonCondXPath.toString();
127 if (strXPath != null) {
128 Preconditions.checkArgument(strXPath.indexOf('[') == -1, "Revision Aware XPath may not contain a condition");
129 if (nonCondXPath.isAbsolute()) {
130 final List<QName> qnamedPath = xpathToQNamePath(context, module, strXPath);
131 if (qnamedPath != null) {
132 return findNodeInSchemaContext(context, qnamedPath);
140 * Method attempts to find DataSchemaNode inside of provided Schema Context
141 * and Yang Module accordingly to Non-conditional relative Revision Aware
142 * XPath. The specified Module MUST be present in Schema Context otherwise
143 * the operation would fail and return <code>null</code>. <br>
144 * The relative Revision Aware XPath MUST be specified WITHOUT the
145 * conditional statement (i.e. without [cond]) in path, because in this
146 * state the Schema Context is completely unaware of data state and will be
147 * not able to properly resolve XPath. If the XPath contains condition the
148 * method will return IllegalArgumentException. <br>
149 * The Actual Schema Node MUST be specified correctly because from this
150 * Schema Node will search starts. If the Actual Schema Node is not correct
151 * the operation will simply fail, because it will be unable to find desired
152 * DataSchemaNode. <br>
153 * In case that Schema Context or Module or Actual Schema Node or relative
154 * Revision Aware XPath contains <code>null</code> references the method
155 * will throw IllegalArgumentException <br>
156 * If the Revision Aware XPath doesn't have flag
157 * <code>isAbsolute == false</code> the method will throw
158 * IllegalArgumentException. <br>
159 * If the relative Revision Aware XPath is correct and desired Data Schema
160 * Node is present in Yang module or in depending module in Schema Context
161 * the method will return specified Data Schema Node, otherwise the
162 * operation will fail and method will return <code>null</code>.
164 * @throws IllegalArgumentException
170 * @param actualSchemaNode
172 * @param relativeXPath
173 * Relative Non Conditional Revision Aware XPath
174 * @return DataSchemaNode if is present in specified Schema Context for
175 * given relative Revision Aware XPath, otherwise will return
178 public static SchemaNode findDataSchemaNodeForRelativeXPath(final SchemaContext context, final Module module,
179 final SchemaNode actualSchemaNode, final RevisionAwareXPath relativeXPath) {
180 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
181 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
182 Preconditions.checkArgument(actualSchemaNode != null, "Actual Schema Node reference cannot be NULL");
183 Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
184 Preconditions.checkState(!relativeXPath.isAbsolute(),
185 "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
186 + "for non relative Revision Aware XPath use findDataSchemaNode method");
188 final SchemaPath actualNodePath = actualSchemaNode.getPath();
189 if (actualNodePath != null) {
190 final Iterable<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualSchemaNode);
192 if (qnamePath != null) {
193 return findNodeInSchemaContext(context, qnamePath);
200 * Returns parent Yang Module for specified Schema Context in which Schema
201 * Node is declared. If the Schema Node is not present in Schema Context the
202 * operation will return <code>null</code>. <br>
203 * If Schema Context or Schema Node contains <code>null</code> references
204 * the method will throw IllegalArgumentException
206 * @throws IllegalArgumentException
212 * @return Yang Module for specified Schema Context and Schema Node, if
213 * Schema Node is NOT present, the method will returns
216 public static Module findParentModule(final SchemaContext context, final SchemaNode schemaNode) {
217 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL!");
218 Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!");
219 Preconditions.checkState(schemaNode.getPath() != null, "Schema Path for Schema Node is not "
220 + "set properly (Schema Path is NULL)");
222 final QName qname = Iterables.getFirst(schemaNode.getPath().getPathTowardsRoot(), null);
223 Preconditions.checkState(qname != null,
224 "Schema Path contains invalid state of path parts. " +
225 "The Schema Path MUST contain at least ONE QName which defines namespace and Local name of path.");
226 return context.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision());
229 public static SchemaNode findNodeInSchemaContext(final SchemaContext context, final Iterable<QName> path) {
230 final QName current = path.iterator().next();
232 LOG.trace("Looking up module {} in context {}", current, path);
233 final Module module = context.findModuleByNamespaceAndRevision(current.getNamespace(), current.getRevision());
234 if (module == null) {
235 LOG.debug("Module {} not found", current);
239 return findNodeInModule(module, path);
243 * Returns NotificationDefinition from Schema Context
245 * @param schema SchemaContext in which lookup should be performed.
246 * @param path Schema Path of notification
247 * @return Notification schema or null, if notification is not present in schema context.
250 public static @Nullable NotificationDefinition getNotificationSchema(@Nonnull final SchemaContext schema,@Nonnull final SchemaPath path) {
251 Preconditions.checkNotNull(schema, "Schema context must not be null.");
252 Preconditions.checkNotNull(path, "Schema path must not be null.");
253 for (final NotificationDefinition potential : schema.getNotifications()) {
254 if (path.equals(potential.getPath())) {
262 * Returns RPC Input or Output Data container from RPC definition.
264 * @param schema SchemaContext in which lookup should be performed.
265 * @param path Schema path of RPC input/output data container
266 * @return Notification schema or null, if notification is not present in schema context.
269 public static @Nullable ContainerSchemaNode getRpcDataSchema(@Nonnull final SchemaContext schema,@Nonnull final SchemaPath path) {
270 Preconditions.checkNotNull(schema, "Schema context must not be null.");
271 Preconditions.checkNotNull(path, "Schema path must not be null.");
272 final Iterator<QName> it = path.getPathFromRoot().iterator();
273 Preconditions.checkArgument(it.hasNext(), "Rpc must have QName.");
274 final QName rpcName = it.next();
275 Preconditions.checkArgument(it.hasNext(), "input or output must be part of path.");
276 final QName inOrOut = it.next();
277 for (final RpcDefinition potential : schema.getOperations()) {
278 if (rpcName.equals(potential.getQName())) {
279 return SchemaNodeUtils.getRpcDataSchema(potential, inOrOut);
285 private static SchemaNode findNodeInModule(final Module module, final Iterable<QName> path) {
287 Preconditions.checkArgument(module != null, "Parent reference cannot be NULL");
288 Preconditions.checkArgument(path != null, "Path reference cannot be NULL");
290 if (!path.iterator().hasNext()) {
291 LOG.debug("No node matching {} found in node {}", path, module);
295 final QName current = path.iterator().next();
296 LOG.trace("Looking for node {} in module {}", current, module);
298 SchemaNode foundNode = null;
299 final Iterable<QName> nextPath = nextLevel(path);
301 foundNode = module.getDataChildByName(current);
302 if (foundNode != null && nextPath.iterator().hasNext()) {
303 foundNode = findNodeIn(foundNode, nextPath);
306 if (foundNode == null) {
307 foundNode = getGroupingByName(module, current);
308 if (foundNode != null && nextPath.iterator().hasNext()) {
309 foundNode = findNodeIn(foundNode, nextPath);
313 if (foundNode == null) {
314 foundNode = getRpcByName(module, current);
315 if (foundNode != null && nextPath.iterator().hasNext()) {
316 foundNode = findNodeIn(foundNode, nextPath);
320 if (foundNode == null) {
321 foundNode = getNotificationByName(module, current);
322 if (foundNode != null && nextPath.iterator().hasNext()) {
323 foundNode = findNodeIn(foundNode, nextPath);
327 if (foundNode == null) {
328 LOG.debug("No node matching {} found in node {}", path, module);
335 private static SchemaNode findNodeIn(final SchemaNode parent, final Iterable<QName> path) {
337 Preconditions.checkArgument(parent != null, "Parent reference cannot be NULL");
338 Preconditions.checkArgument(path != null, "Path reference cannot be NULL");
340 if (!path.iterator().hasNext()) {
341 LOG.debug("No node matching {} found in node {}", path, parent);
345 final QName current = path.iterator().next();
346 LOG.trace("Looking for node {} in node {}", current, parent);
348 SchemaNode foundNode = null;
349 final Iterable<QName> nextPath = nextLevel(path);
351 if (parent instanceof DataNodeContainer) {
352 final DataNodeContainer parentDataNodeContainer = (DataNodeContainer) parent;
354 foundNode = parentDataNodeContainer.getDataChildByName(current);
355 if (foundNode != null && nextPath.iterator().hasNext()) {
356 foundNode = findNodeIn(foundNode, nextPath);
359 if (foundNode == null) {
360 foundNode = getGroupingByName(parentDataNodeContainer, current);
361 if (foundNode != null && nextPath.iterator().hasNext()) {
362 foundNode = findNodeIn(foundNode, nextPath);
367 if (foundNode == null && parent instanceof RpcDefinition) {
368 final RpcDefinition parentRpcDefinition = (RpcDefinition) parent;
370 if (current.getLocalName().equals("input")) {
371 foundNode = parentRpcDefinition.getInput();
372 if (foundNode != null && nextPath.iterator().hasNext()) {
373 foundNode = findNodeIn(foundNode, nextPath);
377 if (current.getLocalName().equals("output")) {
378 foundNode = parentRpcDefinition.getOutput();
379 if (foundNode != null && nextPath.iterator().hasNext()) {
380 foundNode = findNodeIn(foundNode, nextPath);
384 if (foundNode == null) {
385 foundNode = getGroupingByName(parentRpcDefinition, current);
386 if (foundNode != null && nextPath.iterator().hasNext()) {
387 foundNode = findNodeIn(foundNode, nextPath);
392 if (foundNode == null && parent instanceof ChoiceSchemaNode) {
393 foundNode = ((ChoiceSchemaNode) parent).getCaseNodeByName(current);
394 if (foundNode != null && nextPath.iterator().hasNext()) {
395 foundNode = findNodeIn(foundNode, nextPath);
399 if (foundNode == null) {
400 LOG.debug("No node matching {} found in node {}", path, parent);
407 private static Iterable<QName> nextLevel(final Iterable<QName> path) {
408 return Iterables.skip(path, 1);
411 private static RpcDefinition getRpcByName(final Module module, final QName name) {
412 for (final RpcDefinition rpc : module.getRpcs()) {
413 if (rpc.getQName().equals(name)) {
420 private static NotificationDefinition getNotificationByName(final Module module, final QName name) {
421 for (final NotificationDefinition notification : module.getNotifications()) {
422 if (notification.getQName().equals(name)) {
429 private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final QName name) {
430 for (final GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
431 if (grouping.getQName().equals(name)) {
438 private static GroupingDefinition getGroupingByName(final RpcDefinition rpc, final QName name) {
439 for (final GroupingDefinition grouping : rpc.getGroupings()) {
440 if (grouping.getQName().equals(name)) {
448 * Transforms string representation of XPath to Queue of QNames. The XPath
449 * is split by "/" and for each part of XPath is assigned correct module in
451 * If Schema Context, Parent Module or XPath string contains
452 * <code>null</code> values, the method will throws IllegalArgumentException
454 * @throws IllegalArgumentException
458 * @param parentModule
462 * @return return a list of QName
464 private static List<QName> xpathToQNamePath(final SchemaContext context, final Module parentModule, final String xpath) {
465 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
466 Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL");
467 Preconditions.checkArgument(xpath != null, "XPath string reference cannot be NULL");
469 final List<QName> path = new LinkedList<QName>();
470 for (final String pathComponent : SLASH_SPLITTER.split(xpath)) {
471 if (!pathComponent.isEmpty()) {
472 path.add(stringPathPartToQName(context, parentModule, pathComponent));
479 * Transforms part of Prefixed Path as java String to QName. <br>
480 * If the string contains module prefix separated by ":" (i.e.
481 * mod:container) this module is provided from from Parent Module list of
482 * imports. If the Prefixed module is present in Schema Context the QName
483 * can be constructed. <br>
484 * If the Prefixed Path Part does not contains prefix the Parent's Module
485 * namespace is taken for construction of QName. <br>
486 * If Schema Context, Parent Module or Prefixed Path Part refers to
487 * <code>null</code> the method will throw IllegalArgumentException
489 * @throws IllegalArgumentException
493 * @param parentModule
495 * @param prefixedPathPart
496 * Prefixed Path Part string
497 * @return QName from prefixed Path Part String.
499 private static QName stringPathPartToQName(final SchemaContext context, final Module parentModule, final String prefixedPathPart) {
500 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
501 Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL");
502 Preconditions.checkArgument(prefixedPathPart != null, "Prefixed Path Part cannot be NULL!");
504 if (prefixedPathPart.indexOf(':') != -1) {
505 final Iterator<String> prefixedName = COLON_SPLITTER.split(prefixedPathPart).iterator();
506 final String modulePrefix = prefixedName.next();
508 final Module module = resolveModuleForPrefix(context, parentModule, modulePrefix);
509 Preconditions.checkArgument(module != null, "Failed to resolve xpath: no module found for prefix %s in module %s",
510 modulePrefix, parentModule.getName());
512 return QName.create(module.getQNameModule(), prefixedName.next());
514 return QName.create(parentModule.getNamespace(), parentModule.getRevision(), prefixedPathPart);
519 * Method will attempt to resolve and provide Module reference for specified
520 * module prefix. Each Yang module could contains multiple imports which
521 * MUST be associated with corresponding module prefix. The method simply
522 * looks into module imports and returns the module that is bounded with
523 * specified prefix. If the prefix is not present in module or the prefixed
524 * module is not present in specified Schema Context, the method will return
525 * <code>null</code>. <br>
526 * If String prefix is the same as prefix of the specified Module the
527 * reference to this module is returned. <br>
528 * If Schema Context, Module or Prefix are referring to <code>null</code>
529 * the method will return IllegalArgumentException
531 * @throws IllegalArgumentException
539 * @return Module for given prefix in specified Schema Context if is
540 * present, otherwise returns <code>null</code>
542 private static Module resolveModuleForPrefix(final SchemaContext context, final Module module, final String prefix) {
543 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
544 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
545 Preconditions.checkArgument(prefix != null, "Prefix string cannot be NULL");
547 if (prefix.equals(module.getPrefix())) {
551 final Set<ModuleImport> imports = module.getImports();
552 for (final ModuleImport mi : imports) {
553 if (prefix.equals(mi.getPrefix())) {
554 return context.findModuleByName(mi.getModuleName(), mi.getRevision());
561 * @throws IllegalArgumentException
567 * @param relativeXPath
568 * Non conditional Revision Aware Relative XPath
569 * @param actualSchemaNode
571 * @return list of QName
573 private static Iterable<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
574 final RevisionAwareXPath relativeXPath, final SchemaNode actualSchemaNode) {
575 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
576 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
577 Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
578 Preconditions.checkState(!relativeXPath.isAbsolute(),
579 "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
580 + "for non relative Revision Aware XPath use findDataSchemaNode method");
581 Preconditions.checkState(actualSchemaNode.getPath() != null,
582 "Schema Path reference for Leafref cannot be NULL");
584 final Iterable<String> xpaths = SLASH_SPLITTER.split(relativeXPath.toString());
586 // Find out how many "parent" components there are
587 // FIXME: is .contains() the right check here?
588 // FIXME: case ../../node1/node2/../node3/../node4
590 for (final Iterator<String> it = xpaths.iterator(); it.hasNext() && it.next().contains(".."); ) {
594 final Iterable<QName> schemaNodePath = actualSchemaNode.getPath().getPathFromRoot();
596 if (Iterables.size(schemaNodePath) - colCount >= 0) {
597 return Iterables.concat(Iterables.limit(schemaNodePath, Iterables.size(schemaNodePath) - colCount),
598 Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
600 public QName apply(final String input) {
601 return stringPathPartToQName(context, module, input);
605 return Iterables.concat(schemaNodePath,
606 Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
608 public QName apply(final String input) {
609 return stringPathPartToQName(context, module, input);
615 * Extracts the base type of node on which schema node points to. If target node is again of type LeafrefTypeDefinition, methods will be call recursively until it reach concrete
618 * @param typeDefinition
619 * type of node which will be extracted
620 * @param schemaContext
624 * @return recursively found type definition this leafref is pointing to or null if the xpath is incorrect (null is there to preserve backwards compatibility)
626 public static TypeDefinition<?> getBaseTypeForLeafRef(final LeafrefTypeDefinition typeDefinition, final SchemaContext schemaContext, final SchemaNode schema) {
627 RevisionAwareXPath pathStatement = typeDefinition.getPathStatement();
628 pathStatement = new RevisionAwareXPathImpl(stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute());
630 Module parentModule = findParentModuleByType(schemaContext, schema);
632 final DataSchemaNode dataSchemaNode;
633 if(pathStatement.isAbsolute()) {
634 dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, pathStatement);
636 dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule, schema, pathStatement);
639 // FIXME this is just to preserve backwards compatibility since yangtools do not mind wrong leafref xpaths
640 // and current expected behaviour for such cases is to just use pure string
641 // This should throw an exception about incorrect XPath in leafref
642 if(dataSchemaNode == null) {
646 final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode);
648 if(targetTypeDefinition instanceof LeafrefTypeDefinition) {
649 return getBaseTypeForLeafRef(((LeafrefTypeDefinition) targetTypeDefinition), schemaContext, dataSchemaNode);
651 return targetTypeDefinition;
656 * Returns parent Yang Module for specified Schema Context in which Schema
657 * Node is declared. If Schema Node is of type 'ExtendedType' it tries to find parent module
658 * in which the type was originally declared (needed for correct leafref path resolution). <br>
659 * If the Schema Node is not present in Schema Context the
660 * operation will return <code>null</code>. <br>
661 * If Schema Context or Schema Node contains <code>null</code> references
662 * the method will throw IllegalArgumentException
664 * @throws IllegalArgumentException
666 * @param schemaContext
670 * @return Yang Module for specified Schema Context and Schema Node, if
671 * Schema Node is NOT present, the method will returns
674 public static Module findParentModuleByType(final SchemaContext schemaContext, final SchemaNode schemaNode) {
675 Preconditions.checkArgument(schemaContext != null, "Schema Context reference cannot be NULL!");
676 Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!");
677 TypeDefinition<?> nodeType = null;
679 if (schemaNode instanceof LeafSchemaNode) {
680 nodeType = ((LeafSchemaNode) schemaNode).getType();
681 } else if (schemaNode instanceof LeafListSchemaNode) {
682 nodeType = ((LeafListSchemaNode) schemaNode).getType();
685 if (nodeType != null && nodeType instanceof ExtendedType) {
686 while (nodeType.getBaseType() instanceof ExtendedType) {
687 nodeType = nodeType.getBaseType();
690 QNameModule typeDefModuleQname = nodeType.getQName().getModule();
692 return schemaContext.findModuleByNamespaceAndRevision(typeDefModuleQname.getNamespace(),
693 typeDefModuleQname.getRevision());
696 return SchemaContextUtil.findParentModule(schemaContext, schemaNode);
700 * Returns base type for {@code typeDefinition} which belongs to module specified via {@code qName}. This handle case
701 * when leafref type isn't specified as type substatement of leaf or leaf-list but is defined in other module as typedef
702 * which is then imported to referenced module.
704 * Because {@code typeDefinition} is definied via typedef statement, only absolute path is meaningful.
706 * @param typeDefinition
707 * @param schemaContext
711 public static TypeDefinition<?> getBaseTypeForLeafRef(final LeafrefTypeDefinition typeDefinition,
712 final SchemaContext schemaContext, final QName qName) {
713 final RevisionAwareXPath pathStatement = typeDefinition.getPathStatement();
714 final RevisionAwareXPath strippedPathStatement = new RevisionAwareXPathImpl(stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute());
715 if (!strippedPathStatement.isAbsolute()) {
719 final Module parentModule = schemaContext.findModuleByNamespaceAndRevision(qName.getNamespace(),qName.getRevision());
720 final DataSchemaNode dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, strippedPathStatement);
721 final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode);
722 if (targetTypeDefinition instanceof LeafrefTypeDefinition) {
723 return getBaseTypeForLeafRef(((LeafrefTypeDefinition) targetTypeDefinition), schemaContext, dataSchemaNode);
725 return targetTypeDefinition;
730 * Removes conditions from xPath pointed to target node.
732 * @param pathStatement
733 * xPath to target node
734 * @return string representation of xPath without conditions
737 private static String stripConditionsFromXPathString(final RevisionAwareXPath pathStatement) {
738 return pathStatement.toString().replaceAll("\\[.*\\]", "");
742 * Extracts the base type of leaf schema node until it reach concrete type of TypeDefinition.
745 * a node representing LeafSchemaNode
746 * @return concrete type definition of node value
748 private static TypeDefinition<? extends Object> typeDefinition(final LeafSchemaNode node) {
749 TypeDefinition<?> baseType = node.getType();
750 while (baseType.getBaseType() != null) {
751 baseType = baseType.getBaseType();
757 * Extracts the base type of leaf schema node until it reach concrete type of TypeDefinition.
760 * a node representing LeafListSchemaNode
761 * @return concrete type definition of node value
763 private static TypeDefinition<? extends Object> typeDefinition(final LeafListSchemaNode node) {
764 TypeDefinition<?> baseType = node.getType();
765 while (baseType.getBaseType() != null) {
766 baseType = baseType.getBaseType();
772 * Gets the base type of DataSchemaNode value.
775 * a node representing DataSchemaNode
776 * @return concrete type definition of node value
778 private static TypeDefinition<? extends Object> typeDefinition(final DataSchemaNode node) {
779 if (node instanceof LeafListSchemaNode) {
780 return typeDefinition((LeafListSchemaNode) node);
781 } else if (node instanceof LeafSchemaNode) {
782 return typeDefinition((LeafSchemaNode) node);
784 throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object> asList(node).toString());