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