Instance identifier support
[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 QName getQname() {
47         return name;
48     }
49
50     @Override
51     public String getLocalName() {
52         if (compositeNode != null) {
53             return compositeNode.getNodeType().getLocalName();
54         }
55         return localName;
56     }
57
58     @Override
59     public URI getNamespace() {
60         if (compositeNode != null) {
61             return compositeNode.getNodeType().getNamespace();
62         }
63         return namespace;
64     }
65
66     @Override
67     public void setNamespace(URI namespace) {
68         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
69         this.namespace = namespace;
70     }
71
72     public void addValue(NodeWrapper<?> value) {
73         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
74         values.add(value);
75     }
76
77     public void removeValue(NodeWrapper<CompositeNode> value) {
78         Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
79         values.remove(value);
80     }
81     
82     public List<NodeWrapper<?>> getValues() {
83         Preconditions.checkState(compositeNode == null, "Data can be inconsistent.");
84         return Collections.unmodifiableList(values);
85     }
86     
87     @Override
88     public boolean isChangeAllowed() {
89         return compositeNode == null ? true : false;
90     }
91
92     @Override
93     public CompositeNode unwrap() {
94         if (compositeNode == null) {
95             if (name == null) {
96                 Preconditions.checkNotNull(namespace);
97                 name = new QName(namespace, localName);
98             }
99             
100             List<Node<?>> nodeValues = new ArrayList<>();
101             for (NodeWrapper<?> nodeWrapper : values) {
102                 nodeValues.add(nodeWrapper.unwrap());
103             }
104             compositeNode = NodeFactory.createMutableCompositeNode(name, null, nodeValues, null, null);
105             
106             values = null;
107             namespace = null;
108             localName = null;
109             name = null;
110         }
111         return compositeNode;
112     }
113
114     @Override
115     public QName getNodeType() {
116         return unwrap().getNodeType();
117     }
118
119     @Override
120     public CompositeNode getParent() {
121         return unwrap().getParent();
122     }
123
124     @Override
125     public List<Node<?>> getValue() {
126         return unwrap().getValue();
127     }
128
129     @Override
130     public ModifyAction getModificationAction() {
131         return unwrap().getModificationAction();
132     }
133
134     @Override
135     public List<Node<?>> getChildren() {
136         return unwrap().getChildren();
137     }
138
139     @Override
140     public List<CompositeNode> getCompositesByName(QName children) {
141         return unwrap().getCompositesByName(children);
142     }
143
144     @Override
145     public List<CompositeNode> getCompositesByName(String children) {
146         return unwrap().getCompositesByName(children);
147     }
148
149     @Override
150     public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
151         return unwrap().getSimpleNodesByName(children);
152     }
153
154     @Override
155     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
156         return unwrap().getSimpleNodesByName(children);
157     }
158
159     @Override
160     public CompositeNode getFirstCompositeByName(QName container) {
161         return unwrap().getFirstCompositeByName(container);
162     }
163
164     @Override
165     public SimpleNode<?> getFirstSimpleByName(QName leaf) {
166         return unwrap().getFirstSimpleByName(leaf);
167     }
168
169     @Override
170     public MutableCompositeNode asMutable() {
171         return unwrap().asMutable();
172     }
173
174     @Override
175     public QName getKey() {
176         return unwrap().getKey();
177     }
178
179     @Override
180     public List<Node<?>> setValue(List<Node<?>> value) {
181         return unwrap().setValue(value);
182     }
183
184     @Override
185     public int size() {
186         return unwrap().size();
187     }
188
189     @Override
190     public boolean isEmpty() {
191         return unwrap().isEmpty();
192     }
193
194     @Override
195     public boolean containsKey(Object key) {
196         return unwrap().containsKey(key);
197     }
198
199     @Override
200     public boolean containsValue(Object value) {
201         return unwrap().containsValue(value);
202     }
203
204     @Override
205     public List<Node<?>> get(Object key) {
206         return unwrap().get(key);
207     }
208
209     @Override
210     public List<Node<?>> put(QName key, List<Node<?>> value) {
211         return unwrap().put(key, value);
212     }
213
214     @Override
215     public List<Node<?>> remove(Object key) {
216         return unwrap().remove(key);
217     }
218
219     @Override
220     public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
221         unwrap().putAll(m);
222     }
223
224     @Override
225     public void clear() {
226         unwrap().clear();
227     }
228
229     @Override
230     public Set<QName> keySet() {
231         return unwrap().keySet();
232     }
233
234     @Override
235     public Collection<List<Node<?>>> values() {
236         return unwrap().values();
237     }
238
239     @Override
240     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
241         return unwrap().entrySet();
242     }
243
244 }