Changed POST operation
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / SimpleNodeWrapper.java
1 package org.opendaylight.controller.sal.restconf.impl;
2
3 import java.net.URI;
4
5 import org.opendaylight.yangtools.yang.common.QName;
6 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
7 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
8 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
9 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
10 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
11
12 import com.google.common.base.Preconditions;
13
14 public final class SimpleNodeWrapper implements NodeWrapper<SimpleNode<?>>, SimpleNode<Object> {
15     
16     private SimpleNode<Object> simpleNode;
17     
18     private String localName;
19     private Object value;
20     private URI namespace;
21     private QName name;
22
23     public SimpleNodeWrapper(String localName, Object value) {
24         this.localName = Preconditions.checkNotNull(localName);
25         this.value = value;
26     }
27     
28     public SimpleNodeWrapper(URI namespace, String localName, Object value) {
29         this(localName, value);
30         this.namespace = namespace;
31     }
32     
33     @Override
34     public void setQname(QName name) {
35         Preconditions.checkState(simpleNode == null, "Cannot change the object, due to data inconsistencies.");
36         this.name = name;
37     }
38     
39     @Override
40     public String getLocalName() {
41         if (simpleNode != null) {
42             return simpleNode.getNodeType().getLocalName();
43         }
44         return localName;
45     }
46     
47     @Override
48     public URI getNamespace() {
49         if (simpleNode != null) {
50             return simpleNode.getNodeType().getNamespace();
51         }
52         return namespace;
53     }
54
55     @Override
56     public void setNamespace(URI namespace) {
57         Preconditions.checkState(simpleNode == null, "Cannot change the object, due to data inconsistencies.");
58         this.namespace = namespace;
59     }
60
61     @Override
62     public boolean isChangeAllowed() {
63         return simpleNode == null ? true : false;
64     }
65
66     @Override
67     public SimpleNode<Object> unwrap() {
68         if (simpleNode == null) {
69             if (name == null) {
70                 Preconditions.checkNotNull(namespace);
71                 name = new QName(namespace, localName);
72             }
73             simpleNode = NodeFactory.createImmutableSimpleNode(name, null, value);
74             
75             value = null;
76             namespace = null;
77             localName = null;
78             name = null;
79         }
80         return (SimpleNode<Object>) simpleNode;
81     }
82
83     @Override
84     public QName getNodeType() {
85         return unwrap().getNodeType();
86     }
87
88     @Override
89     public CompositeNode getParent() {
90         return unwrap().getParent();
91     }
92
93     @Override
94     public Object getValue() {
95         return unwrap().getValue();
96     }
97
98     @Override
99     public ModifyAction getModificationAction() {
100         return unwrap().getModificationAction();
101     }
102
103     @Override
104     public MutableSimpleNode<Object> asMutable() {
105         return unwrap().asMutable();
106     }
107
108     @Override
109     public QName getKey() {
110         return unwrap().getKey();
111     }
112
113     @Override
114     public Object setValue(Object value) {
115         return unwrap().setValue(value);
116     }
117     
118
119
120 }