Merge "BUG 1839 - HTTP delete of non existing data"
[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 public final class SimpleNodeWrapper implements NodeWrapper<SimpleNode<?>>, SimpleNode<Object> {
20
21     private SimpleNode<Object> simpleNode;
22
23     private String localName;
24     private Object value;
25     private URI namespace;
26     private QName name;
27
28     public SimpleNodeWrapper(final String localName, final Object value) {
29         this.localName = Preconditions.checkNotNull(localName);
30         this.value = value;
31     }
32
33     public SimpleNodeWrapper(final URI namespace, final String localName, final Object value) {
34         this(localName, value);
35         this.namespace = namespace;
36     }
37
38     @Override
39     public void setQname(final QName name) {
40         Preconditions.checkState(simpleNode == null, "Cannot change the object, due to data inconsistencies.");
41         this.name = name;
42     }
43
44     @Override
45     public QName getQname() {
46         return name;
47     }
48
49     @Override
50     public String getLocalName() {
51         if (simpleNode != null) {
52             return simpleNode.getNodeType().getLocalName();
53         }
54         return localName;
55     }
56
57     @Override
58     public URI getNamespace() {
59         if (simpleNode != null) {
60             return simpleNode.getNodeType().getNamespace();
61         }
62         return namespace;
63     }
64
65     @Override
66     public void setNamespace(final URI namespace) {
67         Preconditions.checkState(simpleNode == null, "Cannot change the object, due to data inconsistencies.");
68         this.namespace = namespace;
69     }
70
71     @Override
72     public boolean isChangeAllowed() {
73         return simpleNode == null ? true : false;
74     }
75
76     @Override
77     public SimpleNode<Object> unwrap() {
78         if (simpleNode == null) {
79             if (name == null) {
80                 Preconditions.checkNotNull(namespace);
81                 name = new QName(namespace, localName);
82             }
83             simpleNode = NodeFactory.createImmutableSimpleNode(name, null, value);
84
85             value = null;
86             namespace = null;
87             localName = null;
88             name = null;
89         }
90         return simpleNode;
91     }
92
93     @Override
94     public QName getNodeType() {
95         return unwrap().getNodeType();
96     }
97
98     @Override
99     @Deprecated
100     public CompositeNode getParent() {
101         return unwrap().getParent();
102     }
103
104     @Override
105     public Object getValue() {
106         return unwrap().getValue();
107     }
108
109     @Override
110     @Deprecated
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(final Object value) {
127         return unwrap().setValue(value);
128     }
129
130 }