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 / ListSchemaNodeBuilder.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.Map;
15 import java.util.Set;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
18
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.UsesNode;
30 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingMember;
36 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
38 import org.opendaylight.yangtools.yang.parser.util.Comparators;
39 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
40
41 public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
42         AugmentationTargetBuilder, GroupingMember {
43     private boolean isBuilt;
44     private final ListSchemaNodeImpl instance;
45     // SchemaNode args
46     private SchemaPath schemaPath;
47     private String description;
48     private String reference;
49     private Status status = Status.CURRENT;
50     // DataSchemaNode args
51     private boolean augmenting;
52     private boolean addedByUses;
53     private Boolean configuration;
54     private final ConstraintsBuilder constraints;
55     // DataNodeContainer args
56     private Set<TypeDefinition<?>> typedefs;
57     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
58     private Set<UsesNode> usesNodes;
59     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
60     // AugmentationTarget args
61     private Set<AugmentationSchema> augmentations;
62     private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
63     // ListSchemaNode args
64     private List<QName> keyDefinition = Collections.emptyList();
65     private boolean userOrdered;
66
67     public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
68         super(moduleName, line, qname);
69         this.schemaPath = schemaPath;
70         instance = new ListSchemaNodeImpl(qname);
71         constraints = new ConstraintsBuilder(moduleName, line);
72     }
73
74     @Override
75     public ListSchemaNode build() {
76         if (!isBuilt) {
77             instance.setKeyDefinition(keyDefinition);
78             instance.setPath(schemaPath);
79             instance.setDescription(description);
80             instance.setReference(reference);
81             instance.setStatus(status);
82             instance.setAugmenting(augmenting);
83             instance.setAddedByUses(addedByUses);
84             instance.setConfiguration(configuration);
85             instance.setUserOrdered(userOrdered);
86
87             // CHILD NODES
88             final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
89             if (childNodes == null || childNodes.isEmpty()) {
90                 for (DataSchemaNodeBuilder node : addedChildNodes) {
91                     childs.put(node.getQName(), node.build());
92                 }
93             } else {
94                 for (DataSchemaNode node : childNodes) {
95                     childs.put(node.getQName(), node);
96                 }
97             }
98             instance.setChildNodes(childs);
99
100             // TYPEDEFS
101             if (typedefs == null) {
102                 typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
103                 for (TypeDefinitionBuilder entry : addedTypedefs) {
104                     typedefs.add(entry.build());
105                 }
106             }
107             instance.setTypeDefinitions(typedefs);
108
109             // USES
110             if (usesNodes == null) {
111                 usesNodes = new HashSet<UsesNode>();
112                 for (UsesNodeBuilder builder : addedUsesNodes) {
113                     usesNodes.add(builder.build());
114                 }
115             }
116             instance.setUses(usesNodes);
117
118             // GROUPINGS
119             if (groupings == null) {
120                 groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
121                 for (GroupingBuilder builder : addedGroupings) {
122                     groupings.add(builder.build());
123                 }
124             }
125             instance.setGroupings(groupings);
126
127             // AUGMENTATIONS
128             if (augmentations == null) {
129                 augmentations = new HashSet<AugmentationSchema>();
130                 for (AugmentationSchemaBuilder builder : addedAugmentations) {
131                     augmentations.add(builder.build());
132                 }
133             }
134             instance.setAvailableAugmentations(augmentations);
135
136             // UNKNOWN NODES
137             if (unknownNodes == null) {
138                 unknownNodes = new ArrayList<UnknownSchemaNode>();
139                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
140                     unknownNodes.add(b.build());
141                 }
142                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
143             }
144             instance.setUnknownSchemaNodes(unknownNodes);
145
146             instance.setConstraints(constraints.build());
147             instance.setAvailableAugmentations(augmentations);
148
149             isBuilt = true;
150         }
151         return instance;
152     }
153
154     @Override
155     public void rebuild() {
156         isBuilt = false;
157         build();
158     }
159
160     @Override
161     public void setQName(QName qname) {
162         this.qname = qname;
163     }
164
165     @Override
166     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
167         return addedTypedefs;
168     }
169
170     @Override
171     public void addTypedef(final TypeDefinitionBuilder type) {
172         String typeName = type.getQName().getLocalName();
173         for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
174             throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
175                     + "': typedef with same name already declared at line " + addedTypedef.getLine());
176         }
177         addedTypedefs.add(type);
178     }
179
180     public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
181         this.typedefs = typedefs;
182     }
183
184     @Override
185     public SchemaPath getPath() {
186         return schemaPath;
187     }
188
189     @Override
190     public void setPath(final SchemaPath schemaPath) {
191         this.schemaPath = schemaPath;
192     }
193
194     @Override
195     public String getDescription() {
196         return description;
197     }
198
199     @Override
200     public void setDescription(final String description) {
201         this.description = description;
202     }
203
204     @Override
205     public String getReference() {
206         return reference;
207     }
208
209     @Override
210     public void setReference(String reference) {
211         this.reference = reference;
212     }
213
214     @Override
215     public Status getStatus() {
216         return status;
217     }
218
219     @Override
220     public void setStatus(Status status) {
221         if (status != null) {
222             this.status = status;
223         }
224     }
225
226     @Override
227     public Set<UsesNodeBuilder> getUsesNodes() {
228         return addedUsesNodes;
229     }
230
231     @Override
232     public void addUsesNode(final UsesNodeBuilder usesBuilder) {
233         addedUsesNodes.add(usesBuilder);
234     }
235
236     public void setUsesnodes(final Set<UsesNode> usesNodes) {
237         this.usesNodes = usesNodes;
238     }
239
240     public Set<AugmentationSchemaBuilder> getAugmentations() {
241         return addedAugmentations;
242     }
243
244     @Override
245     public void addAugmentation(AugmentationSchemaBuilder augment) {
246         addedAugmentations.add(augment);
247     }
248
249     public void setAugmentations(final Set<AugmentationSchema> augmentations) {
250         this.augmentations = augmentations;
251     }
252
253     public List<QName> getKeyDefinition() {
254         return keyDefinition;
255     }
256
257     public void setKeyDefinition(final List<QName> keyDefinition) {
258         if (keyDefinition != null) {
259             this.keyDefinition = keyDefinition;
260         }
261     }
262
263     public boolean isAugmenting() {
264         return augmenting;
265     }
266
267     @Override
268     public void setAugmenting(boolean augmenting) {
269         this.augmenting = augmenting;
270     }
271
272     @Override
273     public boolean isAddedByUses() {
274         return addedByUses;
275     }
276
277     @Override
278     public void setAddedByUses(final boolean addedByUses) {
279         this.addedByUses = addedByUses;
280     }
281
282     public Boolean isConfiguration() {
283         return configuration;
284     }
285
286     @Override
287     public void setConfiguration(Boolean configuration) {
288         this.configuration = configuration;
289     }
290
291     @Override
292     public ConstraintsBuilder getConstraints() {
293         return constraints;
294     }
295
296     public boolean isUserOrdered() {
297         return userOrdered;
298     }
299
300     public void setUserOrdered(final boolean userOrdered) {
301         this.userOrdered = userOrdered;
302     }
303
304     @Override
305     public int hashCode() {
306         final int prime = 31;
307         int result = 1;
308         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
309         return result;
310     }
311
312     @Override
313     public boolean equals(Object obj) {
314         if (this == obj) {
315             return true;
316         }
317         if (obj == null) {
318             return false;
319         }
320         if (getClass() != obj.getClass()) {
321             return false;
322         }
323         ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
324         if (schemaPath == null) {
325             if (other.schemaPath != null) {
326                 return false;
327             }
328         } else if (!schemaPath.equals(other.schemaPath)) {
329             return false;
330         }
331         if (parent == null) {
332             if (other.parent != null) {
333                 return false;
334             }
335         } else if (!parent.equals(other.parent)) {
336             return false;
337         }
338         return true;
339     }
340
341     @Override
342     public String toString() {
343         return "list " + qname.getLocalName();
344     }
345
346     public final class ListSchemaNodeImpl implements ListSchemaNode {
347         private final QName qname;
348         private SchemaPath path;
349         private String description;
350         private String reference;
351         private Status status = Status.CURRENT;
352         private List<QName> keyDefinition = Collections.emptyList();
353         private boolean augmenting;
354         private boolean addedByUses;
355         private boolean configuration;
356         private ConstraintDefinition constraints;
357         private Set<AugmentationSchema> augmentations = Collections.emptySet();
358         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
359         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
360         private Set<GroupingDefinition> groupings = Collections.emptySet();
361         private Set<UsesNode> uses = Collections.emptySet();
362         private boolean userOrdered;
363         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
364
365         private ListSchemaNodeImpl(final QName qname) {
366             this.qname = qname;
367         }
368
369         @Override
370         public QName getQName() {
371             return qname;
372         }
373
374         @Override
375         public SchemaPath getPath() {
376             return path;
377         }
378
379         private void setPath(final SchemaPath path) {
380             this.path = path;
381         }
382
383         @Override
384         public String getDescription() {
385             return description;
386         }
387
388         private void setDescription(final String description) {
389             this.description = description;
390         }
391
392         @Override
393         public String getReference() {
394             return reference;
395         }
396
397         private void setReference(final String reference) {
398             this.reference = reference;
399         }
400
401         @Override
402         public Status getStatus() {
403             return status;
404         }
405
406         private void setStatus(Status status) {
407             this.status = status;
408         }
409
410         @Override
411         public List<QName> getKeyDefinition() {
412             return keyDefinition;
413         }
414
415         private void setKeyDefinition(List<QName> keyDefinition) {
416             if (keyDefinition != null) {
417                 this.keyDefinition = keyDefinition;
418             }
419         }
420
421         @Override
422         public boolean isAugmenting() {
423             return augmenting;
424         }
425
426         private void setAugmenting(boolean augmenting) {
427             this.augmenting = augmenting;
428         }
429
430         @Override
431         public boolean isAddedByUses() {
432             return addedByUses;
433         }
434
435         private void setAddedByUses(final boolean addedByUses) {
436             this.addedByUses = addedByUses;
437         }
438
439         @Override
440         public boolean isConfiguration() {
441             return configuration;
442         }
443
444         private void setConfiguration(boolean configuration) {
445             this.configuration = configuration;
446         }
447
448         @Override
449         public ConstraintDefinition getConstraints() {
450             return constraints;
451         }
452
453         private void setConstraints(ConstraintDefinition constraints) {
454             this.constraints = constraints;
455         }
456
457         @Override
458         public Set<AugmentationSchema> getAvailableAugmentations() {
459             return augmentations;
460         }
461
462         private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
463             if (augmentations != null) {
464                 this.augmentations = augmentations;
465             }
466         }
467
468         @Override
469         public Set<DataSchemaNode> getChildNodes() {
470             return new HashSet<DataSchemaNode>(childNodes.values());
471         }
472
473         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
474             if (childNodes != null) {
475                 this.childNodes = childNodes;
476             }
477         }
478
479         @Override
480         public Set<GroupingDefinition> getGroupings() {
481             return groupings;
482         }
483
484         private void setGroupings(Set<GroupingDefinition> groupings) {
485             if (groupings != null) {
486                 this.groupings = groupings;
487             }
488         }
489
490         @Override
491         public Set<TypeDefinition<?>> getTypeDefinitions() {
492             return typeDefinitions;
493         }
494
495         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
496             if (typeDefinitions != null) {
497                 this.typeDefinitions = typeDefinitions;
498             }
499         }
500
501         @Override
502         public Set<UsesNode> getUses() {
503             return uses;
504         }
505
506         private void setUses(Set<UsesNode> uses) {
507             if (uses != null) {
508                 this.uses = uses;
509             }
510         }
511
512         @Override
513         public DataSchemaNode getDataChildByName(QName name) {
514             return childNodes.get(name);
515         }
516
517         @Override
518         public DataSchemaNode getDataChildByName(String name) {
519             DataSchemaNode result = null;
520             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
521                 if (entry.getKey().getLocalName().equals(name)) {
522                     result = entry.getValue();
523                     break;
524                 }
525             }
526             return result;
527         }
528
529         @Override
530         public boolean isUserOrdered() {
531             return userOrdered;
532         }
533
534         private void setUserOrdered(boolean userOrdered) {
535             this.userOrdered = userOrdered;
536         }
537
538         @Override
539         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
540             return unknownNodes;
541         }
542
543         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
544             if (unknownNodes != null) {
545                 this.unknownNodes = unknownNodes;
546             }
547         }
548
549         public ListSchemaNodeBuilder toBuilder() {
550             return ListSchemaNodeBuilder.this;
551         }
552
553         @Override
554         public int hashCode() {
555             final int prime = 31;
556             int result = 1;
557             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
558             result = prime * result + ((path == null) ? 0 : path.hashCode());
559             return result;
560         }
561
562         @Override
563         public boolean equals(Object obj) {
564             if (this == obj) {
565                 return true;
566             }
567             if (obj == null) {
568                 return false;
569             }
570             if (getClass() != obj.getClass()) {
571                 return false;
572             }
573             final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
574             if (qname == null) {
575                 if (other.qname != null) {
576                     return false;
577                 }
578             } else if (!qname.equals(other.qname)) {
579                 return false;
580             }
581             if (path == null) {
582                 if (other.path != null) {
583                     return false;
584                 }
585             } else if (!path.equals(other.path)) {
586                 return false;
587             }
588             return true;
589         }
590
591         @Override
592         public String toString() {
593             StringBuilder sb = new StringBuilder(ListSchemaNodeImpl.class.getSimpleName());
594             sb.append("[");
595             sb.append("qname=" + qname);
596             sb.append(", path=" + path);
597             sb.append(", keyDefinition=" + keyDefinition);
598             sb.append("]");
599             return sb.toString();
600         }
601     }
602
603 }