Migrate a Assert.assertThat reference
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefContextUtils.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.HashMap;
11 import java.util.Iterator;
12 import java.util.LinkedList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20
21 public final class LeafRefContextUtils {
22     private LeafRefContextUtils() {
23         // Hidden on purpose
24     }
25
26     public static LeafRefContext getLeafRefReferencingContext(final SchemaNode node, final LeafRefContext root) {
27         final SchemaPath schemaPath = node.getPath();
28         return getLeafRefReferencingContext(schemaPath, root);
29     }
30
31     public static LeafRefContext getLeafRefReferencingContext(
32             final SchemaPath schemaPath, final LeafRefContext root) {
33         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
34         return getLeafRefReferencingContext(pathFromRoot, root);
35     }
36
37     public static LeafRefContext getLeafRefReferencingContext(final Iterable<QName> pathFromRoot, LeafRefContext root) {
38         LeafRefContext leafRefCtx = null;
39         final Iterator<QName> iterator = pathFromRoot.iterator();
40         while (iterator.hasNext() && root != null) {
41             final QName qname = iterator.next();
42             leafRefCtx = root.getReferencingChildByName(qname);
43             if (iterator.hasNext()) {
44                 root = leafRefCtx;
45             }
46         }
47
48         return leafRefCtx;
49     }
50
51     public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node, final LeafRefContext root) {
52         final SchemaPath schemaPath = node.getPath();
53         return getLeafRefReferencedByContext(schemaPath, root);
54     }
55
56     public static LeafRefContext getLeafRefReferencedByContext(
57             final SchemaPath schemaPath, final LeafRefContext root) {
58         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
59         return getLeafRefReferencedByContext(pathFromRoot, root);
60     }
61
62     public static LeafRefContext getLeafRefReferencedByContext(final Iterable<QName> pathFromRoot,
63             LeafRefContext root) {
64
65         LeafRefContext leafRefCtx = null;
66         final Iterator<QName> iterator = pathFromRoot.iterator();
67         while (iterator.hasNext() && root != null) {
68             final QName qname = iterator.next();
69             leafRefCtx = root.getReferencedChildByName(qname);
70             if (iterator.hasNext()) {
71                 root = leafRefCtx;
72             }
73         }
74
75         return leafRefCtx;
76     }
77
78     public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
79         if (node == null || root == null) {
80             return false;
81         }
82
83         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
84         if (leafRefReferencingContext == null) {
85             return false;
86         }
87
88         return leafRefReferencingContext.isReferencing();
89     }
90
91     public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
92         if (node == null || root == null) {
93             return false;
94         }
95
96         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
97         if (leafRefReferencingContext == null) {
98             return false;
99         }
100
101         return leafRefReferencingContext.hasReferencingChild();
102     }
103
104     public static boolean isReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
105         if (node == null || root == null) {
106             return false;
107         }
108
109         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
110         if (leafRefReferencedByContext == null) {
111             return false;
112         }
113
114         return leafRefReferencedByContext.isReferenced();
115     }
116
117     public static boolean hasChildReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
118         if (node == null || root == null) {
119             return false;
120         }
121
122         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
123         if (leafRefReferencedByContext == null) {
124             return false;
125         }
126
127         return leafRefReferencedByContext.hasReferencedChild();
128     }
129
130     public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node, final LeafRefContext root) {
131         return findAllLeafRefChilds(node.getPath(), root);
132     }
133
134     public static List<LeafRefContext> findAllLeafRefChilds(final SchemaPath schemaPath, final LeafRefContext root) {
135         return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
136     }
137
138     public static List<LeafRefContext> findAllLeafRefChilds(final Iterable<QName> pathFromRoot,
139             final LeafRefContext root) {
140         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(pathFromRoot, root);
141         final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
142
143         return allLeafRefsChilds;
144     }
145
146     public static List<LeafRefContext> findAllLeafRefChilds(final LeafRefContext parent) {
147         final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<>();
148         if (parent == null) {
149             return leafRefChilds;
150         }
151
152         if (parent.isReferencing()) {
153             leafRefChilds.add(parent);
154             return leafRefChilds;
155         }
156
157         final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencingChilds().entrySet();
158         for (final Entry<QName, LeafRefContext> child : childs) {
159             leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
160         }
161         return leafRefChilds;
162     }
163
164     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaNode node,
165             final LeafRefContext root) {
166         return findAllChildsReferencedByLeafRef(node.getPath(), root);
167     }
168
169     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaPath schemaPath,
170             final LeafRefContext root) {
171         return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(), root);
172     }
173
174     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final Iterable<QName> pathFromRoot,
175             final LeafRefContext root) {
176
177         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
178         final List<LeafRefContext> allChildsReferencedByLeafRef =
179                 findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
180
181         return allChildsReferencedByLeafRef;
182     }
183
184     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final LeafRefContext parent) {
185         final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<>();
186         if (parent == null) {
187             return childsReferencedByLeafRef;
188         }
189
190         if (parent.isReferenced()) {
191             childsReferencedByLeafRef.add(parent);
192             return childsReferencedByLeafRef;
193         }
194
195         final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencedByChilds().entrySet();
196         for (final Entry<QName, LeafRefContext> child : childs) {
197             childsReferencedByLeafRef.addAll(findAllChildsReferencedByLeafRef(child.getValue()));
198         }
199         return childsReferencedByLeafRef;
200     }
201
202     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
203             final SchemaNode node, final LeafRefContext root) {
204         return getAllLeafRefsReferencingThisNode(node.getPath(), root);
205     }
206
207     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final SchemaPath path,
208             final LeafRefContext root) {
209         return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
210     }
211
212     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final Iterable<QName> pathFromRoot,
213             final LeafRefContext root) {
214
215         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
216         if (leafRefReferencedByContext == null) {
217             return new HashMap<>();
218         }
219
220         return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();
221     }
222 }