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 {
22 private LeafRefContextUtils() {
26 public static LeafRefContext getLeafRefReferencingContext(final SchemaNode node, final LeafRefContext root) {
27 final SchemaPath schemaPath = node.getPath();
28 return getLeafRefReferencingContext(schemaPath, root);
31 public static LeafRefContext getLeafRefReferencingContext(
32 final SchemaPath schemaPath, final LeafRefContext root) {
33 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
34 return getLeafRefReferencingContext(pathFromRoot, root);
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()) {
51 public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node, final LeafRefContext root) {
52 final SchemaPath schemaPath = node.getPath();
53 return getLeafRefReferencedByContext(schemaPath, root);
56 public static LeafRefContext getLeafRefReferencedByContext(
57 final SchemaPath schemaPath, final LeafRefContext root) {
58 final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
59 return getLeafRefReferencedByContext(pathFromRoot, root);
62 public static LeafRefContext getLeafRefReferencedByContext(final Iterable<QName> pathFromRoot,
63 LeafRefContext root) {
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()) {
78 public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
79 if (node == null || root == null) {
83 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
84 if (leafRefReferencingContext == null) {
88 return leafRefReferencingContext.isReferencing();
91 public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
92 if (node == null || root == null) {
96 final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(node, root);
97 if (leafRefReferencingContext == null) {
101 return leafRefReferencingContext.hasReferencingChild();
104 public static boolean isReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
105 if (node == null || root == null) {
109 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
110 if (leafRefReferencedByContext == null) {
114 return leafRefReferencedByContext.isReferenced();
117 public static boolean hasChildReferencedByLeafRef(final SchemaNode node, final LeafRefContext root) {
118 if (node == null || root == null) {
122 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(node, root);
123 if (leafRefReferencedByContext == null) {
127 return leafRefReferencedByContext.hasReferencedChild();
130 public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node, final LeafRefContext root) {
131 return findAllLeafRefChilds(node.getPath(), root);
134 public static List<LeafRefContext> findAllLeafRefChilds(final SchemaPath schemaPath, final LeafRefContext root) {
135 return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
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);
143 return allLeafRefsChilds;
146 public static List<LeafRefContext> findAllLeafRefChilds(final LeafRefContext parent) {
147 final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<>();
148 if (parent == null) {
149 return leafRefChilds;
152 if (parent.isReferencing()) {
153 leafRefChilds.add(parent);
154 return leafRefChilds;
157 final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencingChilds().entrySet();
158 for (final Entry<QName, LeafRefContext> child : childs) {
159 leafRefChilds.addAll(findAllLeafRefChilds(child.getValue()));
161 return leafRefChilds;
164 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaNode node,
165 final LeafRefContext root) {
166 return findAllChildsReferencedByLeafRef(node.getPath(), root);
169 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final SchemaPath schemaPath,
170 final LeafRefContext root) {
171 return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(), root);
174 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final Iterable<QName> pathFromRoot,
175 final LeafRefContext root) {
177 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
178 final List<LeafRefContext> allChildsReferencedByLeafRef =
179 findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
181 return allChildsReferencedByLeafRef;
184 public static List<LeafRefContext> findAllChildsReferencedByLeafRef(final LeafRefContext parent) {
185 final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<>();
186 if (parent == null) {
187 return childsReferencedByLeafRef;
190 if (parent.isReferenced()) {
191 childsReferencedByLeafRef.add(parent);
192 return childsReferencedByLeafRef;
195 final Set<Entry<QName, LeafRefContext>> childs = parent.getReferencedByChilds().entrySet();
196 for (final Entry<QName, LeafRefContext> child : childs) {
197 childsReferencedByLeafRef.addAll(findAllChildsReferencedByLeafRef(child.getValue()));
199 return childsReferencedByLeafRef;
202 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
203 final SchemaNode node, final LeafRefContext root) {
204 return getAllLeafRefsReferencingThisNode(node.getPath(), root);
207 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final SchemaPath path,
208 final LeafRefContext root) {
209 return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
212 public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(final Iterable<QName> pathFromRoot,
213 final LeafRefContext root) {
215 final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(pathFromRoot, root);
216 if (leafRefReferencedByContext == null) {
217 return new HashMap<>();
220 return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();