e728bfaae2618cc70700a7e1259f71fa5584d74c
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / spi / TreeNodeFactory.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema.tree.spi;
9
10 import java.util.HashMap;
11 import java.util.Map;
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;
16
17 /**
18  * Public entrypoint for other packages. Allows instantiating a tree node
19  * with specified version.
20  */
21 public final class TreeNodeFactory {
22     private TreeNodeFactory() {
23         throw new UnsupportedOperationException("Utility class should not be instantiated");
24     }
25
26     /**
27      * Method creates and returns Container root Node and whole subtree for each child node specified in children nodes.
28      * <br>
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
34      * to data nodes.
35      *
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
40      */
41     @Deprecated
42     private static AbstractContainerNode createNodeRecursively(final Version version, final NormalizedNode<?, ?> data,
43         final Iterable<NormalizedNode<?, ?>> children) {
44
45         final Map<PathArgument, TreeNode> map = new HashMap<>();
46         for (NormalizedNode<?, ?> child : children) {
47             map.put(child.getIdentifier(), TreeNodeFactory.createTreeNodeRecursively(child, version));
48         }
49
50         return new MaterializedContainerNode(data, version, map, version);
51     }
52
53     /**
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()}
56      * <br>
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.
60      *
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.
64      *
65      */
66     @Deprecated
67     private static AbstractContainerNode createNormalizedNodeRecursively(final Version version,
68         final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> container) {
69         return createNodeRecursively(version, container, container.getValue());
70     }
71
72     /**
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()}
75      * <br>
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.
79      *
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.
83      */
84     @Deprecated
85     private static AbstractContainerNode createOrderedNodeRecursively(final Version version,
86         final OrderedNodeContainer<NormalizedNode<?, ?>> container) {
87         return createNodeRecursively(version, container, container.getValue());
88     }
89
90     /**
91      * Create a new AbstractTreeNode from a data node, descending recursively as needed.
92      * This method should only ever be used for new data.
93      *
94      * @param data data node
95      * @param version data node version
96      * @return new AbstractTreeNode instance, covering the data tree provided
97      */
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);
103
104         }
105         if (data instanceof OrderedNodeContainer<?>) {
106             @SuppressWarnings("unchecked")
107             OrderedNodeContainer<NormalizedNode<?, ?>> container = (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
108             return createOrderedNodeRecursively(version, container);
109         }
110
111         return new ValueNode(data, version);
112     }
113
114     /**
115      * Create a new AbstractTreeNode from a data node.
116      *
117      * @param data data node
118      * @param version data node version
119      * @return new AbstractTreeNode instance, covering the data tree provided
120      */
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);
127         }
128         if (data instanceof OrderedNodeContainer<?>) {
129             @SuppressWarnings("unchecked")
130             final OrderedNodeContainer<NormalizedNode<?, ?>> container =
131                     (OrderedNodeContainer<NormalizedNode<?, ?>>) data;
132             return new SimpleContainerNode(container, version);
133         }
134         return new ValueNode(data, version);
135     }
136 }