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