Merge "Bug 2271 - NPE during generation of java binding for case list-grouping-choice"
authorTony Tkacik <ttkacik@cisco.com>
Mon, 1 Dec 2014 08:32:39 +0000 (08:32 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 1 Dec 2014 08:32:40 +0000 (08:32 +0000)
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/util/SchemaContextUtilTest.java
yang/yang-parser-impl/src/test/resources/schema-context-util-test/my-module.yang

index 1c59b9a4fd2e74dbb18844d37b9ee154390a1cd7..95d1bd99ceb1af5439fc7774ab1a087f15cff5b6 100644 (file)
@@ -26,9 +26,11 @@ import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findD
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
 
+import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -37,6 +39,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+
 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
 import org.opendaylight.yangtools.binding.generator.util.BindingTypes;
 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
@@ -1233,13 +1236,12 @@ public class BindingGeneratorImpl implements BindingGenerator {
                         final SchemaPath sp = choiceNode.getPath();
                         parent = findDataSchemaNode(schemaContext, sp.getParent());
                     }
-                    if (parent != null) {
-                        GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
-                        if (childOfType == null) {
-                            childOfType = findGroupingByPath(parent.getPath());
-                        }
-                        resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
+                    Preconditions.checkState(parent != null, "Could not find Choice node parent "+choiceNode.getPath().getParent());
+                    GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath());
+                    if (childOfType == null) {
+                        childOfType = findGroupingByPath(parent.getPath());
                     }
+                    resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes);
                 }
             }
             processUsesAugments(caseNode, module);
index 132edfc347bdb7dd2405aefd755cfc832a118bd9..f9019abcb5a06cf887df90c92be42e1bdaf8b148 100644 (file)
@@ -16,10 +16,12 @@ import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
@@ -236,185 +238,118 @@ public final class SchemaContextUtil {
         return findNodeInModule(module, path);
     }
 
