When a node is going down, remove edges in both directions associated with the node.
[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.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 ContainerSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements
41         AugmentationTargetBuilder, DataSchemaNodeBuilder, GroupingMember {
42     private boolean isBuilt;
43     private final ContainerSchemaNodeImpl instance;
44
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     // ContainerSchemaNode args
64     private boolean presence;
65
66     public ContainerSchemaNodeBuilder(final int line, final QName qname, final SchemaPath schemaPath) {
67         super(line, qname);
68         this.schemaPath = schemaPath;
69         instance = new ContainerSchemaNodeImpl(qname);
70         constraints = new ConstraintsBuilder(line);
71     }
72
73     public ContainerSchemaNodeBuilder(final ContainerSchemaNodeBuilder b) {
74         super(b.getLine(), b.getQName());
75         instance = new ContainerSchemaNodeImpl(b.getQName());
76         constraints = b.getConstraints();
77         schemaPath = b.getPath();
78         description = b.getDescription();
79         reference = b.getReference();
80         status = b.getStatus();
81         presence = b.isPresence();
82         augmenting = b.isAugmenting();
83         addedByUses = b.isAddedByUses();
84         configuration = b.isConfiguration();
85         childNodes = b.getChildNodes();
86         addedChildNodes.addAll(b.getChildNodeBuilders());
87         groupings = b.getGroupings();
88         addedGroupings.addAll(b.getGroupingBuilders());
89         typedefs = b.typedefs;
90         addedTypedefs.addAll(b.getTypeDefinitionBuilders());
91         usesNodes = b.usesNodes;
92         addedUsesNodes.addAll(b.getUsesNodes());
93         augmentations = b.augmentations;
94         addedAugmentations.addAll(b.getAugmentations());
95         unknownNodes = b.unknownNodes;
96         addedUnknownNodes.addAll(b.getUnknownNodeBuilders());
97     }
98
99     @Override
100     public ContainerSchemaNode build() {
101         if (!isBuilt) {
102             instance.setPath(schemaPath);
103             instance.setDescription(description);
104             instance.setReference(reference);
105             instance.setStatus(status);
106             instance.setPresenceContainer(presence);
107             instance.setAugmenting(augmenting);
108             instance.setAddedByUses(addedByUses);
109
110             // if this builder represents rpc input or output, it can has
111             // configuration value set to null
112             if (configuration == null) {
113                 configuration = false;
114             }
115             instance.setConfiguration(configuration);
116
117             // CHILD NODES
118             final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
119             if (childNodes == null) {
120                 for (DataSchemaNodeBuilder node : addedChildNodes) {
121                     childs.put(node.getQName(), node.build());
122                 }
123             } else {
124                 for (DataSchemaNode node : childNodes) {
125                     childs.put(node.getQName(), node);
126                 }
127             }
128             instance.setChildNodes(childs);
129
130             // GROUPINGS
131             if (groupings == null) {
132                 groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
133                 for (GroupingBuilder builder : addedGroupings) {
134                     groupings.add(builder.build());
135                 }
136             }
137             instance.setGroupings(groupings);
138
139             // TYPEDEFS
140             if (typedefs == null) {
141                 typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
142                 for (TypeDefinitionBuilder entry : addedTypedefs) {
143                     typedefs.add(entry.build());
144                 }
145             }
146             instance.setTypeDefinitions(typedefs);
147
148             // USES
149             if (usesNodes == null) {
150                 usesNodes = new HashSet<UsesNode>();
151                 for (UsesNodeBuilder builder : addedUsesNodes) {
152                     usesNodes.add(builder.build());
153                 }
154             }
155             instance.setUses(usesNodes);
156
157             // AUGMENTATIONS
158             if (augmentations == null) {
159                 augmentations = new HashSet<AugmentationSchema>();
160                 for (AugmentationSchemaBuilder builder : addedAugmentations) {
161                     augmentations.add(builder.build());
162                 }
163             }
164             instance.setAvailableAugmentations(augmentations);
165
166             // UNKNOWN NODES
167             if (unknownNodes == null) {
168                 unknownNodes = new ArrayList<UnknownSchemaNode>();
169                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
170                     unknownNodes.add(b.build());
171                 }
172                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
173             }
174             instance.setUnknownSchemaNodes(unknownNodes);
175
176             instance.setConstraints(constraints.build());
177             instance.setAvailableAugmentations(augmentations);
178
179             isBuilt = true;
180         }
181         return instance;
182     }
183
184     @Override
185     public void rebuild() {
186         isBuilt = false;
187         build();
188     }
189
190     @Override
191     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
192         return addedTypedefs;
193     }
194
195     @Override
196     public void addTypedef(final TypeDefinitionBuilder type) {
197         addedTypedefs.add(type);
198     }
199
200     public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
201         this.typedefs = typedefs;
202     }
203
204     public Set<AugmentationSchemaBuilder> getAugmentations() {
205         return addedAugmentations;
206     }
207
208     @Override
209     public void addAugmentation(AugmentationSchemaBuilder augment) {
210         addedAugmentations.add(augment);
211     }
212
213     public void setAugmentations(final Set<AugmentationSchema> augmentations) {
214         this.augmentations = augmentations;
215     }
216
217     public SchemaPath getPath() {
218         return schemaPath;
219     }
220
221     @Override
222     public void setPath(final SchemaPath schemaPath) {
223         this.schemaPath = schemaPath;
224     }
225
226     @Override
227     public String getDescription() {
228         return description;
229     }
230
231     @Override
232     public void setDescription(final String description) {
233         this.description = description;
234     }
235
236     @Override
237     public String getReference() {
238         return reference;
239     }
240
241     @Override
242     public void setReference(String reference) {
243         this.reference = reference;
244     }
245
246     @Override
247     public Status getStatus() {
248         return status;
249     }
250
251     @Override
252     public void setStatus(Status status) {
253         if (status != null) {
254             this.status = status;
255         }
256     }
257
258     @Override
259     public boolean isAugmenting() {
260         return augmenting;
261     }
262
263     @Override
264     public void setAugmenting(boolean augmenting) {
265         this.augmenting = augmenting;
266     }
267
268     @Override
269     public boolean isAddedByUses() {
270         return addedByUses;
271     }
272
273     @Override
274     public void setAddedByUses(final boolean addedByUses) {
275         this.addedByUses = addedByUses;
276     }
277
278     @Override
279     public Boolean isConfiguration() {
280         return configuration;
281     }
282
283     @Override
284     public void setConfiguration(Boolean configuration) {
285         this.configuration = configuration;
286     }
287
288     @Override
289     public ConstraintsBuilder getConstraints() {
290         return constraints;
291     }
292
293     public Set<UsesNodeBuilder> getUsesNodes() {
294         return addedUsesNodes;
295     }
296
297     @Override
298     public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
299         addedUsesNodes.add(usesNodeBuilder);
300     }
301
302     public void setUsesnodes(final Set<UsesNode> usesNodes) {
303         this.usesNodes = usesNodes;
304     }
305
306     public boolean isPresence() {
307         return presence;
308     }
309
310     public void setPresence(boolean presence) {
311         this.presence = presence;
312     }
313
314     @Override
315     public int hashCode() {
316         final int prime = 31;
317         int result = 1;
318         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
319         return result;
320     }
321
322     @Override
323     public boolean equals(Object obj) {
324         if (this == obj) {
325             return true;
326         }
327         if (obj == null) {
328             return false;
329         }
330         if (getClass() != obj.getClass()) {
331             return false;
332         }
333         ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
334         if (schemaPath == null) {
335             if (other.schemaPath != null) {
336                 return false;
337             }
338         } else if (!schemaPath.equals(other.schemaPath)) {
339             return false;
340         }
341         if (parent == null) {
342             if (other.parent != null) {
343                 return false;
344             }
345         } else if (!parent.equals(other.parent)) {
346             return false;
347         }
348         return true;
349     }
350
351     @Override
352     public String toString() {
353         return "container " + qname.getLocalName();
354     }
355
356     public final class ContainerSchemaNodeImpl implements ContainerSchemaNode {
357         private final QName qname;
358         private SchemaPath path;
359         private String description;
360         private String reference;
361         private Status status = Status.CURRENT;
362         private boolean augmenting;
363         private boolean addedByUses;
364         private boolean configuration;
365         private ConstraintDefinition constraints;
366         private Set<AugmentationSchema> augmentations = Collections.emptySet();
367         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
368         private Set<GroupingDefinition> groupings = Collections.emptySet();
369         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
370         private Set<UsesNode> uses = Collections.emptySet();
371         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
372         private boolean presence;
373
374         private ContainerSchemaNodeImpl(QName qname) {
375             this.qname = qname;
376         }
377
378         @Override
379         public QName getQName() {
380             return qname;
381         }
382
383         @Override
384         public SchemaPath getPath() {
385             return path;
386         }
387
388         private void setPath(SchemaPath path) {
389             this.path = path;
390         }
391
392         @Override
393         public String getDescription() {
394             return description;
395         }
396
397         private void setDescription(String description) {
398             this.description = description;
399         }
400
401         @Override
402         public String getReference() {
403             return reference;
404         }
405
406         private void setReference(String reference) {
407             this.reference = reference;
408         }
409
410         @Override
411         public Status getStatus() {
412             return status;
413         }
414
415         private void setStatus(Status status) {
416             if (status != null) {
417                 this.status = status;
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(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 DataSchemaNode getDataChildByName(QName name) {
492             return childNodes.get(name);
493         }
494
495         @Override
496         public DataSchemaNode getDataChildByName(String name) {
497             DataSchemaNode result = null;
498             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
499                 if (entry.getKey().getLocalName().equals(name)) {
500                     result = entry.getValue();
501                     break;
502                 }
503             }
504             return result;
505         }
506
507         @Override
508         public Set<UsesNode> getUses() {
509             return uses;
510         }
511
512         private void setUses(Set<UsesNode> uses) {
513             if (uses != null) {
514                 this.uses = uses;
515             }
516         }
517
518         @Override
519         public boolean isPresenceContainer() {
520             return presence;
521         }
522
523         private void setPresenceContainer(boolean presence) {
524             this.presence = presence;
525         }
526
527         @Override
528         public Set<TypeDefinition<?>> getTypeDefinitions() {
529             return typeDefinitions;
530         }
531
532         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
533             if (typeDefinitions != null) {
534                 this.typeDefinitions = typeDefinitions;
535             }
536         }
537
538         @Override
539         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
540             return unknownNodes;
541         }
542
543         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
544             if (unknownSchemaNodes != null) {
545                 this.unknownNodes = unknownSchemaNodes;
546             }
547         }
548
549         public ContainerSchemaNodeBuilder toBuilder() {
550             return ContainerSchemaNodeBuilder.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             ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) 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(ContainerSchemaNodeImpl.class.getSimpleName());
594             sb.append("[");
595             sb.append("qname=" + qname);
596             sb.append("]");
597             return sb.toString();
598         }
599     }
600
601 }