2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.LinkedList;
13 import java.util.List;
15 import java.util.Map.Entry;
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;
21 public final class LeafRefContextUtils {
23 private LeafRefContextUtils() {
24 throw new UnsupportedOperationException();
27 public static LeafRefContext getLeafRefReferencingContext(final SchemaNode node,
28 final LeafRefContext root) {
29 final SchemaPath schemaPath = node.getPath();
30 return getLeafRefReferencingContext(schemaPath, root);
33 public static LeafRefContext getLeafRefReferencingContext(
34 final SchemaPath schemaPath, final LeafRefContext root) {
35 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
36 return getLeafRefReferencingContext(pathFromRoot, root);
39 public static LeafRefContext getLeafRefReferencingContext(
40 final Iterable<QName> pathFromRoot, LeafRefContext root) {
42 LeafRefContext leafRefCtx = null;
43 final Iterator<QName> iterator = pathFromRoot.iterator();
44 while (iterator.hasNext() && root != null) {
45 final QName qname = iterator.next();
46 leafRefCtx = root.getReferencingChildByName(qname);
47 if (iterator.hasNext()) {
55 public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node,
56 final LeafRefContext root) {
57 final SchemaPath schemaPath = node.getPath();
58 return getLeafRefReferencedByContext(schemaPath, root);
61 public static LeafRefContext getLeafRefReferencedByContext(
62 final SchemaPath schemaPath, final LeafRefContext root) {
63 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
64 return getLeafRefReferencedByContext(pathFromRoot, root);
67 public static LeafRefContext getLeafRefReferencedByContext(
68 final Iterable<QName> pathFromRoot, LeafRefContext root) {
70 LeafRefContext leafRefCtx = null;
71 final Iterator<QName> iterator = pathFromRoot.iterator();
72 while (iterator.hasNext() && root != null) {
73 final QName qname = iterator.next();
74 leafRefCtx = root.getReferencedChildByName(qname);
75 if (iterator.hasNext()) {
83 public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
85 if (node == null || root == null) {
89 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
91 if (leafRefReferencingContext == null) {
95 return leafRefReferencingContext.isReferencing();
98 public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
100 if (node == null || root == null) {
104 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
106 if (leafRefReferencingContext == null) {
110 return leafRefReferencingContext.hasReferencingChild();
113 public static boolean isReferencedByLeafRef(final SchemaNode node,
114 final LeafRefContext root) {
116 if (node == null || root == null) {
120 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
122 if (leafRefReferencedByContext == null) {
126 return leafRefReferencedByContext.isReferenced();
129 public static boolean hasChildReferencedByLeafRef(final SchemaNode node,
130 final LeafRefContext root) {
132 if ((node == null) || (root == null)) {
136 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
138 if (leafRefReferencedByContext == null) {
142 return leafRefReferencedByContext.hasReferencedChild();
145 public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node,
146 final LeafRefContext root) {
148 return findAllLeafRefChilds(node.getPath(), root);
151 public static List<LeafRefContext> findAllLeafRefChilds(
152 final SchemaPath schemaPath, final LeafRefContext root) {
154 return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
157 public static List<LeafRefContext> findAllLeafRefChilds(
158 final Iterable<QName> pathFromRoot, final LeafRefContext root) {
160 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
162 final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
164 return allLeafRefsChilds;
167 public static List<LeafRefContext> findAllLeafRefChilds(
168 final LeafRefContext parent) {
170 final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<LeafRefContext>();
172 if (parent == null) {
173 return leafRefChilds;
176 if (parent.isReferencing()) {
177 leafRefChilds.add(parent);
178 return leafRefChilds;
180 final Set<Entry<QName, LeafRefContext>> childs = parent
181 .getReferencingChilds().entrySet();
182 for (final Entry<QName, LeafRefContext> child : childs) {
183 leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
186 return leafRefChilds;
189 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
190 final SchemaNode node, final LeafRefContext root) {
192 return findAllChildsReferencedByLeafRef(node.getPath(), root);
195 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
196 final SchemaPath schemaPath, final LeafRefContext root) {
198 return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(),
202 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
203 final Iterable<QName> pathFromRoot, final LeafRefContext root) {
205 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
207 final List<LeafRefContext> allChildsReferencedByLeafRef = findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
209 return allChildsReferencedByLeafRef;
212 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
213 final LeafRefContext parent) {
215 final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<LeafRefContext>();
217 if (parent == null) {
218 return childsReferencedByLeafRef;
221 if (parent.isReferenced()) {
222 childsReferencedByLeafRef.add(parent);
223 return childsReferencedByLeafRef;
225 final Set<Entry<QName, LeafRefContext>> childs = parent
226 .getReferencedByChilds().entrySet();
227 for (final Entry<QName, LeafRefContext> child : childs) {
228 childsReferencedByLeafRef
229 .addAll(findAllChildsReferencedByLeafRef(child
233 return childsReferencedByLeafRef;
236 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
237 final SchemaNode node, final LeafRefContext root) {
238 return getAllLeafRefsReferencingThisNode(node.getPath(), root);
241 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
242 final SchemaPath path, final LeafRefContext root) {
243 return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
246 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
247 final Iterable<QName> pathFromRoot, final LeafRefContext root) {
249 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
252 if (leafRefReferencedByContext == null) {
253 return new HashMap<QName, LeafRefContext>();
256 return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();