Merge "Bug 3051: Fixed pattern checks in generated DTOs"
[yangtools.git] / yang / 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
23     private LeafRefContextUtils() {
24         throw new UnsupportedOperationException();
25     }
26
27     public static LeafRefContext getLeafRefReferencingContext(final SchemaNode node,
28             final LeafRefContext root) {
29         final SchemaPath schemaPath = node.getPath();
30         return getLeafRefReferencingContext(schemaPath, root);
31     }
32
33     public static LeafRefContext getLeafRefReferencingContext(
34             final SchemaPath schemaPath, final LeafRefContext root) {
35         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
36         return getLeafRefReferencingContext(pathFromRoot, root);
37     }
38
39     public static LeafRefContext getLeafRefReferencingContext(
40             final Iterable<QName> pathFromRoot, LeafRefContext root) {
41
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()) {
48                 root = leafRefCtx;
49             }
50         }
51
52         return leafRefCtx;
53     }
54
55     public static LeafRefContext getLeafRefReferencedByContext(final SchemaNode node,
56             final LeafRefContext root) {
57         final SchemaPath schemaPath = node.getPath();
58         return getLeafRefReferencedByContext(schemaPath, root);
59     }
60
61     public static LeafRefContext getLeafRefReferencedByContext(
62             final SchemaPath schemaPath, final LeafRefContext root) {
63         final Iterable<QName> pathFromRoot = schemaPath.getPathFromRoot();
64         return getLeafRefReferencedByContext(pathFromRoot, root);
65     }
66
67     public static LeafRefContext getLeafRefReferencedByContext(
68             final Iterable<QName> pathFromRoot, LeafRefContext root) {
69
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()) {
76                 root = leafRefCtx;
77             }
78         }
79
80         return leafRefCtx;
81     }
82
83     public static boolean isLeafRef(final SchemaNode node, final LeafRefContext root) {
84
85         if ((node == null) || (root == null))
86             return false;
87
88         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
89                 node, root);
90         if (leafRefReferencingContext == null)
91             return false;
92
93         return leafRefReferencingContext.isReferencing();
94     }
95
96     public static boolean hasLeafRefChild(final SchemaNode node, final LeafRefContext root) {
97
98         if ((node == null) || (root == null))
99             return false;
100
101         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
102                 node, root);
103         if (leafRefReferencingContext == null)
104             return false;
105
106         return leafRefReferencingContext.hasReferencingChild();
107     }
108
109     public static boolean isReferencedByLeafRef(final SchemaNode node,
110             final LeafRefContext root) {
111
112         if ((node == null) || (root == null))
113             return false;
114
115         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
116                 node, root);
117         if (leafRefReferencedByContext == null)
118             return false;
119
120         return leafRefReferencedByContext.isReferenced();
121     }
122
123     public static boolean hasChildReferencedByLeafRef(final SchemaNode node,
124             final LeafRefContext root) {
125
126         if ((node == null) || (root == null))
127             return false;
128
129         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
130                 node, root);
131         if (leafRefReferencedByContext == null)
132             return false;
133
134         return leafRefReferencedByContext.hasReferencedChild();
135     }
136
137     public static List<LeafRefContext> findAllLeafRefChilds(final SchemaNode node,
138             final LeafRefContext root) {
139
140         return findAllLeafRefChilds(node.getPath(), root);
141     }
142
143     public static List<LeafRefContext> findAllLeafRefChilds(
144             final SchemaPath schemaPath, final LeafRefContext root) {
145
146         return findAllLeafRefChilds(schemaPath.getPathFromRoot(), root);
147     }
148
149     public static List<LeafRefContext> findAllLeafRefChilds(
150             final Iterable<QName> pathFromRoot, final LeafRefContext root) {
151
152         final LeafRefContext leafRefReferencingContext = getLeafRefReferencingContext(
153                 pathFromRoot, root);
154         final List<LeafRefContext> allLeafRefsChilds = findAllLeafRefChilds(leafRefReferencingContext);
155
156         return allLeafRefsChilds;
157     }
158
159     public static List<LeafRefContext> findAllLeafRefChilds(
160             final LeafRefContext parent) {
161
162         final LinkedList<LeafRefContext> leafRefChilds = new LinkedList<LeafRefContext>();
163
164         if (parent == null) {
165             return leafRefChilds;
166         }
167
168         if (parent.isReferencing()) {
169             leafRefChilds.add(parent);
170             return leafRefChilds;
171         } else {
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()));
176             }
177         }
178         return leafRefChilds;
179     }
180
181     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
182             final SchemaNode node, final LeafRefContext root) {
183
184         return findAllChildsReferencedByLeafRef(node.getPath(), root);
185     }
186
187     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
188             final SchemaPath schemaPath, final LeafRefContext root) {
189
190         return findAllChildsReferencedByLeafRef(schemaPath.getPathFromRoot(),
191                 root);
192     }
193
194     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
195             final Iterable<QName> pathFromRoot, final LeafRefContext root) {
196
197         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
198                 pathFromRoot, root);
199         final List<LeafRefContext> allChildsReferencedByLeafRef = findAllChildsReferencedByLeafRef(leafRefReferencedByContext);
200
201         return allChildsReferencedByLeafRef;
202     }
203
204     public static List<LeafRefContext> findAllChildsReferencedByLeafRef(
205             final LeafRefContext parent) {
206
207         final LinkedList<LeafRefContext> childsReferencedByLeafRef = new LinkedList<LeafRefContext>();
208
209         if (parent == null) {
210             return childsReferencedByLeafRef;
211         }
212
213         if (parent.isReferenced()) {
214             childsReferencedByLeafRef.add(parent);
215             return childsReferencedByLeafRef;
216         } else {
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
222                                 .getValue()));
223             }
224         }
225         return childsReferencedByLeafRef;
226     }
227
228     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
229             final SchemaNode node, final LeafRefContext root) {
230         return getAllLeafRefsReferencingThisNode(node.getPath(), root);
231     }
232
233     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
234             final SchemaPath path, final LeafRefContext root) {
235         return getAllLeafRefsReferencingThisNode(path.getPathFromRoot(), root);
236     }
237
238     public static Map<QName, LeafRefContext> getAllLeafRefsReferencingThisNode(
239             final Iterable<QName> pathFromRoot, final LeafRefContext root) {
240
241         final LeafRefContext leafRefReferencedByContext = getLeafRefReferencedByContext(
242                 pathFromRoot, root);
243
244         if (leafRefReferencedByContext == null)
245             return new HashMap<QName, LeafRefContext>();
246
247         return leafRefReferencedByContext.getAllReferencedByLeafRefCtxs();
248     }
249
250 }