a3d44d3572106232e10edaad73e91d1b4d46039a
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / EmptyNodeWrapper.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.Collections;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
15 import org.opendaylight.yangtools.yang.data.api.Node;
16 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
17
18 public final class EmptyNodeWrapper implements NodeWrapper<Node<?>>, Node<Void> {
19
20     private Node<?> unwrapped;
21
22     private String localName;
23     private URI namespace;
24     private QName name;
25
26     private boolean composite;
27
28     public boolean isComposite() {
29         return composite;
30     }
31
32     public void setComposite(final boolean composite) {
33         this.composite = composite;
34     }
35
36     public EmptyNodeWrapper(final URI namespace, final String localName) {
37         this.localName = Preconditions.checkNotNull(localName);
38         this.namespace = namespace;
39     }
40
41     @Override
42     public void setQname(final QName name) {
43         Preconditions.checkState(unwrapped == null, "Cannot change the object, due to data inconsistencies.");
44         this.name = name;
45     }
46
47     @Override
48     public QName getQname() {
49         return name;
50     }
51
52     @Override
53     public String getLocalName() {
54         if (unwrapped != null) {
55             return unwrapped.getNodeType().getLocalName();
56         }
57         return localName;
58     }
59
60     @Override
61     public URI getNamespace() {
62         if (unwrapped != null) {
63             return unwrapped.getNodeType().getNamespace();
64         }
65         return namespace;
66     }
67
68     @Override
69     public void setNamespace(final URI namespace) {
70         Preconditions.checkState(unwrapped == null, "Cannot change the object, due to data inconsistencies.");
71         this.namespace = namespace;
72     }
73
74     @Override
75     public boolean isChangeAllowed() {
76         return unwrapped == null ? true : false;
77     }
78
79     @Override
80     public Node<?> unwrap() {
81         if (unwrapped == null) {
82             if (name == null) {
83                 Preconditions.checkNotNull(namespace);
84                 name = new QName(namespace, localName);
85             }
86             if (composite) {
87                 unwrapped = NodeFactory.createImmutableCompositeNode(name, null, Collections.<Node<?>> emptyList(),
88                         null);
89             } else {
90                 unwrapped = NodeFactory.createImmutableSimpleNode(name, null, null);
91             }
92             namespace = null;
93             localName = null;
94             name = null;
95         }
96         return unwrapped;
97     }
98
99     @Override
100     public QName getNodeType() {
101         return unwrap().getNodeType();
102     }
103
104     @Override
105     @Deprecated
106     public CompositeNode getParent() {
107         return unwrap().getParent();
108     }
109
110     @Override
111     public Void getValue() {
112         return null;
113     }
114
115     @Override
116     public QName getKey() {
117         return unwrap().getKey();
118     }
119
120     @Override
121     public Void setValue(final Void value) {
122         return null;
123     }
124
125 }