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();
30 * @param contextNodeSchemaPath
33 public static LeafRefPath createAbsoluteLeafRefPath(
34 final LeafRefPath leafRefPath, final SchemaPath contextNodeSchemaPath,
35 final Module module) {
37 if (leafRefPath.isAbsolute()) {
41 final Deque<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
42 contextNodeSchemaPath, module);
44 final Iterable<QNameWithPredicate> leafRefTargetPathFromRoot = leafRefPath
46 final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefTargetPathFromRoot
49 while (leafRefTgtPathFromRootIterator.hasNext()) {
50 final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
51 if (qname.equals(QNameWithPredicate.UP_PARENT)) {
52 absoluteLeafRefTargetPathList.removeLast();
54 absoluteLeafRefTargetPathList.add(qname);
58 return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
62 * @param currentNodePath
64 * @param absoluteLeafRefTargetPathList
66 private static Deque<QNameWithPredicate> schemaPathToXPathQNames(
67 final SchemaPath nodePath, final Module module) {
69 final Deque<QNameWithPredicate> xpath = new LinkedList<>();
71 final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot()
74 DataNodeContainer currenDataNodeContainer = module;
75 while (nodePathIterator.hasNext()) {
76 final QName qname = nodePathIterator.next();
77 final DataSchemaNode child = currenDataNodeContainer
78 .getDataChildByName(qname);
80 if (child instanceof DataNodeContainer) {
81 if (!(child instanceof ChoiceCaseNode)) {
82 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
83 qname.getModule(), qname.getLocalName()).build();
86 currenDataNodeContainer = (DataNodeContainer) child;
87 } else if (child instanceof ChoiceSchemaNode) {
88 if (nodePathIterator.hasNext()) {
89 currenDataNodeContainer = ((ChoiceSchemaNode) child)
90 .getCaseNodeByName(nodePathIterator.next());
94 } else if (child instanceof LeafSchemaNode
95 || child instanceof LeafListSchemaNode) {
97 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
98 qname.getModule(), qname.getLocalName()).build();
102 } else if (child == null) {
103 throw new IllegalArgumentException("No child " + qname
104 + " found in node container " + currenDataNodeContainer
105 + " in module " + module.getName());
107 throw new IllegalStateException(
108 "Illegal schema node type in the path: "
116 public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath,
117 final Module module) {
118 final Deque<QNameWithPredicate> xpathQNames = schemaPathToXPathQNames(
120 return LeafRefPath.create(xpathQNames, true);