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 / ChoiceCaseBuilder.java
1 package org.opendaylight.yangtools.yang.parser.builder.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.TreeMap;
10
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
13 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.UsesNode;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.util.Comparators;
29 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
30
31 public final class ChoiceCaseBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
32         AugmentationTargetBuilder {
33     private boolean isBuilt;
34     private final ChoiceCaseNodeImpl instance;
35     // SchemaNode args
36     private SchemaPath schemaPath;
37     private String description;
38     private String reference;
39     private Status status = Status.CURRENT;
40     // DataSchemaNode args
41     private boolean augmenting;
42     private final ConstraintsBuilder constraints;
43     // DataNodeContainer args
44     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
45     // AugmentationTarget args
46     private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
47
48     public ChoiceCaseBuilder(final String moduleName, final int line, final QName qname) {
49         super(moduleName, line, qname);
50         instance = new ChoiceCaseNodeImpl(qname);
51         constraints = new ConstraintsBuilder(moduleName, line);
52     }
53
54     @Override
55     public ChoiceCaseNode build() {
56         if (!isBuilt) {
57             instance.setConstraints(constraints.build());
58             instance.setPath(schemaPath);
59             instance.setDescription(description);
60             instance.setReference(reference);
61             instance.setStatus(status);
62             instance.setAugmenting(augmenting);
63
64             // CHILD NODES
65             final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
66             for (DataSchemaNodeBuilder node : addedChildNodes) {
67                 childs.put(node.getQName(), node.build());
68             }
69             instance.setChildNodes(childs);
70
71             // USES
72             final Set<UsesNode> uses = new HashSet<UsesNode>();
73             for (UsesNodeBuilder builder : addedUsesNodes) {
74                 uses.add(builder.build());
75             }
76             instance.setUses(uses);
77
78             // UNKNOWN NODES
79             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
80             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
81                 unknownNodes.add(b.build());
82             }
83             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
84             instance.setUnknownSchemaNodes(unknownNodes);
85
86             // AUGMENTATIONS
87             final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
88             for (AugmentationSchemaBuilder builder : addedAugmentations) {
89                 augmentations.add(builder.build());
90             }
91             instance.setAvailableAugmentations(augmentations);
92
93             isBuilt = true;
94         }
95
96         return instance;
97     }
98
99     @Override
100     public void rebuild() {
101         isBuilt = false;
102         build();
103     }
104
105     @Override
106     public void setQName(QName qname) {
107         this.qname = qname;
108     }
109
110     @Override
111     public SchemaPath getPath() {
112         return schemaPath;
113     }
114
115     @Override
116     public void setPath(final SchemaPath schemaPath) {
117         this.schemaPath = schemaPath;
118     }
119
120     public String getDescription() {
121         return description;
122     }
123
124     @Override
125     public void setDescription(final String description) {
126         this.description = description;
127     }
128
129     public String getReference() {
130         return reference;
131     }
132
133     @Override
134     public void setReference(String reference) {
135         this.reference = reference;
136     }
137
138     public Status getStatus() {
139         return status;
140     }
141
142     @Override
143     public void setStatus(Status status) {
144         if (status != null) {
145             this.status = status;
146         }
147     }
148
149     public boolean isAugmenting() {
150         return augmenting;
151     }
152
153     @Override
154     public void setAugmenting(boolean augmenting) {
155         this.augmenting = augmenting;
156     }
157
158     public Set<UsesNodeBuilder> getUsesNodes() {
159         return addedUsesNodes;
160     }
161
162     @Override
163     public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
164         addedUsesNodes.add(usesNodeBuilder);
165     }
166
167     @Override
168     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
169         return Collections.emptySet();
170     }
171
172     @Override
173     public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
174         throw new YangParseException(moduleName, line, "Can not add type definition to choice case.");
175     }
176
177     @Override
178     public Boolean isConfiguration() {
179         return false;
180     }
181
182     @Override
183     public void setConfiguration(final Boolean configuration) {
184         throw new YangParseException(moduleName, line, "Can not add config statement to choice case.");
185     }
186
187     @Override
188     public ConstraintsBuilder getConstraints() {
189         return constraints;
190     }
191
192     @Override
193     public void addAugmentation(AugmentationSchemaBuilder augment) {
194         addedAugmentations.add(augment);
195     }
196
197     @Override
198     public int hashCode() {
199         final int prime = 31;
200         int result = 1;
201         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
202         return result;
203     }
204
205     @Override
206     public boolean equals(Object obj) {
207         if (this == obj) {
208             return true;
209         }
210         if (obj == null) {
211             return false;
212         }
213         if (getClass() != obj.getClass()) {
214             return false;
215         }
216         ChoiceCaseBuilder other = (ChoiceCaseBuilder) obj;
217         if (schemaPath == null) {
218             if (other.schemaPath != null) {
219                 return false;
220             }
221         } else if (!schemaPath.equals(other.schemaPath)) {
222             return false;
223         }
224         if (parent == null) {
225             if (other.parent != null) {
226                 return false;
227             }
228         } else if (!parent.equals(other.parent)) {
229             return false;
230         }
231         return true;
232     }
233
234     @Override
235     public String toString() {
236         return "case " + getQName().getLocalName();
237     }
238
239     public final class ChoiceCaseNodeImpl implements ChoiceCaseNode {
240         private final QName qname;
241         private SchemaPath path;
242         private String description;
243         private String reference;
244         private Status status = Status.CURRENT;
245         private boolean augmenting;
246         private ConstraintDefinition constraints;
247         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
248         private Set<AugmentationSchema> augmentations = Collections.emptySet();
249         private Set<UsesNode> uses = Collections.emptySet();
250         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
251
252         private ChoiceCaseNodeImpl(QName qname) {
253             this.qname = qname;
254         }
255
256         @Override
257         public QName getQName() {
258             return qname;
259         }
260
261         @Override
262         public SchemaPath getPath() {
263             return path;
264         }
265
266         private void setPath(SchemaPath path) {
267             this.path = path;
268         }
269
270         @Override
271         public String getDescription() {
272             return description;
273         }
274
275         private void setDescription(String description) {
276             this.description = description;
277         }
278
279         @Override
280         public String getReference() {
281             return reference;
282         }
283
284         private void setReference(String reference) {
285             this.reference = reference;
286         }
287
288         @Override
289         public Status getStatus() {
290             return status;
291         }
292
293         private void setStatus(Status status) {
294             if (status != null) {
295                 this.status = status;
296             }
297         }
298
299         @Override
300         public boolean isConfiguration() {
301             return false;
302         }
303
304         @Override
305         public ConstraintDefinition getConstraints() {
306             return constraints;
307         }
308
309         private void setConstraints(ConstraintDefinition constraints) {
310             this.constraints = constraints;
311         }
312
313         @Override
314         public boolean isAugmenting() {
315             return augmenting;
316         }
317
318         private void setAugmenting(boolean augmenting) {
319             this.augmenting = augmenting;
320         }
321
322         @Override
323         public boolean isAddedByUses() {
324             return false;
325         }
326
327         @Override
328         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
329             return unknownNodes;
330         }
331
332         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
333             if (unknownNodes != null) {
334                 this.unknownNodes = unknownNodes;
335             }
336         }
337
338         /**
339          * Always returns an empty set, because case node can not contains type
340          * definitions.
341          */
342         @Override
343         public Set<TypeDefinition<?>> getTypeDefinitions() {
344             return Collections.emptySet();
345         }
346
347         @Override
348         public Set<DataSchemaNode> getChildNodes() {
349             return new HashSet<DataSchemaNode>(childNodes.values());
350         }
351
352         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
353             if (childNodes != null) {
354                 this.childNodes = childNodes;
355             }
356         }
357
358         @Override
359         public Set<GroupingDefinition> getGroupings() {
360             return Collections.emptySet();
361         }
362
363         @Override
364         public DataSchemaNode getDataChildByName(QName name) {
365             return childNodes.get(name);
366         }
367
368         @Override
369         public DataSchemaNode getDataChildByName(String name) {
370             DataSchemaNode result = null;
371             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
372                 if (entry.getKey().getLocalName().equals(name)) {
373                     result = entry.getValue();
374                     break;
375                 }
376             }
377             return result;
378         }
379
380         @Override
381         public Set<UsesNode> getUses() {
382             return uses;
383         }
384
385         private void setUses(Set<UsesNode> uses) {
386             if (uses != null) {
387                 this.uses = uses;
388             }
389         }
390
391         @Override
392         public Set<AugmentationSchema> getAvailableAugmentations() {
393             return augmentations;
394         }
395
396         private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
397             if (augmentations != null) {
398                 this.augmentations = augmentations;
399             }
400         }
401
402         public ChoiceCaseBuilder toBuilder() {
403             return ChoiceCaseBuilder.this;
404         }
405
406         @Override
407         public int hashCode() {
408             final int prime = 31;
409             int result = 1;
410             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
411             result = prime * result + ((path == null) ? 0 : path.hashCode());
412             return result;
413         }
414
415         @Override
416         public boolean equals(Object obj) {
417             if (this == obj) {
418                 return true;
419             }
420             if (obj == null) {
421                 return false;
422             }
423             if (getClass() != obj.getClass()) {
424                 return false;
425             }
426             ChoiceCaseNodeImpl other = (ChoiceCaseNodeImpl) obj;
427             if (qname == null) {
428                 if (other.qname != null) {
429                     return false;
430                 }
431             } else if (!qname.equals(other.qname)) {
432                 return false;
433             }
434             if (path == null) {
435                 if (other.path != null) {
436                     return false;
437                 }
438             } else if (!path.equals(other.path)) {
439                 return false;
440             }
441             return true;
442         }
443
444         @Override
445         public String toString() {
446             StringBuilder sb = new StringBuilder(ChoiceCaseNodeImpl.class.getSimpleName());
447             sb.append("[");
448             sb.append("qname=" + qname);
449             sb.append("]");
450             return sb.toString();
451         }
452     }
453
454 }