YANGTOOLS-706: Split out yang-parser-rfc7950
[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      * 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
40         if (leafRefPath.isAbsolute()) {
41             return leafRefPath;
42         }
43
44         final Deque<QNameWithPredicate> absoluteLeafRefTargetPathList = schemaPathToXPathQNames(
45                 contextNodeSchemaPath, module);
46         final Iterator<QNameWithPredicate> leafRefTgtPathFromRootIterator = leafRefPath.getPathFromRoot().iterator();
47
48         while (leafRefTgtPathFromRootIterator.hasNext()) {
49             final QNameWithPredicate qname = leafRefTgtPathFromRootIterator.next();
50             if (qname.equals(QNameWithPredicate.UP_PARENT)) {
51                 absoluteLeafRefTargetPathList.removeLast();
52             } else {
53                 absoluteLeafRefTargetPathList.add(qname);
54             }
55         }
56
57         return LeafRefPath.create(absoluteLeafRefTargetPathList, true);
58     }
59
60     private static Deque<QNameWithPredicate> schemaPathToXPathQNames(
61             final SchemaPath nodePath, final Module module) {
62
63         final Deque<QNameWithPredicate> xpath = new LinkedList<>();
64
65         final Iterator<QName> nodePathIterator = nodePath.getPathFromRoot()
66                 .iterator();
67
68         DataNodeContainer currenDataNodeContainer = module;
69         while (nodePathIterator.hasNext()) {
70             final QName qname = nodePathIterator.next();
71             final DataSchemaNode child = currenDataNodeContainer
72                     .getDataChildByName(qname);
73
74             if (child instanceof DataNodeContainer) {
75                 if (!(child instanceof ChoiceCaseNode)) {
76                     final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
77                             qname.getModule(), qname.getLocalName()).build();
78                     xpath.add(newQName);
79                 }
80                 currenDataNodeContainer = (DataNodeContainer) child;
81             } else if (child instanceof ChoiceSchemaNode) {
82                 if (nodePathIterator.hasNext()) {
83                     currenDataNodeContainer = ((ChoiceSchemaNode) child)
84                             .getCaseNodeByName(nodePathIterator.next());
85                 } else {
86                     break;
87                 }
88             } else if (child instanceof LeafSchemaNode
89                     || child instanceof LeafListSchemaNode) {
90
91                 final QNameWithPredicate newQName = new QNameWithPredicateBuilder(
92                         qname.getModule(), qname.getLocalName()).build();
93                 xpath.add(newQName);
94                 break;
95
96             } else if (child == null) {
97                 throw new IllegalArgumentException("No child " + qname
98                         + " found in node container " + currenDataNodeContainer
99                         + " in module " + module.getName());
100             } else {
101                 throw new IllegalStateException(
102                         "Illegal schema node type in the path: "
103                                 + child.getClass());
104             }
105         }
106
107         return xpath;
108     }
109
110     public static LeafRefPath schemaPathToLeafRefPath(final SchemaPath nodePath, final Module module) {
111         return LeafRefPath.create(schemaPathToXPathQNames(nodePath, module), true);
112     }
113 }