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