Fixed serialization issues with Augments and java types
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / CompositeNodeWrapper.java
1 package org.opendaylight.controller.sal.restconf.impl;
2
3 import java.net.URI;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Set;
10
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
13 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
14 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
15 import org.opendaylight.yangtools.yang.data.api.Node;
16 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
17 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
18
19 import com.google.common.base.Preconditions;
20
21 public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, CompositeNode {
22
23     private MutableCompositeNode compositeNode;
24
25     private String localName;
26     private URI namespace;
27     private QName name;
28     private List<NodeWrapper<?>> values = new ArrayList<>();
29
30     public CompositeNodeWrapper(String localName) {
31         this.localName = Preconditions.checkNotNull(localName);
32     }
33
34     public CompositeNodeWrapper(URI namespace, String localName) {
35         this(localName);
36         this.namespace = namespace;
37     }
38     
39     @Override
40     public void setQname(QName name) {
41         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
42         this.name = name;
43     }
44
45     @Override
46     public String getLocalName() {
47         if (compositeNode != null) {
48             return compositeNode.getNodeType().getLocalName();
49         }
50         return localName;
51     }
52
53     @Override
54     public URI getNamespace() {
55         if (compositeNode != null) {
56             return compositeNode.getNodeType().getNamespace();
57         }
58         return namespace;
59     }
60
61     @Override
62     public void setNamespace(URI namespace) {
63         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
64         this.namespace = namespace;
65     }
66
67     public void addValue(NodeWrapper<?> value) {
68         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
69         values.add(value);
70     }
71
72     public void removeValue(NodeWrapper<CompositeNode> value) {
73         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
74         values.remove(value);
75     }
76     
77     public List<NodeWrapper<?>> getValues() {
78         Preconditions.checkState(compositeNode == null, "Data can be inconsistent.");
79         return Collections.unmodifiableList(values);
80     }
81
82     @Override
83     public CompositeNode unwrap() {
84         if (compositeNode == null) {
85             if (name == null) {
86                 Preconditions.checkNotNull(namespace);
87                 name = new QName(namespace, localName);
88             }
89             
90             List<Node<?>> nodeValues = new ArrayList<>();
91             for (NodeWrapper<?> nodeWrapper : values) {
92                 nodeValues.add(nodeWrapper.unwrap());
93             }
94             compositeNode = NodeFactory.createMutableCompositeNode(name, null, nodeValues, null, null);
95             
96             values = null;
97             namespace = null;
98             localName = null;
99             name = null;
100         }
101         return compositeNode;
102     }
103
104     @Override
105     public QName getNodeType() {
106         return unwrap().getNodeType();
107     }
108
109     @Override
110     public CompositeNode getParent() {
111         return unwrap().getParent();
112     }
113
114     @Override
115     public List<Node<?>> getValue() {
116         return unwrap().getValue();
117     }
118
119     @Override
120     public ModifyAction getModificationAction() {
121         return unwrap().getModificationAction();
122     }
123
124     @Override
125     public List<Node<?>> getChildren() {
126         return unwrap().getChildren();
127     }
128
129     @Override
130     public List<CompositeNode> getCompositesByName(QName children) {
131         return unwrap().getCompositesByName(children);
132     }
133
134     @Override
135     public List<CompositeNode> getCompositesByName(String children) {
136         return unwrap().getCompositesByName(children);
137     }
138
139     @Override
140     public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
141         return unwrap().getSimpleNodesByName(children);
142     }
143
144     @Override
145     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
146         return unwrap().getSimpleNodesByName(children);
147     }
148
149     @Override
150     public CompositeNode getFirstCompositeByName(QName container) {
151         return unwrap().getFirstCompositeByName(container);
152     }
153
154     @Override
155     public SimpleNode<?> getFirstSimpleByName(QName leaf) {
156         return unwrap().getFirstSimpleByName(leaf);
157     }
158
159     @Override
160     public MutableCompositeNode asMutable() {
161         return unwrap().asMutable();
162     }
163
164     @Override
165     public QName getKey() {
166         return unwrap().getKey();
167     }
168
169     @Override
170     public List<Node<?>> setValue(List<Node<?>> value) {
171         return unwrap().setValue(value);
172     }
173
174     @Override
175     public int size() {
176         return unwrap().size();
177     }
178
179     @Override
180     public boolean isEmpty() {
181         return unwrap().isEmpty();
182     }
183
184     @Override
185     public boolean containsKey(Object key) {
186         return unwrap().containsKey(key);
187     }
188
189     @Override
190     public boolean containsValue(Object value) {
191         return unwrap().containsValue(value);
192     }
193
194     @Override
195     public List<Node<?>> get(Object key) {
196         return unwrap().get(key);
197     }
198
199     @Override
200     public List<Node<?>> put(QName key, List<Node<?>> value) {
201         return unwrap().put(key, value);
202     }
203
204     @Override
205     public List<Node<?>> remove(Object key) {
206         return unwrap().remove(key);
207     }
208
209     @Override
210     public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
211         unwrap().putAll(m);
212     }
213
214     @Override
215     public void clear() {
216         unwrap().clear();
217     }
218
219     @Override
220     public Set<QName> keySet() {
221         return unwrap().keySet();
222     }
223
224     @Override
225     public Collection<List<Node<?>>> values() {
226         return unwrap().values();
227     }
228
229     @Override
230     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
231         return unwrap().entrySet();
232     }
233 }