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;
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) {
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)) {
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;
}
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;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Collections;
-import java.util.Set;
import static org.junit.Assert.*;
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"),
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"),
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"),
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());
}
+ @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