Added getParent() method to DataSchemaNode and DataNodeContainer. Fixed Bugs.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / 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.yangtools.yang.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.YangNode;
21 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractTypeAwareBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.util.Comparators;
24 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
25
26 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
27     private boolean isBuilt;
28     private final LeafSchemaNodeImpl instance;
29     // SchemaNode args
30     private SchemaPath schemaPath;
31     private String description;
32     private String reference;
33     private Status status = Status.CURRENT;
34     // DataSchemaNode args
35     private boolean augmenting;
36     private boolean addedByUses;
37     private Boolean configuration;
38     private final ConstraintsBuilder constraints;
39     // leaf args
40     private String defaultStr;
41     private String unitsStr;
42
43     public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
44         super(moduleName, line, qname);
45         this.schemaPath = schemaPath;
46         instance = new LeafSchemaNodeImpl(qname);
47         constraints = new ConstraintsBuilder(moduleName, line);
48     }
49
50     @Override
51     public LeafSchemaNode build(YangNode parent) {
52         if (!isBuilt) {
53             instance.setParent(parent);
54             instance.setPath(schemaPath);
55             instance.setConstraints(constraints.build());
56             instance.setDescription(description);
57             instance.setReference(reference);
58             instance.setStatus(status);
59             instance.setAugmenting(augmenting);
60             instance.setAddedByUses(addedByUses);
61             instance.setConfiguration(configuration);
62             instance.setDefault(defaultStr);
63             instance.setUnits(unitsStr);
64
65             if (type == null && typedef == null) {
66                 throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
67             }
68
69             // TYPE
70             if (type == null) {
71                 instance.setType(typedef.build(instance));
72             } else {
73                 instance.setType(type);
74             }
75
76             // UNKNOWN NODES
77             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
78                 unknownNodes.add(b.build(instance));
79             }
80             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
81             instance.setUnknownSchemaNodes(unknownNodes);
82
83             isBuilt = true;
84         }
85         return instance;
86     }
87
88     @Override
89     public void setQName(QName qname) {
90         this.qname = qname;
91     }
92
93     @Override
94     public SchemaPath getPath() {
95         return schemaPath;
96     }
97
98     @Override
99     public void setPath(final SchemaPath path) {
100         this.schemaPath = path;
101     }
102
103     @Override
104     public ConstraintsBuilder getConstraints() {
105         return constraints;
106     }
107
108     @Override
109     public String getDescription() {
110         return description;
111     }
112
113     @Override
114     public void setDescription(final String description) {
115         this.description = description;
116     }
117
118     @Override
119     public String getReference() {
120         return reference;
121     }
122
123     @Override
124     public void setReference(final String reference) {
125         this.reference = reference;
126     }
127
128     @Override
129     public Status getStatus() {
130         return status;
131     }
132
133     @Override
134     public void setStatus(final Status status) {
135         if (status != null) {
136             this.status = status;
137         }
138     }
139
140     @Override
141     public boolean isAugmenting() {
142         return augmenting;
143     }
144
145     @Override
146     public void setAugmenting(final boolean augmenting) {
147         this.augmenting = augmenting;
148     }
149
150     @Override
151     public boolean isAddedByUses() {
152         return addedByUses;
153     }
154
155     @Override
156     public void setAddedByUses(final boolean addedByUses) {
157         this.addedByUses = addedByUses;
158     }
159
160     @Override
161     public Boolean isConfiguration() {
162         return configuration;
163     }
164
165     @Override
166     public void setConfiguration(final Boolean configuration) {
167         this.configuration = configuration;
168     }
169
170     public String getDefaultStr() {
171         return defaultStr;
172     }
173
174     public void setDefaultStr(String defaultStr) {
175         this.defaultStr = defaultStr;
176     }
177
178     public String getUnits() {
179         return unitsStr;
180     }
181
182     public void setUnits(String unitsStr) {
183         this.unitsStr = unitsStr;
184     }
185
186     @Override
187     public int hashCode() {
188         final int prime = 31;
189         int result = 1;
190         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
191         return result;
192     }
193
194     @Override
195     public boolean equals(Object obj) {
196         if (this == obj) {
197             return true;
198         }
199         if (obj == null) {
200             return false;
201         }
202         if (getClass() != obj.getClass()) {
203             return false;
204         }
205         LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
206         if (schemaPath == null) {
207             if (other.schemaPath != null) {
208                 return false;
209             }
210         } else if (!schemaPath.equals(other.schemaPath)) {
211             return false;
212         }
213         if (parentBuilder == null) {
214             if (other.parentBuilder != null) {
215                 return false;
216             }
217         } else if (!parentBuilder.equals(other.parentBuilder)) {
218             return false;
219         }
220         return true;
221     }
222
223     @Override
224     public String toString() {
225         return "leaf " + qname.getLocalName();
226     }
227
228     private final class LeafSchemaNodeImpl implements LeafSchemaNode {
229         private final QName qname;
230         private SchemaPath path;
231         private YangNode parent;
232         private String description;
233         private String reference;
234         private Status status = Status.CURRENT;
235         private boolean augmenting;
236         private boolean addedByUses;
237         private boolean configuration;
238         private ConstraintDefinition constraintsDef;
239         private TypeDefinition<?> type;
240         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
241         private String defaultStr;
242         private String unitsStr;
243
244         private LeafSchemaNodeImpl(final QName qname) {
245             this.qname = qname;
246         }
247
248         @Override
249         public QName getQName() {
250             return qname;
251         }
252
253         @Override
254         public SchemaPath getPath() {
255             return path;
256         }
257
258         private void setPath(final SchemaPath path) {
259             this.path = path;
260         }
261
262         @Override
263         public YangNode getParent() {
264             return parent;
265         }
266
267         private void setParent(YangNode parent) {
268             this.parent = parent;
269         }
270
271         @Override
272         public String getDescription() {
273             return description;
274         }
275
276         private void setDescription(String description) {
277             this.description = description;
278         }
279
280         @Override
281         public String getReference() {
282             return reference;
283         }
284
285         private void setReference(String reference) {
286             this.reference = reference;
287         }
288
289         @Override
290         public Status getStatus() {
291             return status;
292         }
293
294         private void setStatus(Status status) {
295             if (status != null) {
296                 this.status = status;
297             }
298         }
299
300         @Override
301         public boolean isAugmenting() {
302             return augmenting;
303         }
304
305         private void setAugmenting(boolean augmenting) {
306             this.augmenting = augmenting;
307         }
308
309         @Override
310         public boolean isAddedByUses() {
311             return addedByUses;
312         }
313
314         private void setAddedByUses(final boolean addedByUses) {
315             this.addedByUses = addedByUses;
316         }
317
318         @Override
319         public boolean isConfiguration() {
320             return configuration;
321         }
322
323         private void setConfiguration(boolean configuration) {
324             this.configuration = configuration;
325         }
326
327         @Override
328         public ConstraintDefinition getConstraints() {
329             return constraintsDef;
330         }
331
332         private void setConstraints(ConstraintDefinition constraintsDef) {
333             this.constraintsDef = constraintsDef;
334         }
335
336         @Override
337         public TypeDefinition<?> getType() {
338             return type;
339         }
340
341         private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
342             this.type = type;
343         }
344
345         @Override
346         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
347             return unknownNodes;
348         }
349
350         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
351             if (unknownNodes != null) {
352                 this.unknownNodes = unknownNodes;
353             }
354         }
355
356         @Override
357         public String getDefault() {
358             return defaultStr;
359         }
360
361         private void setDefault(String defaultStr) {
362             this.defaultStr = defaultStr;
363         }
364
365         @Override
366         public String getUnits() {
367             return unitsStr;
368         }
369
370         public void setUnits(String unitsStr) {
371             this.unitsStr = unitsStr;
372         }
373
374         @Override
375         public int hashCode() {
376             final int prime = 31;
377             int result = 1;
378             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
379             result = prime * result + ((path == null) ? 0 : path.hashCode());
380             return result;
381         }
382
383         @Override
384         public boolean equals(Object obj) {
385             if (this == obj) {
386                 return true;
387             }
388             if (obj == null) {
389                 return false;
390             }
391             if (getClass() != obj.getClass()) {
392                 return false;
393             }
394             LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
395             if (qname == null) {
396                 if (other.qname != null) {
397                     return false;
398                 }
399             } else if (!qname.equals(other.qname)) {
400                 return false;
401             }
402             if (path == null) {
403                 if (other.path != null) {
404                     return false;
405                 }
406             } else if (!path.equals(other.path)) {
407                 return false;
408             }
409             return true;
410         }
411
412         @Override
413         public String toString() {
414             StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
415             sb.append("[");
416             sb.append("qname=" + qname);
417             sb.append(", path=" + path);
418             sb.append("]");
419             return sb.toString();
420         }
421     }
422
423 }