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