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