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