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