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