cd3d29643e5d8e31ecda96378373a29c84b9418e
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
9
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;
22
23 public final class LeafRefUtils {
24     private LeafRefUtils() {
25         // Hidden on purpose
26     }
27
28     /**
29      * Create an absolute leafref path.
30      *
31      * @param leafRefPath leafRefPath
32      * @param contextNodeSchemaPath contextNodeSchemaPath
33      * @param module module
34      * @return LeafRefPath object
35      */
36     public static LeafRefPath createAbsoluteLeafRefPath(
37             final LeafRefPath leafRefPath, final SchemaPath contextNodeSchemaPath,
38             final Module module) {
39         if (leafRefPath.isAbsolute()) {
40             return leafRefPath;
41         }
42
43         final Deque<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
44                 contextNodeSchemaPath, module);
45         final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefPath.getPathFromRoot().iterator();
46
47         while (leafRefTgtPathFromRootIterator.hasNext()) {
48             final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
49             if (qname.equals(QNameWithPredicate.UP_PARENT)) {
50                 absoluteLeafRefTargetPathList.removeLast();
51             } else {
52                 absoluteLeafRefTargetPathList.add(qname);
53             }
54         }
55
56         return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
57     }
58
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();
62
63         DataNodeContainer currenDataNodeContainer = module;
64         while (nodePathIterator.hasNext()) {
65             final QName qname = nodePathIterator.next();
66             final DataSchemaNode child = currenDataNodeContainer.getDataChildByName(qname);
67
68             if (child instanceof DataNodeContainer) {
69                 if (!(child instanceof CaseSchemaNode)) {
70                     xpath.add(new SimpleQNameWithPredicate(qname));
71                 }
72                 currenDataNodeContainer = (DataNodeContainer) child;
73             } else if (child instanceof ChoiceSchemaNode) {
74                 if (nodePathIterator.hasNext()) {
75                     currenDataNodeContainer = ((ChoiceSchemaNode) child).findCase(nodePathIterator.next()).orElse(null);
76                 } else {
77                     break;
78                 }
79             } else if (child instanceof LeafSchemaNode || child instanceof LeafListSchemaNode) {
80                 xpath.add(new SimpleQNameWithPredicate(qname));
81                 break;
82             } else if (child == null) {
83                 throw new IllegalArgumentException("No child " + qname + " found in node container "
84                         + currenDataNodeContainer + " in module " + module.getName());
85             } else {
86                 throw new IllegalStateException("Illegal schema node type in the path: " + child.getClass());
87             }
88         }
89
90         return xpath;
91     }
92
93     public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath, final Module module) {
94         return LeafRefPath.create(schemaPathToXPathQNames(nodePath, module), true);
95     }
96 }