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