Merge "Prevent ConfigPusher from killing its thread"
[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 java.net.URI;
11 import java.util.Collections;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
15 import org.opendaylight.yangtools.yang.data.api.Node;
16 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
17
18 import com.google.common.base.Preconditions;
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(boolean composite) {
35         this.composite = composite;
36     }
37     
38     public EmptyNodeWrapper(URI namespace, String localName) {
39         this.localName = Preconditions.checkNotNull(localName);
40         this.namespace = namespace;
41     }
42     
43     @Override
44     public void setQname(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(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     public CompositeNode getParent() {
107         return unwrap().getParent();
108     }
109
110     @Override
111     public Void getValue() {
112         return null;
113     }
114
115     @Override
116     public QName getKey() {
117         return unwrap().getKey();
118     }
119
120     @Override
121     public Void setValue(Void value) {
122         return null;
123     }
124
125 }