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;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.Date;
16 import java.util.LinkedList;
17 import java.util.List;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
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.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
32 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
33 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
34 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.UsesNode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.base.Preconditions;
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);
54 private SchemaContextUtil() {
58 * Method attempts to find DataSchemaNode in Schema Context via specified
59 * Schema Path. The returned DataSchemaNode from method will be the node at
60 * the end of the SchemaPath. If the DataSchemaNode is not present in the
61 * Schema Context the method will return <code>null</code>. <br>
62 * In case that Schema Context or Schema Path are not specified correctly
63 * (i.e. contains <code>null</code> values) the method will return
64 * IllegalArgumentException.
66 * @throws IllegalArgumentException
71 * Schema Path to search for
72 * @return SchemaNode from the end of the Schema Path or <code>null</code>
73 * if the Node is not present.
75 public static SchemaNode findDataSchemaNode(final SchemaContext context, final SchemaPath schemaPath) {
76 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
77 Preconditions.checkArgument(schemaPath != null, "Schema Path reference cannot be NULL");
79 final List<QName> prefixedPath = (schemaPath.getPath());
80 if (prefixedPath == null) {
81 LOG.debug("Schema path {} has null path", schemaPath);
85 LOG.trace("Looking for path {} in context {}", schemaPath, context);
86 return findNodeInSchemaContext(context, prefixedPath);
90 * Method attempts to find DataSchemaNode inside of provided Schema Context
91 * and Yang Module accordingly to Non-conditional Revision Aware XPath. The
92 * specified Module MUST be present in Schema Context otherwise the
93 * operation would fail and return <code>null</code>. <br>
94 * The Revision Aware XPath MUST be specified WITHOUT the conditional
95 * statement (i.e. without [cond]) in path, because in this state the Schema
96 * Context is completely unaware of data state and will be not able to
97 * properly resolve XPath. If the XPath contains condition the method will
98 * return IllegalArgumentException. <br>
99 * In case that Schema Context or Module or Revision Aware XPath contains
100 * <code>null</code> references the method will throw
101 * IllegalArgumentException <br>
102 * If the Revision Aware XPath is correct and desired Data Schema Node is
103 * present in Yang module or in depending module in Schema Context the
104 * method will return specified Data Schema Node, otherwise the operation
105 * will fail and method will return <code>null</code>.
107 * @throws IllegalArgumentException
113 * @param nonCondXPath
114 * Non Conditional Revision Aware XPath
115 * @return Returns Data Schema Node for specified Schema Context for given
116 * Non-conditional Revision Aware XPath, or <code>null</code> if the
117 * DataSchemaNode is not present in Schema Context.
119 public static SchemaNode findDataSchemaNode(final SchemaContext context, final Module module, final RevisionAwareXPath nonCondXPath) {
120 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
121 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
122 Preconditions.checkArgument(nonCondXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
124 String strXPath = nonCondXPath.toString();
125 if (strXPath != null) {
126 Preconditions.checkArgument(strXPath.indexOf('[') == -1, "Revision Aware XPath may not contain a condition");
127 if (nonCondXPath.isAbsolute()) {
128 List<QName> qnamedPath = xpathToQNamePath(context, module, strXPath);
129 if (qnamedPath != null) {
130 return findNodeInSchemaContext(context, qnamedPath);
138 * Method attempts to find DataSchemaNode inside of provided Schema Context
139 * and Yang Module accordingly to Non-conditional relative Revision Aware
140 * XPath. The specified Module MUST be present in Schema Context otherwise
141 * the operation would fail and return <code>null</code>. <br>
142 * The relative Revision Aware XPath MUST be specified WITHOUT the
143 * conditional statement (i.e. without [cond]) in path, because in this
144 * state the Schema Context is completely unaware of data state and will be
145 * not able to properly resolve XPath. If the XPath contains condition the
146 * method will return IllegalArgumentException. <br>
147 * The Actual Schema Node MUST be specified correctly because from this
148 * Schema Node will search starts. If the Actual Schema Node is not correct
149 * the operation will simply fail, because it will be unable to find desired
150 * DataSchemaNode. <br>
151 * In case that Schema Context or Module or Actual Schema Node or relative
152 * Revision Aware XPath contains <code>null</code> references the method
153 * will throw IllegalArgumentException <br>
154 * If the Revision Aware XPath doesn't have flag
155 * <code>isAbsolute == false</code> the method will throw
156 * IllegalArgumentException. <br>
157 * If the relative Revision Aware XPath is correct and desired Data Schema
158 * Node is present in Yang module or in depending module in Schema Context
159 * the method will return specified Data Schema Node, otherwise the
160 * operation will fail and method will return <code>null</code>.
162 * @throws IllegalArgumentException
168 * @param actualSchemaNode
170 * @param relativeXPath
171 * Relative Non Conditional Revision Aware XPath
172 * @return DataSchemaNode if is present in specified Schema Context for
173 * given relative Revision Aware XPath, otherwise will return
176 public static SchemaNode findDataSchemaNodeForRelativeXPath(final SchemaContext context, final Module module,
177 final SchemaNode actualSchemaNode, final RevisionAwareXPath relativeXPath) {
178 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
179 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
180 Preconditions.checkArgument(actualSchemaNode != null, "Actual Schema Node reference cannot be NULL");
181 Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
182 Preconditions.checkState(!relativeXPath.isAbsolute(),
183 "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
184 + "for non relative Revision Aware XPath use findDataSchemaNode method");
186 SchemaPath actualNodePath = actualSchemaNode.getPath();
187 if (actualNodePath != null) {
188 List<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualSchemaNode);
190 if (qnamePath != null) {
191 return findNodeInSchemaContext(context, qnamePath);
198 * Returns parent Yang Module for specified Schema Context in which Schema
199 * Node is declared. If the Schema Node is not present in Schema Context the
200 * operation will return <code>null</code>. <br>
201 * If Schema Context or Schema Node contains <code>null</code> references
202 * the method will throw IllegalArgumentException
204 * @throws IllegalArgumentException
210 * @return Yang Module for specified Schema Context and Schema Node, if
211 * Schema Node is NOT present, the method will returns
214 public static Module findParentModule(final SchemaContext context, final SchemaNode schemaNode) {
215 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL!");
216 Preconditions.checkArgument(schemaNode != null, "Schema Node cannot be NULL!");
217 Preconditions.checkState(schemaNode.getPath() != null, "Schema Path for Schema Node is not "
218 + "set properly (Schema Path is NULL)");
220 List<QName> qnamedPath = schemaNode.getPath().getPath();
221 if (qnamedPath == null || qnamedPath.isEmpty()) {
222 throw new IllegalStateException("Schema Path contains invalid state of path parts."
223 + "The Schema Path MUST contain at least ONE QName which defines namespace and Local name"
226 QName qname = qnamedPath.get(qnamedPath.size() - 1);
227 return context.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision());
230 public static SchemaNode findNodeInSchemaContext(final SchemaContext context, final List<QName> path) {
231 final QName current = path.get(0);
233 LOG.trace("Looking up module {} in context {}", current, path);
234 final Module module = context.findModuleByNamespaceAndRevision(current.getNamespace(), current.getRevision());
235 if (module == null) {
236 LOG.debug("Module {} not found", current);
240 return findNodeInModule(module, path);
243 public static GroupingDefinition findGrouping(final SchemaContext context, final Module module, final List<QName> path) {
244 QName first = path.get(0);
245 Module m = context.findModuleByNamespace(first.getNamespace()).iterator().next();
246 DataNodeContainer currentParent = m;
247 for (QName qname : path) {
248 boolean found = false;
249 DataNodeContainer node = (DataNodeContainer) currentParent.getDataChildByName(qname.getLocalName());
251 Set<GroupingDefinition> groupings = currentParent.getGroupings();
252 for (GroupingDefinition gr : groupings) {
253 if (gr.getQName().getLocalName().equals(qname.getLocalName())) {
260 currentParent = node;
263 Preconditions.checkArgument(found, "Failed to find referenced grouping: %s(%s)", path, qname.getLocalName());
266 return (GroupingDefinition) currentParent;
269 private static SchemaNode findNodeInModule(final Module module, final List<QName> path) {
270 final QName current = path.get(0);
272 LOG.trace("Looking for data container {} in module {}", current, module);
273 SchemaNode parent = module.getDataChildByName(current);
274 if (parent != null) {
275 final SchemaNode ret = findNode((DataSchemaNode) parent, nextLevel(path));
281 LOG.trace("Looking for RPC {} in module {}", current, module);
282 parent = getRpcByName(module, current);
283 if (parent != null) {
284 final SchemaNode ret = findNodeInRpc((RpcDefinition) parent, nextLevel(path));
290 LOG.trace("Looking for notification {} in module {}", current, module);
291 parent = getNotificationByName(module, current);
292 if (parent != null) {
293 final SchemaNode ret = findNodeInNotification((NotificationDefinition) parent, nextLevel(path));
299 LOG.trace("Looking for grouping {} in module {}", current, module);
300 parent = getGroupingByName(module, current);
301 if (parent != null) {
302 final SchemaNode ret = findNodeInGrouping((GroupingDefinition) parent, nextLevel(path));
308 LOG.debug("No node matching {} found in module {}", path, module);
312 private static SchemaNode findNodeInGrouping(final GroupingDefinition grouping, final List<QName> path) {
313 if (path.isEmpty()) {
314 LOG.debug("Found grouping {}", grouping);
318 LOG.trace("Looking for path {} in grouping {}", path, grouping);
319 final QName current = path.get(0);
320 final DataSchemaNode node = grouping.getDataChildByName(current);
322 LOG.debug("No node matching {} found in grouping {}", current, grouping);
326 return findNode(node, nextLevel(path));
329 private static SchemaNode findNodeInRpc(final RpcDefinition rpc, final List<QName> path) {
330 if (path.isEmpty()) {
331 LOG.debug("Found RPC {}", rpc);
335 LOG.trace("Looking for path {} in rpc {}", path, rpc);
336 final QName current = path.get(0);
337 switch (current.getLocalName()) {
339 return findNode(rpc.getInput(), nextLevel(path));
341 return findNode(rpc.getOutput(), nextLevel(path));
343 LOG.debug("Invalid component {} of path {} in RPC {}", current, path, rpc);
348 private static SchemaNode findNodeInNotification(final NotificationDefinition ntf, final List<QName> path) {
349 if (path.isEmpty()) {
350 LOG.debug("Found notification {}", ntf);
354 LOG.trace("Looking for path {} in notification {}", path, ntf);
355 final QName current = path.get(0);
356 DataSchemaNode node = ntf.getDataChildByName(current);
358 LOG.debug("No node matching {} found in notification {}", current, ntf);
362 return findNode(node, nextLevel(path));
365 private static SchemaNode findNode(final ChoiceNode parent, final List<QName> path) {
366 if (path.isEmpty()) {
369 QName current = path.get(0);
370 ChoiceCaseNode node = parent.getCaseNodeByName(current);
372 return findNodeInCase(node, nextLevel(path));
377 private static SchemaNode findNode(final ContainerSchemaNode parent, final List<QName> path) {
378 if (path.isEmpty()) {
382 final QName current = path.get(0);
383 final DataSchemaNode node = parent.getDataChildByName(current);
385 LOG.debug("Failed to find {} in parent {}", path, parent);
389 return findNode(node, nextLevel(path));
392 private static SchemaNode findNode(final ListSchemaNode parent, final List<QName> path) {
393 if (path.isEmpty()) {
397 QName current = path.get(0);
398 DataSchemaNode node = parent.getDataChildByName(current);
400 LOG.debug("Failed to find {} in parent {}", path, parent);
403 return findNode(node, nextLevel(path));
406 private static SchemaNode findNode(final DataSchemaNode parent, final List<QName> path) {
407 final SchemaNode node;
408 if (!path.isEmpty()) {
409 if (parent instanceof ContainerSchemaNode) {
410 node = findNode((ContainerSchemaNode) parent, path);
411 } else if (parent instanceof ListSchemaNode) {
412 node = findNode((ListSchemaNode) parent, path);
413 } else if (parent instanceof ChoiceNode) {
414 node = findNode((ChoiceNode) parent, path);
416 throw new IllegalArgumentException(
417 String.format("Path nesting violation in parent %s path %s", parent, path));
424 LOG.debug("Failed to find {} in parent {}", path, parent);
430 public static SchemaNode findNodeInCase(final ChoiceCaseNode parent, final List<QName> path) {
431 if (path.isEmpty()) {
435 QName current = path.get(0);
436 DataSchemaNode node = parent.getDataChildByName(current);
438 LOG.debug("Failed to find {} in parent {}", path, parent);
441 return findNode(node, nextLevel(path));
444 public static RpcDefinition getRpcByName(final Module module, final QName name) {
445 for (RpcDefinition rpc : module.getRpcs()) {
446 if (rpc.getQName().equals(name)) {
453 private static List<QName> nextLevel(final List<QName> path) {
454 return path.subList(1, path.size());
457 public static NotificationDefinition getNotificationByName(final Module module, final QName name) {
458 for (NotificationDefinition notification : module.getNotifications()) {
459 if (notification.getQName().equals(name)) {
466 public static GroupingDefinition getGroupingByName(final Module module, final QName name) {
467 for (GroupingDefinition grouping : module.getGroupings()) {
468 if (grouping.getQName().equals(name)) {
476 * Utility method which search for original node defined in grouping.
481 public static DataSchemaNode findOriginal(final DataSchemaNode node, final SchemaContext ctx) {
482 DataSchemaNode result = findCorrectTargetFromGrouping(node, ctx);
483 if (result == null) {
484 result = findCorrectTargetFromAugment(node, ctx);
485 if (result != null) {
486 if (result.isAddedByUses()) {
487 result = findOriginal(result, ctx);
494 private static DataSchemaNode findCorrectTargetFromGrouping(final DataSchemaNode node, final SchemaContext ctx) {
495 if (node.getPath().getPath().size() == 1) {
496 // uses is under module statement
497 Module m = findParentModule(ctx, node);
498 DataSchemaNode result = null;
499 for (UsesNode u : m.getUses()) {
500 SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPath());
501 if (!(targetGrouping instanceof GroupingDefinition)) {
502 throw new IllegalArgumentException(String.format("Failed to generate code for augment in %s", u));
504 GroupingDefinition gr = (GroupingDefinition) targetGrouping;
505 result = gr.getDataChildByName(node.getQName().getLocalName());
507 if (result == null) {
508 throw new IllegalArgumentException("Failed to generate code for augment");
512 DataSchemaNode result = null;
513 QName currentName = node.getQName();
514 List<QName> tmpPath = new ArrayList<>();
515 Object parent = null;
517 SchemaPath sp = node.getPath();
518 List<QName> names = sp.getPath();
519 List<QName> newNames = new ArrayList<>(names);
520 newNames.remove(newNames.size() - 1);
521 SchemaPath newSp = SchemaPath.create(newNames, sp.isAbsolute());
522 parent = findDataSchemaNode(ctx, newSp);
525 tmpPath.add(currentName);
526 if (parent instanceof DataNodeContainer) {
527 DataNodeContainer dataNodeParent = (DataNodeContainer) parent;
528 for (UsesNode u : dataNodeParent.getUses()) {
529 if (result == null) {
530 result = getResultFromUses(u, currentName.getLocalName(), ctx);
534 if (result == null) {
535 currentName = ((SchemaNode) parent).getQName();
536 if (parent instanceof SchemaNode) {
537 SchemaPath nodeSp = ((SchemaNode) parent).getPath();
538 List<QName> nodeNames = nodeSp.getPath();
539 List<QName> nodeNewNames = new ArrayList<>(nodeNames);
540 nodeNewNames.remove(nodeNewNames.size() - 1);
541 if (nodeNewNames.isEmpty()) {
542 parent = getParentModule((SchemaNode) parent, ctx);
544 SchemaPath nodeNewSp = SchemaPath.create(nodeNewNames, nodeSp.isAbsolute());
545 parent = findDataSchemaNode(ctx, nodeNewSp);
548 throw new IllegalArgumentException("Failed to generate code for augment");
551 } while (result == null && !(parent instanceof Module));
553 if (result != null) {
554 result = getTargetNode(tmpPath, result, ctx);
560 private static DataSchemaNode findCorrectTargetFromAugment(final DataSchemaNode node, final SchemaContext ctx) {
561 if (!node.isAugmenting()) {
565 QName currentName = node.getQName();
566 Object currentNode = node;
567 Object parent = node;
568 List<QName> tmpPath = new ArrayList<QName>();
569 List<SchemaNode> tmpTree = new ArrayList<SchemaNode>();
571 AugmentationSchema augment = null;
573 SchemaPath sp = ((SchemaNode) parent).getPath();
574 List<QName> names = sp.getPath();
575 List<QName> newNames = new ArrayList<>(names);
576 newNames.remove(newNames.size() - 1);
577 SchemaPath newSp = SchemaPath.create(newNames, sp.isAbsolute());
578 parent = findDataSchemaNode(ctx, newSp);
579 if (parent instanceof AugmentationTarget) {
580 tmpPath.add(currentName);
581 tmpTree.add((SchemaNode) currentNode);
582 augment = findNodeInAugment(((AugmentationTarget) parent).getAvailableAugmentations(), currentName);
583 if (augment == null) {
584 currentName = ((DataSchemaNode) parent).getQName();
585 currentNode = parent;
588 } while (((DataSchemaNode) parent).isAugmenting() && augment == null);
590 if (augment == null) {
593 Collections.reverse(tmpPath);
594 Collections.reverse(tmpTree);
595 Object actualParent = augment;
596 DataSchemaNode result = null;
597 for (QName name : tmpPath) {
598 if (actualParent instanceof DataNodeContainer) {
599 result = ((DataNodeContainer) actualParent).getDataChildByName(name.getLocalName());
600 actualParent = ((DataNodeContainer) actualParent).getDataChildByName(name.getLocalName());
602 if (actualParent instanceof ChoiceNode) {
603 result = ((ChoiceNode) actualParent).getCaseNodeByName(name.getLocalName());
604 actualParent = ((ChoiceNode) actualParent).getCaseNodeByName(name.getLocalName());
609 if (result.isAddedByUses()) {
610 result = findCorrectTargetFromAugmentGrouping(result, augment, tmpTree, ctx);
617 private static DataSchemaNode getResultFromUses(final UsesNode u, final String currentName, final SchemaContext ctx) {
618 SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPath());
620 Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
621 "Failed to generate code for augment in %s", u);
622 GroupingDefinition gr = (GroupingDefinition) targetGrouping;
623 return gr.getDataChildByName(currentName);
626 private static Module getParentModule(final SchemaNode node, final SchemaContext ctx) {
627 QName qname = node.getPath().getPath().get(0);
628 URI namespace = qname.getNamespace();
629 Date revision = qname.getRevision();
630 return ctx.findModuleByNamespaceAndRevision(namespace, revision);
633 private static DataSchemaNode getTargetNode(final List<QName> tmpPath, final DataSchemaNode node, final SchemaContext ctx) {
634 DataSchemaNode result = node;
635 if (tmpPath.size() == 1) {
636 if (result != null && result.isAddedByUses()) {
637 result = findOriginal(result, ctx);
641 DataSchemaNode newParent = result;
642 Collections.reverse(tmpPath);
645 for (QName name : tmpPath) {
646 // searching by local name is must, because node has different
647 // namespace in its original location
648 if (newParent == null) {
651 if (newParent instanceof DataNodeContainer) {
652 newParent = ((DataNodeContainer) newParent).getDataChildByName(name.getLocalName());
654 newParent = ((ChoiceNode) newParent).getCaseNodeByName(name.getLocalName());
657 if (newParent != null && newParent.isAddedByUses()) {
658 newParent = findOriginal(newParent, ctx);
664 private static AugmentationSchema findNodeInAugment(final Collection<AugmentationSchema> augments, final QName name) {
665 for (AugmentationSchema augment : augments) {
666 DataSchemaNode node = augment.getDataChildByName(name);
674 private static DataSchemaNode findCorrectTargetFromAugmentGrouping(final DataSchemaNode node,
675 final AugmentationSchema parentNode, final List<SchemaNode> dataTree, final SchemaContext ctx) {
677 DataSchemaNode result = null;
678 QName currentName = node.getQName();
679 List<QName> tmpPath = new ArrayList<>();
680 tmpPath.add(currentName);
682 Object parent = null;
685 if (dataTree.size() < 2 || dataTree.size() == i) {
688 parent = dataTree.get(dataTree.size() - (i + 1));
689 tmpPath.add(((SchemaNode) parent).getQName());
692 if (parent instanceof DataNodeContainer) {
693 DataNodeContainer dataNodeParent = (DataNodeContainer) parent;
694 for (UsesNode u : dataNodeParent.getUses()) {
695 if (result == null) {
696 result = getResultFromUses(u, currentName.getLocalName(), ctx);
701 if (result == null) {
703 currentName = ((SchemaNode) parent).getQName();
705 } while (result == null);
707 if (result != null) {
708 result = getTargetNode(tmpPath, result, ctx);
714 * Transforms string representation of XPath to Queue of QNames. The XPath
715 * is split by "/" and for each part of XPath is assigned correct module in
717 * If Schema Context, Parent Module or XPath string contains
718 * <code>null</code> values, the method will throws IllegalArgumentException
720 * @throws IllegalArgumentException
724 * @param parentModule
728 * @return return a list of QName
730 private static List<QName> xpathToQNamePath(final SchemaContext context, final Module parentModule, final String xpath) {
731 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
732 Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL");
733 Preconditions.checkArgument(xpath != null, "XPath string reference cannot be NULL");
735 List<QName> path = new LinkedList<QName>();
736 String[] prefixedPath = xpath.split("/");
737 for (String pathComponent : prefixedPath) {
738 if (!pathComponent.isEmpty()) {
739 path.add(stringPathPartToQName(context, parentModule, pathComponent));
746 * Transforms part of Prefixed Path as java String to QName. <br>
747 * If the string contains module prefix separated by ":" (i.e.
748 * mod:container) this module is provided from from Parent Module list of
749 * imports. If the Prefixed module is present in Schema Context the QName
750 * can be constructed. <br>
751 * If the Prefixed Path Part does not contains prefix the Parent's Module
752 * namespace is taken for construction of QName. <br>
753 * If Schema Context, Parent Module or Prefixed Path Part refers to
754 * <code>null</code> the method will throw IllegalArgumentException
756 * @throws IllegalArgumentException
760 * @param parentModule
762 * @param prefixedPathPart
763 * Prefixed Path Part string
764 * @return QName from prefixed Path Part String.
766 private static QName stringPathPartToQName(final SchemaContext context, final Module parentModule, final String prefixedPathPart) {
767 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
768 Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL");
769 Preconditions.checkArgument(prefixedPathPart != null, "Prefixed Path Part cannot be NULL!");
771 if (prefixedPathPart.contains(":")) {
772 String[] prefixedName = prefixedPathPart.split(":");
773 Module module = resolveModuleForPrefix(context, parentModule, prefixedName[0]);
774 Preconditions.checkArgument(module != null, "Failed to resolve xpath: no module found for prefix %s in module %s",
775 prefixedName[0], parentModule.getName());
776 return new QName(module.getNamespace(), module.getRevision(), prefixedName[1]);
778 return new QName(parentModule.getNamespace(), parentModule.getRevision(), prefixedPathPart);
783 * Method will attempt to resolve and provide Module reference for specified
784 * module prefix. Each Yang module could contains multiple imports which
785 * MUST be associated with corresponding module prefix. The method simply
786 * looks into module imports and returns the module that is bounded with
787 * specified prefix. If the prefix is not present in module or the prefixed
788 * module is not present in specified Schema Context, the method will return
789 * <code>null</code>. <br>
790 * If String prefix is the same as prefix of the specified Module the
791 * reference to this module is returned. <br>
792 * If Schema Context, Module or Prefix are referring to <code>null</code>
793 * the method will return IllegalArgumentException
795 * @throws IllegalArgumentException
803 * @return Module for given prefix in specified Schema Context if is
804 * present, otherwise returns <code>null</code>
806 private static Module resolveModuleForPrefix(final SchemaContext context, final Module module, final String prefix) {
807 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
808 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
809 Preconditions.checkArgument(prefix != null, "Prefix string cannot be NULL");
811 if (prefix.equals(module.getPrefix())) {
815 Set<ModuleImport> imports = module.getImports();
816 for (ModuleImport mi : imports) {
817 if (prefix.equals(mi.getPrefix())) {
818 return context.findModuleByName(mi.getModuleName(), mi.getRevision());
825 * @throws IllegalArgumentException
831 * @param relativeXPath
832 * Non conditional Revision Aware Relative XPath
833 * @param leafrefSchemaPath
834 * Schema Path for Leafref
835 * @return list of QName
837 private static List<QName> resolveRelativeXPath(final SchemaContext context, final Module module,
838 final RevisionAwareXPath relativeXPath, final SchemaNode leafrefParentNode) {
839 Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL");
840 Preconditions.checkArgument(module != null, "Module reference cannot be NULL");
841 Preconditions.checkArgument(relativeXPath != null, "Non Conditional Revision Aware XPath cannot be NULL");
842 Preconditions.checkState(!relativeXPath.isAbsolute(),
843 "Revision Aware XPath MUST be relative i.e. MUST contains ../, "
844 + "for non relative Revision Aware XPath use findDataSchemaNode method");
845 Preconditions.checkState(leafrefParentNode.getPath() != null,
846 "Schema Path reference for Leafref cannot be NULL");
848 List<QName> absolutePath = new LinkedList<QName>();
849 String strXPath = relativeXPath.toString();
850 String[] xpaths = strXPath.split("/");
853 while (xpaths[colCount].contains("..")) {
854 colCount = colCount + 1;
856 List<QName> path = leafrefParentNode.getPath().getPath();
858 int lenght = path.size() - colCount;
859 absolutePath.addAll(path.subList(0, lenght));
860 List<String> xpathsList = Arrays.asList(xpaths);
861 List<String> sublistedXPath = xpathsList.subList(colCount, xpaths.length);
862 List<QName> sublist = new ArrayList<>();
863 for (String pathPart : sublistedXPath) {
864 sublist.add(stringPathPartToQName(context, module, pathPart));
866 absolutePath.addAll(sublist);