Merge "Fix race conditions between config-manager and persister."
[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(CompositeNode parent) {
84         if (compositeNode == null) {
85             if (name == null) {
86                 Preconditions.checkNotNull(namespace);
87                 name = new QName(namespace, localName);
88             }
89             compositeNode = NodeFactory.createMutableCompositeNode(name, parent, new ArrayList<Node<?>>(), null, null);
90             
91             List<Node<?>> nodeValues = new ArrayList<>();
92             for (NodeWrapper<?> nodeWrapper : values) {
93                 nodeValues.add(nodeWrapper.unwrap(compositeNode));
94             }
95             compositeNode.setValue(nodeValues);
96             
97             values = null;
98             namespace = null;
99             localName = null;
100             name = null;
101         }
102         return compositeNode;
103     }
104
105     @Override
106     public QName getNodeType() {
107         return unwrap(null).getNodeType();
108     }
109
110     @Override
111     public CompositeNode getParent() {
112         return unwrap(null).getParent();
113     }
114
115     @Override
116     public List<Node<?>> getValue() {
117         return unwrap(null).getValue();
118     }
119
120     @Override
121     public ModifyAction getModificationAction() {
122         return unwrap(null).getModificationAction();
123     }
124
125     @Override
126     public List<Node<?>> getChildren() {
127         return unwrap(null).getChildren();
128     }
129
130     @Override
131     public List<CompositeNode> getCompositesByName(QName children) {
132         return unwrap(null).getCompositesByName(children);
133     }
134
135     @Override
136     public List<CompositeNode> getCompositesByName(String children) {
137         return unwrap(null).getCompositesByName(children);
138     }
139
140     @Override
141     public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
142         return unwrap(null).getSimpleNodesByName(children);
143     }
144
145     @Override
146     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
147         return unwrap(null).getSimpleNodesByName(children);
148     }
149
150     @Override
151     public CompositeNode getFirstCompositeByName(QName container) {
152         return unwrap(null).getFirstCompositeByName(container);
153     }
154
155     @Override
156     public SimpleNode<?> getFirstSimpleByName(QName leaf) {
157         return unwrap(null).getFirstSimpleByName(leaf);
158     }
159
160     @Override
161     public MutableCompositeNode asMutable() {
162         return unwrap(null).asMutable();
163     }
164
165     @Override
166     public QName getKey() {
167         return unwrap(null).getKey();
168     }
169
170     @Override
171     public List<Node<?>> setValue(List<Node<?>> value) {
172         return unwrap(null).setValue(value);
173     }
174
175     @Override
176     public int size() {
177         return unwrap(null).size();
178     }
179
180     @Override
181     public boolean isEmpty() {
182         return unwrap(null).isEmpty();
183     }
184
185     @Override
186     public boolean containsKey(Object key) {
187         return unwrap(null).containsKey(key);
188     }
189
190     @Override
191     public boolean containsValue(Object value) {
192         return unwrap(null).containsValue(value);
193     }
194
195     @Override
196     public List<Node<?>> get(Object key) {
197         return unwrap(null).get(key);
198     }
199
200     @Override
201     public List<Node<?>> put(QName key, List<Node<?>> value) {
202         return unwrap(null).put(key, value);
203     }
204
205     @Override
206     public List<Node<?>> remove(Object key) {
207         return unwrap(null).remove(key);
208     }
209
210     @Override
211     public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
212         unwrap(null).putAll(m);
213     }
214
215     @Override
216     public void clear() {
217         unwrap(null).clear();
218     }
219
220     @Override
221     public Set<QName> keySet() {
222         return unwrap(null).keySet();
223     }
224
225     @Override
226     public Collection<List<Node<?>>> values() {
227         return unwrap(null).values();
228     }
229
230     @Override
231     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
232         return unwrap(null).entrySet();
233     }
234
235 }