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))
88 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
90 if (leafRefReferencingContext == null)
93 return leafRefReferencingContext.isReferencing();
96 public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
98 if ((node == null) || (root == null))
101 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
103 if (leafRefReferencingContext == null)
106 return leafRefReferencingContext.hasReferencingChild();
109 public static boolean isReferencedByLeafRef(final SchemaNode node,
110 final LeafRefContext root) {
112 if ((node == null) || (root == null))
115 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
117 if (leafRefReferencedByContext == null)
120 return leafRefReferencedByContext.isReferenced();
123 public static boolean hasChildReferencedByLeafRef(final SchemaNode node,
124 final LeafRefContext root) {
126 if ((node == null) || (root == null))
129 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
131 if (leafRefReferencedByContext == null)
134 return leafRefReferencedByContext.hasReferencedChild();
137 public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node,
138 final LeafRefContext root) {
140 return findAllLeafRefChilds(node.getPath(), root);
143 public static List<LeafRefContext> findAllLeafRefChilds(
144 final SchemaPath schemaPath, final LeafRefContext root) {
146 return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
149 public static List<LeafRefContext> findAllLeafRefChilds(
150 final Iterable<QName> pathFromRoot, final LeafRefContext root) {
152 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
154 final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
156 return allLeafRefsChilds;
159 public static List<LeafRefContext> findAllLeafRefChilds(
160 final LeafRefContext parent) {
162 final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<LeafRefContext>();
164 if (parent == null) {
165 return leafRefChilds;
168 if (parent.isReferencing()) {
169 leafRefChilds.add(parent);
170 return leafRefChilds;
172 final Set<Entry<QName, LeafRefContext>> childs = parent
173 .getReferencingChilds().entrySet();
174 for (final Entry<QName, LeafRefContext> child : childs) {
175 leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
178 return leafRefChilds;
181 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
182 final SchemaNode node, final LeafRefContext root) {
184 return findAllChildsReferencedByLeafRef(node.getPath(), root);
187 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
188 final SchemaPath schemaPath, final LeafRefContext root) {
190 return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(),
194 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
195 final Iterable<QName> pathFromRoot, final LeafRefContext root) {
197 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
199 final List<LeafRefContext> allChildsReferencedByLeafRef = findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
201 return allChildsReferencedByLeafRef;
204 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
205 final LeafRefContext parent) {
207 final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<LeafRefContext>();
209 if (parent == null) {
210 return childsReferencedByLeafRef;
213 if (parent.isReferenced()) {
214 childsReferencedByLeafRef.add(parent);
215 return childsReferencedByLeafRef;
217 final Set<Entry<QName, LeafRefContext>> childs = parent
218 .getReferencedByChilds().entrySet();
219 for (final Entry<QName, LeafRefContext> child : childs) {
220 childsReferencedByLeafRef
221 .addAll(findAllChildsReferencedByLeafRef(child
225 return childsReferencedByLeafRef;
228 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
229 final SchemaNode node, final LeafRefContext root) {
230 return getAllLeafRefsReferencingThisNode(node.getPath(), root);
233 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
234 final SchemaPath path, final LeafRefContext root) {
235 return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
238 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
239 final Iterable<QName> pathFromRoot, final LeafRefContext root) {
241 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
244 if (leafRefReferencedByContext == null)
245 return new HashMap<QName, LeafRefContext>();
247 return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();