*/
package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
-import java.util.HashMap;
-import java.util.Map;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
import org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer;
throw new UnsupportedOperationException("Utility class should not be instantiated");
}
- /**
- * Method creates and returns Container root Node and whole subtree for each child node specified in children nodes.
- * <br>
- * Reason why is method used recursively is that for each child in children nodes there is call to
- * {@link TreeNodeFactory#createTreeNodeRecursively}. Each call to <code>createTreeNodeRecursively</code>
- * calls either {@link #createNormalizedNodeRecursively} or {@link #createOrderedNodeRecursively}
- * which depends on type of child node.
- * <br> The root node that is returned holds reference to data node and whole subtree of children also containing
- * references to data nodes.
- *
- * @param version version of indexed data
- * @param data reference to data node
- * @param children direct children of root node that is being created
- * @return Root node with reference to data node and whole subtree of child nodes
- */
- @Deprecated
- private static AbstractContainerNode createNodeRecursively(final Version version, final NormalizedNode<?, ?> data,
- final Iterable<NormalizedNode<?, ?>> children) {
-
- final Map<PathArgument, TreeNode> map = new HashMap<>();
- for (NormalizedNode<?, ?> child : children) {
- map.put(child.getIdentifier(), TreeNodeFactory.createTreeNodeRecursively(child, version));
- }
-
- return new MaterializedContainerNode(data, version, map, version);
- }
-
- /**
- * Method creates and returns Normalized Node Container as root and recursively creates whole subtree
- * from all of the container child iterables stored in
- * {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer#getValue()}
- * <br>
- * The reason why is this method called recursively is that in background method calls
- * {@link TreeNodeFactory#createTreeNodeRecursively} for each child stored in NormalizedNode and after each child
- * is created the method calls again {@link #createNormalizedNodeRecursively} method until all of the children are
- * resolved.
- *
- * @param version version of indexed data
- * @param container Normalized Node Container
- * @return Normalized Node Container as root and all whole subtree created from container iterables.
- *
- */
- @Deprecated
- private static AbstractContainerNode createNormalizedNodeRecursively(final Version version,
- final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container) {
- return createNodeRecursively(version, container, container.getValue());
- }
-
- /**
- * Method creates and returns Ordered Node Container as root and recursively creates whole subtree
- * from all of the container child iterables stored in
- * {@link org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer#getValue()}
- * <br>
- * The reason why is this method called recursively is that in background method calls
- * {@link TreeNodeFactory#createTreeNodeRecursively} for each child stored in NormalizedNode and after each child
- * is created the method calls again {@link #createNormalizedNodeRecursively} method until all of the children are
- * resolved.
- *
- * @param version version of indexed data
- * @param container Ordered Node Container
- * @return Normalized Ordered Container as root and all whole subtree created from container iterables.
- */
- @Deprecated
- private static AbstractContainerNode createOrderedNodeRecursively(final Version version,
- final OrderedNodeContainer<NormalizedNode<?, ?>> container) {
- return createNodeRecursively(version, container, container.getValue());
- }
-
- /**
- * Create a new AbstractTreeNode from a data node, descending recursively as needed.
- * This method should only ever be used for new data.
- *
- * @param data data node
- * @param version data node version
- * @return new AbstractTreeNode instance, covering the data tree provided
- *
- * @deprecated Use lazy node initialization via {@link #createTreeNode(NormalizedNode, Version)}.
- */
- @Deprecated
- public static TreeNode createTreeNodeRecursively(final NormalizedNode<?, ?> data, final Version version) {
- if (data instanceof NormalizedNodeContainer<?, ?, ?>) {
- @SuppressWarnings("unchecked")
- NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container =
- (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
- return createNormalizedNodeRecursively(version, container);
-
- }
- if (data instanceof OrderedNodeContainer<?>) {
- @SuppressWarnings("unchecked")
- OrderedNodeContainer<NormalizedNode<?, ?>> container = (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
- return createOrderedNodeRecursively(version, container);
- }
-
- return new ValueNode(data, version);
- }
-
/**
* Create a new AbstractTreeNode from a data node.
*
import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
-import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
}
- private static ContainerNode createTestContainer() {
- return ImmutableContainerNodeBuilder
- .create()
- .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
- .withChild(
- mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
- .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
- .withChild(BAR_NODE).build()).build();
- }
-
- private static <T> T assertPresentAndType(final Optional<?> potential, final Class<T> type) {
- assertNotNull(potential);
- assertTrue(potential.isPresent());
- assertTrue(type.isInstance(potential.get()));
- return type.cast(potential.get());
- }
-
@Test
public void findNodeTestNodeFound() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
final Optional<TreeNode> node = StoreTreeNodes.findNode(rootNode, OUTER_LIST_1_PATH);
assertPresentAndType(node, TreeNode.class);
@Test
public void findNodeTestNodeNotFound() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
.nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
@Test
public void findNodeCheckedTestNodeFound() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
TreeNode foundNode = null;
try {
@Test
public void findNodeCheckedTestNodeNotFound() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
final YangInstanceIdentifier outerList1InvalidPath = YangInstanceIdentifier.builder(TestModel.OUTER_LIST_PATH)
.nodeWithKey(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 3) //
@Test
public void findClosestOrFirstMatchTestNodeExists() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, TWO_TWO_PATH);
assertPresentAndType(expectedNode, TreeNode.class);
- final Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
- TWO_TWO_PATH);
- assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
+
+ final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode, TWO_TWO_PATH);
+ assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
}
@Test
public void findClosestOrFirstMatchTestNodeDoesNotExist() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
final YangInstanceIdentifier outerListInnerListPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
.node(TestModel.INNER_LIST_QNAME)
.build();
final YangInstanceIdentifier twoTwoInvalidPath = YangInstanceIdentifier.builder(OUTER_LIST_2_PATH)
- .node(TestModel.INNER_LIST_QNAME) //
- .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three") //
+ .node(TestModel.INNER_LIST_QNAME)
+ .nodeWithKey(TestModel.INNER_LIST_QNAME, TestModel.NAME_QNAME, "three")
.build();
final Optional<TreeNode> expectedNode = StoreTreeNodes.findNode(rootNode, outerListInnerListPath);
assertPresentAndType(expectedNode, TreeNode.class);
- final Map.Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
+
+ final Entry<YangInstanceIdentifier, TreeNode> actualNode = StoreTreeNodes.findClosest(rootNode,
twoTwoInvalidPath);
- assertEquals("Expected node and actual node are not the same", expectedNode.get(), actualNode.getValue());
+ assertTreeNodeEquals(expectedNode.get(), actualNode.getValue());
}
@Test
public void getChildTestChildFound() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
TestModel.TEST_PATH.getLastPathArgument());
@Test
public void getChildTestChildNotFound() {
final InMemoryDataTreeSnapshot inMemoryDataTreeSnapshot = new InMemoryDataTreeSnapshot(schemaContext,
- TreeNodeFactory.createTreeNodeRecursively(createDocumentOne(), Version.initial()), rootOper);
+ TreeNodeFactory.createTreeNode(createDocumentOne(), Version.initial()), rootOper);
final TreeNode rootNode = inMemoryDataTreeSnapshot.getRootNode();
final Optional<TreeNode> node = StoreTreeNodes.getChild(Optional.ofNullable(rootNode),
TestModel.OUTER_LIST_PATH.getLastPathArgument());
assertFalse(node.isPresent());
}
+
+ private static ContainerNode createTestContainer() {
+ return ImmutableContainerNodeBuilder
+ .create()
+ .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
+ .withChild(
+ mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
+ .withChild(mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, ONE_ID))
+ .withChild(BAR_NODE).build()).build();
+ }
+
+ private static <T extends TreeNode> T assertPresentAndType(final Optional<TreeNode> potential,
+ final Class<T> type) {
+ assertNotNull(potential);
+ assertTrue(potential.isPresent());
+ assertTrue(type.isInstance(potential.get()));
+ return type.cast(potential.get());
+ }
+
+ private static void assertTreeNodeEquals(final TreeNode expected, final TreeNode actual) {
+ assertEquals(expected.getIdentifier(), actual.getIdentifier());
+ assertEquals(expected.getVersion(), actual.getVersion());
+ assertEquals(expected.getSubtreeVersion(), actual.getSubtreeVersion());
+ assertEquals(expected.getData(), actual.getData());
+ assertEquals(expected.toString(), actual.toString());
+ }
}