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