Merge "Bug 258 fix"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ChoiceBuilder.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.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15 import java.util.TreeSet;
16
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.YangNode;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.util.Comparators;
31 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
32 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
33
34 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
35         AugmentationTargetBuilder {
36     private boolean isBuilt;
37     private final ChoiceNodeImpl instance;
38     private YangNode parent;
39     // DataSchemaNode args
40     private boolean augmenting;
41     private boolean addedByUses;
42     private Boolean configuration;
43     private final ConstraintsBuilder constraints;
44     // AugmentationTarget args
45     private final List<AugmentationSchema> augmentations = new ArrayList<>();
46     private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
47     // ChoiceNode args
48     private Set<ChoiceCaseNode> cases = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
49     private final Set<ChoiceCaseBuilder> caseBuilders = new HashSet<>();
50     private String defaultCase;
51
52     public ChoiceBuilder(final String moduleName, final int line, final QName qname) {
53         super(moduleName, line, qname);
54         instance = new ChoiceNodeImpl(qname);
55         constraints = new ConstraintsBuilder(moduleName, line);
56     }
57
58     @Override
59     public ChoiceNode build(YangNode parent) {
60         if (!isBuilt) {
61             this.parent = parent;
62             instance.setParent(parent);
63             instance.setPath(schemaPath);
64             instance.setDescription(description);
65             instance.setReference(reference);
66             instance.setStatus(status);
67             instance.setAugmenting(augmenting);
68             instance.setAddedByUses(addedByUses);
69             instance.setConfiguration(configuration);
70             instance.setConstraints(constraints.build());
71             instance.setDefaultCase(defaultCase);
72
73             // CASES
74             for (ChoiceCaseBuilder caseBuilder : caseBuilders) {
75                 cases.add(caseBuilder.build(instance));
76             }
77             instance.setCases(cases);
78
79             // AUGMENTATIONS
80             for (AugmentationSchemaBuilder builder : augmentationBuilders) {
81                 augmentations.add(builder.build(instance));
82             }
83             instance.setAvailableAugmentations(new HashSet<>(augmentations));
84
85             // UNKNOWN NODES
86             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
87                 unknownNodes.add(b.build(instance));
88             }
89             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
90             instance.setUnknownSchemaNodes(unknownNodes);
91
92             isBuilt = true;
93         }
94         return instance;
95     }
96
97     @Override
98     public void rebuild() {
99         isBuilt = false;
100         build(parent);
101     }
102
103     @Override
104     public void setQName(QName qname) {
105         this.qname = qname;
106         instance.setQName(qname);
107     }
108
109     public Set<ChoiceCaseBuilder> getCases() {
110         return caseBuilders;
111     }
112
113     /**
114      * Get case by name.
115      *
116      * @param caseName
117      *            name of case to search
118      * @return case with given name if present, null otherwise
119      */
120     public ChoiceCaseBuilder getCaseNodeByName(String caseName) {
121         for (ChoiceCaseBuilder addedCase : caseBuilders) {
122             if (addedCase.getQName().getLocalName().equals(caseName)) {
123                 return addedCase;
124             }
125         }
126         return null;
127     }
128
129     /**
130      * Add case node to this choice.
131      *
132      * If node is not declared with 'case' keyword, create new case builder and
133      * make this node child of newly created case.
134      *
135      * @param caseNode
136      *            case node
137      */
138     public void addCase(DataSchemaNodeBuilder caseNode) {
139         QName caseQName = caseNode.getQName();
140         String caseName = caseQName.getLocalName();
141
142         for (ChoiceCaseBuilder addedCase : caseBuilders) {
143             if (addedCase.getQName().getLocalName().equals(caseName)) {
144                 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
145                         + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
146                         + "': case with same name already declared at line " + addedCase.getLine());
147             }
148         }
149
150         if (caseNode instanceof ChoiceCaseBuilder) {
151             caseBuilders.add((ChoiceCaseBuilder) caseNode);
152         } else {
153             ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
154                     caseQName);
155             if (caseNode.isAugmenting()) {
156                 // if node is added by augmentation, set case builder augmenting
157                 // as true and node augmenting as false
158                 caseBuilder.setAugmenting(true);
159                 caseNode.setAugmenting(false);
160             }
161             caseBuilder.setPath(caseNode.getPath());
162             SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseQName);
163             caseNode.setPath(newPath);
164             caseBuilder.addChildNode(caseNode);
165             caseBuilders.add(caseBuilder);
166         }
167     }
168
169     public void setCases(Set<ChoiceCaseNode> cases) {
170         this.cases = cases;
171     }
172
173     @Override
174     public boolean isAugmenting() {
175         return augmenting;
176     }
177
178     @Override
179     public void setAugmenting(boolean augmenting) {
180         this.augmenting = augmenting;
181     }
182
183     @Override
184     public boolean isAddedByUses() {
185         return addedByUses;
186     }
187
188     @Override
189     public void setAddedByUses(final boolean addedByUses) {
190         this.addedByUses = addedByUses;
191     }
192
193     public Boolean isConfiguration() {
194         return configuration;
195     }
196
197     @Override
198     public void setConfiguration(Boolean configuration) {
199         this.configuration = configuration;
200     }
201
202     @Override
203     public ConstraintsBuilder getConstraints() {
204         return constraints;
205     }
206
207     @Override
208     public void addAugmentation(AugmentationSchemaBuilder augment) {
209         augmentationBuilders.add(augment);
210     }
211
212     public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
213         return augmentationBuilders;
214     }
215
216     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
217         return addedUnknownNodes;
218     }
219
220     public String getDefaultCase() {
221         return defaultCase;
222     }
223
224     public void setDefaultCase(String defaultCase) {
225         this.defaultCase = defaultCase;
226     }
227
228     @Override
229     public int hashCode() {
230         final int prime = 31;
231         int result = 1;
232         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
233         return result;
234     }
235
236     @Override
237     public boolean equals(Object obj) {
238         if (this == obj) {
239             return true;
240         }
241         if (obj == null) {
242             return false;
243         }
244         if (getClass() != obj.getClass()) {
245             return false;
246         }
247         ChoiceBuilder other = (ChoiceBuilder) obj;
248         if (schemaPath == null) {
249             if (other.schemaPath != null) {
250                 return false;
251             }
252         } else if (!schemaPath.equals(other.schemaPath)) {
253             return false;
254         }
255         if (parentBuilder == null) {
256             if (other.parentBuilder != null) {
257                 return false;
258             }
259         } else if (!parentBuilder.equals(other.parentBuilder)) {
260             return false;
261         }
262         return true;
263     }
264
265     @Override
266     public String toString() {
267         return "choice " + qname.getLocalName();
268     }
269
270     public final class ChoiceNodeImpl implements ChoiceNode {
271         private QName qname;
272         private SchemaPath path;
273         private YangNode parent;
274         private String description;
275         private String reference;
276         private Status status = Status.CURRENT;
277         private boolean augmenting;
278         private boolean addedByUses;
279         private boolean configuration;
280         private ConstraintDefinition constraints;
281         private Set<ChoiceCaseNode> cases = Collections.emptySet();
282         private Set<AugmentationSchema> augmentations = Collections.emptySet();
283         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
284         private String defaultCase;
285
286         private ChoiceNodeImpl(QName qname) {
287             this.qname = qname;
288         }
289
290         @Override
291         public QName getQName() {
292             return qname;
293         }
294
295         private void setQName(QName qname) {
296             this.qname = qname;
297         }
298
299         @Override
300         public SchemaPath getPath() {
301             return path;
302         }
303
304         private void setPath(SchemaPath path) {
305             this.path = path;
306         }
307
308         @Override
309         public YangNode getParent() {
310             return parent;
311         }
312
313         private void setParent(YangNode parent) {
314             this.parent = parent;
315         }
316
317         @Override
318         public String getDescription() {
319             return description;
320         }
321
322         private void setDescription(String description) {
323             this.description = description;
324         }
325
326         @Override
327         public String getReference() {
328             return reference;
329         }
330
331         private void setReference(String reference) {
332             this.reference = reference;
333         }
334
335         @Override
336         public Status getStatus() {
337             return status;
338         }
339
340         private void setStatus(Status status) {
341             if (status != null) {
342                 this.status = status;
343             }
344         }
345
346         @Override
347         public boolean isAugmenting() {
348             return augmenting;
349         }
350
351         private void setAugmenting(boolean augmenting) {
352             this.augmenting = augmenting;
353         }
354
355         @Override
356         public boolean isAddedByUses() {
357             return addedByUses;
358         }
359
360         private void setAddedByUses(boolean addedByUses) {
361             this.addedByUses = addedByUses;
362         }
363
364         @Override
365         public boolean isConfiguration() {
366             return configuration;
367         }
368
369         private void setConfiguration(boolean configuration) {
370             this.configuration = configuration;
371         }
372
373         @Override
374         public ConstraintDefinition getConstraints() {
375             return constraints;
376         }
377
378         private void setConstraints(ConstraintDefinition constraints) {
379             this.constraints = constraints;
380         }
381
382         @Override
383         public Set<AugmentationSchema> getAvailableAugmentations() {
384             return augmentations;
385         }
386
387         private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
388             if (availableAugmentations != null) {
389                 this.augmentations = availableAugmentations;
390             }
391         }
392
393         @Override
394         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
395             return unknownNodes;
396         }
397
398         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
399             if (unknownSchemaNodes != null) {
400                 this.unknownNodes = unknownSchemaNodes;
401             }
402         }
403
404         @Override
405         public Set<ChoiceCaseNode> getCases() {
406             return cases;
407         }
408
409         @Override
410         public ChoiceCaseNode getCaseNodeByName(final QName name) {
411             if (name == null) {
412                 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
413             }
414             for (final ChoiceCaseNode caseNode : cases) {
415                 if (caseNode != null && name.equals(caseNode.getQName())) {
416                     return caseNode;
417                 }
418             }
419             return null;
420         }
421
422         @Override
423         public ChoiceCaseNode getCaseNodeByName(final String name) {
424             if (name == null) {
425                 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
426             }
427             for (final ChoiceCaseNode caseNode : cases) {
428                 if (caseNode != null && (caseNode.getQName() != null)
429                         && name.equals(caseNode.getQName().getLocalName())) {
430                     return caseNode;
431                 }
432             }
433             return null;
434         }
435
436         private void setCases(Set<ChoiceCaseNode> cases) {
437             if (cases != null) {
438                 this.cases = cases;
439             }
440         }
441
442         @Override
443         public String getDefaultCase() {
444             return defaultCase;
445         }
446
447         private void setDefaultCase(String defaultCase) {
448             this.defaultCase = defaultCase;
449         }
450
451         public ChoiceBuilder toBuilder() {
452             return ChoiceBuilder.this;
453         }
454
455         @Override
456         public int hashCode() {
457             final int prime = 31;
458             int result = 1;
459             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
460             result = prime * result + ((path == null) ? 0 : path.hashCode());
461             return result;
462         }
463
464         @Override
465         public boolean equals(Object obj) {
466             if (this == obj) {
467                 return true;
468             }
469             if (obj == null) {
470                 return false;
471             }
472             if (getClass() != obj.getClass()) {
473                 return false;
474             }
475             ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
476             if (qname == null) {
477                 if (other.qname != null) {
478                     return false;
479                 }
480             } else if (!qname.equals(other.qname)) {
481                 return false;
482             }
483             if (path == null) {
484                 if (other.path != null) {
485                     return false;
486                 }
487             } else if (!path.equals(other.path)) {
488                 return false;
489             }
490             return true;
491         }
492
493         @Override
494         public String toString() {
495             StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
496             sb.append("[");
497             sb.append("qname=" + qname);
498             sb.append("]");
499             return sb.toString();
500         }
501     }
502
503 }