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() {
25 throw new UnsupportedOperationException();
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) {
40 if (leafRefPath.isAbsolute()) {
44 final Deque<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
45 contextNodeSchemaPath, module);
46 final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefPath.getPathFromRoot().iterator();
48 while (leafRefTgtPathFromRootIterator.hasNext()) {
49 final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
50 if (qname.equals(QNameWithPredicate.UP_PARENT)) {
51 absoluteLeafRefTargetPathList.removeLast();
53 absoluteLeafRefTargetPathList.add(qname);
57 return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
60 private static Deque<QNameWithPredicate> schemaPathToXPathQNames(
61 final SchemaPath nodePath, final Module module) {
63 final Deque<QNameWithPredicate> xpath = new LinkedList<>();
65 final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot()
68 DataNodeContainer currenDataNodeContainer = module;
69 while (nodePathIterator.hasNext()) {
70 final QName qname = nodePathIterator.next();
71 final DataSchemaNode child = currenDataNodeContainer
72 .getDataChildByName(qname);
74 if (child instanceof DataNodeContainer) {
75 if (!(child instanceof CaseSchemaNode)) {
76 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
77 qname.getModule(), qname.getLocalName()).build();
80 currenDataNodeContainer = (DataNodeContainer) child;
81 } else if (child instanceof ChoiceSchemaNode) {
82 if (nodePathIterator.hasNext()) {
83 currenDataNodeContainer = ((ChoiceSchemaNode) child)
84 .getCaseNodeByName(nodePathIterator.next());
88 } else if (child instanceof LeafSchemaNode
89 || child instanceof LeafListSchemaNode) {
91 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
92 qname.getModule(), qname.getLocalName()).build();
96 } else if (child == null) {
97 throw new IllegalArgumentException("No child " + qname
98 + " found in node container " + currenDataNodeContainer
99 + " in module " + module.getName());
101 throw new IllegalStateException(
102 "Illegal schema node type in the path: "
110 public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath, final Module module) {
111 return LeafRefPath.create(schemaPathToXPathQNames(nodePath, module), true);