69b975dab7e5ef15bb7e5ad4cf389ed55f0ef9c8
[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
12 import java.net.URI;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
22 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
23 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.Node;
25 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
26 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
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(final String localName) {
38         this.localName = Preconditions.checkNotNull(localName);
39     }
40
41     public CompositeNodeWrapper(final URI namespace, final String localName) {
42         this(localName);
43         this.namespace = namespace;
44     }
45
46     @Override
47     public void setQname(final 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(final 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(final 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(final 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<>(values.size());
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     @Deprecated
127     @Override
128     public CompositeNode getParent() {
129         return unwrap().getParent();
130     }
131
132     @Override
133     public List<Node<?>> getValue() {
134         return unwrap().getValue();
135     }
136
137     @Override
138     public ModifyAction getModificationAction() {
139         return unwrap().getModificationAction();
140     }
141
142     /**
143      * @deprecated Use {@link #getValue()} instead.
144      */
145     @Deprecated
146     @Override
147     public List<Node<?>> getChildren() {
148         return unwrap().getValue();
149     }
150
151     @Override
152     public List<CompositeNode> getCompositesByName(final QName children) {
153         return unwrap().getCompositesByName(children);
154     }
155
156     @Override
157     public List<CompositeNode> getCompositesByName(final String children) {
158         return unwrap().getCompositesByName(children);
159     }
160
161     @Override
162     public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
163         return unwrap().getSimpleNodesByName(children);
164     }
165
166     @Override
167     public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
168         return unwrap().getSimpleNodesByName(children);
169     }
170
171     @Override
172     public CompositeNode getFirstCompositeByName(final QName container) {
173         return unwrap().getFirstCompositeByName(container);
174     }
175
176     @Override
177     public SimpleNode<?> getFirstSimpleByName(final QName leaf) {
178         return unwrap().getFirstSimpleByName(leaf);
179     }
180
181     @Override
182     public MutableCompositeNode asMutable() {
183         return unwrap().asMutable();
184     }
185
186     @Override
187     public QName getKey() {
188         return unwrap().getKey();
189     }
190
191     @Override
192     public List<Node<?>> setValue(final List<Node<?>> value) {
193         return unwrap().setValue(value);
194     }
195
196     @Override
197     public int size() {
198         return unwrap().size();
199     }
200
201     @Override
202     public boolean isEmpty() {
203         return unwrap().isEmpty();
204     }
205
206     @Override
207     public boolean containsKey(final Object key) {
208         return unwrap().containsKey(key);
209     }
210
211     @Override
212     public boolean containsValue(final Object value) {
213         return unwrap().containsValue(value);
214     }
215
216     @Override
217     public List<Node<?>> get(final Object key) {
218         return unwrap().get(key);
219     }
220
221     @Override
222     public List<Node<?>> put(final QName key, final List<Node<?>> value) {
223         return unwrap().put(key, value);
224     }
225
226     @Override
227     public List<Node<?>> remove(final Object key) {
228         return unwrap().remove(key);
229     }
230
231     @Override
232     public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
233         unwrap().putAll(m);
234     }
235
236     @Override
237     public void clear() {
238         unwrap().clear();
239     }
240
241     @Override
242     public Set<QName> keySet() {
243         return unwrap().keySet();
244     }
245
246     @Override
247     public Collection<List<Node<?>>> values() {
248         return unwrap().values();
249     }
250
251     @Override
252     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
253         return unwrap().entrySet();
254     }
255
256 }