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