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