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);
76 public ContainerSchemaNode build() {
78 instance.setPath(schemaPath);
79 instance.setDescription(description);
80 instance.setReference(reference);
81 instance.setStatus(status);
82 instance.setPresenceContainer(presence);
83 instance.setAugmenting(augmenting);
84 instance.setAddedByUses(addedByUses);
86 // if this builder represents rpc input or output, it can has
87 // configuration value set to null
88 if (configuration == null) {
89 configuration = false;
91 instance.setConfiguration(configuration);
94 if (usesNodes == null) {
95 usesNodes = new HashSet<UsesNode>();
96 for (UsesNodeBuilder builder : addedUsesNodes) {
97 usesNodes.add(builder.build());
100 instance.setUses(usesNodes);
103 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
104 if (childNodes == null || childNodes.isEmpty()) {
105 for (DataSchemaNodeBuilder node : addedChildNodes) {
106 childs.put(node.getQName(), node.build());
109 for (DataSchemaNode node : childNodes) {
110 childs.put(node.getQName(), node);
113 instance.setChildNodes(childs);
116 if (groupings == null) {
117 groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
118 for (GroupingBuilder builder : addedGroupings) {
119 groupings.add(builder.build());
122 instance.setGroupings(groupings);
125 if (typedefs == null) {
126 typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
127 for (TypeDefinitionBuilder entry : addedTypedefs) {
128 typedefs.add(entry.build());
131 instance.setTypeDefinitions(typedefs);
134 if (augmentations == null) {
135 augmentations = new HashSet<AugmentationSchema>();
136 for (AugmentationSchemaBuilder builder : addedAugmentations) {
137 augmentations.add(builder.build());
140 instance.setAvailableAugmentations(augmentations);
143 if (unknownNodes == null) {
144 unknownNodes = new ArrayList<UnknownSchemaNode>();
145 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
146 unknownNodes.add(b.build());
148 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
150 instance.setUnknownSchemaNodes(unknownNodes);
152 instance.setConstraints(constraints.build());
153 instance.setAvailableAugmentations(augmentations);
161 public void rebuild() {
167 public void setQName(QName qname) {
172 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
173 return addedTypedefs;
177 public void addTypedef(final TypeDefinitionBuilder type) {
178 String typeName = type.getQName().getLocalName();
179 for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
180 throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
181 + "': typedef with same name already declared at line " + addedTypedef.getLine());
183 addedTypedefs.add(type);
186 public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
187 this.typedefs = typedefs;
190 public Set<AugmentationSchemaBuilder> getAugmentations() {
191 return addedAugmentations;
195 public void addAugmentation(AugmentationSchemaBuilder augment) {
196 addedAugmentations.add(augment);
199 public void setAugmentations(final Set<AugmentationSchema> augmentations) {
200 this.augmentations = augmentations;
203 public SchemaPath getPath() {
208 public void setPath(final SchemaPath schemaPath) {
209 this.schemaPath = schemaPath;
213 public String getDescription() {
218 public void setDescription(final String description) {
219 this.description = description;
223 public String getReference() {
228 public void setReference(String reference) {
229 this.reference = reference;
233 public Status getStatus() {
238 public void setStatus(Status status) {
239 if (status != null) {
240 this.status = status;
245 public boolean isAugmenting() {
250 public void setAugmenting(boolean augmenting) {
251 this.augmenting = augmenting;
255 public boolean isAddedByUses() {
260 public void setAddedByUses(final boolean addedByUses) {
261 this.addedByUses = addedByUses;
265 public Boolean isConfiguration() {
266 return configuration;
270 public void setConfiguration(Boolean configuration) {
271 this.configuration = configuration;
275 public ConstraintsBuilder getConstraints() {
280 public Set<UsesNodeBuilder> getUsesNodes() {
281 return addedUsesNodes;
285 public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
286 addedUsesNodes.add(usesNodeBuilder);
289 public void setUsesnodes(final Set<UsesNode> usesNodes) {
290 this.usesNodes = usesNodes;
293 public boolean isPresence() {
297 public void setPresence(boolean presence) {
298 this.presence = presence;
302 public int hashCode() {
303 final int prime = 31;
305 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
310 public boolean equals(Object obj) {
317 if (getClass() != obj.getClass()) {
320 ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
321 if (schemaPath == null) {
322 if (other.schemaPath != null) {
325 } else if (!schemaPath.equals(other.schemaPath)) {
328 if (parent == null) {
329 if (other.parent != null) {
332 } else if (!parent.equals(other.parent)) {
339 public String toString() {
340 return "container " + qname.getLocalName();
343 public final class ContainerSchemaNodeImpl implements ContainerSchemaNode {
344 private final QName qname;
345 private SchemaPath path;
346 private String description;
347 private String reference;
348 private Status status = Status.CURRENT;
349 private boolean augmenting;
350 private boolean addedByUses;
351 private boolean configuration;
352 private ConstraintDefinition constraints;
353 private Set<AugmentationSchema> augmentations = Collections.emptySet();
354 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
355 private Set<GroupingDefinition> groupings = Collections.emptySet();
356 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
357 private Set<UsesNode> uses = Collections.emptySet();
358 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
359 private boolean presence;
361 private ContainerSchemaNodeImpl(QName qname) {
366 public QName getQName() {
371 public SchemaPath getPath() {
375 private void setPath(SchemaPath path) {
380 public String getDescription() {
384 private void setDescription(String description) {
385 this.description = description;
389 public String getReference() {
393 private void setReference(String reference) {
394 this.reference = reference;
398 public Status getStatus() {
402 private void setStatus(Status status) {
403 if (status != null) {
404 this.status = status;
409 public boolean isAugmenting() {
413 private void setAugmenting(boolean augmenting) {
414 this.augmenting = augmenting;
418 public boolean isAddedByUses() {
422 private void setAddedByUses(boolean addedByUses) {
423 this.addedByUses = addedByUses;
427 public boolean isConfiguration() {
428 return configuration;
431 private void setConfiguration(boolean configuration) {
432 this.configuration = configuration;
436 public ConstraintDefinition getConstraints() {
440 private void setConstraints(ConstraintDefinition constraints) {
441 this.constraints = constraints;
445 public Set<AugmentationSchema> getAvailableAugmentations() {
446 return augmentations;
449 private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
450 if (augmentations != null) {
451 this.augmentations = augmentations;
456 public Set<DataSchemaNode> getChildNodes() {
457 return new HashSet<DataSchemaNode>(childNodes.values());
460 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
461 if (childNodes != null) {
462 this.childNodes = childNodes;
467 public Set<GroupingDefinition> getGroupings() {
471 private void setGroupings(Set<GroupingDefinition> groupings) {
472 if (groupings != null) {
473 this.groupings = groupings;
478 public DataSchemaNode getDataChildByName(QName name) {
479 return childNodes.get(name);
483 public DataSchemaNode getDataChildByName(String name) {
484 DataSchemaNode result = null;
485 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
486 if (entry.getKey().getLocalName().equals(name)) {
487 result = entry.getValue();
495 public Set<UsesNode> getUses() {
499 private void setUses(Set<UsesNode> uses) {
506 public boolean isPresenceContainer() {
510 private void setPresenceContainer(boolean presence) {
511 this.presence = presence;
515 public Set<TypeDefinition<?>> getTypeDefinitions() {
516 return typeDefinitions;
519 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
520 if (typeDefinitions != null) {
521 this.typeDefinitions = typeDefinitions;
526 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
530 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
531 if (unknownSchemaNodes != null) {
532 this.unknownNodes = unknownSchemaNodes;
536 public ContainerSchemaNodeBuilder toBuilder() {
537 return ContainerSchemaNodeBuilder.this;
541 public int hashCode() {
542 final int prime = 31;
544 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
545 result = prime * result + ((path == null) ? 0 : path.hashCode());
550 public boolean equals(Object obj) {
557 if (getClass() != obj.getClass()) {
560 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
562 if (other.qname != null) {
565 } else if (!qname.equals(other.qname)) {
569 if (other.path != null) {
572 } else if (!path.equals(other.path)) {
579 public String toString() {
580 StringBuilder sb = new StringBuilder(ContainerSchemaNodeImpl.class.getSimpleName());
582 sb.append("qname=" + qname);
584 return sb.toString();