Merge "Added more Rpc markers to yang-binding."
[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         System.out.println();
178     }
179
180     public SchemaPath getPath() {
181         return schemaPath;
182     }
183
184     @Override
185     public void setPath(final SchemaPath schemaPath) {
186         this.schemaPath = schemaPath;
187     }
188
189     @Override
190     public String getDescription() {
191         return description;
192     }
193
194     @Override
195     public void setDescription(final String description) {
196         this.description = description;
197     }
198
199     @Override
200     public String getReference() {
201         return reference;
202     }
203
204     @Override
205     public void setReference(String reference) {
206         this.reference = reference;
207     }
208
209     @Override
210     public Status getStatus() {
211         return status;
212     }
213
214     @Override
215     public void setStatus(Status status) {
216         if (status != null) {
217             this.status = status;
218         }
219     }
220
221     @Override
222     public boolean isAugmenting() {
223         return augmenting;
224     }
225
226     @Override
227     public void setAugmenting(boolean augmenting) {
228         this.augmenting = augmenting;
229     }
230
231     @Override
232     public boolean isAddedByUses() {
233         return addedByUses;
234     }
235
236     @Override
237     public void setAddedByUses(final boolean addedByUses) {
238         this.addedByUses = addedByUses;
239     }
240
241     @Override
242     public Boolean isConfiguration() {
243         return configuration;
244     }
245
246     @Override
247     public void setConfiguration(Boolean configuration) {
248         this.configuration = configuration;
249     }
250
251     @Override
252     public ConstraintsBuilder getConstraints() {
253         return constraints;
254     }
255
256     public boolean isPresence() {
257         return presence;
258     }
259
260     public void setPresence(boolean presence) {
261         this.presence = presence;
262     }
263
264     @Override
265     public int hashCode() {
266         final int prime = 31;
267         int result = 1;
268         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
269         return result;
270     }
271
272     @Override
273     public boolean equals(Object obj) {
274         if (this == obj) {
275             return true;
276         }
277         if (obj == null) {
278             return false;
279         }
280         if (getClass() != obj.getClass()) {
281             return false;
282         }
283         ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
284         if (schemaPath == null) {
285             if (other.schemaPath != null) {
286                 return false;
287             }
288         } else if (!schemaPath.equals(other.schemaPath)) {
289             return false;
290         }
291         if (parent == null) {
292             if (other.parent != null) {
293                 return false;
294             }
295         } else if (!parent.equals(other.parent)) {
296             return false;
297         }
298         return true;
299     }
300
301     @Override
302     public String toString() {
303         return "container " + qname.getLocalName();
304     }
305
306     public final class ContainerSchemaNodeImpl implements ContainerSchemaNode {
307         private final QName qname;
308         private SchemaPath path;
309         private YangNode parent;
310         private String description;
311         private String reference;
312         private Status status = Status.CURRENT;
313         private boolean augmenting;
314         private boolean addedByUses;
315         private boolean configuration;
316         private ConstraintDefinition constraints;
317         private Set<AugmentationSchema> augmentations = Collections.emptySet();
318         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
319         private Set<GroupingDefinition> groupings = Collections.emptySet();
320         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
321         private Set<UsesNode> uses = Collections.emptySet();
322         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
323         private boolean presence;
324
325         private ContainerSchemaNodeImpl(QName qname) {
326             this.qname = qname;
327         }
328
329         @Override
330         public QName getQName() {
331             return qname;
332         }
333
334         @Override
335         public SchemaPath getPath() {
336             return path;
337         }
338
339         private void setPath(SchemaPath path) {
340             this.path = path;
341         }
342
343         @Override
344         public YangNode getParent() {
345             return parent;
346         }
347
348         private void setParent(YangNode parent) {
349             this.parent = parent;
350         }
351
352         @Override
353         public String getDescription() {
354             return description;
355         }
356
357         private void setDescription(String description) {
358             this.description = description;
359         }
360
361         @Override
362         public String getReference() {
363             return reference;
364         }
365
366         private void setReference(String reference) {
367             this.reference = reference;
368         }
369
370         @Override
371         public Status getStatus() {
372             return status;
373         }
374
375         private void setStatus(Status status) {
376             if (status != null) {
377                 this.status = status;
378             }
379         }
380
381         @Override
382         public boolean isAugmenting() {
383             return augmenting;
384         }
385
386         private void setAugmenting(boolean augmenting) {
387             this.augmenting = augmenting;
388         }
389
390         @Override
391         public boolean isAddedByUses() {
392             return addedByUses;
393         }
394
395         private void setAddedByUses(boolean addedByUses) {
396             this.addedByUses = addedByUses;
397         }
398
399         @Override
400         public boolean isConfiguration() {
401             return configuration;
402         }
403
404         private void setConfiguration(boolean configuration) {
405             this.configuration = configuration;
406         }
407
408         @Override
409         public ConstraintDefinition getConstraints() {
410             return constraints;
411         }
412
413         private void setConstraints(ConstraintDefinition constraints) {
414             this.constraints = constraints;
415         }
416
417         @Override
418         public Set<AugmentationSchema> getAvailableAugmentations() {
419             return augmentations;
420         }
421
422         private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
423             if (augmentations != null) {
424                 this.augmentations = augmentations;
425             }
426         }
427
428         @Override
429         public Set<DataSchemaNode> getChildNodes() {
430             return new HashSet<>(childNodes.values());
431         }
432
433         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
434             if (childNodes != null) {
435                 this.childNodes = childNodes;
436             }
437         }
438
439         @Override
440         public Set<GroupingDefinition> getGroupings() {
441             return groupings;
442         }
443
444         private void setGroupings(Set<GroupingDefinition> groupings) {
445             if (groupings != null) {
446                 this.groupings = groupings;
447             }
448         }
449
450         @Override
451         public DataSchemaNode getDataChildByName(QName name) {
452             return childNodes.get(name);
453         }
454
455         @Override
456         public DataSchemaNode getDataChildByName(String name) {
457             DataSchemaNode result = null;
458             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
459                 if (entry.getKey().getLocalName().equals(name)) {
460                     result = entry.getValue();
461                     break;
462                 }
463             }
464             return result;
465         }
466
467         @Override
468         public Set<UsesNode> getUses() {
469             return uses;
470         }
471
472         private void setUses(Set<UsesNode> uses) {
473             if (uses != null) {
474                 this.uses = uses;
475             }
476         }
477
478         @Override
479         public boolean isPresenceContainer() {
480             return presence;
481         }
482
483         private void setPresenceContainer(boolean presence) {
484             this.presence = presence;
485         }
486
487         @Override
488         public Set<TypeDefinition<?>> getTypeDefinitions() {
489             return typeDefinitions;
490         }
491
492         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
493             if (typeDefinitions != null) {
494                 this.typeDefinitions = typeDefinitions;
495             }
496         }
497
498         @Override
499         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
500             return unknownNodes;
501         }
502
503         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
504             if (unknownSchemaNodes != null) {
505                 this.unknownNodes = unknownSchemaNodes;
506             }
507         }
508
509         public ContainerSchemaNodeBuilder toBuilder() {
510             return ContainerSchemaNodeBuilder.this;
511         }
512
513         @Override
514         public int hashCode() {
515             final int prime = 31;
516             int result = 1;
517             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
518             result = prime * result + ((path == null) ? 0 : path.hashCode());
519             return result;
520         }
521
522         @Override
523         public boolean equals(Object obj) {
524             if (this == obj) {
525                 return true;
526             }
527             if (obj == null) {
528                 return false;
529             }
530             if (getClass() != obj.getClass()) {
531                 return false;
532             }
533             ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
534             if (qname == null) {
535                 if (other.qname != null) {
536                     return false;
537                 }
538             } else if (!qname.equals(other.qname)) {
539                 return false;
540             }
541             if (path == null) {
542                 if (other.path != null) {
543                     return false;
544                 }
545             } else if (!path.equals(other.path)) {
546                 return false;
547             }
548             return true;
549         }
550
551         @Override
552         public String toString() {
553             StringBuilder sb = new StringBuilder(ContainerSchemaNodeImpl.class.getSimpleName());
554             sb.append("[");
555             sb.append("qname=");
556             sb.append(qname);
557             sb.append("]");
558             return sb.toString();
559         }
560     }
561
562 }