2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl;
10 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
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;
26 public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, CompositeNode {
28 private MutableCompositeNode compositeNode;
30 private String localName;
31 private URI namespace;
33 private List<NodeWrapper<?>> values = new ArrayList<>();
35 public CompositeNodeWrapper(final String localName) {
36 this.localName = Preconditions.checkNotNull(localName);
39 public CompositeNodeWrapper(final URI namespace, final String localName) {
41 this.namespace = namespace;
45 public void setQname(final QName name) {
46 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
51 public QName getQname() {
56 public String getLocalName() {
57 if (compositeNode != null) {
58 return compositeNode.getNodeType().getLocalName();
64 public URI getNamespace() {
65 if (compositeNode != null) {
66 return compositeNode.getNodeType().getNamespace();
72 public void setNamespace(final URI namespace) {
73 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
74 this.namespace = namespace;
77 public void addValue(final NodeWrapper<?> value) {
78 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
82 public void removeValue(final NodeWrapper<CompositeNode> value) {
83 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
87 public List<NodeWrapper<?>> getValues() {
88 Preconditions.checkState(compositeNode == null, "Data can be inconsistent.");
89 return Collections.unmodifiableList(values);
93 public boolean isChangeAllowed() {
94 return compositeNode == null ? true : false;
98 public CompositeNode unwrap() {
99 if (compositeNode == null) {
101 Preconditions.checkNotNull(namespace);
102 name = new QName(namespace, localName);
105 List<Node<?>> nodeValues = new ArrayList<>(values.size());
106 for (NodeWrapper<?> nodeWrapper : values) {
107 nodeValues.add(nodeWrapper.unwrap());
109 compositeNode = NodeFactory.createMutableCompositeNode(name, null, nodeValues, null, null);
116 return compositeNode;
120 public QName getNodeType() {
121 return unwrap().getNodeType();
126 public CompositeNode getParent() {
127 return unwrap().getParent();
131 public List<Node<?>> getValue() {
132 return unwrap().getValue();
136 public ModifyAction getModificationAction() {
137 return unwrap().getModificationAction();
141 * @deprecated Use {@link #getValue()} instead.
145 public List<Node<?>> getChildren() {
146 return unwrap().getValue();
150 public List<CompositeNode> getCompositesByName(final QName children) {
151 return unwrap().getCompositesByName(children);
155 public List<CompositeNode> getCompositesByName(final String children) {
156 return unwrap().getCompositesByName(children);
160 public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
161 return unwrap().getSimpleNodesByName(children);
165 public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
166 return unwrap().getSimpleNodesByName(children);
170 public CompositeNode getFirstCompositeByName(final QName container) {
171 return unwrap().getFirstCompositeByName(container);
175 public SimpleNode<?> getFirstSimpleByName(final QName leaf) {
176 return unwrap().getFirstSimpleByName(leaf);
180 public MutableCompositeNode asMutable() {
181 return unwrap().asMutable();
185 public QName getKey() {
186 return unwrap().getKey();
190 public List<Node<?>> setValue(final List<Node<?>> value) {
191 return unwrap().setValue(value);
196 return unwrap().size();
200 public boolean isEmpty() {
201 return unwrap().isEmpty();
205 public boolean containsKey(final Object key) {
206 return unwrap().containsKey(key);
210 public boolean containsValue(final Object value) {
211 return unwrap().containsValue(value);
215 public List<Node<?>> get(final Object key) {
216 return unwrap().get(key);
220 public List<Node<?>> put(final QName key, final List<Node<?>> value) {
221 return unwrap().put(key, value);
225 public List<Node<?>> remove(final Object key) {
226 return unwrap().remove(key);
230 public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
235 public void clear() {
240 public Set<QName> keySet() {
241 return unwrap().keySet();
245 public Collection<List<Node<?>>> values() {
246 return unwrap().values();
250 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
251 return unwrap().entrySet();