Bug 7246 - Fix of SchemaTracker initialization and lookup of schema nodes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / SchemaUtils.java
index eb6e4bbd8eb985dbc4fafd061c97de09bb4ae5bb..1999c657d5c8f80c83bcd9e7dbbfa2ecdc9ae5fb 100644 (file)
  */
 package org.opendaylight.yangtools.yang.data.impl.schema;
 
-
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
 import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
-
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
+import javax.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 public final class SchemaUtils {
-
     private SchemaUtils() {
+        throw new UnsupportedOperationException();
     }
 
-    public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
+    /**
+     * @param qname - schema node to find
+     * @param dataSchemaNode - iterable of schemaNodes to look through
+     * @return - schema node with newest revision or absent if no schema node with matching qname is found
+     */
+    public static Optional<DataSchemaNode> findFirstSchema(final QName qname, final Iterable<DataSchemaNode> dataSchemaNode) {
+        DataSchemaNode sNode = null;
         if (dataSchemaNode != null && qname != null) {
-            for (DataSchemaNode dsn : dataSchemaNode) {
+            for (final DataSchemaNode dsn : dataSchemaNode) {
                 if (qname.isEqualWithoutRevision(dsn.getQName())) {
-                    return Optional.<DataSchemaNode> of(dsn);
-                } else if (dsn instanceof ChoiceNode) {
-                    for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
-                        Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
-                        if (foundDsn != null && foundDsn.isPresent()) {
+                    if (sNode == null || sNode.getQName().getRevision().compareTo(dsn.getQName().getRevision()) < 0) {
+                        sNode = dsn;
+                    }
+                } else if (dsn instanceof ChoiceSchemaNode) {
+                    for (final ChoiceCaseNode choiceCase : ((ChoiceSchemaNode) dsn).getCases()) {
+
+                        final DataSchemaNode dataChildByName = choiceCase.getDataChildByName(qname);
+                        if (dataChildByName != null) {
+                            return Optional.of(dataChildByName);
+                        }
+                        final Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
+                        if (foundDsn.isPresent()) {
                             return foundDsn;
                         }
                     }
                 }
             }
         }
-        return Optional.absent();
+        return Optional.fromNullable(sNode);
     }
 
+    /**
+     *
+     * Find child schema node identified by its QName within a provided schema node
+     *
+     * @param schema schema for parent node - search root
+     * @param qname qname(with or without a revision) of a child node to be found in the parent schema
+     * @return found schema node
+     * @throws java.lang.IllegalStateException if the child was not found in parent schema node
+     */
     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname) {
-        return findSchemaForChild(schema, qname, schema.getChildNodes());
+        // Try to find child schema node directly, but use a fallback that compares QNames without revisions and auto-expands choices
+        final DataSchemaNode dataChildByName = schema.getDataChildByName(qname);
+        return dataChildByName == null ? findSchemaForChild(schema, qname, schema.getChildNodes()) : dataChildByName;
+    }
+
+    @Nullable
+    public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final boolean strictMode) {
+        if (strictMode) {
+            return findSchemaForChild(schema, qname);
+        }
+
+        final Optional<DataSchemaNode> childSchemaOptional = findFirstSchema(qname, schema.getChildNodes());
+        if (!childSchemaOptional.isPresent()) {
+            return null;
+        }
+        return childSchemaOptional.get();
     }
 
     public static DataSchemaNode findSchemaForChild(final DataNodeContainer schema, final QName qname, final Iterable<DataSchemaNode> childNodes) {
-        Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
+        final Optional<DataSchemaNode> childSchema = findFirstSchema(qname, childNodes);
         Preconditions.checkState(childSchema.isPresent(),
                 "Unknown child(ren) node(s) detected, identified by: %s, in: %s", qname, schema);
         return childSchema.get();
     }
 
     public static AugmentationSchema findSchemaForAugment(final AugmentationTarget schema, final Set<QName> qNames) {
-        Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
+        final Optional<AugmentationSchema> schemaForAugment = findAugment(schema, qNames);
         Preconditions.checkState(schemaForAugment.isPresent(), "Unknown augmentation node detected, identified by: %s, in: %s",
                 qNames, schema);
         return schemaForAugment.get();
     }
 
-    public static AugmentationSchema findSchemaForAugment(final ChoiceNode schema, final Set<QName> qNames) {
+    public static AugmentationSchema findSchemaForAugment(final ChoiceSchemaNode schema, final Set<QName> qNames) {
         Optional<AugmentationSchema> schemaForAugment = Optional.absent();
 
-        for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
+        for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
             schemaForAugment = findAugment(choiceCaseNode, qNames);
-            if(schemaForAugment.isPresent()) {
+            if (schemaForAugment.isPresent()) {
                 break;
             }
         }
@@ -88,16 +135,11 @@ public final class SchemaUtils {
     }
 
     private static Optional<AugmentationSchema> findAugment(final AugmentationTarget schema, final Set<QName> qNames) {
-        for (AugmentationSchema augment : schema.getAvailableAugmentations()) {
+        for (final AugmentationSchema augment : schema.getAvailableAugmentations()) {
+            final HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(),
+                DataSchemaNode::getQName));
 
-            HashSet<QName> qNamesFromAugment = Sets.newHashSet(Collections2.transform(augment.getChildNodes(), new Function<DataSchemaNode, QName>() {
-                @Override
-                public QName apply(final DataSchemaNode input) {
-                    return input.getQName();
-                }
-            }));
-
-            if(qNamesFromAugment.equals(qNames)) {
+            if (qNamesFromAugment.equals(qNames)) {
                 return Optional.of(augment);
             }
         }
@@ -105,9 +147,9 @@ public final class SchemaUtils {
         return Optional.absent();
     }
 
-    public static DataSchemaNode findSchemaForChild(final ChoiceNode schema, final QName childPartialQName) {
-        for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
-            Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
+    public static DataSchemaNode findSchemaForChild(final ChoiceSchemaNode schema, final QName childPartialQName) {
+        for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
+            final Optional<DataSchemaNode> childSchema = findFirstSchema(childPartialQName, choiceCaseNode.getChildNodes());
             if (childSchema.isPresent()) {
                 return childSchema.get();
             }
@@ -121,26 +163,27 @@ public final class SchemaUtils {
     /**
      * Recursively find all child nodes that come from choices.
      *
+     * @param schema schema
      * @return Map with all child nodes, to their most top augmentation
      */
-    public static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
+    public static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema) {
         return mapChildElementsFromChoices(schema, schema.getChildNodes());
     }
 
-    private static Map<QName, ChoiceNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
-        Map<QName, ChoiceNode> mappedChoices = Maps.newLinkedHashMap();
+    private static Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(final DataNodeContainer schema, final Iterable<DataSchemaNode> childNodes) {
+        final Map<QName, ChoiceSchemaNode> mappedChoices = Maps.newLinkedHashMap();
 
         for (final DataSchemaNode childSchema : childNodes) {
-            if(childSchema instanceof ChoiceNode) {
+            if (childSchema instanceof ChoiceSchemaNode) {
 
-                if(isFromAugment(schema, childSchema)) {
+                if (isFromAugment(schema, childSchema)) {
                     continue;
                 }
 
-                for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
+                for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
 
-                    for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
-                        mappedChoices.put(qName, (ChoiceNode) childSchema);
+                    for (final QName qName : getChildNodesRecursive(choiceCaseNode)) {
+                        mappedChoices.put(qName, (ChoiceSchemaNode) childSchema);
                     }
                 }
             }
@@ -150,12 +193,12 @@ public final class SchemaUtils {
     }
 
     private static boolean isFromAugment(final DataNodeContainer schema, final DataSchemaNode childSchema) {
-        if(schema instanceof AugmentationTarget == false) {
+        if (!(schema instanceof AugmentationTarget)) {
             return false;
         }
 
-        for (AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
-            if(augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
+        for (final AugmentationSchema augmentationSchema : ((AugmentationTarget) schema).getAvailableAugmentations()) {
+            if (augmentationSchema.getDataChildByName(childSchema.getQName()) != null) {
                 return true;
             }
         }
@@ -166,16 +209,17 @@ public final class SchemaUtils {
     /**
      * Recursively find all child nodes that come from augmentations.
      *
+     * @param schema schema
      * @return Map with all child nodes, to their most top augmentation
      */
     public static Map<QName, AugmentationSchema> mapChildElementsFromAugments(final AugmentationTarget schema) {
 
-        Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
+        final Map<QName, AugmentationSchema> childNodesToAugmentation = Maps.newLinkedHashMap();
 
         // Find QNames of augmented child nodes
-        Map<QName, AugmentationSchema> augments = Maps.newHashMap();
+        final Map<QName, AugmentationSchema> augments = Maps.newHashMap();
         for (final AugmentationSchema augmentationSchema : schema.getAvailableAugmentations()) {
-            for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
+            for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
                 augments.put(dataSchemaNode.getQName(), augmentationSchema);
             }
         }
@@ -184,22 +228,22 @@ public final class SchemaUtils {
         // because nodes from augment do not contain nodes from other augmentations
         if (schema instanceof DataNodeContainer) {
 
-            for (DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
+            for (final DataSchemaNode child : ((DataNodeContainer) schema).getChildNodes()) {
                 // If is not augmented child, continue
-                if (augments.containsKey(child.getQName()) == false) {
+                if (!(augments.containsKey(child.getQName()))) {
                     continue;
                 }
 
-                AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
+                final AugmentationSchema mostTopAugmentation = augments.get(child.getQName());
 
                 // recursively add all child nodes in case of augment, case and choice
                 if (child instanceof AugmentationSchema || child instanceof ChoiceCaseNode) {
-                    for (QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
+                    for (final QName qName : getChildNodesRecursive((DataNodeContainer) child)) {
                         childNodesToAugmentation.put(qName, mostTopAugmentation);
                     }
-                } else if (child instanceof ChoiceNode) {
-                    for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) child).getCases()) {
-                        for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
+                } else if (child instanceof ChoiceSchemaNode) {
+                    for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) child).getCases()) {
+                        for (final QName qName : getChildNodesRecursive(choiceCaseNode)) {
                             childNodesToAugmentation.put(qName, mostTopAugmentation);
                         }
                     }
@@ -210,13 +254,13 @@ public final class SchemaUtils {
         }
 
         // Choice Node has to map child nodes from all its cases
-        if (schema instanceof ChoiceNode) {
-            for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) schema).getCases()) {
-                if (augments.containsKey(choiceCaseNode.getQName()) == false) {
+        if (schema instanceof ChoiceSchemaNode) {
+            for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) schema).getCases()) {
+                if (!(augments.containsKey(choiceCaseNode.getQName()))) {
                     continue;
                 }
 
-                for (QName qName : getChildNodesRecursive(choiceCaseNode)) {
+                for (final QName qName : getChildNodesRecursive(choiceCaseNode)) {
                     childNodesToAugmentation.put(qName, augments.get(choiceCaseNode.getQName()));
                 }
             }
@@ -229,16 +273,19 @@ public final class SchemaUtils {
      * Recursively list all child nodes.
      *
      * In case of choice, augment and cases, step in.
+     *
+     * @param nodeContainer node container
+     * @return set of QNames
      */
     public static Set<QName> getChildNodesRecursive(final DataNodeContainer nodeContainer) {
-        Set<QName> allChildNodes = Sets.newHashSet();
+        final Set<QName> allChildNodes = Sets.newHashSet();
 
-        for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
-            if(childSchema instanceof ChoiceNode) {
-                for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) childSchema).getCases()) {
+        for (final DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
+            if (childSchema instanceof ChoiceSchemaNode) {
+                for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) childSchema).getCases()) {
                     allChildNodes.addAll(getChildNodesRecursive(choiceCaseNode));
                 }
-            } else if(childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
+            } else if (childSchema instanceof AugmentationSchema || childSchema instanceof ChoiceCaseNode) {
                 allChildNodes.addAll(getChildNodesRecursive((DataNodeContainer) childSchema));
             }
             else {
@@ -253,22 +300,25 @@ public final class SchemaUtils {
      * Retrieves real schemas for augmented child node.
      *
      * Schema of the same child node from augment, and directly from target is not the same.
-     * Schema of child node from augment is incomplete, therefore its useless for xml <-> normalizedNode translation.
+     * Schema of child node from augment is incomplete, therefore its useless for XML/NormalizedNode translation.
      *
+     * @param targetSchema target schema
+     * @param augmentSchema augment schema
+     * @return set of nodes
      */
     public static Set<DataSchemaNode> getRealSchemasForAugment(final AugmentationTarget targetSchema, final AugmentationSchema augmentSchema) {
-        if(targetSchema.getAvailableAugmentations().contains(augmentSchema) == false) {
+        if (!(targetSchema.getAvailableAugmentations().contains(augmentSchema))) {
             return Collections.emptySet();
         }
 
         Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
 
-        if(targetSchema instanceof DataNodeContainer) {
+        if (targetSchema instanceof DataNodeContainer) {
             realChildNodes = getRealSchemasForAugment((DataNodeContainer)targetSchema, augmentSchema);
-        } else if(targetSchema instanceof ChoiceNode) {
-            for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
-                for (ChoiceCaseNode choiceCaseNode : ((ChoiceNode) targetSchema).getCases()) {
-                    if(getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
+        } else if (targetSchema instanceof ChoiceSchemaNode) {
+            for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
+                for (final ChoiceCaseNode choiceCaseNode : ((ChoiceSchemaNode) targetSchema).getCases()) {
+                    if (getChildNodesRecursive(choiceCaseNode).contains(dataSchemaNode.getQName())) {
                         realChildNodes.add(choiceCaseNode.getDataChildByName(dataSchemaNode.getQName()));
                     }
                 }
@@ -280,19 +330,18 @@ public final class SchemaUtils {
 
     public static Set<DataSchemaNode> getRealSchemasForAugment(final DataNodeContainer targetSchema,
             final AugmentationSchema augmentSchema) {
-        Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
-        for (DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
-            DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
+        final Set<DataSchemaNode> realChildNodes = Sets.newHashSet();
+        for (final DataSchemaNode dataSchemaNode : augmentSchema.getChildNodes()) {
+            final DataSchemaNode realChild = targetSchema.getDataChildByName(dataSchemaNode.getQName());
             realChildNodes.add(realChild);
         }
         return realChildNodes;
     }
 
-    public static Optional<ChoiceCaseNode> detectCase(final ChoiceNode schema, final DataContainerChild<?, ?> child) {
-        for (ChoiceCaseNode choiceCaseNode : schema.getCases()) {
+    public static Optional<ChoiceCaseNode> detectCase(final ChoiceSchemaNode schema, final DataContainerChild<?, ?> child) {
+        for (final ChoiceCaseNode choiceCaseNode : schema.getCases()) {
             if (child instanceof AugmentationNode
-                    && belongsToCaseAugment(choiceCaseNode,
-                            (YangInstanceIdentifier.AugmentationIdentifier) child.getIdentifier())) {
+                    && belongsToCaseAugment(choiceCaseNode, (AugmentationIdentifier) child.getIdentifier())) {
                 return Optional.of(choiceCaseNode);
             } else if (choiceCaseNode.getDataChildByName(child.getNodeType()) != null) {
                 return Optional.of(choiceCaseNode);
@@ -302,15 +351,15 @@ public final class SchemaUtils {
         return Optional.absent();
     }
 
-    public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final YangInstanceIdentifier.AugmentationIdentifier childToProcess) {
-        for (AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
+    public static boolean belongsToCaseAugment(final ChoiceCaseNode caseNode, final AugmentationIdentifier childToProcess) {
+        for (final AugmentationSchema augmentationSchema : caseNode.getAvailableAugmentations()) {
 
-            Set<QName> currentAugmentChildNodes = Sets.newHashSet();
-            for (DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
+            final Set<QName> currentAugmentChildNodes = Sets.newHashSet();
+            for (final DataSchemaNode dataSchemaNode : augmentationSchema.getChildNodes()) {
                 currentAugmentChildNodes.add(dataSchemaNode.getQName());
             }
 
-            if(childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
+            if (childToProcess.getPossibleChildNames().equals(currentAugmentChildNodes)){
                 return true;
             }
         }
@@ -318,17 +367,263 @@ public final class SchemaUtils {
         return false;
     }
 
-    public static YangInstanceIdentifier.AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
-        return new YangInstanceIdentifier.AugmentationIdentifier(getChildQNames(schema));
+    /**
+     * Tries to find in {@code parent} which is dealed as augmentation target node with QName as {@code child}. If such
+     * node is found then it is returned, else null.
+     *
+     * @param parent parent node
+     * @param child child node
+     * @return augmentation schema
+     */
+    public static AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
+        if (parent instanceof AugmentationTarget && !(parent instanceof ChoiceSchemaNode)) {
+            for (final AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
+                final DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
+                if (childInAugmentation != null) {
+                    return augmentation;
+                }
+            }
+        }
+        return null;
+    }
+
+    public static AugmentationIdentifier getNodeIdentifierForAugmentation(final AugmentationSchema schema) {
+        final Collection<QName> qnames = Collections2.transform(schema.getChildNodes(), DataSchemaNode::getQName);
+        return new AugmentationIdentifier(ImmutableSet.copyOf(qnames));
+    }
+
+    /**
+     * Finds schema node for given path in schema context. This method performs
+     * lookup in the namespace of all leafs, leaf-lists, lists, containers,
+     * choices, rpcs, actions, notifications, anydatas, and anyxmls according to
+     * Rfc6050/Rfc7950 section 6.2.1.
+     *
+     * @param schemaContext
+     *            schema context
+     * @param path
+     *            path
+     * @return schema node on path
+     */
+    public static SchemaNode findDataParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
+        SchemaNode current = Preconditions.checkNotNull(schemaContext);
+        for (final QName qname : path.getPathFromRoot()) {
+            current = findDataChildSchemaByQName(current, qname);
+        }
+        return current;
+    }
+
+    /**
+     * Finds schema node for given path in schema context. This method performs
+     * lookup in both the namespace of groupings and the namespace of all leafs,
+     * leaf-lists, lists, containers, choices, rpcs, actions, notifications,
+     * anydatas, and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
+     *
+     * This method is deprecated, because name conflicts can occur between the
+     * namespace of groupings and namespace of data nodes and in consequence
+     * lookup could be ambiguous.
+     *
+     * @param schemaContext
+     *            schema context
+     * @param path
+     *            path
+     * @return schema node on path
+     *
+     * @deprecated use
+     *             {@link #findParentSchemaNodesOnPath(SchemaContext, SchemaPath)}
+     *             instead.
+     */
+    @Deprecated
+    public static SchemaNode findParentSchemaOnPath(final SchemaContext schemaContext, final SchemaPath path) {
+        SchemaNode current = Preconditions.checkNotNull(schemaContext);
+        for (final QName qname : path.getPathFromRoot()) {
+            current = findChildSchemaByQName(current, qname);
+        }
+        return current;
+    }
+
+    /**
+     * Find child data schema node identified by its QName within a provided
+     * schema node. This method performs lookup in the namespace of all leafs,
+     * leaf-lists, lists, containers, choices, rpcs, actions, notifications,
+     * anydatas, and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
+     *
+     * @param node
+     *            schema node
+     * @param qname
+     *            QName
+     * @return data child schema node
+     * @throws java.lang.IllegalArgumentException
+     *             if the schema node does not allow children
+     */
+    @Nullable
+    public static SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) {
+        SchemaNode child = null;
+        if (node instanceof DataNodeContainer) {
+            child = ((DataNodeContainer) node).getDataChildByName(qname);
+            if (child == null && node instanceof SchemaContext) {
+                child = tryFindRpc((SchemaContext) node, qname).orNull();
+            }
+            if (child == null && node instanceof NotificationNodeContainer) {
+                child = tryFindNotification((NotificationNodeContainer) node, qname).orNull();
+            }
+            if (child == null && node instanceof ActionNodeContainer) {
+                child = tryFindAction((ActionNodeContainer) node, qname).orNull();
+            }
+        } else if (node instanceof ChoiceSchemaNode) {
+            child = ((ChoiceSchemaNode) node).getCaseNodeByName(qname);
+        } else if (node instanceof RpcDefinition) {
+            switch (qname.getLocalName()) {
+            case "input":
+                child = ((RpcDefinition) node).getInput();
+                break;
+            case "output":
+                child = ((RpcDefinition) node).getOutput();
+                break;
+            default:
+                child = null;
+                break;
+            }
+        } else {
+            throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
+        }
+
+        return child;
+    }
+
+    /**
+     * Find child schema node identified by its QName within a provided schema
+     * node. This method performs lookup in both the namespace of groupings and
+     * the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs,
+     * actions, notifications, anydatas, and anyxmls according to
+     * Rfc6050/Rfc7950 section 6.2.1.
+     *
+     * This method is deprecated, because name conflicts can occur between the
+     * namespace of groupings and namespace of data nodes and in consequence
+     * lookup could be ambiguous.
+     *
+     * @param node
+     *            schema node
+     * @param qname
+     *            QName
+     * @return child schema node
+     * @throws java.lang.IllegalArgumentException
+     *             if the schema node does not allow children
+     *
+     * @deprecated use
+     *             {@link #findChildSchemaNodesByQName(SchemaNode, QName)}
+     *             instead.
+     */
+    @Deprecated
+    public static SchemaNode findChildSchemaByQName(final SchemaNode node, final QName qname) {
+        SchemaNode child = findDataChildSchemaByQName(node, qname);
+        if (child == null && node instanceof DataNodeContainer) {
+            child = tryFindGroupings((DataNodeContainer) node, qname).orNull();
+        }
+
+        return child;
     }
 
-    public static Set<QName> getChildQNames(final AugmentationSchema schema) {
-        Set<QName> qnames = Sets.newHashSet();
+    /**
+     * Finds schema node for given path in schema context. This method performs
+     * lookup in both the namespace of groupings and the namespace of all leafs,
+     * leaf-lists, lists, containers, choices, rpcs, actions, notifications,
+     * anydatas, and anyxmls according to Rfc6050/Rfc7950 section 6.2.1.
+     *
+     * This method returns collection of SchemaNodes, because name conflicts can
+     * occur between the namespace of groupings and namespace of data nodes.
+     * This method finds and collects all schema nodes that matches supplied
+     * SchemaPath and returns them all as collection of schema nodes.
+     *
+     * @param schemaContext
+     *            schema context
+     * @param path
+     *            path
+     * @return collection of schema nodes on path
+     *
+     */
+    public static Collection<SchemaNode> findParentSchemaNodesOnPath(final SchemaContext schemaContext,
+            final SchemaPath path) {
+        final Collection<SchemaNode> currentNodes = new ArrayList<>();
+        final Collection<SchemaNode> childNodes = new ArrayList<>();
+        currentNodes.add(Preconditions.checkNotNull(schemaContext));
+        for (final QName qname : path.getPathFromRoot()) {
+            for (final SchemaNode current : currentNodes) {
+                childNodes.addAll(findChildSchemaNodesByQName(current, qname));
+            }
+            currentNodes.clear();
+            currentNodes.addAll(childNodes);
+            childNodes.clear();
+        }
+
+        return currentNodes;
+    }
+
+    /**
+     * Find child schema node identified by its QName within a provided schema
+     * node. This method performs lookup in both the namespace of groupings and
+     * the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs,
+     * actions, notifications, anydatas, and anyxmls according to
+     * Rfc6050/Rfc7950 section 6.2.1.
+     *
+     * This method returns collection of SchemaNodes, because name conflicts can
+     * occur between the namespace of groupings and namespace of data nodes.
+     * This method finds and collects all schema nodes with supplied QName and
+     * returns them all as collection of schema nodes.
+     *
+     * @param node
+     *            schema node
+     * @param qname
+     *            QName
+     * @return collection of child schema nodes
+     * @throws java.lang.IllegalArgumentException
+     *             if the schema node does not allow children
+     *
+     */
+    public static Collection<SchemaNode> findChildSchemaNodesByQName(final SchemaNode node, final QName qname) {
+        final List<SchemaNode> childNodes = new ArrayList<>();
+        final SchemaNode dataNode = findDataChildSchemaByQName(node, qname);
+        if (dataNode != null) {
+            childNodes.add(dataNode);
+        }
+        if (node instanceof DataNodeContainer) {
+            final SchemaNode groupingNode = tryFindGroupings((DataNodeContainer) node, qname).orNull();
+            if (groupingNode != null) {
+                childNodes.add(groupingNode);
+            }
+        }
+        return childNodes.isEmpty() ? Collections.emptyList() : ImmutableList.copyOf(childNodes);
+    }
+
+    private static Optional<SchemaNode> tryFindGroupings(final DataNodeContainer dataNodeContainer, final QName qname) {
+        return Optional
+                .fromNullable(Iterables.find(dataNodeContainer.getGroupings(), new SchemaNodePredicate(qname), null));
+    }
+
+    private static Optional<SchemaNode> tryFindRpc(final SchemaContext ctx, final QName qname) {
+        return Optional.fromNullable(Iterables.find(ctx.getOperations(), new SchemaNodePredicate(qname), null));
+    }
+
+    private static Optional<SchemaNode> tryFindNotification(final NotificationNodeContainer notificationContanier,
+            final QName qname) {
+        return Optional.fromNullable(
+                Iterables.find(notificationContanier.getNotifications(), new SchemaNodePredicate(qname), null));
+    }
+
+    private static Optional<SchemaNode> tryFindAction(final ActionNodeContainer actionContanier, final QName qname) {
+        return Optional.fromNullable(Iterables.find(actionContanier.getActions(), new SchemaNodePredicate(qname), null));
+    }
 
-        for (DataSchemaNode dataSchemaNode : schema.getChildNodes()) {
-            qnames.add(dataSchemaNode.getQName());
+    private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
+        private final QName qname;
+
+        public SchemaNodePredicate(final QName qname) {
+            this.qname = qname;
         }
 
-        return qnames;
+        @Override
+        public boolean apply(final SchemaNode input) {
+            return input.getQName().equals(qname);
+        }
     }
+
 }