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