Merge "Custom Node Type jaxb string to id"
[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(
106                     childNode.getQName(), childNode.getLine());
107             caseBuilder.addChildNode(childNode);
108             cases.add(caseBuilder);
109         } else {
110             cases.add((ChoiceCaseBuilder) childNode);
111         }
112     }
113
114     @Override
115     public QName getQName() {
116         return qname;
117     }
118
119     public SchemaPath getPath() {
120         return schemaPath;
121     }
122
123     @Override
124     public void setPath(final SchemaPath schemaPath) {
125         this.schemaPath = schemaPath;
126     }
127
128     public String getDescription() {
129         return description;
130     }
131
132     @Override
133     public void setDescription(final String description) {
134         this.description = description;
135     }
136
137     public String getReference() {
138         return reference;
139     }
140
141     @Override
142     public void setReference(String reference) {
143         this.reference = reference;
144     }
145
146     public Status getStatus() {
147         return status;
148     }
149
150     @Override
151     public void setStatus(Status status) {
152         if (status != null) {
153             this.status = status;
154         }
155     }
156
157     public boolean isAugmenting() {
158         return augmenting;
159     }
160
161     @Override
162     public void setAugmenting(boolean augmenting) {
163         this.augmenting = augmenting;
164     }
165
166     public boolean isConfiguration() {
167         return configuration;
168     }
169
170     @Override
171     public void setConfiguration(boolean configuration) {
172         this.configuration = configuration;
173     }
174
175     @Override
176     public ConstraintsBuilder getConstraints() {
177         return constraints;
178     }
179
180     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
181         return addedUnknownNodes;
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     @Override
194     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
195         addedUnknownNodes.add(unknownNode);
196     }
197
198     public String getDefaultCase() {
199         return defaultCase;
200     }
201
202     public void setDefaultCase(String defaultCase) {
203         this.defaultCase = defaultCase;
204     }
205
206
207     private final class ChoiceNodeImpl implements ChoiceNode {
208         private final QName qname;
209         private SchemaPath path;
210         private String description;
211         private String reference;
212         private Status status = Status.CURRENT;
213         private boolean augmenting;
214         private boolean configuration;
215         private ConstraintDefinition constraints;
216         private Set<ChoiceCaseNode> cases = Collections.emptySet();
217         private Set<AugmentationSchema> augmentations = Collections.emptySet();
218         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
219         private String defaultCase;
220
221         private ChoiceNodeImpl(QName qname) {
222             this.qname = qname;
223         }
224
225         @Override
226         public QName getQName() {
227             return qname;
228         }
229
230         @Override
231         public SchemaPath getPath() {
232             return path;
233         }
234
235         private void setPath(SchemaPath path) {
236             this.path = path;
237         }
238
239         @Override
240         public String getDescription() {
241             return description;
242         }
243
244         private void setDescription(String description) {
245             this.description = description;
246         }
247
248         @Override
249         public String getReference() {
250             return reference;
251         }
252
253         private void setReference(String reference) {
254             this.reference = reference;
255         }
256
257         @Override
258         public Status getStatus() {
259             return status;
260         }
261
262         private void setStatus(Status status) {
263             if (status != null) {
264                 this.status = status;
265             }
266         }
267
268         @Override
269         public boolean isAugmenting() {
270             return augmenting;
271         }
272
273         private void setAugmenting(boolean augmenting) {
274             this.augmenting = augmenting;
275         }
276
277         @Override
278         public boolean isConfiguration() {
279             return configuration;
280         }
281
282         private void setConfiguration(boolean configuration) {
283             this.configuration = configuration;
284         }
285
286         @Override
287         public ConstraintDefinition getConstraints() {
288             return constraints;
289         }
290
291         private void setConstraints(ConstraintDefinition constraints) {
292             this.constraints = constraints;
293         }
294
295         @Override
296         public Set<AugmentationSchema> getAvailableAugmentations() {
297             return augmentations;
298         }
299
300         private void setAvailableAugmentations(
301                 Set<AugmentationSchema> availableAugmentations) {
302             if (availableAugmentations != null) {
303                 this.augmentations = availableAugmentations;
304             }
305         }
306
307         @Override
308         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
309             return unknownNodes;
310         }
311
312         private void setUnknownSchemaNodes(
313                 List<UnknownSchemaNode> unknownSchemaNodes) {
314             if (unknownSchemaNodes != null) {
315                 this.unknownNodes = unknownSchemaNodes;
316             }
317         }
318
319         @Override
320         public Set<ChoiceCaseNode> getCases() {
321             return cases;
322         }
323
324         private void setCases(Set<ChoiceCaseNode> cases) {
325             if (cases != null) {
326                 this.cases = cases;
327             }
328         }
329
330         public String getDefaultCase() {
331             return defaultCase;
332         }
333
334         private void setDefaultCase(String defaultCase) {
335             this.defaultCase = defaultCase;
336         }
337
338         @Override
339         public int hashCode() {
340             final int prime = 31;
341             int result = 1;
342             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
343             result = prime * result + ((path == null) ? 0 : path.hashCode());
344             return result;
345         }
346
347         @Override
348         public boolean equals(Object obj) {
349             if (this == obj) {
350                 return true;
351             }
352             if (obj == null) {
353                 return false;
354             }
355             if (getClass() != obj.getClass()) {
356                 return false;
357             }
358             ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
359             if (qname == null) {
360                 if (other.qname != null) {
361                     return false;
362                 }
363             } else if (!qname.equals(other.qname)) {
364                 return false;
365             }
366             if (path == null) {
367                 if (other.path != null) {
368                     return false;
369                 }
370             } else if (!path.equals(other.path)) {
371                 return false;
372             }
373             return true;
374         }
375
376         @Override
377         public String toString() {
378             StringBuilder sb = new StringBuilder(
379                     ChoiceNodeImpl.class.getSimpleName());
380             sb.append("[");
381             sb.append("qname=" + qname);
382             sb.append("]");
383             return sb.toString();
384         }
385     }
386
387 }