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;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 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 import com.google.common.base.Preconditions;
28 public final class CompositeNodeWrapper implements NodeWrapper<CompositeNode>, CompositeNode {
30 private MutableCompositeNode compositeNode;
32 private String localName;
33 private URI namespace;
35 private List<NodeWrapper<?>> values = new ArrayList<>();
37 public CompositeNodeWrapper(String localName) {
38 this.localName = Preconditions.checkNotNull(localName);
41 public CompositeNodeWrapper(URI namespace, String localName) {
43 this.namespace = namespace;
47 public void setQname(QName name) {
48 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
53 public QName getQname() {
58 public String getLocalName() {
59 if (compositeNode != null) {
60 return compositeNode.getNodeType().getLocalName();
66 public URI getNamespace() {
67 if (compositeNode != null) {
68 return compositeNode.getNodeType().getNamespace();
74 public void setNamespace(URI namespace) {
75 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
76 this.namespace = namespace;
79 public void addValue(NodeWrapper<?> value) {
80 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
84 public void removeValue(NodeWrapper<CompositeNode> value) {
85 Preconditions.checkState(compositeNode == null, "Cannot change the object, due to data inconsistencies.");
89 public List<NodeWrapper<?>> getValues() {
90 Preconditions.checkState(compositeNode == null, "Data can be inconsistent.");
91 return Collections.unmodifiableList(values);
95 public boolean isChangeAllowed() {
96 return compositeNode == null ? true : false;
100 public CompositeNode unwrap() {
101 if (compositeNode == null) {
103 Preconditions.checkNotNull(namespace);
104 name = new QName(namespace, localName);
107 List<Node<?>> nodeValues = new ArrayList<>();
108 for (NodeWrapper<?> nodeWrapper : values) {
109 nodeValues.add(nodeWrapper.unwrap());
111 compositeNode = NodeFactory.createMutableCompositeNode(name, null, nodeValues, null, null);
118 return compositeNode;
122 public QName getNodeType() {
123 return unwrap().getNodeType();
127 public CompositeNode getParent() {
128 return unwrap().getParent();
132 public List<Node<?>> getValue() {
133 return unwrap().getValue();
137 public ModifyAction getModificationAction() {
138 return unwrap().getModificationAction();
142 public List<Node<?>> getChildren() {
143 return unwrap().getChildren();
147 public List<CompositeNode> getCompositesByName(QName children) {
148 return unwrap().getCompositesByName(children);
152 public List<CompositeNode> getCompositesByName(String children) {
153 return unwrap().getCompositesByName(children);
157 public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
158 return unwrap().getSimpleNodesByName(children);
162 public List<SimpleNode<?>> getSimpleNodesByName(String children) {
163 return unwrap().getSimpleNodesByName(children);
167 public CompositeNode getFirstCompositeByName(QName container) {
168 return unwrap().getFirstCompositeByName(container);
172 public SimpleNode<?> getFirstSimpleByName(QName leaf) {
173 return unwrap().getFirstSimpleByName(leaf);
177 public MutableCompositeNode asMutable() {
178 return unwrap().asMutable();
182 public QName getKey() {
183 return unwrap().getKey();
187 public List<Node<?>> setValue(List<Node<?>> value) {
188 return unwrap().setValue(value);
193 return unwrap().size();
197 public boolean isEmpty() {
198 return unwrap().isEmpty();
202 public boolean containsKey(Object key) {
203 return unwrap().containsKey(key);
207 public boolean containsValue(Object value) {
208 return unwrap().containsValue(value);
212 public List<Node<?>> get(Object key) {
213 return unwrap().get(key);
217 public List<Node<?>> put(QName key, List<Node<?>> value) {
218 return unwrap().put(key, value);
222 public List<Node<?>> remove(Object key) {
223 return unwrap().remove(key);
227 public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
232 public void clear() {
237 public Set<QName> keySet() {
238 return unwrap().keySet();
242 public Collection<List<Node<?>>> values() {
243 return unwrap().values();
247 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
248 return unwrap().entrySet();