Cleanup checkstyle warnings and turn enforcement on in yang-data-impl
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
1 /*
2  * Copyright (c) 2013 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.schema;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Predicate;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.Maps;
18 import com.google.common.collect.Sets;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import javax.annotation.Nullable;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
31 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
33 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
34 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
35 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43
44 public final class SchemaUtils {
45     private SchemaUtils() {
46         throw new UnsupportedOperationException();
47     }
48
49     /**
50      * Find the first schema with specified QName.
51      *
52      * @param qname schema node to find
53      * @param dataSchemaNode Iterable of schemaNodes to look through
54      * @return schema node with newest revision or absent if no schema node with matching qname is found
55      */
56     public static Optional<DataSchemaNode> findFirstSchema(final QName qname,
57             final Iterable<DataSchemaNode> dataSchemaNode) {
58         DataSchemaNode schema = null;
59         if (dataSchemaNode != null && qname != null) {
60             for (final DataSchemaNode dsn : dataSchemaNode) {
61                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
62                     if (schema == null || schema.getQName().getRevision().compareTo(dsn.getQName().getRevision()) < 0) {
63                         schema = dsn;
64                     }
65                 } else if (dsn instanceof ChoiceSchemaNode) {
66                     for (final ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
67
68                         final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
69                         if (dataChildByName != null) {
70                             return Optional.of(dataChildByName);
71                         }
72                         final Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
73                         if (foundDsn.isPresent()) {
74                             return foundDsn;
75                         }
76                     }
77                 }
78             }
79         }
80         return Optional.fromNullable(schema);
81     }
82
83     /**
84      * Find child schema node identified by its QName within a provided schema node.
85      *
86      * @param schema schema for parent node - search root
87      * @param qname qname(with or without a revision) of a child node to be found in the parent schema
88      * @return found schema node
89      * @throws java.lang.IllegalStateException if the child was not found in parent schema node
90      */
91     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
92         // Try to find child schema node directly, but use a fallback that compares QNames without revisions
93         // and auto-expands choices
94         final DataSchemaNode dataChildByName = schema.getDataChildByName(qname);
95         return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName;
96     }
97
98     @Nullable
99     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname,
100             final boolean strictMode) {
101         if (strictMode) {
102             return findSchemaForChild(schema, qname);
103         }
104
105         final Optional<DataSchemaNode> childSchemaOptional = findFirstSchema(qname, schema.getChildNodes());
106         if (!childSchemaOptional.isPresent()) {
107             return null;
108         }
109         return childSchemaOptional.get();
110     }
111
112     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname,
113             final Iterable<DataSchemaNode> childNodes) {
114         final Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
115         Preconditions.checkState(childSchema.isPresent(),
116                 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
117         return childSchema.get();
118     }
119
120     public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
121         for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
122             final Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName,
123                 choiceCaseNode.getChildNodes());
124             if (childSchema.isPresent()) {
125                 return childSchema.get();
126             }
127         }
128
129
130         throw new IllegalStateException(String.format("Unknown child(ren) node(s) detected, identified by: %s, in: %s",
131                 childPartialQName, schema));
132     }
133
134     public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qnames) {
135         final Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qnames);
136         Preconditions.checkState(schemaForAugment.isPresent(),
137             "Unknown augmentation node detected, identified by: %s, in: %s", qnames, schema);
138         return schemaForAugment.get();
139     }
140
141     public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qnames) {
142         Optional<AugmentationSchema> schemaForAugment = Optional.absent();
143
144         for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
145             schemaForAugment = findAugment(choiceCaseNode, qnames);
146             if (schemaForAugment.isPresent()) {
147                 break;
148             }
149         }
150
151         Preconditions.checkState(schemaForAugment.isPresent(),
152             "Unknown augmentation node detected, identified by: %s, in: %s", qnames, schema);
153         return schemaForAugment.get();
154     }
155
156     private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qnames) {
157         for (final AugmentationSchema augment : schema.getAvailableAugmentations()) {
158             final HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(),
159                 DataSchemaNode::getQName));
160
161             if (qNamesFromAugment.equals(qnames)) {
162                 return Optional.of(augment);
163             }
164         }
165
166         return Optional.absent();
167     }
168
169     /**
170      * Recursively find all child nodes that come from choices.
171      *
172      * @param schema schema
173      * @return Map with all child nodes, to their most top augmentation
174      */
175     public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
176         return mapChildElementsFromChoices(schema, schema.getChildNodes());
177     }
178
179     private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema,
180             final Iterable<DataSchemaNode> childNodes) {
181         final Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
182
183         for (final DataSchemaNode childSchema : childNodes) {
184             if (childSchema instanceof ChoiceSchemaNode) {
185
186                 if (isFromAugment(schema, childSchema)) {
187                     continue;
188                 }
189
190                 for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
191                     for (final QName qname : getChildNodesRecursive(choiceCaseNode)) {
192                         mappedChoices.put(qname, (ChoiceSchemaNode) childSchema);
193                     }
194                 }
195             }
196         }
197
198         return mappedChoices;
199     }
200
201     private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
202         if (!(schema instanceof AugmentationTarget)) {
203             return false;
204         }
205
206         for (final AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
207             if (augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
208                 return true;
209             }
210         }
211
212         return false;
213     }
214
215     /**
216      * Recursively find all child nodes that come from augmentations.
217      *
218      * @param schema schema
219      * @return Map with all child nodes, to their most top augmentation
220      */
221     public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
222
223         final Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
224
225         // Find QNames of augmented child nodes
226         final Map<QName, AugmentationSchema> augments = Maps.newHashMap();
227         for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
228             for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
229                 augments.put(dataSchemaNode.getQName(), augmentationSchema);
230             }
231         }
232
233         // Augmented nodes have to be looked up directly in augmentationTarget
234         // because nodes from augment do not contain nodes from other augmentations
235         if (schema instanceof DataNodeContainer) {
236
237             for (final DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
238                 // If is not augmented child, continue
239                 if (!augments.containsKey(child.getQName())) {
240                     continue;
241                 }
242
243                 final AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
244
245                 // recursively add all child nodes in case of augment, case and choice
246                 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
247                     for (final QName qname : getChildNodesRecursive((DataNodeContainer) child)) {
248                         childNodesToAugmentation.put(qname, mostTopAugmentation);
249                     }
250                 } else if (child instanceof ChoiceSchemaNode) {
251                     for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
252                         for (final QName qname : getChildNodesRecursive(choiceCaseNode)) {
253                             childNodesToAugmentation.put(qname, mostTopAugmentation);
254                         }
255                     }
256                 } else {
257                     childNodesToAugmentation.put(child.getQName(), mostTopAugmentation);
258                 }
259             }
260         }
261
262         // Choice Node has to map child nodes from all its cases
263         if (schema instanceof ChoiceSchemaNode) {
264             for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
265                 if (!augments.containsKey(choiceCaseNode.getQName())) {
266                     continue;
267                 }
268
269                 for (final QName qname : getChildNodesRecursive(choiceCaseNode)) {
270                     childNodesToAugmentation.put(qname, augments.get(choiceCaseNode.getQName()));
271                 }
272             }
273         }
274
275         return childNodesToAugmentation;
276     }
277
278     /**
279      * Recursively list all child nodes. In case of choice, augment and cases, step in.
280      *
281      * @param nodeContainer node container
282      * @return set of QNames
283      */
284     public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
285         final Set<QName> allChildNodes = Sets.newHashSet();
286
287         for (final DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
288             if (childSchema instanceof ChoiceSchemaNode) {
289                 for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
290                     allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
291                 }
292             } else if (childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
293                 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
294             } else {
295                 allChildNodes.add(childSchema.getQName());
296             }
297         }
298
299         return allChildNodes;
300     }
301
302     /**
303      * Retrieves real schemas for augmented child node.
304      *
305      * <p>
306      * Schema of the same child node from augment, and directly from target is not the same.
307      * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
308      *
309      * @param targetSchema target schema
310      * @param augmentSchema augment schema
311      * @return set of nodes
312      */
313     public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema,
314             final AugmentationSchema augmentSchema) {
315         if (!targetSchema.getAvailableAugmentations().contains(augmentSchema)) {
316             return Collections.emptySet();
317         }
318
319         Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
320
321         if (targetSchema instanceof DataNodeContainer) {
322             realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
323         } else if (targetSchema instanceof ChoiceSchemaNode) {
324             for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
325                 for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
326                     if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
327                         realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
328                     }
329                 }
330             }
331         }
332
333         return realChildNodes;
334     }
335
336     public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
337             final AugmentationSchema augmentSchema) {
338         final Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
339         for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
340             final DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
341             realChildNodes.add(realChild);
342         }
343         return realChildNodes;
344     }
345
346     public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema,
347             final DataContainerChild<?, ?> child) {
348         for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
349             if (child instanceof AugmentationNode
350                     && belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) {
351                 return Optional.of(choiceCaseNode);
352             } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
353                 return Optional.of(choiceCaseNode);
354             }
355         }
356
357         return Optional.absent();
358     }
359
360     public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode,
361             final AugmentationIdentifier childToProcess) {
362         for (final AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
363
364             final Set<QName> currentAugmentChildNodes = Sets.newHashSet();
365             for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
366                 currentAugmentChildNodes.add(dataSchemaNode.getQName());
367             }
368
369             if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)) {
370                 return true;
371             }
372         }
373
374         return false;
375     }
376
377     /**
378      * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
379      * node is found then it is returned, else null.
380      *
381      * @param parent parent node
382      * @param child child node
383      * @return augmentation schema
384      */
385     public static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
386         if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
387             for (final AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
388                 final DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
389                 if (childInAugmentation != null) {
390                     return augmentation;
391                 }
392             }
393         }
394         return null;
395     }
396
397     public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
398         final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), DataSchemaNode::getQName);
399         return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
400     }
401
402     /**
403      * Finds schema node for given path in schema context. This method performs
404      * lookup in the namespace of all leafs, leaf-lists, lists, containers,
405      * choices, rpcs, actions, notifications, anydatas, and anyxmls according to
406      * Rfc6050/Rfc7950 section 6.2.1.
407      *
408      * @param schemaContext
409      *            schema context
410      * @param path
411      *            path
412      * @return schema node on path
413      */
414     public static SchemaNode findDataParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
415         SchemaNode current = Preconditions.checkNotNull(schemaContext);
416         for (final QName qname : path.getPathFromRoot()) {
417             current = findDataChildSchemaByQName(current, qname);
418         }
419         return current;
420     }
421
422     /**
423      * Finds schema node for given path in schema context. This method performs lookup in both the namespace
424      * of groupings and the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions,
425      * notifications, anydatas and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
426      *
427      * <p>
428      * This method is deprecated, because name conflicts can occur between the namespace of groupings and namespace
429      * of data nodes and in consequence lookup could be ambiguous.
430      *
431      * @param schemaContext
432      *            schema context
433      * @param path
434      *            path
435      * @return schema node on path
436      *
437      * @deprecated Use {@link #findParentSchemaNodesOnPath(SchemaContext, SchemaPath)} instead.
438      */
439     @Deprecated
440     public static SchemaNode findParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
441         SchemaNode current = Preconditions.checkNotNull(schemaContext);
442         for (final QName qname : path.getPathFromRoot()) {
443             current = findChildSchemaByQName(current, qname);
444         }
445         return current;
446     }
447
448     /**
449      * Find child data schema node identified by its QName within a provided schema node. This method performs lookup
450      * in the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions, notifications, anydatas
451      * and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
452      *
453      * @param node
454      *            schema node
455      * @param qname
456      *            QName
457      * @return data child schema node
458      * @throws IllegalArgumentException
459      *             if the schema node does not allow children
460      */
461     @Nullable
462     public static SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) {
463         SchemaNode child = null;
464         if (node instanceof DataNodeContainer) {
465             child = ((DataNodeContainer) node).getDataChildByName(qname);
466             if (child == null && node instanceof SchemaContext) {
467                 child = tryFindRpc((SchemaContext) node, qname).orNull();
468             }
469             if (child == null && node instanceof NotificationNodeContainer) {
470                 child = tryFindNotification((NotificationNodeContainer) node, qname).orNull();
471             }
472             if (child == null && node instanceof ActionNodeContainer) {
473                 child = tryFindAction((ActionNodeContainer) node, qname).orNull();
474             }
475         } else if (node instanceof ChoiceSchemaNode) {
476             child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
477         } else if (node instanceof RpcDefinition) {
478             switch (qname.getLocalName()) {
479                 case "input":
480                     child = ((RpcDefinition) node).getInput();
481                     break;
482                 case "output":
483                     child = ((RpcDefinition) node).getOutput();
484                     break;
485                 default:
486                     child = null;
487                     break;
488             }
489         } else {
490             throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
491         }
492
493         return child;
494     }
495
496     /**
497      * Find child schema node identified by its QName within a provided schema node. This method performs lookup
498      * in both the namespace of groupings and the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs,
499      * actions, notifications, anydatas and anyxmls according to RFC6050/RFC7950 section 6.2.1.
500      *
501      * <p>
502      * This method is deprecated, because name conflicts can occur between the namespace of groupings and namespace
503      * of data nodes and in consequence lookup could be ambiguous.
504      *
505      * @param node
506      *            schema node
507      * @param qname
508      *            QName
509      * @return child schema node
510      * @throws IllegalArgumentException
511      *             if the schema node does not allow children
512      *
513      * @deprecated Use {@link #findChildSchemaNodesByQName(SchemaNode, QName)} instead.
514      */
515     @Deprecated
516     public static SchemaNode findChildSchemaByQName(final SchemaNode node, final QName qname) {
517         SchemaNode child = findDataChildSchemaByQName(node, qname);
518         if (child == null && node instanceof DataNodeContainer) {
519             child = tryFindGroupings((DataNodeContainer) node, qname).orNull();
520         }
521
522         return child;
523     }
524
525     /**
526      * Finds schema node for given path in schema context. This method performs lookup in both the namespace
527      * of groupings and the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions,
528      * notifications, anydatas and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
529      *
530      * <p>
531      * This method returns collection of SchemaNodes, because name conflicts can occur between the namespace
532      * of groupings and namespace of data nodes. This method finds and collects all schema nodes that matches supplied
533      * SchemaPath and returns them all as collection of schema nodes.
534      *
535      * @param schemaContext
536      *            schema context
537      * @param path
538      *            path
539      * @return collection of schema nodes on path
540      */
541     public static Collection<SchemaNode> findParentSchemaNodesOnPath(final SchemaContext schemaContext,
542             final SchemaPath path) {
543         final Collection<SchemaNode> currentNodes = new ArrayList<>();
544         final Collection<SchemaNode> childNodes = new ArrayList<>();
545         currentNodes.add(Preconditions.checkNotNull(schemaContext));
546         for (final QName qname : path.getPathFromRoot()) {
547             for (final SchemaNode current : currentNodes) {
548                 childNodes.addAll(findChildSchemaNodesByQName(current, qname));
549             }
550             currentNodes.clear();
551             currentNodes.addAll(childNodes);
552             childNodes.clear();
553         }
554
555         return currentNodes;
556     }
557
558     /**
559      * Find child schema node identified by its QName within a provided schema node. This method performs lookup in both
560      * the namespace of groupings and the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs,
561      * actions, notifications, anydatas and anyxmls according to RFC6050/RFC7950 section 6.2.1.
562      *
563      * <p>
564      * This method returns collection of SchemaNodes, because name conflicts can occur between the namespace
565      * of groupings and namespace of data nodes. This method finds and collects all schema nodes with supplied QName
566      * and returns them all as collection of schema nodes.
567      *
568      * @param node
569      *            schema node
570      * @param qname
571      *            QName
572      * @return collection of child schema nodes
573      * @throws IllegalArgumentException
574      *             if the schema node does not allow children
575      */
576     public static Collection<SchemaNode> findChildSchemaNodesByQName(final SchemaNode node, final QName qname) {
577         final List<SchemaNode> childNodes = new ArrayList<>();
578         final SchemaNode dataNode = findDataChildSchemaByQName(node, qname);
579         if (dataNode != null) {
580             childNodes.add(dataNode);
581         }
582         if (node instanceof DataNodeContainer) {
583             final SchemaNode groupingNode = tryFindGroupings((DataNodeContainer) node, qname).orNull();
584             if (groupingNode != null) {
585                 childNodes.add(groupingNode);
586             }
587         }
588         return childNodes.isEmpty() ? Collections.emptyList() : ImmutableList.copyOf(childNodes);
589     }
590
591     private static Optional<SchemaNode> tryFindGroupings(final DataNodeContainer dataNodeContainer, final QName qname) {
592         return Optional
593                 .fromNullable(Iterables.find(dataNodeContainer.getGroupings(), new SchemaNodePredicate(qname), null));
594     }
595
596     private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
597         return Optional.fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
598     }
599
600     private static Optional<SchemaNode> tryFindNotification(final NotificationNodeContainer notificationContanier,
601             final QName qname) {
602         return Optional.fromNullable(
603                 Iterables.find(notificationContanier.getNotifications(), new SchemaNodePredicate(qname), null));
604     }
605
606     private static Optional<SchemaNode> tryFindAction(final ActionNodeContainer actionContanier, final QName qname) {
607         return Optional.fromNullable(Iterables.find(actionContanier.getActions(), new SchemaNodePredicate(qname),
608             null));
609     }
610
611     private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
612         private final QName qname;
613
614         SchemaNodePredicate(final QName qname) {
615             this.qname = qname;
616         }
617
618         @Override
619         public boolean apply(final SchemaNode input) {
620             return input.getQName().equals(qname);
621         }
622     }
623 }