Java Code Examples for org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier#relativeTo()

The following examples show how to use org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier#relativeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NormalizedNodesTest.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testFindNode() {
    final DataContainerNode<?> mockedDataContainerNode = mock(DataContainerNode.class);
    final ContainerNode mockedContainerNode = mock(ContainerNode.class);
    final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
    doReturn(Optional.of(mockedContainerNode)).when(mockedDataContainerNode).getChild(any(PathArgument.class));
    doReturn(Optional.of(mockedLeafNode)).when(mockedContainerNode).getChild(any(PathArgument.class));

    final QName node1QName = QName.create("test-ns", "2016-09-16", "node1");
    final QName node2Qname = QName.create("test-ns", "2016-09-16", "node2");
    final QName node3QName = QName.create("test-ns", "2016-09-16", "node3");
    final QName node4Qname = QName.create("test-ns", "2016-09-16", "node4");

    final YangInstanceIdentifier rootPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
            new NodeIdentifier(node2Qname));
    final YangInstanceIdentifier childPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
            new NodeIdentifier(node2Qname), new NodeIdentifier(node3QName), new NodeIdentifier(node4Qname));

    assertEquals(mockedLeafNode, NormalizedNodes.findNode(rootPath, mockedDataContainerNode, childPath).get());
    assertEquals(Optional.empty(), NormalizedNodes.findNode(childPath, mockedDataContainerNode, rootPath));

    final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
    final PathArgument[] pathArguments = relativePath.get().getPathArguments().toArray(new PathArgument[2]);

    assertEquals(mockedLeafNode, NormalizedNodes.findNode(Optional.of(mockedDataContainerNode),
            pathArguments).get());

    assertEquals(mockedLeafNode, NormalizedNodes.findNode(mockedDataContainerNode, pathArguments).get());
}
 
Example 2
Source File: NormalizedNodes.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath,
        final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
    final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
    return relativePath.isPresent() ? findNode(rootNode, relativePath.get()) : Optional.empty();
}