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.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Splitter;
13 import com.google.common.collect.Iterables;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Date;
20 import java.util.Iterator;
21 import java.util.LinkedList;
22 import java.util.List;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
27 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
30 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
37 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
38 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
39 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.api.UsesNode;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The Schema Context Util contains support methods for searching through Schema
49 * Context modules for specified schema nodes via Schema Path or Revision Aware
50 * XPath. The Schema Context Util is designed as mixin, so it is not
54 public final class SchemaContextUtil {
55 private static final Logger LOG = LoggerFactory.getLogger(SchemaContextUtil.class);
56 private static final Splitter COLON_SPLITTER = Splitter.on(':');
57 private static final Splitter SLASH_SPLITTER = Splitter.on('/');
59 private SchemaContextUtil() {
63 * Method attempts to find DataSchemaNode in Schema Context via specified
64 * Schema Path. The returned DataSchemaNode from method will be the node at
65 * the end of the SchemaPath. If the DataSchemaNode is not present in the
66 * Schema Context the method will return <code>null</code>. <br>
67 * In case that Schema Context or Schema Path are not specified correctly
68 * (i.e. contains <code>null</code> values) the method will return
69 * IllegalArgumentException.
71 * @throws IllegalArgumentException
76 * Schema Path to search for
77 * @return SchemaNode from the end of the Schema Path or <code>null</code>
78 * if the Node is not present.
80 public static SchemaNode findDataSchemaNode(final SchemaContext context, final SchemaPath schemaPath) {
81 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
82 Preconditions.checkArgument(schemaPath != null, "Schema Path reference cannot be NULL");
84 final Iterable<QName> prefixedPath = schemaPath.getPathFromRoot();
85 if (prefixedPath == null) {
86 LOG.debug("Schema path {} has null path", schemaPath);
90 LOG.trace("Looking for path {} in context {}", schemaPath, context);
91 return findNodeInSchemaContext(context, prefixedPath);
95 * Method attempts to find DataSchemaNode inside of provided Schema Context
96 * and Yang Module accordingly to Non-conditional Revision Aware XPath. The
97 * specified Module MUST be present in Schema Context otherwise the
98 * operation would fail and return <code>null</code>. <br>
99 * The Revision Aware XPath MUST be specified WITHOUT the conditional
100 * statement (i.e. without [cond]) in path, because in this state the Schema
101 * Context is completely unaware of data state and will be not able to
102 * properly resolve XPath. If the XPath contains condition the method will
103 * return IllegalArgumentException. <br>
104 * In case that Schema Context or Module or Revision Aware XPath contains
105 * <code>null</code> references the method will throw
106 * IllegalArgumentException <br>
107 * If the Revision Aware XPath is correct and desired Data Schema Node is
108 * present in Yang module or in depending module in Schema Context the
109 * method will return specified Data Schema Node, otherwise the operation
110 * will fail and method will return <code>null</code>.
112 * @throws IllegalArgumentException
118 * @param nonCondXPath
119 * Non Conditional Revision Aware XPath
120 * @return Returns Data Schema Node for specified Schema Context for given
121 * Non-conditional Revision Aware XPath, or <code>null</code> if the
122 * DataSchemaNode is not present in Schema Context.
124 public static SchemaNode findDataSchemaNode(final SchemaContext context, final Module module, final RevisionAwareXPath nonCondXPath) {
125 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
126 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
127 Preconditions.checkArgument(nonCondXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
129 String strXPath = nonCondXPath.toString();
130 if (strXPath != null) {
131 Preconditions.checkArgument(strXPath.indexOf('[') == -1, "Revision Aware XPath may not contain a condition");
132 if (nonCondXPath.isAbsolute()) {
133 List<QName> qnamedPath = xpathToQNamePath(context, module, strXPath);
134 if (qnamedPath != null) {
135 return findNodeInSchemaContext(context, qnamedPath);
143 * Method attempts to find DataSchemaNode inside of provided Schema Context
144 * and Yang Module accordingly to Non-conditional relative Revision Aware
145 * XPath. The specified Module MUST be present in Schema Context otherwise
146 * the operation would fail and return <code>null</code>. <br>
147 * The relative Revision Aware XPath MUST be specified WITHOUT the
148 * conditional statement (i.e. without [cond]) in path, because in this
149 * state the Schema Context is completely unaware of data state and will be
150 * not able to properly resolve XPath. If the XPath contains condition the
151 * method will return IllegalArgumentException. <br>
152 * The Actual Schema Node MUST be specified correctly because from this
153 * Schema Node will search starts. If the Actual Schema Node is not correct
154 * the operation will simply fail, because it will be unable to find desired
155 * DataSchemaNode. <br>
156 * In case that Schema Context or Module or Actual Schema Node or relative
157 * Revision Aware XPath contains <code>null</code> references the method
158 * will throw IllegalArgumentException <br>
159 * If the Revision Aware XPath doesn't have flag
160 * <code>isAbsolute == false</code> the method will throw
161 * IllegalArgumentException. <br>
162 * If the relative Revision Aware XPath is correct and desired Data Schema
163 * Node is present in Yang module or in depending module in Schema Context
164 * the method will return specified Data Schema Node, otherwise the
165 * operation will fail and method will return <code>null</code>.
167 * @throws IllegalArgumentException
173 * @param actualSchemaNode
175 * @param relativeXPath
176 * Relative Non Conditional Revision Aware XPath
177 * @return DataSchemaNode if is present in specified Schema Context for
178 * given relative Revision Aware XPath, otherwise will return
181 public static SchemaNode findDataSchemaNodeForRelativeXPath(final SchemaContext context, final Module module,
182 final SchemaNode actualSchemaNode, final RevisionAwareXPath relativeXPath) {
183 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
184 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
185 Preconditions.checkArgument(actualSchemaNode != null, "Actual Schema Node reference cannot be NULL");
186 Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
187 Preconditions.checkState(!relativeXPath.isAbsolute(),
188 "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
189 + "for non relative Revision Aware XPath use findDataSchemaNode method");
191 SchemaPath actualNodePath = actualSchemaNode.getPath();
192 if (actualNodePath != null) {
193 Iterable<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualSchemaNode);
195 if (qnamePath != null) {
196 return findNodeInSchemaContext(context, qnamePath);
203 * Returns parent Yang Module for specified Schema Context in which Schema
204 * Node is declared. If the Schema Node is not present in Schema Context the
205 * operation will return <code>null</code>. <br>
206 * If Schema Context or Schema Node contains <code>null</code> references
207 * the method will throw IllegalArgumentException
209 * @throws IllegalArgumentException
215 * @return Yang Module for specified Schema Context and Schema Node, if
216 * Schema Node is NOT present, the method will returns
219 public static Module findParentModule(final SchemaContext context, final SchemaNode schemaNode) {
220 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL!");
221 Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!");
222 Preconditions.checkState(schemaNode.getPath() != null, "Schema Path for Schema Node is not "
223 + "set properly (Schema Path is NULL)");
225 final QName qname = Iterables.getFirst(schemaNode.getPath().getPathTowardsRoot(), null);
226 Preconditions.checkState(qname != null,
227 "Schema Path contains invalid state of path parts. " +
228 "The Schema Path MUST contain at least ONE QName which defines namespace and Local name of path.");
229 return context.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision());
232 public static SchemaNode findNodeInSchemaContext(final SchemaContext context, final Iterable<QName> path) {
233 final QName current = path.iterator().next();
235 LOG.trace("Looking up module {} in context {}", current, path);
236 final Module module = context.findModuleByNamespaceAndRevision(current.getNamespace(), current.getRevision());
237 if (module == null) {
238 LOG.debug("Module {} not found", current);
242 return findNodeInModule(module, path);
245 public static GroupingDefinition findGrouping(final SchemaContext context, final Module module, final List<QName> path) {
246 QName first = path.get(0);
247 Module m = context.findModuleByNamespace(first.getNamespace()).iterator().next();
248 DataNodeContainer currentParent = m;
249 for (QName qname : path) {
250 boolean found = false;
251 DataNodeContainer node = (DataNodeContainer) currentParent.getDataChildByName(qname.getLocalName());
253 Set<GroupingDefinition> groupings = currentParent.getGroupings();
254 for (GroupingDefinition gr : groupings) {
255 if (gr.getQName().getLocalName().equals(qname.getLocalName())) {
262 currentParent = node;
265 Preconditions.checkArgument(found, "Failed to find referenced grouping: %s(%s)", path, qname.getLocalName());
268 return (GroupingDefinition) currentParent;
271 private static SchemaNode findNodeInModule(final Module module, final Iterable<QName> path) {
272 final QName current = path.iterator().next();
274 LOG.trace("Looking for data container {} in module {}", current, module);
275 SchemaNode parent = module.getDataChildByName(current);
276 if (parent != null) {
277 final SchemaNode ret = findNode((DataSchemaNode) parent, nextLevel(path));
283 LOG.trace("Looking for RPC {} in module {}", current, module);
284 parent = getRpcByName(module, current);
285 if (parent != null) {
286 final SchemaNode ret = findNodeInRpc((RpcDefinition) parent, nextLevel(path));
292 LOG.trace("Looking for notification {} in module {}", current, module);
293 parent = getNotificationByName(module, current);
294 if (parent != null) {
295 final SchemaNode ret = findNodeInNotification((NotificationDefinition) parent, nextLevel(path));
301 LOG.trace("Looking for grouping {} in module {}", current, module);
302 parent = getGroupingByName(module, current);
303 if (parent != null) {
304 final SchemaNode ret = findNodeInGrouping((GroupingDefinition) parent, nextLevel(path));
310 LOG.debug("No node matching {} found in module {}", path, module);
314 private static SchemaNode findNodeInGrouping(final GroupingDefinition grouping, final Iterable<QName> path) {
315 final QName current = Iterables.getFirst(path, null);
316 if (current == null) {
317 LOG.debug("Found grouping {}", grouping);
321 LOG.trace("Looking for path {} in grouping {}", path, grouping);
322 final DataSchemaNode node = grouping.getDataChildByName(current);
324 LOG.debug("No node matching {} found in grouping {}", current, grouping);
328 return findNode(node, nextLevel(path));
331 private static SchemaNode findNodeInRpc(final RpcDefinition rpc, final Iterable<QName> path) {
332 final QName current = Iterables.getFirst(path, null);
333 if (current == null) {
334 LOG.debug("Found RPC {}", rpc);
338 LOG.trace("Looking for path {} in rpc {}", path, rpc);
339 switch (current.getLocalName()) {
341 return findNode(rpc.getInput(), nextLevel(path));
343 return findNode(rpc.getOutput(), nextLevel(path));
345 LOG.debug("Invalid component {} of path {} in RPC {}", current, path, rpc);
350 private static SchemaNode findNodeInNotification(final NotificationDefinition ntf, final Iterable<QName> path) {
351 final QName current = Iterables.getFirst(path, null);
352 if (current == null) {
353 LOG.debug("Found notification {}", ntf);
357 LOG.trace("Looking for path {} in notification {}", path, ntf);
358 DataSchemaNode node = ntf.getDataChildByName(current);
360 LOG.debug("No node matching {} found in notification {}", current, ntf);
364 return findNode(node, nextLevel(path));
367 private static SchemaNode findNode(final ChoiceNode parent, final Iterable<QName> path) {
368 final QName current = Iterables.getFirst(path, null);
369 if (current == null) {
372 ChoiceCaseNode node = parent.getCaseNodeByName(current);
374 return findNodeInCase(node, nextLevel(path));
379 private static SchemaNode findNode(final ContainerSchemaNode parent, final Iterable<QName> path) {
380 final QName current = Iterables.getFirst(path, null);
381 if (current == null) {
385 final DataSchemaNode node = parent.getDataChildByName(current);
387 LOG.debug("Failed to find {} in parent {}", path, parent);
391 return findNode(node, nextLevel(path));
394 private static SchemaNode findNode(final ListSchemaNode parent, final Iterable<QName> path) {
395 final QName current = Iterables.getFirst(path, null);
396 if (current == null) {
400 DataSchemaNode node = parent.getDataChildByName(current);
402 LOG.debug("Failed to find {} in parent {}", path, parent);
405 return findNode(node, nextLevel(path));
408 private static SchemaNode findNode(final DataSchemaNode parent, final Iterable<QName> path) {
409 final SchemaNode node;
410 if (!Iterables.isEmpty(path)) {
411 if (parent instanceof ContainerSchemaNode) {
412 node = findNode((ContainerSchemaNode) parent, path);
413 } else if (parent instanceof ListSchemaNode) {
414 node = findNode((ListSchemaNode) parent, path);
415 } else if (parent instanceof ChoiceNode) {
416 node = findNode((ChoiceNode) parent, path);
418 throw new IllegalArgumentException(
419 String.format("Path nesting violation in parent %s path %s", parent, path));
426 LOG.debug("Failed to find {} in parent {}", path, parent);
432 public static SchemaNode findNodeInCase(final ChoiceCaseNode parent, final Iterable<QName> path) {
433 final QName current = Iterables.getFirst(path, null);
434 if (current == null) {
438 DataSchemaNode node = parent.getDataChildByName(current);
440 LOG.debug("Failed to find {} in parent {}", path, parent);
443 return findNode(node, nextLevel(path));
446 public static RpcDefinition getRpcByName(final Module module, final QName name) {
447 for (RpcDefinition rpc : module.getRpcs()) {
448 if (rpc.getQName().equals(name)) {
455 private static Iterable<QName> nextLevel(final Iterable<QName> path) {
456 return Iterables.skip(path, 1);
459 public static NotificationDefinition getNotificationByName(final Module module, final QName name) {
460 for (NotificationDefinition notification : module.getNotifications()) {
461 if (notification.getQName().equals(name)) {
468 public static GroupingDefinition getGroupingByName(final Module module, final QName name) {
469 for (GroupingDefinition grouping : module.getGroupings()) {
470 if (grouping.getQName().equals(name)) {
478 * Utility method which search for original node defined in grouping.
483 public static DataSchemaNode findOriginal(final DataSchemaNode node, final SchemaContext ctx) {
484 DataSchemaNode result = findCorrectTargetFromGrouping(node, ctx);
485 if (result == null) {
486 result = findCorrectTargetFromAugment(node, ctx);
487 if (result != null) {
488 if (result.isAddedByUses()) {
489 result = findOriginal(result, ctx);
496 private static DataSchemaNode findCorrectImmediateTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) {
497 // uses is under module statement
498 final Module m = findParentModule(ctx, node);
499 Preconditions.checkArgument(m != null, "Failed to find module for node {} in context {}", node, ctx);
501 for (final UsesNode u : m.getUses()) {
502 final SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
503 Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
504 "Failed to generate code for augment in %s", u);
506 LOG.trace("Checking grouping {} for node {}", targetGrouping, node);
507 final GroupingDefinition gr = (GroupingDefinition) targetGrouping;
508 final DataSchemaNode result = gr.getDataChildByName(node.getQName().getLocalName());
509 if (result != null) {
513 LOG.debug("Skipped grouping {}, no matching node found", gr);
516 throw new IllegalArgumentException(
517 String.format("Failed to find uses node matching {} in context {}", node, ctx));
520 private static DataSchemaNode findCorrectTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) {
521 if (Iterables.size(node.getPath().getPathTowardsRoot()) != 1) {
522 QName currentName = node.getQName();
523 // tmpPath is used to track level of nesting
524 List<QName> tmpPath = new ArrayList<>();
525 Object parent = null;
527 // create schema path of parent node
528 SchemaPath sp = node.getPath().getParent();
529 parent = findDataSchemaNode(ctx, sp);
532 tmpPath.add(currentName);
534 DataSchemaNode result = null;
535 // search parent node's used groupings for presence of wanted
537 if (parent instanceof DataNodeContainer) {
538 DataNodeContainer dataNodeParent = (DataNodeContainer) parent;
539 for (UsesNode u : dataNodeParent.getUses()) {
540 result = getResultFromUses(u, currentName.getLocalName(), ctx);
541 if (result != null) {
547 // if node is not found in any of current parent's used
548 // groupings => parent is added by grouping too, so repeat same
549 // process for parent
550 if (result == null) {
551 final SchemaNode sn = (SchemaNode) parent;
553 // set current name to name of parent node
554 currentName = sn.getQName();
555 Preconditions.checkArgument(parent instanceof SchemaNode,
556 "Failed to generate code for augmend node {} at parent {}", node, parent);
558 // create schema path for parent of current parent
559 final SchemaPath parentSp = sn.getPath().getParent();
560 parent = parentSp.getPathFromRoot().iterator().hasNext() ? findDataSchemaNode(ctx, parentSp)
561 : getParentModule(sn, ctx);
563 // if wanted node was found in grouping, traverse this node
564 // based on level of nesting
565 return getTargetNode(tmpPath, result, ctx);
567 } while (!(parent instanceof Module));
571 return findCorrectImmediateTargetFromGrouping(node, ctx);
575 private static DataSchemaNode findCorrectTargetFromAugment(final DataSchemaNode node, final SchemaContext ctx) {
576 if (!node.isAugmenting()) {
580 QName currentName = node.getQName();
581 Object currentNode = node;
582 Object parent = node;
583 List<QName> tmpPath = new ArrayList<QName>();
584 List<SchemaNode> tmpTree = new ArrayList<SchemaNode>();
586 AugmentationSchema augment = null;
588 SchemaPath sp = ((SchemaNode) parent).getPath();
589 parent = findDataSchemaNode(ctx, sp.getParent());
590 if (parent instanceof AugmentationTarget) {
591 tmpPath.add(currentName);
592 tmpTree.add((SchemaNode) currentNode);
593 augment = findNodeInAugment(((AugmentationTarget) parent).getAvailableAugmentations(), currentName);
594 if (augment == null) {
595 currentName = ((DataSchemaNode) parent).getQName();
596 currentNode = parent;
599 } while (((DataSchemaNode) parent).isAugmenting() && augment == null);
601 if (augment == null) {
604 Collections.reverse(tmpPath);
605 Collections.reverse(tmpTree);
606 Object actualParent = augment;
607 DataSchemaNode result = null;
608 for (QName name : tmpPath) {
609 if (actualParent instanceof DataNodeContainer) {
610 result = ((DataNodeContainer) actualParent).getDataChildByName(name.getLocalName());
611 actualParent = ((DataNodeContainer) actualParent).getDataChildByName(name.getLocalName());
613 if (actualParent instanceof ChoiceNode) {
614 result = ((ChoiceNode) actualParent).getCaseNodeByName(name.getLocalName());
615 actualParent = ((ChoiceNode) actualParent).getCaseNodeByName(name.getLocalName());
620 if (result.isAddedByUses()) {
621 result = findCorrectTargetFromAugmentGrouping(result, augment, tmpTree, ctx);
628 private static DataSchemaNode getResultFromUses(final UsesNode u, final String currentName, final SchemaContext ctx) {
629 SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
631 Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
632 "Failed to generate code for augment in %s", u);
633 GroupingDefinition gr = (GroupingDefinition) targetGrouping;
634 return gr.getDataChildByName(currentName);
637 private static Module getParentModule(final SchemaNode node, final SchemaContext ctx) {
638 QName qname = node.getPath().getPathFromRoot().iterator().next();
639 URI namespace = qname.getNamespace();
640 Date revision = qname.getRevision();
641 return ctx.findModuleByNamespaceAndRevision(namespace, revision);
644 private static DataSchemaNode getTargetNode(final List<QName> tmpPath, final DataSchemaNode node, final SchemaContext ctx) {
645 DataSchemaNode result = node;
646 if (tmpPath.size() == 1) {
647 if (result != null && result.isAddedByUses()) {
648 result = findOriginal(result, ctx);
652 DataSchemaNode newParent = result;
653 Collections.reverse(tmpPath);
656 for (QName name : tmpPath) {
657 // searching by local name is must, because node has different
658 // namespace in its original location
659 if (newParent == null) {
662 if (newParent instanceof DataNodeContainer) {
663 newParent = ((DataNodeContainer) newParent).getDataChildByName(name.getLocalName());
665 newParent = ((ChoiceNode) newParent).getCaseNodeByName(name.getLocalName());
668 if (newParent != null && newParent.isAddedByUses()) {
669 newParent = findOriginal(newParent, ctx);
675 private static AugmentationSchema findNodeInAugment(final Collection<AugmentationSchema> augments, final QName name) {
676 for (AugmentationSchema augment : augments) {
677 DataSchemaNode node = augment.getDataChildByName(name);
685 private static DataSchemaNode findCorrectTargetFromAugmentGrouping(final DataSchemaNode node,
686 final AugmentationSchema parentNode, final List<SchemaNode> dataTree, final SchemaContext ctx) {
688 DataSchemaNode result = null;
689 QName currentName = node.getQName();
690 List<QName> tmpPath = new ArrayList<>();
691 tmpPath.add(currentName);
693 Object parent = null;
696 if (dataTree.size() < 2 || dataTree.size() == i) {
699 parent = dataTree.get(dataTree.size() - (i + 1));
700 tmpPath.add(((SchemaNode) parent).getQName());
703 if (parent instanceof DataNodeContainer) {
704 DataNodeContainer dataNodeParent = (DataNodeContainer) parent;
705 for (UsesNode u : dataNodeParent.getUses()) {
706 if (result == null) {
707 result = getResultFromUses(u, currentName.getLocalName(), ctx);
712 if (result == null) {
714 currentName = ((SchemaNode) parent).getQName();
716 } while (result == null);
718 if (result != null) {
719 result = getTargetNode(tmpPath, result, ctx);
725 * Transforms string representation of XPath to Queue of QNames. The XPath
726 * is split by "/" and for each part of XPath is assigned correct module in
728 * If Schema Context, Parent Module or XPath string contains
729 * <code>null</code> values, the method will throws IllegalArgumentException
731 * @throws IllegalArgumentException
735 * @param parentModule
739 * @return return a list of QName
741 private static List<QName> xpathToQNamePath(final SchemaContext context, final Module parentModule, final String xpath) {
742 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
743 Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL");
744 Preconditions.checkArgument(xpath != null, "XPath string reference cannot be NULL");
746 List<QName> path = new LinkedList<QName>();
747 for (String pathComponent : SLASH_SPLITTER.split(xpath)) {
748 if (!pathComponent.isEmpty()) {
749 path.add(stringPathPartToQName(context, parentModule, pathComponent));
756 * Transforms part of Prefixed Path as java String to QName. <br>
757 * If the string contains module prefix separated by ":" (i.e.
758 * mod:container) this module is provided from from Parent Module list of
759 * imports. If the Prefixed module is present in Schema Context the QName
760 * can be constructed. <br>
761 * If the Prefixed Path Part does not contains prefix the Parent's Module
762 * namespace is taken for construction of QName. <br>
763 * If Schema Context, Parent Module or Prefixed Path Part refers to
764 * <code>null</code> the method will throw IllegalArgumentException
766 * @throws IllegalArgumentException
770 * @param parentModule
772 * @param prefixedPathPart
773 * Prefixed Path Part string
774 * @return QName from prefixed Path Part String.
776 private static QName stringPathPartToQName(final SchemaContext context, final Module parentModule, final String prefixedPathPart) {
777 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
778 Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL");
779 Preconditions.checkArgument(prefixedPathPart != null, "Prefixed Path Part cannot be NULL!");
781 if (prefixedPathPart.indexOf(':') != -1) {
782 final Iterator<String> prefixedName = COLON_SPLITTER.split(prefixedPathPart).iterator();
783 final String modulePrefix = prefixedName.next();
785 Module module = resolveModuleForPrefix(context, parentModule, modulePrefix);
786 Preconditions.checkArgument(module != null, "Failed to resolve xpath: no module found for prefix %s in module %s",
787 modulePrefix, parentModule.getName());
789 // FIXME: Module should have a QNameModule handle
790 return QName.create(module.getNamespace(), module.getRevision(), prefixedName.next());
792 return QName.create(parentModule.getNamespace(), parentModule.getRevision(), prefixedPathPart);
797 * Method will attempt to resolve and provide Module reference for specified
798 * module prefix. Each Yang module could contains multiple imports which
799 * MUST be associated with corresponding module prefix. The method simply
800 * looks into module imports and returns the module that is bounded with
801 * specified prefix. If the prefix is not present in module or the prefixed
802 * module is not present in specified Schema Context, the method will return
803 * <code>null</code>. <br>
804 * If String prefix is the same as prefix of the specified Module the
805 * reference to this module is returned. <br>
806 * If Schema Context, Module or Prefix are referring to <code>null</code>
807 * the method will return IllegalArgumentException
809 * @throws IllegalArgumentException
817 * @return Module for given prefix in specified Schema Context if is
818 * present, otherwise returns <code>null</code>
820 private static Module resolveModuleForPrefix(final SchemaContext context, final Module module, final String prefix) {
821 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
822 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
823 Preconditions.checkArgument(prefix != null, "Prefix string cannot be NULL");
825 if (prefix.equals(module.getPrefix())) {
829 Set<ModuleImport> imports = module.getImports();
830 for (ModuleImport mi : imports) {
831 if (prefix.equals(mi.getPrefix())) {
832 return context.findModuleByName(mi.getModuleName(), mi.getRevision());
839 * @throws IllegalArgumentException
845 * @param relativeXPath
846 * Non conditional Revision Aware Relative XPath
847 * @param leafrefSchemaPath
848 * Schema Path for Leafref
849 * @return list of QName
851 private static Iterable<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
852 final RevisionAwareXPath relativeXPath, final SchemaNode leafrefParentNode) {
853 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
854 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
855 Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
856 Preconditions.checkState(!relativeXPath.isAbsolute(),
857 "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
858 + "for non relative Revision Aware XPath use findDataSchemaNode method");
859 Preconditions.checkState(leafrefParentNode.getPath() != null,
860 "Schema Path reference for Leafref cannot be NULL");
862 final Iterable<String> xpaths = SLASH_SPLITTER.split(relativeXPath.toString());
864 // Find out how many "parent" components there are
865 // FIXME: is .contains() the right check here?
867 for (Iterator<String> it = xpaths.iterator(); it.hasNext() && it.next().contains(".."); ) {
871 final Iterable<QName> parent = leafrefParentNode.getPath().getPathFromRoot();
872 return Iterables.concat(Iterables.limit(parent, Iterables.size(parent) - colCount),
873 Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
875 public QName apply(final String input) {
876 return stringPathPartToQName(context, module, input);