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