2 * Copyright (c) 2015 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.data.impl.leafref;
10 import java.util.Deque;
11 import java.util.Iterator;
12 import java.util.LinkedList;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 public final class LeafRefUtils {
24 private LeafRefUtils() {
25 throw new UnsupportedOperationException();
29 * @param leafRefPath leafRefPath
30 * @param contextNodeSchemaPath contextNodeSchemaPath
31 * @param module module
32 * @return LeafRefPath object
34 public static LeafRefPath createAbsoluteLeafRefPath(
35 final LeafRefPath leafRefPath, final SchemaPath contextNodeSchemaPath,
36 final Module module) {
38 if (leafRefPath.isAbsolute()) {
42 final Deque<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
43 contextNodeSchemaPath, module);
45 final Iterable<QNameWithPredicate> leafRefTargetPathFromRoot = leafRefPath
47 final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefTargetPathFromRoot
50 while (leafRefTgtPathFromRootIterator.hasNext()) {
51 final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
52 if (qname.equals(QNameWithPredicate.UP_PARENT)) {
53 absoluteLeafRefTargetPathList.removeLast();
55 absoluteLeafRefTargetPathList.add(qname);
59 return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
63 * @param currentNodePath
65 * @param absoluteLeafRefTargetPathList
67 private static Deque<QNameWithPredicate> schemaPathToXPathQNames(
68 final SchemaPath nodePath, final Module module) {
70 final Deque<QNameWithPredicate> xpath = new LinkedList<>();
72 final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot()
75 DataNodeContainer currenDataNodeContainer = module;
76 while (nodePathIterator.hasNext()) {
77 final QName qname = nodePathIterator.next();
78 final DataSchemaNode child = currenDataNodeContainer
79 .getDataChildByName(qname);
81 if (child instanceof DataNodeContainer) {
82 if (!(child instanceof ChoiceCaseNode)) {
83 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
84 qname.getModule(), qname.getLocalName()).build();
87 currenDataNodeContainer = (DataNodeContainer) child;
88 } else if (child instanceof ChoiceSchemaNode) {
89 if (nodePathIterator.hasNext()) {
90 currenDataNodeContainer = ((ChoiceSchemaNode) child)
91 .getCaseNodeByName(nodePathIterator.next());
95 } else if (child instanceof LeafSchemaNode
96 || child instanceof LeafListSchemaNode) {
98 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
99 qname.getModule(), qname.getLocalName()).build();
103 } else if (child == null) {
104 throw new IllegalArgumentException("No child " + qname
105 + " found in node container " + currenDataNodeContainer
106 + " in module " + module.getName());
108 throw new IllegalStateException(
109 "Illegal schema node type in the path: "
117 public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath,
118 final Module module) {
119 final Deque<QNameWithPredicate> xpathQNames = schemaPathToXPathQNames(
121 return LeafRefPath.create(xpathQNames, true);