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