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