Updated yangtools version to 0.5.6-SNAPSHOT.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / 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.yangtools.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 import java.util.TreeSet;
16
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingMember;
30 import org.opendaylight.yangtools.yang.parser.util.Comparators;
31 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
32 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
33
34 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
35         AugmentationTargetBuilder, GroupingMember {
36     private boolean isBuilt;
37     private final ChoiceNodeImpl instance;
38     // DataSchemaNode args
39     private boolean augmenting;
40     private boolean addedByUses;
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 Set<ChoiceCaseNode> cases;
47     private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
48     private String defaultCase;
49
50     public ChoiceBuilder(final String moduleName, final int line, final QName qname) {
51         super(moduleName, line, qname);
52         instance = new ChoiceNodeImpl(qname);
53         constraints = new ConstraintsBuilder(moduleName, line);
54     }
55
56     public ChoiceBuilder(ChoiceBuilder b, QName qname) {
57         super(b.getModuleName(), b.getLine(), qname);
58         parent = b.getParent();
59         instance = new ChoiceNodeImpl(qname);
60         constraints = new ConstraintsBuilder(b.getConstraints());
61         schemaPath = b.getPath();
62         description = b.getDescription();
63         reference = b.getReference();
64         status = b.getStatus();
65         unknownNodes = b.unknownNodes;
66         addedUnknownNodes.addAll(b.getUnknownNodes());
67         augmenting = b.isAugmenting();
68         addedByUses = b.isAddedByUses();
69         configuration = b.isConfiguration();
70         addedAugmentations.addAll(b.getAugmentations());
71         cases = b.cases;
72         addedCases.addAll(b.getCases());
73         defaultCase = b.getDefaultCase();
74     }
75
76     @Override
77     public ChoiceNode build() {
78         if (!isBuilt) {
79             instance.setPath(schemaPath);
80             instance.setDescription(description);
81             instance.setReference(reference);
82             instance.setStatus(status);
83             instance.setAugmenting(augmenting);
84             instance.setAddedByUses(addedByUses);
85             instance.setConfiguration(configuration);
86             instance.setConstraints(constraints.build());
87             instance.setDefaultCase(defaultCase);
88
89             // CASES
90             if (cases == null) {
91                 cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
92                 for (ChoiceCaseBuilder caseBuilder : addedCases) {
93                     cases.add(caseBuilder.build());
94                 }
95             }
96             instance.setCases(cases);
97
98             // AUGMENTATIONS
99             final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
100             for (AugmentationSchemaBuilder builder : addedAugmentations) {
101                 augmentations.add(builder.build());
102             }
103             instance.setAvailableAugmentations(augmentations);
104
105             // UNKNOWN NODES
106             if (unknownNodes == null) {
107                 unknownNodes = new ArrayList<UnknownSchemaNode>();
108                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
109                     unknownNodes.add(b.build());
110                 }
111                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
112             }
113             instance.setUnknownSchemaNodes(unknownNodes);
114
115             isBuilt = true;
116         }
117         return instance;
118     }
119
120     @Override
121     public void rebuild() {
122         isBuilt = false;
123         build();
124     }
125
126     public Set<ChoiceCaseBuilder> getCases() {
127         return addedCases;
128     }
129
130     /**
131      * Add case node to this choice.
132      *
133      * If node is not declared with 'case' keyword, create new case builder and
134      * make this node child of newly created case.
135      *
136      * @param caseNode
137      *            case node
138      */
139     public void addCase(DataSchemaNodeBuilder caseNode) {
140         QName caseQName = caseNode.getQName();
141         String caseName = caseQName.getLocalName();
142         for (ChoiceCaseBuilder addedCase : addedCases) {
143             if (addedCase.getQName().getLocalName().equals(caseName)) {
144                 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
145                         + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
146                         + "': case with same name already declared at line " + addedCase.getLine());
147             }
148         }
149
150         if (caseNode instanceof ChoiceCaseBuilder) {
151             addedCases.add((ChoiceCaseBuilder) caseNode);
152         } else {
153             ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
154                     caseQName);
155             if (caseNode.isAugmenting()) {
156                 // if node is added by augmentation, set case builder augmenting
157                 // as true and node augmenting as false
158                 caseBuilder.setAugmenting(true);
159                 caseNode.setAugmenting(false);
160             }
161             caseBuilder.setPath(caseNode.getPath());
162             SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseQName);
163             caseNode.setPath(newPath);
164             caseBuilder.addChildNode(caseNode);
165             addedCases.add(caseBuilder);
166         }
167     }
168
169     public void setCases(Set<ChoiceCaseNode> cases) {
170         this.cases = cases;
171     }
172
173     @Override
174     public boolean isAugmenting() {
175         return augmenting;
176     }
177
178     @Override
179     public void setAugmenting(boolean augmenting) {
180         this.augmenting = augmenting;
181     }
182
183     @Override
184     public boolean isAddedByUses() {
185         return addedByUses;
186     }
187
188     @Override
189     public void setAddedByUses(final boolean addedByUses) {
190         this.addedByUses = addedByUses;
191     }
192
193     public Boolean isConfiguration() {
194         return configuration;
195     }
196
197     @Override
198     public void setConfiguration(Boolean configuration) {
199         this.configuration = configuration;
200     }
201
202     @Override
203     public ConstraintsBuilder getConstraints() {
204         return constraints;
205     }
206
207     public Set<AugmentationSchemaBuilder> getAugmentations() {
208         return addedAugmentations;
209     }
210
211     @Override
212     public void addAugmentation(AugmentationSchemaBuilder augment) {
213         addedAugmentations.add(augment);
214     }
215
216     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
217         return addedUnknownNodes;
218     }
219
220     public String getDefaultCase() {
221         return defaultCase;
222     }
223
224     public void setDefaultCase(String defaultCase) {
225         this.defaultCase = defaultCase;
226     }
227
228     @Override
229     public int hashCode() {
230         final int prime = 31;
231         int result = 1;
232         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
233         return result;
234     }
235
236     @Override
237     public boolean equals(Object obj) {
238         if (this == obj) {
239             return true;
240         }
241         if (obj == null) {
242             return false;
243         }
244         if (getClass() != obj.getClass()) {
245             return false;
246         }
247         ChoiceBuilder other = (ChoiceBuilder) obj;
248         if (schemaPath == null) {
249             if (other.schemaPath != null) {
250                 return false;
251             }
252         } else if (!schemaPath.equals(other.schemaPath)) {
253             return false;
254         }
255         if (parent == null) {
256             if (other.parent != null) {
257                 return false;
258             }
259         } else if (!parent.equals(other.parent)) {
260             return false;
261         }
262         return true;
263     }
264
265     @Override
266     public String toString() {
267         return "choice " + qname.getLocalName();
268     }
269
270     public final class ChoiceNodeImpl implements ChoiceNode {
271         private final QName qname;
272         private SchemaPath path;
273         private String description;
274         private String reference;
275         private Status status = Status.CURRENT;
276         private boolean augmenting;
277         private boolean addedByUses;
278         private boolean configuration;
279         private ConstraintDefinition constraints;
280         private Set<ChoiceCaseNode> cases = Collections.emptySet();
281         private Set<AugmentationSchema> augmentations = Collections.emptySet();
282         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
283         private String defaultCase;
284
285         private ChoiceNodeImpl(QName qname) {
286             this.qname = qname;
287         }
288
289         @Override
290         public QName getQName() {
291             return qname;
292         }
293
294         @Override
295         public SchemaPath getPath() {
296             return path;
297         }
298
299         private void setPath(SchemaPath path) {
300             this.path = path;
301         }
302
303         @Override
304         public String getDescription() {
305             return description;
306         }
307
308         private void setDescription(String description) {
309             this.description = description;
310         }
311
312         @Override
313         public String getReference() {
314             return reference;
315         }
316
317         private void setReference(String reference) {
318             this.reference = reference;
319         }
320
321         @Override
322         public Status getStatus() {
323             return status;
324         }
325
326         private void setStatus(Status status) {
327             if (status != null) {
328                 this.status = status;
329             }
330         }
331
332         @Override
333         public boolean isAugmenting() {
334             return augmenting;
335         }
336
337         private void setAugmenting(boolean augmenting) {
338             this.augmenting = augmenting;
339         }
340
341         @Override
342         public boolean isAddedByUses() {
343             return addedByUses;
344         }
345
346         private void setAddedByUses(boolean addedByUses) {
347             this.addedByUses = addedByUses;
348         }
349
350         @Override
351         public boolean isConfiguration() {
352             return configuration;
353         }
354
355         private void setConfiguration(boolean configuration) {
356             this.configuration = configuration;
357         }
358
359         @Override
360         public ConstraintDefinition getConstraints() {
361             return constraints;
362         }
363
364         private void setConstraints(ConstraintDefinition constraints) {
365             this.constraints = constraints;
366         }
367
368         @Override
369         public Set<AugmentationSchema> getAvailableAugmentations() {
370             return augmentations;
371         }
372
373         private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
374             if (availableAugmentations != null) {
375                 this.augmentations = availableAugmentations;
376             }
377         }
378
379         @Override
380         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
381             return unknownNodes;
382         }
383
384         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
385             if (unknownSchemaNodes != null) {
386                 this.unknownNodes = unknownSchemaNodes;
387             }
388         }
389
390         @Override
391         public Set<ChoiceCaseNode> getCases() {
392             return cases;
393         }
394
395         @Override
396         public ChoiceCaseNode getCaseNodeByName(final QName name) {
397             if (name == null) {
398                 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
399             }
400             for (final ChoiceCaseNode caseNode : cases) {
401                 if (caseNode != null) {
402                     if (name.equals(caseNode.getQName())) {
403                         return caseNode;
404                     }
405                 }
406             }
407             return null;
408         }
409
410         @Override
411         public ChoiceCaseNode getCaseNodeByName(final String name) {
412             if (name == null) {
413                 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
414             }
415             for (final ChoiceCaseNode caseNode : cases) {
416                 if (caseNode != null && (caseNode.getQName() != null)) {
417                     if (name.equals(caseNode.getQName().getLocalName())) {
418                         return caseNode;
419                     }
420                 }
421             }
422             return null;
423         }
424
425         private void setCases(Set<ChoiceCaseNode> cases) {
426             if (cases != null) {
427                 this.cases = cases;
428             }
429         }
430
431         @Override
432         public String getDefaultCase() {
433             return defaultCase;
434         }
435
436         private void setDefaultCase(String defaultCase) {
437             this.defaultCase = defaultCase;
438         }
439
440         public ChoiceBuilder toBuilder() {
441             return ChoiceBuilder.this;
442         }
443
444         @Override
445         public int hashCode() {
446             final int prime = 31;
447             int result = 1;
448             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
449             result = prime * result + ((path == null) ? 0 : path.hashCode());
450             return result;
451         }
452
453         @Override
454         public boolean equals(Object obj) {
455             if (this == obj) {
456                 return true;
457             }
458             if (obj == null) {
459                 return false;
460             }
461             if (getClass() != obj.getClass()) {
462                 return false;
463             }
464             ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
465             if (qname == null) {
466                 if (other.qname != null) {
467                     return false;
468                 }
469             } else if (!qname.equals(other.qname)) {
470                 return false;
471             }
472             if (path == null) {
473                 if (other.path != null) {
474                     return false;
475                 }
476             } else if (!path.equals(other.path)) {
477                 return false;
478             }
479             return true;
480         }
481
482         @Override
483         public String toString() {
484             StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
485             sb.append("[");
486             sb.append("qname=" + qname);
487             sb.append("]");
488             return sb.toString();
489         }
490     }
491
492 }