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