2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
10 import java.util.HashMap;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
15 import org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer;
18 * Public entrypoint for other packages. Allows instantiating a tree node
19 * with specified version.
21 public final class TreeNodeFactory {
22 private TreeNodeFactory() {
23 throw new UnsupportedOperationException("Utility class should not be instantiated");
27 * Method creates and returns Container root Node and whole subtree for each child node specified in children nodes.
29 * Reason why is method used recursively is that for each child in children nodes there is call to
30 * {@link TreeNodeFactory#createTreeNodeRecursively}. Each call to <code>createTreeNodeRecursively</code>
31 * calls either {@link #createNormalizedNodeRecursively} or {@link #createOrderedNodeRecursively}
32 * which depends on type of child node.
33 * <br> The root node that is returned holds reference to data node and whole subtree of children also containing references
36 * @param version version of indexed data
37 * @param data reference to data node
38 * @param children direct children of root node that is being created
39 * @return Root node with reference to data node and whole subtree of child nodes
42 private static AbstractContainerNode createNodeRecursively(final Version version, final NormalizedNode<?, ?> data,
43 final Iterable<NormalizedNode<?, ?>> children) {
45 final Map<PathArgument, TreeNode> map = new HashMap<>();
46 for (NormalizedNode<?, ?> child : children) {
47 map.put(child.getIdentifier(), TreeNodeFactory.createTreeNodeRecursively(child, version));
50 return new MaterializedContainerNode(data, version, map, version);
54 * Method creates and returns Normalized Node Container as root and recursively creates whole subtree
55 * from all of the container child iterables stored in {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer#getValue()}
57 * The reason why is this method called recursively is that in background method calls {@link TreeNodeFactory#createTreeNodeRecursively}
58 * for each child stored in NormalizedNode and after each child is created the method calls again {@link #createNormalizedNodeRecursively} method
59 * until all of the children are resolved.
61 * @param version version of indexed data
62 * @param container Normalized Node Container
63 * @return Normalized Node Container as root and all whole subtree created from container iterables.
67 private static AbstractContainerNode createNormalizedNodeRecursively(final Version version,
68 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container) {
69 return createNodeRecursively(version, container, container.getValue());
73 * Method creates and returns Ordered Node Container as root and recursively creates whole subtree
74 * from all of the container child iterables stored in {@link org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer#getValue()}
76 * The reason why is this method called recursively is that in background method calls {@link TreeNodeFactory#createTreeNodeRecursively}
77 * for each child stored in NormalizedNode and after each child is created the method calls again {@link #createNormalizedNodeRecursively} method
78 * until all of the children are resolved.
80 * @param version version of indexed data
81 * @param container Ordered Node Container
82 * @return Normalized Ordered Container as root and all whole subtree created from container iterables.
85 private static AbstractContainerNode createOrderedNodeRecursively(final Version version,
86 final OrderedNodeContainer<NormalizedNode<?, ?>> container) {
87 return createNodeRecursively(version, container, container.getValue());
91 * Create a new AbstractTreeNode from a data node, descending recursively as needed.
92 * This method should only ever be used for new data.
94 * @param data data node
95 * @param version data node version
96 * @return new AbstractTreeNode instance, covering the data tree provided
98 public static TreeNode createTreeNodeRecursively(final NormalizedNode<?, ?> data, final Version version) {
99 if (data instanceof NormalizedNodeContainer<?, ?, ?>) {
100 @SuppressWarnings("unchecked")
101 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
102 return createNormalizedNodeRecursively(version, container);
105 if (data instanceof OrderedNodeContainer<?>) {
106 @SuppressWarnings("unchecked")
107 OrderedNodeContainer<NormalizedNode<?, ?>> container = (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
108 return createOrderedNodeRecursively(version, container);
111 return new ValueNode(data, version);
115 * Create a new AbstractTreeNode from a data node.
117 * @param data data node
118 * @param version data node version
119 * @return new AbstractTreeNode instance, covering the data tree provided
121 public static TreeNode createTreeNode(final NormalizedNode<?, ?> data, final Version version) {
122 if (data instanceof NormalizedNodeContainer<?, ?, ?>) {
123 @SuppressWarnings("unchecked")
124 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container =
125 (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
126 return new SimpleContainerNode(container, version);
128 if (data instanceof OrderedNodeContainer<?>) {
129 @SuppressWarnings("unchecked")
130 final OrderedNodeContainer<NormalizedNode<?, ?>> container =
131 (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
132 return new SimpleContainerNode(container, version);
134 return new ValueNode(data, version);