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