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.CaseSchemaNode;
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() {
29 * Create an absolute leafref path.
31 * @param leafRefPath leafRefPath
32 * @param contextNodeSchemaPath contextNodeSchemaPath
33 * @param module module
34 * @return LeafRefPath object
36 public static LeafRefPath createAbsoluteLeafRefPath(
37 final LeafRefPath leafRefPath, final SchemaPath contextNodeSchemaPath,
38 final Module module) {
39 if (leafRefPath.isAbsolute()) {
43 final Deque<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
44 contextNodeSchemaPath, module);
45 final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefPath.getPathFromRoot().iterator();
47 while (leafRefTgtPathFromRootIterator.hasNext()) {
48 final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
49 if (qname.equals(QNameWithPredicate.UP_PARENT)) {
50 absoluteLeafRefTargetPathList.removeLast();
52 absoluteLeafRefTargetPathList.add(qname);
56 return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
59 private static Deque<QNameWithPredicate> schemaPathToXPathQNames(final SchemaPath nodePath, final Module module) {
60 final Deque<QNameWithPredicate> xpath = new LinkedList<>();
61 final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot().iterator();
63 DataNodeContainer currenDataNodeContainer = module;
64 while (nodePathIterator.hasNext()) {
65 final QName qname = nodePathIterator.next();
66 final DataSchemaNode child = currenDataNodeContainer.getDataChildByName(qname);
68 if (child instanceof DataNodeContainer) {
69 if (!(child instanceof CaseSchemaNode)) {
70 xpath.add(new SimpleQNameWithPredicate(qname));
72 currenDataNodeContainer = (DataNodeContainer) child;
73 } else if (child instanceof ChoiceSchemaNode) {
74 if (nodePathIterator.hasNext()) {
75 currenDataNodeContainer = ((ChoiceSchemaNode) child).getCases().get(nodePathIterator.next());
79 } else if (child instanceof LeafSchemaNode || child instanceof LeafListSchemaNode) {
80 xpath.add(new SimpleQNameWithPredicate(qname));
82 } else if (child == null) {
83 throw new IllegalArgumentException("No child " + qname + " found in node container "
84 + currenDataNodeContainer + " in module " + module.getName());
86 throw new IllegalStateException("Illegal schema node type in the path: " + child.getClass());
93 public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath, final Module module) {
94 return LeafRefPath.create(schemaPathToXPathQNames(nodePath, module), true);