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, final LeafRefContext root) {
28 final SchemaPath schemaPath = node.getPath();
29 return getLeafRefReferencingContext(schemaPath, root);
32 public static LeafRefContext getLeafRefReferencingContext(
33 final SchemaPath schemaPath, final LeafRefContext root) {
34 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
35 return getLeafRefReferencingContext(pathFromRoot, root);
38 public static LeafRefContext getLeafRefReferencingContext(final Iterable<QName> pathFromRoot, LeafRefContext root) {
39 LeafRefContext leafRefCtx = null;
40 final Iterator<QName> iterator = pathFromRoot.iterator();
41 while (iterator.hasNext() && root != null) {
42 final QName qname = iterator.next();
43 leafRefCtx = root.getReferencingChildByName(qname);
44 if (iterator.hasNext()) {
52 public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node, final LeafRefContext root) {
53 final SchemaPath schemaPath = node.getPath();
54 return getLeafRefReferencedByContext(schemaPath, root);
57 public static LeafRefContext getLeafRefReferencedByContext(
58 final SchemaPath schemaPath, final LeafRefContext root) {
59 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
60 return getLeafRefReferencedByContext(pathFromRoot, root);
63 public static LeafRefContext getLeafRefReferencedByContext(final Iterable<QName> pathFromRoot,
64 LeafRefContext root) {
66 LeafRefContext leafRefCtx = null;
67 final Iterator<QName> iterator = pathFromRoot.iterator();
68 while (iterator.hasNext() && root != null) {
69 final QName qname = iterator.next();
70 leafRefCtx = root.getReferencedChildByName(qname);
71 if (iterator.hasNext()) {
79 public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
80 if (node == null || root == null) {
84 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
85 if (leafRefReferencingContext == null) {
89 return leafRefReferencingContext.isReferencing();
92 public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
93 if (node == null || root == null) {
97 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
98 if (leafRefReferencingContext == null) {
102 return leafRefReferencingContext.hasReferencingChild();
105 public static boolean isReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
106 if (node == null || root == null) {
110 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
111 if (leafRefReferencedByContext == null) {
115 return leafRefReferencedByContext.isReferenced();
118 public static boolean hasChildReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
119 if (node == null || root == null) {
123 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
124 if (leafRefReferencedByContext == null) {
128 return leafRefReferencedByContext.hasReferencedChild();
131 public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node, final LeafRefContext root) {
132 return findAllLeafRefChilds(node.getPath(), root);
135 public static List<LeafRefContext> findAllLeafRefChilds(final SchemaPath schemaPath, final LeafRefContext root) {
136 return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
139 public static List<LeafRefContext> findAllLeafRefChilds(final Iterable<QName> pathFromRoot,
140 final LeafRefContext root) {
141 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(pathFromRoot, root);
142 final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
144 return allLeafRefsChilds;
147 public static List<LeafRefContext> findAllLeafRefChilds(final LeafRefContext parent) {
148 final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<>();
149 if (parent == null) {
150 return leafRefChilds;
153 if (parent.isReferencing()) {
154 leafRefChilds.add(parent);
155 return leafRefChilds;
158 final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencingChilds().entrySet();
159 for (final Entry<QName, LeafRefContext> child : childs) {
160 leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
162 return leafRefChilds;
165 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaNode node,
166 final LeafRefContext root) {
167 return findAllChildsReferencedByLeafRef(node.getPath(), root);
170 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaPath schemaPath,
171 final LeafRefContext root) {
172 return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(), root);
175 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final Iterable<QName> pathFromRoot,
176 final LeafRefContext root) {
178 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
179 final List<LeafRefContext> allChildsReferencedByLeafRef =
180 findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
182 return allChildsReferencedByLeafRef;
185 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final LeafRefContext parent) {
186 final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<>();
187 if (parent == null) {
188 return childsReferencedByLeafRef;
191 if (parent.isReferenced()) {
192 childsReferencedByLeafRef.add(parent);
193 return childsReferencedByLeafRef;
196 final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencedByChilds().entrySet();
197 for (final Entry<QName, LeafRefContext> child : childs) {
198 childsReferencedByLeafRef.addAll(findAllChildsReferencedByLeafRef(child.getValue()));
200 return childsReferencedByLeafRef;
203 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
204 final SchemaNode node, final LeafRefContext root) {
205 return getAllLeafRefsReferencingThisNode(node.getPath(), root);
208 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final SchemaPath path,
209 final LeafRefContext root) {
210 return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
213 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final Iterable<QName> pathFromRoot,
214 final LeafRefContext root) {
216 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
217 if (leafRefReferencedByContext == null) {
218 return new HashMap<>();
221 return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();