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