Added YANG typedef support in the YANG parser
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / LeafSchemaNodeBuilder.java
1 /*
2  * Copyright (c) 2013 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.yang.model.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
16 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.controller.yang.model.api.SchemaPath;
18 import org.opendaylight.controller.yang.model.api.Status;
19 import org.opendaylight.controller.yang.model.api.TypeDefinition;
20 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
23 import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder;
24
25 public class LeafSchemaNodeBuilder implements DataSchemaNodeBuilder,
26         SchemaNodeBuilder, TypeAwareBuilder {
27
28     private final QName qname;
29     private final LeafSchemaNodeImpl instance;
30     private final ConstraintsBuilder constraintsBuilder = new ConstraintsBuilder();
31     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
32     private TypeDefinition<?> type;
33
34     LeafSchemaNodeBuilder(QName qname) {
35         this.qname = qname;
36         instance = new LeafSchemaNodeImpl(qname);
37     }
38
39     @Override
40     public LeafSchemaNode build() {
41         // UNKNOWN NODES
42         final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
43         for(UnknownSchemaNodeBuilder b : addedUnknownNodes) {
44             unknownNodes.add(b.build());
45         }
46         instance.setUnknownSchemaNodes(unknownNodes);
47
48         instance.setConstraints(constraintsBuilder.build());
49         return instance;
50     }
51
52     @Override
53     public QName getQName() {
54         return qname;
55     }
56
57     @Override
58     public void setPath(SchemaPath path) {
59         instance.setPath(path);
60     }
61
62     @Override
63     public void setDescription(String description) {
64         instance.setDescription(description);
65     }
66
67     @Override
68     public void setReference(String reference) {
69         instance.setReference(reference);
70     }
71
72     @Override
73     public void setStatus(Status status) {
74         if(status != null) {
75             instance.setStatus(status);
76         }
77     }
78
79     @Override
80     public void setAugmenting(boolean augmenting) {
81         instance.setAugmenting(augmenting);
82     }
83
84     @Override
85     public void setConfiguration(boolean configuration) {
86         instance.setConfiguration(configuration);
87     }
88
89     @Override
90     public ConstraintsBuilder getConstraintsBuilder() {
91         return constraintsBuilder;
92     }
93
94     @Override
95     public TypeDefinition<?> getType() {
96         return type;
97     }
98
99     @Override
100     public void setType(TypeDefinition<?> type) {
101         this.type = type;
102         instance.setType(type);
103     }
104
105     @Override
106     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) {
107         addedUnknownNodes.add(unknownSchemaNodeBuilder);
108     }
109
110     private class LeafSchemaNodeImpl implements LeafSchemaNode {
111         private final QName qname;
112         private SchemaPath path;
113         private String description;
114         private String reference;
115         private Status status = Status.CURRENT;
116         private boolean augmenting;
117         private boolean configuration;
118         private ConstraintDefinition constraints;
119         private TypeDefinition<?> type;
120         private List<UnknownSchemaNode> unknownSchemaNodes = Collections.emptyList();
121
122         private LeafSchemaNodeImpl(QName qname) {
123             this.qname = qname;
124         }
125
126         @Override
127         public QName getQName() {
128             return qname;
129         }
130
131         @Override
132         public SchemaPath getPath() {
133             return path;
134         }
135
136         private void setPath(SchemaPath path) {
137             this.path = path;
138         }
139
140         @Override
141         public String getDescription() {
142             return description;
143         }
144
145         private void setDescription(String description) {
146             this.description = description;
147         }
148
149         @Override
150         public String getReference() {
151             return reference;
152         }
153
154         private void setReference(String reference) {
155             this.reference = reference;
156         }
157
158         @Override
159         public Status getStatus() {
160             return status;
161         }
162
163         private void setStatus(Status status) {
164             if (status != null) {
165                 this.status = status;
166             }
167         }
168
169         @Override
170         public boolean isAugmenting() {
171             return augmenting;
172         }
173
174         private void setAugmenting(boolean augmenting) {
175             this.augmenting = augmenting;
176         }
177
178         @Override
179         public boolean isConfiguration() {
180             return configuration;
181         }
182
183         private void setConfiguration(boolean configuration) {
184             this.configuration = configuration;
185         }
186
187         @Override
188         public ConstraintDefinition getConstraints() {
189             return constraints;
190         }
191
192         private void setConstraints(ConstraintDefinition constraints) {
193             this.constraints = constraints;
194         }
195
196         @Override
197         public TypeDefinition<?> getType() {
198             return type;
199         }
200
201         private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
202             this.type = type;
203         }
204
205         @Override
206         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
207             return unknownSchemaNodes;
208         }
209
210         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
211             if(unknownSchemaNodes != null) {
212                 this.unknownSchemaNodes = unknownSchemaNodes;
213             }
214         }
215
216         @Override
217         public int hashCode() {
218             final int prime = 31;
219             int result = 1;
220             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
221             result = prime * result + ((path == null) ? 0 : path.hashCode());
222             return result;
223         }
224
225         @Override
226         public boolean equals(Object obj) {
227             if (this == obj) {
228                 return true;
229             }
230             if (obj == null) {
231                 return false;
232             }
233             if (getClass() != obj.getClass()) {
234                 return false;
235             }
236             LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
237             if (qname == null) {
238                 if (other.qname != null) {
239                     return false;
240                 }
241             } else if (!qname.equals(other.qname)) {
242                 return false;
243             }
244             if (path == null) {
245                 if (other.path != null) {
246                     return false;
247                 }
248             } else if (!path.equals(other.path)) {
249                 return false;
250             }
251             return true;
252         }
253
254         @Override
255         public String toString() {
256             StringBuilder sb = new StringBuilder(
257                     LeafSchemaNodeImpl.class.getSimpleName());
258             sb.append("[");
259             sb.append("qname=" + qname);
260             sb.append(", path=" + path);
261             sb.append(", description=" + description);
262             sb.append(", reference=" + reference);
263             sb.append(", status=" + status);
264             sb.append(", augmenting=" + augmenting);
265             sb.append(", configuration=" + configuration);
266             sb.append(", constraints=" + constraints);
267             sb.append(", type=" + type);
268             sb.append(", constraints=" + constraints);
269             sb.append("]");
270             return sb.toString();
271         }
272     }
273
274 }