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.Iterator;
11 import java.util.LinkedList;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
14 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 public class LeafRefUtils {
26 * @param contextNodeSchemaPath
29 public static LeafRefPath createAbsoluteLeafRefPath(
30 final LeafRefPath leafRefPath, final SchemaPath contextNodeSchemaPath,
31 final Module module) {
33 if (leafRefPath.isAbsolute())
36 final LinkedList<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
37 contextNodeSchemaPath, module);
39 final Iterable<QNameWithPredicate> leafRefTargetPathFromRoot = leafRefPath
41 final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefTargetPathFromRoot
44 while (leafRefTgtPathFromRootIterator.hasNext()) {
45 final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
46 if (qname.equals(QNameWithPredicate.UP_PARENT)) {
47 absoluteLeafRefTargetPathList.removeLast();
49 absoluteLeafRefTargetPathList.add(qname);
53 return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
57 * @param currentNodePath
59 * @param absoluteLeafRefTargetPathList
61 private static LinkedList<QNameWithPredicate> schemaPathToXPathQNames(
62 final SchemaPath nodePath, final Module module) {
64 final LinkedList<QNameWithPredicate> xpath = new LinkedList<QNameWithPredicate>();
66 final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot()
69 DataNodeContainer currenDataNodeContainer = module;
70 while (nodePathIterator.hasNext()) {
71 final QName qname = nodePathIterator.next();
72 final DataSchemaNode child = currenDataNodeContainer
73 .getDataChildByName(qname);
75 if (child instanceof DataNodeContainer) {
76 if (!(child instanceof ChoiceCaseNode)) {
77 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
78 qname.getModule(), qname.getLocalName()).build();
81 currenDataNodeContainer = (DataNodeContainer) child;
82 } else if (child instanceof ChoiceSchemaNode) {
83 if (nodePathIterator.hasNext()) {
84 currenDataNodeContainer = ((ChoiceSchemaNode) child)
85 .getCaseNodeByName(nodePathIterator.next());
89 } else if (child instanceof LeafSchemaNode
90 || child instanceof LeafListSchemaNode) {
92 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
93 qname.getModule(), qname.getLocalName()).build();
97 } else if (child == null) {
98 throw new IllegalArgumentException("No child " + qname
99 + " found in node container " + currenDataNodeContainer
100 + " in module " + module.getName());
102 throw new IllegalStateException(
103 "Illegal schema node type in the path: "
111 public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath,
112 final Module module) {
113 final LinkedList<QNameWithPredicate> xpathQNames = schemaPathToXPathQNames(
115 return LeafRefPath.create(xpathQNames, true);