2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.controller.yang.data.impl;
\r
10 import java.util.AbstractMap.SimpleEntry;
\r
11 import java.util.ArrayList;
\r
12 import java.util.Arrays;
\r
13 import java.util.List;
\r
14 import java.util.Map;
\r
15 import java.util.Stack;
\r
17 import org.opendaylight.controller.yang.common.QName;
\r
18 import org.opendaylight.controller.yang.data.api.CompositeNode;
\r
19 import org.opendaylight.controller.yang.data.api.ModifyAction;
\r
20 import org.opendaylight.controller.yang.data.api.MutableCompositeNode;
\r
21 import org.opendaylight.controller.yang.data.api.MutableSimpleNode;
\r
22 import org.opendaylight.controller.yang.data.api.Node;
\r
23 import org.opendaylight.controller.yang.data.api.SimpleNode;
\r
26 * @author michal.rehak
\r
29 public abstract class NodeFactory {
\r
35 * @return simple node modification, based on given qname, value and parent
\r
37 public static <T> SimpleNode<T> createSimpleNode(QName qName,
\r
38 CompositeNode parent, T value) {
\r
39 SimpleNodeTOImpl<T> simpleNodeTOImpl = new SimpleNodeTOImpl<T>(qName, parent, value);
\r
40 return simpleNodeTOImpl;
\r
47 * @return simple node modification, based on given qname, value and parent
\r
49 public static <T> MutableSimpleNode<T> createMutableSimpleNode(QName qName,
\r
50 CompositeNode parent, T value) {
\r
51 MutableSimpleNodeTOImpl<T> simpleNodeTOImpl =
\r
52 new MutableSimpleNodeTOImpl<T>(qName, parent, value, null);
\r
53 return simpleNodeTOImpl;
\r
60 * @return composite node modification, based on given qname, value (children), parent and modifyAction
\r
62 public static CompositeNode createCompositeNode(QName qName,
\r
63 CompositeNode parent, List<Node<?>> value) {
\r
64 CompositeNode compositeNodeTOImpl = new CompositeNodeTOImpl(qName, parent, value);
\r
65 return compositeNodeTOImpl;
\r
72 * @return composite node modification, based on given qname, value (children), parent and modifyAction
\r
74 public static MutableCompositeNode createMutableCompositeNode(QName qName,
\r
75 CompositeNode parent, List<Node<?>> value) {
\r
76 MutableCompositeNodeTOImpl compositeNodeTOImpl =
\r
77 new MutableCompositeNodeTOImpl(qName, parent, value, null);
\r
78 return compositeNodeTOImpl;
\r
86 * @param modifyAction
\r
87 * @return simple node modification, based on given qname, value, parent and modifyAction
\r
89 public static <T> SimpleNodeModificationTOImpl<T> createSimpleNodeModification(QName qName,
\r
90 CompositeNode parent, T value, ModifyAction modifyAction) {
\r
91 SimpleNodeModificationTOImpl<T> simpleNodeModTOImpl =
\r
92 new SimpleNodeModificationTOImpl<T>(qName, parent, value, modifyAction);
\r
93 return simpleNodeModTOImpl;
\r
100 * @param modifyAction
\r
101 * @return composite node modification, based on given qname, value (children), parent and modifyAction
\r
103 public static CompositeNodeModificationTOImpl createCompositeNodeModification(QName qName,
\r
104 CompositeNode parent, List<Node<?>> value, ModifyAction modifyAction) {
\r
105 CompositeNodeModificationTOImpl compositeNodeModTOImpl =
\r
106 new CompositeNodeModificationTOImpl(qName, parent, value, modifyAction);
\r
107 return compositeNodeModTOImpl;
\r
112 * @return copy of given node, parent and value are the same, but parent
\r
113 * has no reference to this copy
\r
115 public static <T> SimpleNode<T> copyNode(SimpleNode<T> node) {
\r
116 SimpleNode<T> twinNode = createSimpleNode(
\r
117 node.getNodeType(), node.getParent(), node.getValue());
\r
123 * @return copy of given node, parent and value are the same, but parent
\r
124 * has no reference to this copy
\r
126 public static <T> SimpleNode<T> copyNodeAsMutable(SimpleNode<T> node) {
\r
127 SimpleNode<T> twinNode = createMutableSimpleNode(
\r
128 node.getNodeType(), node.getParent(), node.getValue());
\r
135 * @return copy of given node, parent and children are the same, but parent and children
\r
136 * have no reference to this copy
\r
138 public static CompositeNode copyNode(CompositeNode node, Node<?>... children) {
\r
139 CompositeNode twinNode = createCompositeNode(
\r
140 node.getNodeType(), node.getParent(), Arrays.asList(children));
\r
146 * @return copy of given node, parent and children are the same, but parent and children
\r
147 * have no reference to this copy
\r
149 public static CompositeNode copyNode(CompositeNode node) {
\r
150 return copyNode(node, node.getChildren().toArray(new Node<?>[0]));
\r
154 * @param node root of original tree
\r
155 * @param originalToMutable (optional) empty map, where binding between original and copy
\r
157 * @return copy of given node, parent and children are the same, but parent and children
\r
158 * have no reference to this copy
\r
160 public static MutableCompositeNode copyDeepNode(CompositeNode node,
\r
161 Map<Node<?>, Node<?>> originalToMutable) {
\r
163 MutableCompositeNode mutableRoot =
\r
164 createMutableCompositeNode(node.getNodeType(), null, null);
\r
165 Stack<SimpleEntry<CompositeNode, MutableCompositeNode>> jobQueue = new Stack<>();
\r
166 jobQueue.push(new SimpleEntry<CompositeNode, MutableCompositeNode>(node, mutableRoot));
\r
167 if (originalToMutable != null) {
\r
168 originalToMutable.put(node, mutableRoot);
\r
171 while (!jobQueue.isEmpty()) {
\r
172 SimpleEntry<CompositeNode, MutableCompositeNode> job = jobQueue.pop();
\r
173 CompositeNode originalNode = job.getKey();
\r
174 MutableCompositeNode mutableNode = job.getValue();
\r
175 mutableNode.setValue(new ArrayList<Node<?>>());
\r
177 for (Node<?> child : originalNode.getChildren()) {
\r
178 Node<?> mutableAscendant = null;
\r
179 if (child instanceof CompositeNode) {
\r
180 MutableCompositeNode newMutable =
\r
181 createMutableCompositeNode(child.getNodeType(), mutableNode, null);
\r
182 jobQueue.push(new SimpleEntry<CompositeNode, MutableCompositeNode>(
\r
183 (CompositeNode) child, newMutable));
\r
184 mutableAscendant = newMutable;
\r
185 } else if (child instanceof SimpleNode<?>) {
\r
186 mutableAscendant =
\r
187 createMutableSimpleNode(child.getNodeType(), mutableNode, child.getValue());
\r
189 throw new IllegalStateException("Node class deep copy not supported: "
\r
190 +child.getClass().getName());
\r
193 mutableNode.getChildren().add(mutableAscendant);
\r
194 if (originalToMutable != null) {
\r
195 originalToMutable.put(child, mutableAscendant);
\r
198 mutableNode.init();
\r
201 return mutableRoot;
\r