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