Merge "Fixed validation bug of YANG import statement"
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / 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.model.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
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.controller.yang.model.api.ChoiceNode;
20 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
31
32 public class ChoiceBuilder implements DataSchemaNodeBuilder, ChildNodeBuilder,
33         AugmentationTargetBuilder {
34     private final QName qname;
35     private SchemaPath schemaPath;
36     private final ConstraintsBuilder constraints;
37     private final ChoiceNodeImpl instance;
38     private String description;
39     private String reference;
40     private Status status = Status.CURRENT;
41     private boolean augmenting;
42     private boolean configuration;
43     private String defaultCase;
44
45     private final Set<ChoiceCaseBuilder> cases = new HashSet<ChoiceCaseBuilder>();
46     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
47     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
48     private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
49     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
50
51     public ChoiceBuilder(QName qname) {
52         this.qname = qname;
53         instance = new ChoiceNodeImpl(qname);
54         constraints = new ConstraintsBuilder();
55     }
56
57     @Override
58     public ChoiceNode build() {
59         instance.setPath(schemaPath);
60         instance.setDescription(description);
61         instance.setReference(reference);
62         instance.setStatus(status);
63         instance.setAugmenting(augmenting);
64         instance.setConfiguration(configuration);
65         instance.setDefaultCase(defaultCase);
66
67         // CASES
68         final Set<ChoiceCaseNode> choiceCases = new HashSet<ChoiceCaseNode>();
69         for (ChoiceCaseBuilder caseBuilder : cases) {
70             choiceCases.add(caseBuilder.build());
71         }
72         instance.setCases(choiceCases);
73
74         // AUGMENTATIONS
75         final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
76         for (AugmentationSchemaBuilder builder : addedAugmentations) {
77             augmentations.add(builder.build());
78         }
79         instance.setAvailableAugmentations(augmentations);
80
81         // UNKNOWN NODES
82         final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
83         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
84             unknownNodes.add(b.build());
85         }
86         instance.setUnknownSchemaNodes(unknownNodes);
87
88         instance.setConstraints(constraints.build());
89
90         return instance;
91     }
92
93     public Set<ChoiceCaseBuilder> getCases() {
94         return cases;
95     }
96
97     @Override
98     public void addChildNode(DataSchemaNodeBuilder childNode) {
99         if (!(childNode instanceof ChoiceCaseBuilder)) {
100             ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(childNode.getQName());
101             caseBuilder.addChildNode(childNode);
102             cases.add(caseBuilder);
103         } else {
104             cases.add((ChoiceCaseBuilder) childNode);
105         }
106     }
107
108     @Override
109     public QName getQName() {
110         return qname;
111     }
112
113     /**
114      * Choice can not contains grouping statements, so this method always
115      * returns an empty set.
116      *
117      * @return
118      */
119     public Set<GroupingBuilder> getGroupings() {
120         return Collections.emptySet();
121     }
122
123     @Override
124     public void addGrouping(GroupingBuilder groupingBuilder) {
125         throw new IllegalStateException(
126                 "Can not add grouping to 'choice' node.");
127     }
128
129     public Set<TypeDefinitionBuilder> getTypedefs() {
130         return addedTypedefs;
131     }
132
133     @Override
134     public void addTypedef(final TypeDefinitionBuilder type) {
135         addedTypedefs.add(type);
136     }
137
138     public SchemaPath getPath() {
139         return schemaPath;
140     }
141
142     @Override
143     public void setPath(final SchemaPath schemaPath) {
144         this.schemaPath = schemaPath;
145     }
146
147     public String getDescription() {
148         return description;
149     }
150
151     @Override
152     public void setDescription(final String description) {
153         this.description = description;
154     }
155
156     public String getReference() {
157         return reference;
158     }
159
160     @Override
161     public void setReference(String reference) {
162         this.reference = reference;
163     }
164
165     public Status getStatus() {
166         return status;
167     }
168
169     @Override
170     public void setStatus(Status status) {
171         if (status != null) {
172             this.status = status;
173         }
174     }
175
176     public boolean isAugmenting() {
177         return augmenting;
178     }
179
180     @Override
181     public void setAugmenting(boolean augmenting) {
182         this.augmenting = augmenting;
183     }
184
185     public boolean isConfiguration() {
186         return configuration;
187     }
188
189     @Override
190     public void setConfiguration(boolean configuration) {
191         this.configuration = configuration;
192     }
193
194     @Override
195     public ConstraintsBuilder getConstraints() {
196         return constraints;
197     }
198
199     public Set<UsesNodeBuilder> getUsesNodes() {
200         return addedUsesNodes;
201     }
202
203     @Override
204     public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
205         addedUsesNodes.add(usesNodeBuilder);
206     }
207
208     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
209         return addedUnknownNodes;
210     }
211
212     public Set<AugmentationSchemaBuilder> getAugmentations() {
213         return addedAugmentations;
214     }
215
216     @Override
217     public void addAugmentation(AugmentationSchemaBuilder augment) {
218         addedAugmentations.add(augment);
219     }
220
221     @Override
222     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
223         addedUnknownNodes.add(unknownNode);
224     }
225
226     public String getDefaultCase() {
227         return defaultCase;
228     }
229
230     public void setDefaultCase(String defaultCase) {
231         this.defaultCase = defaultCase;
232     }
233
234     private static class ChoiceNodeImpl implements ChoiceNode {
235         private final QName qname;
236         private SchemaPath path;
237         private String description;
238         private String reference;
239         private Status status = Status.CURRENT;
240         private boolean augmenting;
241         private boolean configuration;
242         private ConstraintDefinition constraints;
243         private Set<ChoiceCaseNode> cases = Collections.emptySet();
244         private Set<AugmentationSchema> augmentations = Collections.emptySet();
245         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
246         private String defaultCase;
247
248         private ChoiceNodeImpl(QName qname) {
249             this.qname = qname;
250         }
251
252         @Override
253         public QName getQName() {
254             return qname;
255         }
256
257         @Override
258         public SchemaPath getPath() {
259             return path;
260         }
261
262         private void setPath(SchemaPath path) {
263             this.path = path;
264         }
265
266         @Override
267         public String getDescription() {
268             return description;
269         }
270
271         private void setDescription(String description) {
272             this.description = description;
273         }
274
275         @Override
276         public String getReference() {
277             return reference;
278         }
279
280         private void setReference(String reference) {
281             this.reference = reference;
282         }
283
284         @Override
285         public Status getStatus() {
286             return status;
287         }
288
289         private void setStatus(Status status) {
290             if (status != null) {
291                 this.status = status;
292             }
293         }
294
295         @Override
296         public boolean isAugmenting() {
297             return augmenting;
298         }
299
300         private void setAugmenting(boolean augmenting) {
301             this.augmenting = augmenting;
302         }
303
304         @Override
305         public boolean isConfiguration() {
306             return configuration;
307         }
308
309         private void setConfiguration(boolean configuration) {
310             this.configuration = configuration;
311         }
312
313         @Override
314         public ConstraintDefinition getConstraints() {
315             return constraints;
316         }
317
318         private void setConstraints(ConstraintDefinition constraints) {
319             this.constraints = constraints;
320         }
321
322         @Override
323         public Set<AugmentationSchema> getAvailableAugmentations() {
324             return augmentations;
325         }
326
327         private void setAvailableAugmentations(
328                 Set<AugmentationSchema> availableAugmentations) {
329             if (availableAugmentations != null) {
330                 this.augmentations = availableAugmentations;
331             }
332         }
333
334         @Override
335         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
336             return unknownNodes;
337         }
338
339         private void setUnknownSchemaNodes(
340                 List<UnknownSchemaNode> unknownSchemaNodes) {
341             if (unknownSchemaNodes != null) {
342                 this.unknownNodes = unknownSchemaNodes;
343             }
344         }
345
346         @Override
347         public Set<ChoiceCaseNode> getCases() {
348             return cases;
349         }
350
351         private void setCases(Set<ChoiceCaseNode> cases) {
352             if (cases != null) {
353                 this.cases = cases;
354             }
355         }
356
357         public String getDefaultCase() {
358             return defaultCase;
359         }
360
361         private void setDefaultCase(String defaultCase) {
362             this.defaultCase = defaultCase;
363         }
364
365         @Override
366         public int hashCode() {
367             final int prime = 31;
368             int result = 1;
369             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
370             result = prime * result + ((path == null) ? 0 : path.hashCode());
371             return result;
372         }
373
374         @Override
375         public boolean equals(Object obj) {
376             if (this == obj) {
377                 return true;
378             }
379             if (obj == null) {
380                 return false;
381             }
382             if (getClass() != obj.getClass()) {
383                 return false;
384             }
385             ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
386             if (qname == null) {
387                 if (other.qname != null) {
388                     return false;
389                 }
390             } else if (!qname.equals(other.qname)) {
391                 return false;
392             }
393             if (path == null) {
394                 if (other.path != null) {
395                     return false;
396                 }
397             } else if (!path.equals(other.path)) {
398                 return false;
399             }
400             return true;
401         }
402
403         @Override
404         public String toString() {
405             StringBuilder sb = new StringBuilder(
406                     ChoiceNodeImpl.class.getSimpleName());
407             sb.append("[");
408             sb.append("qname=" + qname);
409             sb.append("]");
410             return sb.toString();
411         }
412     }
413
414 }