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 / ChoiceCaseBuilder.java
1 package org.opendaylight.yangtools.yang.parser.builder.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
12 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
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.UsesNode;
21 import org.opendaylight.yangtools.yang.model.api.YangNode;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.util.Comparators;
29 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
30
31 public final class ChoiceCaseBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
32         AugmentationTargetBuilder {
33     private boolean isBuilt;
34     private final ChoiceCaseNodeImpl instance;
35     private YangNode parent;
36     // SchemaNode args
37     private SchemaPath schemaPath;
38     private String description;
39     private String reference;
40     private Status status = Status.CURRENT;
41     // DataSchemaNode args
42     private boolean augmenting;
43     private boolean addedByUses;
44     private final ConstraintsBuilder constraints;
45     // AugmentationTarget args
46     private final List<AugmentationSchema> augmentations = new ArrayList<>();
47     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
48
49     public ChoiceCaseBuilder(final String moduleName, final int line, final QName qname) {
50         super(moduleName, line, qname);
51         instance = new ChoiceCaseNodeImpl(qname);
52         constraints = new ConstraintsBuilder(moduleName, line);
53     }
54
55     @Override
56     public ChoiceCaseNode build(YangNode parent) {
57         if (!isBuilt) {
58             this.parent = parent;
59             instance.setParent(parent);
60             instance.setConstraints(constraints.build());
61             instance.setPath(schemaPath);
62             instance.setDescription(description);
63             instance.setReference(reference);
64             instance.setStatus(status);
65             instance.setAugmenting(augmenting);
66             instance.setAddedByUses(addedByUses);
67
68             // CHILD NODES
69             for (DataSchemaNodeBuilder node : addedChildNodes) {
70                 DataSchemaNode child = node.build(instance);
71                 childNodes.put(child.getQName(), child);
72             }
73             instance.setChildNodes(childNodes);
74
75             // USES
76             for (UsesNodeBuilder builder : addedUsesNodes) {
77                 usesNodes.add(builder.build(instance));
78             }
79             instance.setUses(usesNodes);
80
81             // UNKNOWN NODES
82             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
83                 unknownNodes.add(b.build(instance));
84             }
85             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
86             instance.setUnknownSchemaNodes(unknownNodes);
87
88             // AUGMENTATIONS
89             for (AugmentationSchemaBuilder builder : augmentationBuilders) {
90                 augmentations.add(builder.build(instance));
91             }
92             instance.setAvailableAugmentations(new HashSet<>(augmentations));
93
94             isBuilt = true;
95         }
96
97         return instance;
98     }
99
100     @Override
101     public void rebuild() {
102         isBuilt = false;
103         build(parent);
104     }
105
106     @Override
107     public void setQName(QName qname) {
108         this.qname = qname;
109     }
110
111     @Override
112     public SchemaPath getPath() {
113         return schemaPath;
114     }
115
116     @Override
117     public void setPath(final SchemaPath schemaPath) {
118         this.schemaPath = schemaPath;
119         instance.setPath(schemaPath);
120     }
121
122     public String getDescription() {
123         return description;
124     }
125
126     @Override
127     public void setDescription(final String description) {
128         this.description = description;
129     }
130
131     public String getReference() {
132         return reference;
133     }
134
135     @Override
136     public void setReference(String reference) {
137         this.reference = reference;
138     }
139
140     public Status getStatus() {
141         return status;
142     }
143
144     @Override
145     public void setStatus(Status status) {
146         if (status != null) {
147             this.status = status;
148         }
149     }
150
151     @Override
152     public boolean isAugmenting() {
153         return augmenting;
154     }
155
156     @Override
157     public void setAugmenting(boolean augmenting) {
158         this.augmenting = augmenting;
159     }
160
161     @Override
162     public boolean isAddedByUses() {
163         return addedByUses;
164     }
165
166     @Override
167     public void setAddedByUses(boolean addedByUses) {
168         this.addedByUses = addedByUses;
169     }
170
171     @Override
172     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
173         return Collections.emptySet();
174     }
175
176     @Override
177     public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
178         throw new YangParseException(moduleName, line, "Can not add type definition to choice case.");
179     }
180
181     @Override
182     public Boolean isConfiguration() {
183         return false;
184     }
185
186     @Override
187     public void setConfiguration(final Boolean configuration) {
188         throw new YangParseException(moduleName, line, "Can not add config statement to choice case.");
189     }
190
191     @Override
192     public ConstraintsBuilder getConstraints() {
193         return constraints;
194     }
195
196     @Override
197     public void addAugmentation(AugmentationSchemaBuilder augment) {
198         augmentationBuilders.add(augment);
199     }
200
201     @Override
202     public int hashCode() {
203         final int prime = 31;
204         int result = 1;
205         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
206         return result;
207     }
208
209     @Override
210     public boolean equals(Object obj) {
211         if (this == obj) {
212             return true;
213         }
214         if (obj == null) {
215             return false;
216         }
217         if (getClass() != obj.getClass()) {
218             return false;
219         }
220         ChoiceCaseBuilder other = (ChoiceCaseBuilder) obj;
221         if (schemaPath == null) {
222             if (other.schemaPath != null) {
223                 return false;
224             }
225         } else if (!schemaPath.equals(other.schemaPath)) {
226             return false;
227         }
228         if (parentBuilder == null) {
229             if (other.parentBuilder != null) {
230                 return false;
231             }
232         } else if (!parentBuilder.equals(other.parentBuilder)) {
233             return false;
234         }
235         return true;
236     }
237
238     @Override
239     public String toString() {
240         return "case " + getQName().getLocalName();
241     }
242
243     public final class ChoiceCaseNodeImpl implements ChoiceCaseNode {
244         private final QName qname;
245         private SchemaPath path;
246         private YangNode parent;
247         private String description;
248         private String reference;
249         private Status status = Status.CURRENT;
250         private boolean augmenting;
251         private boolean addedByUses;
252         private ConstraintDefinition constraints;
253         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
254         private Set<AugmentationSchema> augmentations = Collections.emptySet();
255         private Set<UsesNode> uses = Collections.emptySet();
256         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
257
258         private ChoiceCaseNodeImpl(QName qname) {
259             this.qname = qname;
260         }
261
262         @Override
263         public QName getQName() {
264             return qname;
265         }
266
267         @Override
268         public SchemaPath getPath() {
269             return path;
270         }
271
272         private void setPath(SchemaPath path) {
273             this.path = path;
274         }
275
276         @Override
277         public YangNode getParent() {
278             return parent;
279         }
280
281         private void setParent(YangNode parent) {
282             this.parent = parent;
283         }
284
285         @Override
286         public String getDescription() {
287             return description;
288         }
289
290         private void setDescription(String description) {
291             this.description = description;
292         }
293
294         @Override
295         public String getReference() {
296             return reference;
297         }
298
299         private void setReference(String reference) {
300             this.reference = reference;
301         }
302
303         @Override
304         public Status getStatus() {
305             return status;
306         }
307
308         private void setStatus(Status status) {
309             if (status != null) {
310                 this.status = status;
311             }
312         }
313
314         @Override
315         public boolean isConfiguration() {
316             return false;
317         }
318
319         @Override
320         public ConstraintDefinition getConstraints() {
321             return constraints;
322         }
323
324         private void setConstraints(ConstraintDefinition constraints) {
325             this.constraints = constraints;
326         }
327
328         @Override
329         public boolean isAugmenting() {
330             return augmenting;
331         }
332
333         private void setAugmenting(boolean augmenting) {
334             this.augmenting = augmenting;
335         }
336
337         @Override
338         public boolean isAddedByUses() {
339             return addedByUses;
340         }
341
342         private void setAddedByUses(boolean addedByUses) {
343             this.addedByUses = addedByUses;
344         }
345
346         @Override
347         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
348             return unknownNodes;
349         }
350
351         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
352             if (unknownNodes != null) {
353                 this.unknownNodes = unknownNodes;
354             }
355         }
356
357         /**
358          * Always returns an empty set, because case node can not contains type
359          * definitions.
360          */
361         @Override
362         public Set<TypeDefinition<?>> getTypeDefinitions() {
363             return Collections.emptySet();
364         }
365
366         @Override
367         public Set<DataSchemaNode> getChildNodes() {
368             return new HashSet<>(childNodes.values());
369         }
370
371         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
372             if (childNodes != null) {
373                 this.childNodes = childNodes;
374             }
375         }
376
377         @Override
378         public Set<GroupingDefinition> getGroupings() {
379             return Collections.emptySet();
380         }
381
382         @Override
383         public DataSchemaNode getDataChildByName(QName name) {
384             return childNodes.get(name);
385         }
386
387         @Override
388         public DataSchemaNode getDataChildByName(String name) {
389             DataSchemaNode result = null;
390             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
391                 if (entry.getKey().getLocalName().equals(name)) {
392                     result = entry.getValue();
393                     break;
394                 }
395             }
396             return result;
397         }
398
399         @Override
400         public Set<UsesNode> getUses() {
401             return uses;
402         }
403
404         private void setUses(Set<UsesNode> uses) {
405             if (uses != null) {
406                 this.uses = uses;
407             }
408         }
409
410         @Override
411         public Set<AugmentationSchema> getAvailableAugmentations() {
412             return augmentations;
413         }
414
415         private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
416             if (augmentations != null) {
417                 this.augmentations = augmentations;
418             }
419         }
420
421         public ChoiceCaseBuilder toBuilder() {
422             return ChoiceCaseBuilder.this;
423         }
424
425         @Override
426         public int hashCode() {
427             final int prime = 31;
428             int result = 1;
429             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
430             result = prime * result + ((path == null) ? 0 : path.hashCode());
431             return result;
432         }
433
434         @Override
435         public boolean equals(Object obj) {
436             if (this == obj) {
437                 return true;
438             }
439             if (obj == null) {
440                 return false;
441             }
442             if (getClass() != obj.getClass()) {
443                 return false;
444             }
445             ChoiceCaseNodeImpl other = (ChoiceCaseNodeImpl) obj;
446             if (qname == null) {
447                 if (other.qname != null) {
448                     return false;
449                 }
450             } else if (!qname.equals(other.qname)) {
451                 return false;
452             }
453             if (path == null) {
454                 if (other.path != null) {
455                     return false;
456                 }
457             } else if (!path.equals(other.path)) {
458                 return false;
459             }
460             return true;
461         }
462
463         @Override
464         public String toString() {
465             StringBuilder sb = new StringBuilder(ChoiceCaseNodeImpl.class.getSimpleName());
466             sb.append("[");
467             sb.append("qname=");
468             sb.append(qname);
469             sb.append("]");
470             return sb.toString();
471         }
472     }
473
474 }