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