-    private static SchemaNode findNodeInModule(final Module module, final Iterable<QName> path) {
-        final QName current = path.iterator().next();
+    private static SchemaNode findNodeInModule(Module module, Iterable<QName> path) {
 
-        LOG.trace("Looking for data container {} in module {}", current, module);
-        SchemaNode parent = module.getDataChildByName(current);
-        if (parent != null) {
-            final SchemaNode ret = findNode((DataSchemaNode) parent, nextLevel(path));
-            if (ret != null) {
-                return ret;
-            }
-        }
+        Preconditions.checkArgument(module != null, "Parent reference cannot be NULL");
+        Preconditions.checkArgument(path != null, "Path reference cannot be NULL");
 
-        LOG.trace("Looking for RPC {} in module {}", current, module);
-        parent = getRpcByName(module, current);
-        if (parent != null) {
-            final SchemaNode ret = findNodeInRpc((RpcDefinition) parent, nextLevel(path));
-            if (ret != null) {
-                return ret;
-            }
+        if (!path.iterator().hasNext()) {
+            LOG.debug("No node matching {} found in node {}", path, module);
+            return null;
         }
 
-        LOG.trace("Looking for notification {} in module {}", current, module);
-        parent = getNotificationByName(module, current);
-        if (parent != null) {
-            final SchemaNode ret = findNodeInNotification((NotificationDefinition) parent, nextLevel(path));
-            if (ret != null) {
-                return ret;
-            }
-        }
+        QName current = path.iterator().next();
+        LOG.trace("Looking for node {} in module {}", current, module);
 
-        LOG.trace("Looking for grouping {} in module {}", current, module);
-        parent = getGroupingByName(module, current);
-        if (parent != null) {
-            final SchemaNode ret = findNodeInGrouping((GroupingDefinition) parent, nextLevel(path));
-            if (ret != null) {
-                return ret;
-            }
-        }
+        SchemaNode foundNode = null;
+        Iterable<QName> nextPath = nextLevel(path);
 
-        LOG.debug("No node matching {} found in module {}", path, module);
-        return null;
-    }
+        foundNode = module.getDataChildByName(current);
+        if (foundNode != null && nextPath.iterator().hasNext())
+            foundNode = findNodeIn(foundNode, nextPath);
 
-    private static SchemaNode findNodeInGrouping(
-            final GroupingDefinition grouping, final Iterable<QName> path) {
-        final QName current = Iterables.getFirst(path, null);
-        if (current == null) {
-            LOG.debug("Found grouping {}", grouping);
-            return grouping;
+        if (foundNode == null) {
+            foundNode = getGroupingByName(module, current);
+            if (foundNode != null && nextPath.iterator().hasNext())
+                foundNode = findNodeIn(foundNode, nextPath);
         }
 
-        LOG.trace("Looking for path {} in grouping {}", path, grouping);
-        final DataSchemaNode node = grouping.getDataChildByName(current);
-
-        if (node != null)
-            return findNode(node, nextLevel(path));
+        if (foundNode == null) {
+            foundNode = getRpcByName(module, current);
+            if (foundNode != null && nextPath.iterator().hasNext())
+                foundNode = findNodeIn(foundNode, nextPath);
+        }
 
-        for (GroupingDefinition groupingDefinition : grouping.getGroupings()) {
-            if (groupingDefinition.getQName().equals(current))
-                return findNodeInGrouping(groupingDefinition, nextLevel(path));
+        if (foundNode == null) {
+            foundNode = getNotificationByName(module, current);
+            if (foundNode != null && nextPath.iterator().hasNext())
+                foundNode = findNodeIn(foundNode, nextPath);
         }
 
-        LOG.debug("No node matching {} found in grouping {}", current, grouping);
-        return null;
-    }
+        if (foundNode == null)
+            LOG.debug("No node matching {} found in node {}", path, module);
 
-    private static SchemaNode findNodeInRpc(final RpcDefinition rpc, final Iterable<QName> path) {
-        final QName current = Iterables.getFirst(path, null);
-        if (current == null) {
-            LOG.debug("Found RPC {}", rpc);
-            return rpc;
-        }
+        return foundNode;
 
-        LOG.trace("Looking for path {} in rpc {}", path, rpc);
-        switch (current.getLocalName()) {
-        case "input":
-            return findNode(rpc.getInput(), nextLevel(path));
-        case "output":
-            return findNode(rpc.getOutput(), nextLevel(path));
-        default:
-            LOG.debug("Invalid component {} of path {} in RPC {}", current, path, rpc);
-            return null;
-        }
     }
 
-    private static SchemaNode findNodeInNotification(final NotificationDefinition ntf, final Iterable<QName> path) {
-        final QName current = Iterables.getFirst(path, null);
-        if (current == null) {
-            LOG.debug("Found notification {}", ntf);
-            return ntf;
-        }
+    private static SchemaNode findNodeIn(SchemaNode parent, Iterable<QName> path) {
+
+        Preconditions.checkArgument(parent != null, "Parent reference cannot be NULL");
+        Preconditions.checkArgument(path != null, "Path reference cannot be NULL");
 
-        LOG.trace("Looking for path {} in notification {}", path, ntf);
-        DataSchemaNode node = ntf.getDataChildByName(current);
-        if (node == null) {
-            LOG.debug("No node matching {} found in notification {}", current, ntf);
+        if (!path.iterator().hasNext()) {
+            LOG.debug("No node matching {} found in node {}", path, parent);
             return null;
         }
 
-        return findNode(node, nextLevel(path));
-    }
+        QName current = path.iterator().next();
+        LOG.trace("Looking for node {} in node {}", current, parent);
 
-    private static SchemaNode findNode(final ChoiceNode parent, final Iterable<QName> path) {
-        final QName current = Iterables.getFirst(path, null);
-        if (current == null) {
-            return parent;
-        }
-        ChoiceCaseNode node = parent.getCaseNodeByName(current);
-        if (node != null) {
-            return findNodeInCase(node, nextLevel(path));
-        }
-        return null;
-    }
+        SchemaNode foundNode = null;
+        Iterable<QName> nextPath = nextLevel(path);
 
-    private static SchemaNode findNode(final ContainerSchemaNode parent, final Iterable<QName> path) {
-        final QName current = Iterables.getFirst(path, null);
-        if (current == null) {
-            return parent;
-        }
+        if (parent instanceof DataNodeContainer) {
+            DataNodeContainer parentDataNodeContainer = (DataNodeContainer) parent;
 
-        final DataSchemaNode node = parent.getDataChildByName(current);
-        if (node == null) {
-            LOG.debug("Failed to find {} in parent {}", path, parent);
-            return null;
+            foundNode = parentDataNodeContainer.getDataChildByName(current);
+            if (foundNode != null && nextPath.iterator().hasNext())
+                foundNode = findNodeIn(foundNode, nextPath);
+
+            if (foundNode == null) {
+                foundNode = getGroupingByName(parentDataNodeContainer, current);
+                if (foundNode != null && nextPath.iterator().hasNext())
+                    foundNode = findNodeIn(foundNode, nextPath);
+            }
         }
 
-        return findNode(node, nextLevel(path));
-    }
+        if (foundNode == null && parent instanceof RpcDefinition) {
+            RpcDefinition parentRpcDefinition = (RpcDefinition) parent;
 
-    private static SchemaNode findNode(final ListSchemaNode parent, final Iterable<QName> path) {
-        final QName current = Iterables.getFirst(path, null);
-        if (current == null) {
-            return parent;
-        }
+            if (current.getLocalName().equals("input")) {
+                foundNode = parentRpcDefinition.getInput();
+                if (foundNode != null && nextPath.iterator().hasNext())
+                    foundNode = findNodeIn(foundNode, nextPath);
+            }
 
-        DataSchemaNode node = parent.getDataChildByName(current);
-        if (node == null) {
-            LOG.debug("Failed to find {} in parent {}", path, parent);
-            return null;
-        }
-        return findNode(node, nextLevel(path));
-    }
+            if (current.getLocalName().equals("output")) {
+                foundNode = parentRpcDefinition.getOutput();
+                if (foundNode != null && nextPath.iterator().hasNext())
+                    foundNode = findNodeIn(foundNode, nextPath);
+            }
 
-    private static SchemaNode findNode(final DataSchemaNode parent, final Iterable<QName> path) {
-        final SchemaNode node;
-        if (!Iterables.isEmpty(path)) {
-            if (parent instanceof ContainerSchemaNode) {
-                node = findNode((ContainerSchemaNode) parent, path);
-            } else if (parent instanceof ListSchemaNode) {
-                node = findNode((ListSchemaNode) parent, path);
-            } else if (parent instanceof ChoiceNode) {
-                node = findNode((ChoiceNode) parent, path);
-            } else {
-                throw new IllegalArgumentException(
-                        String.format("Path nesting violation in parent %s path %s", parent, path));
+            if (foundNode == null) {
+                foundNode = getGroupingByName(parentRpcDefinition, current);
+                if (foundNode != null && nextPath.iterator().hasNext())
+                    foundNode = findNodeIn(foundNode, nextPath);
             }
-        } else {
-            node = parent;
         }
 
-        if (node == null) {
-            LOG.debug("Failed to find {} in parent {}", path, parent);
-            return null;
+        if (foundNode == null && parent instanceof ChoiceNode) {
+            foundNode = ((ChoiceNode) parent).getCaseNodeByName(current);
+            if (foundNode != null && nextPath.iterator().hasNext())
+                foundNode = findNodeIn(foundNode, nextPath);
         }
-        return node;
-    }
 
-    private static SchemaNode findNodeInCase(final ChoiceCaseNode parent, final Iterable<QName> path) {
-        final QName current = Iterables.getFirst(path, null);
-        if (current == null) {
-            return parent;
-        }
+        if (foundNode == null)
+            LOG.debug("No node matching {} found in node {}", path, parent);
+
+        return foundNode;
 
-        DataSchemaNode node = parent.getDataChildByName(current);
-        if (node == null) {
-            LOG.debug("Failed to find {} in parent {}", path, parent);
-            return null;
-        }
-        return findNode(node, nextLevel(path));
+    }
+
+    private static Iterable<QName> nextLevel(final Iterable<QName> path) {
+        return Iterables.skip(path, 1);
     }
 
     private static RpcDefinition getRpcByName(final Module module, final QName name) {
@@ -426,10 +361,6 @@ public final class SchemaContextUtil {
         return null;
     }
 
-    private static Iterable<QName> nextLevel(final Iterable<QName> path) {
-        return Iterables.skip(path, 1);
-    }
-
     private static NotificationDefinition getNotificationByName(final Module module, final QName name) {
         for (NotificationDefinition notification : module.getNotifications()) {
             if (notification.getQName().equals(name)) {
@@ -439,8 +370,17 @@ public final class SchemaContextUtil {
         return null;
     }
 
-    private static GroupingDefinition getGroupingByName(final Module module, final QName name) {
-        for (GroupingDefinition grouping : module.getGroupings()) {
+    private static GroupingDefinition getGroupingByName(final DataNodeContainer dataNodeContainer, final QName name) {
+        for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
+            if (grouping.getQName().equals(name)) {
+                return grouping;
+            }
+        }
+        return null;
+    }
+
+    private static GroupingDefinition getGroupingByName(final RpcDefinition rpc, final QName name) {
+        for (GroupingDefinition grouping : rpc.getGroupings()) {
             if (grouping.getQName().equals(name)) {
                 return grouping;
             }
index db9942f568f520a4b6eaec3bcfe4ce68a6e2f6d5..b8d270054856124690b8aa5517b4abdc9a26352f 100644 (file)
@@ -14,6 +14,7 @@ import org.mockito.MockitoAnnotations;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
@@ -36,7 +37,6 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.text.ParseException;
 import java.util.Collections;
-import java.util.Set;
 
 import static org.junit.Assert.*;
 
@@ -95,8 +95,7 @@ public class SchemaContextUtilTest {
         assertNotNull(foundNode);
         assertEquals(testNode, foundNode);
 
-        Set<RpcDefinition> rpcs = myModule.getRpcs();
-        RpcDefinition rpc = rpcs.iterator().next();
+        RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
         testNode = rpc.getInput().getDataChildByName("my-input-leaf");
 
         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
@@ -109,7 +108,7 @@ public class SchemaContextUtilTest {
         assertNotNull(foundNode);
         assertEquals(testNode, foundNode);
 
-        rpc = myModule.getRpcs().iterator().next();
+        rpc = getRpcByName(myModule,"my-rpc");
         testNode = rpc.getOutput().getDataChildByName("my-output-leaf");
 
         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
@@ -213,8 +212,7 @@ public class SchemaContextUtilTest {
         assertNull(testNode);
         assertNull(foundNode);
 
-        Set<RpcDefinition> rpcs = myModule.getRpcs();
-        RpcDefinition rpc = rpcs.iterator().next();
+        RpcDefinition rpc = getRpcByName(myModule,"my-rpc");
         testNode = rpc.getInput().getDataChildByName("no-input-leaf");
 
         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
@@ -311,7 +309,7 @@ public class SchemaContextUtilTest {
         assertNotNull(foundNode);
         assertEquals(testNode, foundNode);
 
-        testNode = myModule.getRpcs().iterator().next();
+        testNode = getRpcByName(myModule,"my-rpc");
 
         path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"));
         foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
@@ -539,4 +537,368 @@ public class SchemaContextUtilTest {
 
     }
 
+    @Test
+    public void findNodeInSchemaContextGroupingsTest() throws URISyntaxException, IOException,
+            YangSyntaxErrorException, ParseException {
+
+        File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
+        File resourceDir = resourceFile.getParentFile();
+
+        YangParserImpl parser = YangParserImpl.getInstance();
+        SchemaContext context = parser.parseFile(resourceFile, resourceDir);
+
+        Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
+                QName.parseRevision("2014-10-07"));
+
+        // find grouping in container
+        DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
+        SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container");
+
+        SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-container"));
+        SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-container");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-container"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        // find grouping in list
+        dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
+                .getDataChildByName("my-list");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-list");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
+                QName.create(myModule.getQNameModule(), "my-list"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-list"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-list");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-list"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        // find grouping in grouping
+        dataContainer = getGroupingByName(myModule, "my-grouping");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-grouping"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-grouping");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-grouping"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        // find grouping in rpc
+        RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
+        for (GroupingDefinition grouping : rpc.getGroupings()) {
+            if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc")) {
+                testNode = grouping;
+            }
+        }
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-rpc"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-rpc");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-rpc"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        // find grouping in output
+        dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-output");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
+                QName.create(myModule.getQNameModule(), "output"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-output"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-output");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-output"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        // find grouping in input
+        dataContainer = getRpcByName(myModule, "my-rpc").getInput();
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-input");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
+                QName.create(myModule.getQNameModule(), "input"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-input"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-input");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-input"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        // find grouping in notification
+        dataContainer = getNotificationByName(myModule, "my-notification");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-notification");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-notification"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-notification");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-notification"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        // find grouping in case
+        dataContainer = (DataNodeContainer) ((ChoiceNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
+                "one").getDataChildByName("my-container-in-case");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-case");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
+                QName.create(myModule.getQNameModule(), "one"),
+                QName.create(myModule.getQNameModule(), "my-container-in-case"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-case"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+        testNode = ((GroupingDefinition) testNode).getDataChildByName("my-leaf-in-grouping-in-case");
+        path = path.createChild(QName.create(myModule.getQNameModule(), "my-leaf-in-grouping-in-case"));
+
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+    }
+
+    @Test
+    public void findNodeInSchemaContextGroupingsTest2() throws URISyntaxException, IOException,
+            YangSyntaxErrorException, ParseException {
+
+        File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
+        File resourceDir = resourceFile.getParentFile();
+
+        YangParserImpl parser = YangParserImpl.getInstance();
+        SchemaContext context = parser.parseFile(resourceFile, resourceDir);
+
+        Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
+                QName.parseRevision("2014-10-07"));
+
+        // find grouping in container
+        DataNodeContainer dataContainer = (DataNodeContainer) myModule.getDataChildByName("my-container");
+        SchemaNode testNode = getGroupingByName(dataContainer, "my-grouping-in-container2");
+
+        SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-container2"));
+        SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+        // find grouping in list
+        dataContainer = (DataNodeContainer) ((DataNodeContainer) myModule.getDataChildByName("my-container"))
+                .getDataChildByName("my-list");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-list2");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-container"),
+                QName.create(myModule.getQNameModule(), "my-list"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-list2"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+        // find grouping in grouping
+        dataContainer = getGroupingByName(myModule, "my-grouping");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-grouping2");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-grouping"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-grouping2"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+        // find grouping in rpc
+        RpcDefinition rpc = getRpcByName(myModule, "my-rpc");
+        for (GroupingDefinition grouping : rpc.getGroupings()) {
+            if (grouping.getQName().getLocalName().equals("my-grouping-in-rpc2")) {
+                testNode = grouping;
+            }
+        }
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-rpc2"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+        // find grouping in output
+        dataContainer = getRpcByName(myModule, "my-rpc").getOutput();
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-output2");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
+                QName.create(myModule.getQNameModule(), "output"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-output2"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+        // find grouping in input
+        dataContainer = getRpcByName(myModule, "my-rpc").getInput();
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-input2");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-rpc"),
+                QName.create(myModule.getQNameModule(), "input"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-input2"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+        // find grouping in notification
+        dataContainer = getNotificationByName(myModule, "my-notification");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-notification2");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-notification"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-notification2"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+        // find grouping in case
+        dataContainer = (DataNodeContainer) ((ChoiceNode) myModule.getDataChildByName("my-choice")).getCaseNodeByName(
+                "one").getDataChildByName("my-container-in-case");
+        testNode = getGroupingByName(dataContainer, "my-grouping-in-case2");
+
+        path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-choice"),
+                QName.create(myModule.getQNameModule(), "one"),
+                QName.create(myModule.getQNameModule(), "my-container-in-case"),
+                QName.create(myModule.getQNameModule(), "my-grouping-in-case2"));
+        foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNull(testNode);
+        assertNull(foundNode);
+
+    }
+
+    private static GroupingDefinition getGroupingByName(DataNodeContainer dataNodeContainer, String name) {
+        for (GroupingDefinition grouping : dataNodeContainer.getGroupings()) {
+            if (grouping.getQName().getLocalName().equals(name)) {
+                return grouping;
+            }
+        }
+        return null;
+    }
+
+    private static RpcDefinition getRpcByName(Module module, String name) {
+        for (RpcDefinition rpc : module.getRpcs()) {
+            if (rpc.getQName().getLocalName().equals(name)) {
+                return rpc;
+            }
+        }
+        return null;
+    }
+
+    private static NotificationDefinition getNotificationByName(Module module, String name) {
+        for (NotificationDefinition notification : module.getNotifications()) {
+            if (notification.getQName().getLocalName().equals(name)) {
+                return notification;
+            }
+        }
+        return null;
+    }
+
+    @Test
+    public void findNodeInSchemaContextTheSameNameOfSiblingsTest() throws URISyntaxException, IOException,
+            YangSyntaxErrorException, ParseException {
+
+        File resourceFile = new File(getClass().getResource("/schema-context-util-test/my-module.yang").toURI());
+        File resourceDir = resourceFile.getParentFile();
+
+        YangParserImpl parser = YangParserImpl.getInstance();
+        SchemaContext context = parser.parseFile(resourceFile, resourceDir);
+
+        Module myModule = context.findModuleByNamespaceAndRevision(new URI("uri:my-module"),
+                QName.parseRevision("2014-10-07"));
+
+        ChoiceNode choice = (ChoiceNode)getRpcByName(myModule,"my-name").getInput().getDataChildByName("my-choice");
+        SchemaNode testNode = choice.getCaseNodeByName("case-two").getDataChildByName("two");
+
+        SchemaPath path = SchemaPath.create(true, QName.create(myModule.getQNameModule(), "my-name"),
+                QName.create(myModule.getQNameModule(), "input"),
+                QName.create(myModule.getQNameModule(), "my-choice"),
+                QName.create(myModule.getQNameModule(), "case-two"),
+                QName.create(myModule.getQNameModule(), "two"));
+        SchemaNode foundNode = SchemaContextUtil.findNodeInSchemaContext(context, path.getPathFromRoot());
+
+        assertNotNull(testNode);
+        assertNotNull(foundNode);
+        assertEquals(testNode, foundNode);
+
+    }
+
 }
\ No newline at end of file
index 2ea1fe2d179c299ab77841a0944e0450b2c49a18..1bb1d21c2068b388526666c1f59b9a5d7f5b4e61 100644 (file)
@@ -19,6 +19,11 @@ module my-module {
         leaf my-leaf-in-gouping2 {
             type string;
         }
+        grouping my-grouping-in-grouping {
+            leaf my-leaf-in-grouping-in-grouping {
+                type string;
+            }
+        }
     }
 
     container my-container {
@@ -33,6 +38,16 @@ module my-module {
             leaf-list my-leaf-list-in-list {
                 type int16;
             }
+            grouping my-grouping-in-list {
+                leaf my-leaf-in-grouping-in-list {
+                    type string;
+                }
+            }
+        }
+        grouping my-grouping-in-container {
+            leaf my-leaf-in-grouping-in-container {
+                type string;
+            }
         }
     }
 
@@ -41,12 +56,28 @@ module my-module {
             leaf my-input-leaf {
                 type string;
             }
+            grouping my-grouping-in-input {
+                leaf my-leaf-in-grouping-in-input {
+                    type string;
+                }
+            }
         }
 
         output {
             leaf my-output-leaf {
                 type int16;
             }
+            grouping my-grouping-in-output {
+                leaf my-leaf-in-grouping-in-output {
+                    type string;
+                }
+            }
+        }
+
+        grouping my-grouping-in-rpc {
+            leaf my-leaf-in-grouping-in-rpc {
+                type string;
+            }
         }
     }
 
@@ -54,6 +85,11 @@ module my-module {
         leaf my-notification-leaf {
             type string;
         }
+        grouping my-grouping-in-notification {
+            leaf my-leaf-in-grouping-in-notification {
+                type string;
+            }
+        }
     }
 
     choice my-choice {
@@ -61,6 +97,13 @@ module my-module {
             leaf my-choice-leaf-one {
                 type string;
             }
+            container my-container-in-case {
+                grouping my-grouping-in-case {
+                    leaf my-leaf-in-grouping-in-case {
+                        type string;
+                    }
+                }
+            }
         }
         case two {
             leaf my-choice-leaf-two {
@@ -68,5 +111,25 @@ module my-module {
             }
         }
     }
-}
 
+    grouping my-name {
+        choice my-choice {
+            case case-one {
+                container one {
+                    //empty
+                }
+            }
+            case case-two {
+                container two {
+                    //empty
+                }
+            }
+        }
+    }
+
+    rpc my-name {
+        input {
+            uses my-name;
+        }
+    }
+}