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