LeafRefUtils is a utility class
[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.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;
22
23 public final class LeafRefUtils {
24     private LeafRefUtils() {
25         throw new UnsupportedOperationException();
26     }
27
28     /**
29      * @param leafRefPath
30      * @param contextNodeSchemaPath
31      * @return
32      */
33     public static LeafRefPath createAbsoluteLeafRefPath(
34             final LeafRefPath leafRefPath, final SchemaPath contextNodeSchemaPath,
35             final Module module) {
36
37         if (leafRefPath.isAbsolute()) {
38             return leafRefPath;
39         }
40
41         final Deque<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
42                 contextNodeSchemaPath, module);
43
44         final Iterable<QNameWithPredicate> leafRefTargetPathFromRoot = leafRefPath
45                 .getPathFromRoot();
46         final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefTargetPathFromRoot
47                 .iterator();
48
49         while (leafRefTgtPathFromRootIterator.hasNext()) {
50             final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
51             if (qname.equals(QNameWithPredicate.UP_PARENT)) {
52                 absoluteLeafRefTargetPathList.removeLast();
53             } else {
54                 absoluteLeafRefTargetPathList.add(qname);
55             }
56         }
57
58         return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
59     }
60
61     /**
62      * @param currentNodePath
63      * @param module
64      * @param absoluteLeafRefTargetPathList
65      */
66     private static Deque<QNameWithPredicate> schemaPathToXPathQNames(
67             final SchemaPath nodePath, final Module module) {
68
69         final Deque<QNameWithPredicate> xpath = new LinkedList<>();
70
71         final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot()
72                 .iterator();
73
74         DataNodeContainer currenDataNodeContainer = module;
75         while (nodePathIterator.hasNext()) {
76             final QName qname = nodePathIterator.next();
77             final DataSchemaNode child = currenDataNodeContainer
78                     .getDataChildByName(qname);
79
80             if (child instanceof DataNodeContainer) {
81                 if (!(child instanceof ChoiceCaseNode)) {
82                     final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
83                             qname.getModule(), qname.getLocalName()).build();
84                     xpath.add(newQName);
85                 }
86                 currenDataNodeContainer = (DataNodeContainer) child;
87             } else if (child instanceof ChoiceSchemaNode) {
88                 if (nodePathIterator.hasNext()) {
89                     currenDataNodeContainer = ((ChoiceSchemaNode) child)
90                             .getCaseNodeByName(nodePathIterator.next());
91                 } else {
92                     break;
93                 }
94             } else if (child instanceof LeafSchemaNode
95                     || child instanceof LeafListSchemaNode) {
96
97                 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
98                         qname.getModule(), qname.getLocalName()).build();
99                 xpath.add(newQName);
100                 break;
101
102             } else if (child == null) {
103                 throw new IllegalArgumentException("No child " + qname
104                         + " found in node container " + currenDataNodeContainer
105                         + " in module " + module.getName());
106             } else {
107                 throw new IllegalStateException(
108                         "Illegal schema node type in the path: "
109                                 + child.getClass());
110             }
111         }
112
113         return xpath;
114     }
115
116     public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath,
117             final Module module) {
118         final Deque<QNameWithPredicate> xpathQNames = schemaPathToXPathQNames(
119                 nodePath, module);
120         return LeafRefPath.create(xpathQNames, true);
121     }
122
123 }