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
34 * references to data nodes.
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
56 * {@link org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer#getValue()}
58 * The reason why is this method called recursively is that in background method calls
59 * {@link TreeNodeFactory#createTreeNodeRecursively} for each child stored in NormalizedNode and after each child
60 * is created the method calls again {@link #createNormalizedNodeRecursively} method until all of the children are
63 * @param version version of indexed data
64 * @param container Normalized Node Container
65 * @return Normalized Node Container as root and all whole subtree created from container iterables.
69 private static AbstractContainerNode createNormalizedNodeRecursively(final Version version,
70 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container) {
71 return createNodeRecursively(version, container, container.getValue());
75 * Method creates and returns Ordered Node Container as root and recursively creates whole subtree
76 * from all of the container child iterables stored in
77 * {@link org.opendaylight.yangtools.yang.data.api.schema.OrderedNodeContainer#getValue()}
79 * The reason why is this method called recursively is that in background method calls
80 * {@link TreeNodeFactory#createTreeNodeRecursively} for each child stored in NormalizedNode and after each child
81 * is created the method calls again {@link #createNormalizedNodeRecursively} method until all of the children are
84 * @param version version of indexed data
85 * @param container Ordered Node Container
86 * @return Normalized Ordered Container as root and all whole subtree created from container iterables.
89 private static AbstractContainerNode createOrderedNodeRecursively(final Version version,
90 final OrderedNodeContainer<NormalizedNode<?, ?>> container) {
91 return createNodeRecursively(version, container, container.getValue());
95 * Create a new AbstractTreeNode from a data node, descending recursively as needed.
96 * This method should only ever be used for new data.
98 * @param data data node
99 * @param version data node version
100 * @return new AbstractTreeNode instance, covering the data tree provided
102 * @deprecated Use lazy node initialization via {@link #createTreeNode(NormalizedNode, Version)}.
105 public static TreeNode createTreeNodeRecursively(final NormalizedNode<?, ?> data, final Version version) {
106 if (data instanceof NormalizedNodeContainer<?, ?, ?>) {
107 @SuppressWarnings("unchecked")
108 NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container =
109 (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
110 return createNormalizedNodeRecursively(version, container);
113 if (data instanceof OrderedNodeContainer<?>) {
114 @SuppressWarnings("unchecked")
115 OrderedNodeContainer<NormalizedNode<?, ?>> container = (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
116 return createOrderedNodeRecursively(version, container);
119 return new ValueNode(data, version);
123 * Create a new AbstractTreeNode from a data node.
125 * @param data data node
126 * @param version data node version
127 * @return new AbstractTreeNode instance, covering the data tree provided
129 public static TreeNode createTreeNode(final NormalizedNode<?, ?> data, final Version version) {
130 if (data instanceof NormalizedNodeContainer<?, ?, ?>) {
131 @SuppressWarnings("unchecked")
132 final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container =
133 (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
134 return new SimpleContainerNode(container, version);
136 if (data instanceof OrderedNodeContainer<?>) {
137 @SuppressWarnings("unchecked")
138 final OrderedNodeContainer<NormalizedNode<?, ?>> container =
139 (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
140 return new SimpleContainerNode(container, version);
142 return new ValueNode(data, version);