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;
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;
39 public final class ContainerSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements
40 AugmentationTargetBuilder, DataSchemaNodeBuilder {
41 private boolean isBuilt;
42 private final ContainerSchemaNodeImpl instance;
43 private YangNode parent;
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;
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);
70 public ContainerSchemaNode build(YangNode 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);
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;
87 instance.setConfiguration(configuration);
90 for (UsesNodeBuilder builder : addedUsesNodes) {
91 usesNodes.add(builder.build(instance));
93 instance.setUses(usesNodes);
96 for (DataSchemaNodeBuilder node : addedChildNodes) {
97 DataSchemaNode child = node.build(instance);
98 childNodes.put(child.getQName(), node.build(instance));
100 instance.setChildNodes(childNodes);
103 for (GroupingBuilder builder : addedGroupings) {
104 groupings.add(builder.build(instance));
106 instance.setGroupings(groupings);
109 for (TypeDefinitionBuilder entry : addedTypedefs) {
110 typedefs.add(entry.build(instance));
112 instance.setTypeDefinitions(typedefs);
115 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
116 augmentations.add(builder.build(instance));
118 instance.setAvailableAugmentations(new HashSet<>(augmentations));
121 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
122 unknownNodes.add(b.build(instance));
124 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
125 instance.setUnknownSchemaNodes(unknownNodes);
127 instance.setConstraints(constraints.build());
135 public void rebuild() {
141 public void setQName(QName qname) {
146 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
147 return addedTypedefs;
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());
159 addedTypedefs.add(type);
162 public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
163 this.typedefs = typedefs;
166 public List<AugmentationSchema> getAugmentations() {
167 return augmentations;
170 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
171 return augmentationBuilders;
175 public void addAugmentation(AugmentationSchemaBuilder augment) {
176 augmentationBuilders.add(augment);
177 System.out.println();
180 public SchemaPath getPath() {
185 public void setPath(final SchemaPath schemaPath) {
186 this.schemaPath = schemaPath;
190 public String getDescription() {
195 public void setDescription(final String description) {
196 this.description = description;
200 public String getReference() {
205 public void setReference(String reference) {
206 this.reference = reference;
210 public Status getStatus() {
215 public void setStatus(Status status) {
216 if (status != null) {
217 this.status = status;
222 public boolean isAugmenting() {
227 public void setAugmenting(boolean augmenting) {
228 this.augmenting = augmenting;
232 public boolean isAddedByUses() {
237 public void setAddedByUses(final boolean addedByUses) {
238 this.addedByUses = addedByUses;
242 public Boolean isConfiguration() {
243 return configuration;
247 public void setConfiguration(Boolean configuration) {
248 this.configuration = configuration;
252 public ConstraintsBuilder getConstraints() {
256 public boolean isPresence() {
260 public void setPresence(boolean presence) {
261 this.presence = presence;
265 public int hashCode() {
266 final int prime = 31;
268 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
273 public boolean equals(Object obj) {
280 if (getClass() != obj.getClass()) {
283 ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
284 if (schemaPath == null) {
285 if (other.schemaPath != null) {
288 } else if (!schemaPath.equals(other.schemaPath)) {
291 if (parent == null) {
292 if (other.parent != null) {
295 } else if (!parent.equals(other.parent)) {
302 public String toString() {
303 return "container " + qname.getLocalName();
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;
325 private ContainerSchemaNodeImpl(QName qname) {
330 public QName getQName() {
335 public SchemaPath getPath() {
339 private void setPath(SchemaPath path) {
344 public YangNode getParent() {
348 private void setParent(YangNode parent) {
349 this.parent = parent;
353 public String getDescription() {
357 private void setDescription(String description) {
358 this.description = description;
362 public String getReference() {
366 private void setReference(String reference) {
367 this.reference = reference;
371 public Status getStatus() {
375 private void setStatus(Status status) {
376 if (status != null) {
377 this.status = status;
382 public boolean isAugmenting() {
386 private void setAugmenting(boolean augmenting) {
387 this.augmenting = augmenting;
391 public boolean isAddedByUses() {
395 private void setAddedByUses(boolean addedByUses) {
396 this.addedByUses = addedByUses;
400 public boolean isConfiguration() {
401 return configuration;
404 private void setConfiguration(boolean configuration) {
405 this.configuration = configuration;
409 public ConstraintDefinition getConstraints() {
413 private void setConstraints(ConstraintDefinition constraints) {
414 this.constraints = constraints;
418 public Set<AugmentationSchema> getAvailableAugmentations() {
419 return augmentations;
422 private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
423 if (augmentations != null) {
424 this.augmentations = augmentations;
429 public Set<DataSchemaNode> getChildNodes() {
430 return new HashSet<>(childNodes.values());
433 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
434 if (childNodes != null) {
435 this.childNodes = childNodes;
440 public Set<GroupingDefinition> getGroupings() {
444 private void setGroupings(Set<GroupingDefinition> groupings) {
445 if (groupings != null) {
446 this.groupings = groupings;
451 public DataSchemaNode getDataChildByName(QName name) {
452 return childNodes.get(name);
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();
468 public Set<UsesNode> getUses() {
472 private void setUses(Set<UsesNode> uses) {
479 public boolean isPresenceContainer() {
483 private void setPresenceContainer(boolean presence) {
484 this.presence = presence;
488 public Set<TypeDefinition<?>> getTypeDefinitions() {
489 return typeDefinitions;
492 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
493 if (typeDefinitions != null) {
494 this.typeDefinitions = typeDefinitions;
499 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
503 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
504 if (unknownSchemaNodes != null) {
505 this.unknownNodes = unknownSchemaNodes;
509 public ContainerSchemaNodeBuilder toBuilder() {
510 return ContainerSchemaNodeBuilder.this;
514 public int hashCode() {
515 final int prime = 31;
517 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
518 result = prime * result + ((path == null) ? 0 : path.hashCode());
523 public boolean equals(Object obj) {
530 if (getClass() != obj.getClass()) {
533 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
535 if (other.qname != null) {
538 } else if (!qname.equals(other.qname)) {
542 if (other.path != null) {
545 } else if (!path.equals(other.path)) {
552 public String toString() {
553 StringBuilder sb = new StringBuilder(ContainerSchemaNodeImpl.class.getSimpleName());
558 return sb.toString();