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