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