2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.UsesNode;
30 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingMember;
36 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
37 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
38 import org.opendaylight.yangtools.yang.parser.util.Comparators;
39 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
41 public final class ContainerSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements
42 AugmentationTargetBuilder, DataSchemaNodeBuilder, GroupingMember {
43 private boolean isBuilt;
44 private final ContainerSchemaNodeImpl instance;
47 private SchemaPath schemaPath;
48 private String description;
49 private String reference;
50 private Status status = Status.CURRENT;
51 // DataSchemaNode args
52 private boolean augmenting;
53 private boolean addedByUses;
54 private Boolean configuration;
55 private final ConstraintsBuilder constraints;
56 // DataNodeContainer args
57 private Set<TypeDefinition<?>> typedefs;
58 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
59 private Set<UsesNode> usesNodes;
60 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
61 // AugmentationTarget args
62 private Set<AugmentationSchema> augmentations;
63 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
64 // ContainerSchemaNode args
65 private boolean presence;
67 public ContainerSchemaNodeBuilder(final String moduleName, final int line, final QName qname,
68 final SchemaPath schemaPath) {
69 super(moduleName, line, qname);
70 this.schemaPath = schemaPath;
71 instance = new ContainerSchemaNodeImpl(qname);
72 constraints = new ConstraintsBuilder(moduleName, line);
75 public ContainerSchemaNodeBuilder(final ContainerSchemaNodeBuilder b) {
76 super(b.getModuleName(), b.getLine(), b.getQName());
77 instance = new ContainerSchemaNodeImpl(b.getQName());
78 constraints = b.getConstraints();
79 schemaPath = b.getPath();
80 description = b.getDescription();
81 reference = b.getReference();
82 status = b.getStatus();
83 presence = b.isPresence();
84 augmenting = b.isAugmenting();
85 addedByUses = b.isAddedByUses();
86 configuration = b.isConfiguration();
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());
102 public ContainerSchemaNode build() {
104 instance.setPath(schemaPath);
105 instance.setDescription(description);
106 instance.setReference(reference);
107 instance.setStatus(status);
108 instance.setPresenceContainer(presence);
109 instance.setAugmenting(augmenting);
110 instance.setAddedByUses(addedByUses);
112 // if this builder represents rpc input or output, it can has
113 // configuration value set to null
114 if (configuration == null) {
115 configuration = false;
117 instance.setConfiguration(configuration);
120 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
121 if (childNodes == null || childNodes.isEmpty()) {
122 for (DataSchemaNodeBuilder node : addedChildNodes) {
123 childs.put(node.getQName(), node.build());
126 for (DataSchemaNode node : childNodes) {
127 childs.put(node.getQName(), node);
130 instance.setChildNodes(childs);
133 if (groupings == null) {
134 groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
135 for (GroupingBuilder builder : addedGroupings) {
136 groupings.add(builder.build());
139 instance.setGroupings(groupings);
142 if (typedefs == null) {
143 typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
144 for (TypeDefinitionBuilder entry : addedTypedefs) {
145 typedefs.add(entry.build());
148 instance.setTypeDefinitions(typedefs);
151 if (usesNodes == null) {
152 usesNodes = new HashSet<UsesNode>();
153 for (UsesNodeBuilder builder : addedUsesNodes) {
154 usesNodes.add(builder.build());
157 instance.setUses(usesNodes);
160 if (augmentations == null) {
161 augmentations = new HashSet<AugmentationSchema>();
162 for (AugmentationSchemaBuilder builder : addedAugmentations) {
163 augmentations.add(builder.build());
166 instance.setAvailableAugmentations(augmentations);
169 if (unknownNodes == null) {
170 unknownNodes = new ArrayList<UnknownSchemaNode>();
171 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
172 unknownNodes.add(b.build());
174 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
176 instance.setUnknownSchemaNodes(unknownNodes);
178 instance.setConstraints(constraints.build());
179 instance.setAvailableAugmentations(augmentations);
187 public void rebuild() {
193 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
194 return addedTypedefs;
198 public void addTypedef(final TypeDefinitionBuilder type) {
199 String typeName = type.getQName().getLocalName();
200 for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
201 throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
202 + "': typedef with same name already declared at line " + addedTypedef.getLine());
204 addedTypedefs.add(type);
207 public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
208 this.typedefs = typedefs;
211 public Set<AugmentationSchemaBuilder> getAugmentations() {
212 return addedAugmentations;
216 public void addAugmentation(AugmentationSchemaBuilder augment) {
217 addedAugmentations.add(augment);
220 public void setAugmentations(final Set<AugmentationSchema> augmentations) {
221 this.augmentations = augmentations;
224 public SchemaPath getPath() {
229 public void setPath(final SchemaPath schemaPath) {
230 this.schemaPath = schemaPath;
234 public String getDescription() {
239 public void setDescription(final String description) {
240 this.description = description;
244 public String getReference() {
249 public void setReference(String reference) {
250 this.reference = reference;
254 public Status getStatus() {
259 public void setStatus(Status status) {
260 if (status != null) {
261 this.status = status;
266 public boolean isAugmenting() {
271 public void setAugmenting(boolean augmenting) {
272 this.augmenting = augmenting;
276 public boolean isAddedByUses() {
281 public void setAddedByUses(final boolean addedByUses) {
282 this.addedByUses = addedByUses;
286 public Boolean isConfiguration() {
287 return configuration;
291 public void setConfiguration(Boolean configuration) {
292 this.configuration = configuration;
296 public ConstraintsBuilder getConstraints() {
300 public Set<UsesNodeBuilder> getUsesNodes() {
301 return addedUsesNodes;
305 public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
306 addedUsesNodes.add(usesNodeBuilder);
309 public void setUsesnodes(final Set<UsesNode> usesNodes) {
310 this.usesNodes = usesNodes;
313 public boolean isPresence() {
317 public void setPresence(boolean presence) {
318 this.presence = presence;
322 public int hashCode() {
323 final int prime = 31;
325 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
330 public boolean equals(Object obj) {
337 if (getClass() != obj.getClass()) {
340 ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
341 if (schemaPath == null) {
342 if (other.schemaPath != null) {
345 } else if (!schemaPath.equals(other.schemaPath)) {
348 if (parent == null) {
349 if (other.parent != null) {
352 } else if (!parent.equals(other.parent)) {
359 public String toString() {
360 return "container " + qname.getLocalName();
363 public final class ContainerSchemaNodeImpl implements ContainerSchemaNode {
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 boolean augmenting;
370 private boolean addedByUses;
371 private boolean configuration;
372 private ConstraintDefinition constraints;
373 private Set<AugmentationSchema> augmentations = Collections.emptySet();
374 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
375 private Set<GroupingDefinition> groupings = Collections.emptySet();
376 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
377 private Set<UsesNode> uses = Collections.emptySet();
378 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
379 private boolean presence;
381 private ContainerSchemaNodeImpl(QName qname) {
386 public QName getQName() {
391 public SchemaPath getPath() {
395 private void setPath(SchemaPath path) {
400 public String getDescription() {
404 private void setDescription(String description) {
405 this.description = description;
409 public String getReference() {
413 private void setReference(String reference) {
414 this.reference = reference;
418 public Status getStatus() {
422 private void setStatus(Status status) {
423 if (status != null) {
424 this.status = status;
429 public boolean isAugmenting() {
433 private void setAugmenting(boolean augmenting) {
434 this.augmenting = augmenting;
438 public boolean isAddedByUses() {
442 private void setAddedByUses(boolean addedByUses) {
443 this.addedByUses = addedByUses;
447 public boolean isConfiguration() {
448 return configuration;
451 private void setConfiguration(boolean configuration) {
452 this.configuration = configuration;
456 public ConstraintDefinition getConstraints() {
460 private void setConstraints(ConstraintDefinition constraints) {
461 this.constraints = constraints;
465 public Set<AugmentationSchema> getAvailableAugmentations() {
466 return augmentations;
469 private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
470 if (augmentations != null) {
471 this.augmentations = augmentations;
476 public Set<DataSchemaNode> getChildNodes() {
477 return new HashSet<DataSchemaNode>(childNodes.values());
480 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
481 if (childNodes != null) {
482 this.childNodes = childNodes;
487 public Set<GroupingDefinition> getGroupings() {
491 private void setGroupings(Set<GroupingDefinition> groupings) {
492 if (groupings != null) {
493 this.groupings = groupings;
498 public DataSchemaNode getDataChildByName(QName name) {
499 return childNodes.get(name);
503 public DataSchemaNode getDataChildByName(String name) {
504 DataSchemaNode result = null;
505 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
506 if (entry.getKey().getLocalName().equals(name)) {
507 result = entry.getValue();
515 public Set<UsesNode> getUses() {
519 private void setUses(Set<UsesNode> uses) {
526 public boolean isPresenceContainer() {
530 private void setPresenceContainer(boolean presence) {
531 this.presence = presence;
535 public Set<TypeDefinition<?>> getTypeDefinitions() {
536 return typeDefinitions;
539 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
540 if (typeDefinitions != null) {
541 this.typeDefinitions = typeDefinitions;
546 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
550 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
551 if (unknownSchemaNodes != null) {
552 this.unknownNodes = unknownSchemaNodes;
556 public ContainerSchemaNodeBuilder toBuilder() {
557 return ContainerSchemaNodeBuilder.this;
561 public int hashCode() {
562 final int prime = 31;
564 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
565 result = prime * result + ((path == null) ? 0 : path.hashCode());
570 public boolean equals(Object obj) {
577 if (getClass() != obj.getClass()) {
580 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
582 if (other.qname != null) {
585 } else if (!qname.equals(other.qname)) {
589 if (other.path != null) {
592 } else if (!path.equals(other.path)) {
599 public String toString() {
600 StringBuilder sb = new StringBuilder(ContainerSchemaNodeImpl.class.getSimpleName());
602 sb.append("qname=" + qname);
604 return sb.toString();