1 package org.opendaylight.yangtools.yang.parser.builder.impl;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
10 import org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
12 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.UsesNode;
21 import org.opendaylight.yangtools.yang.model.api.YangNode;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.util.Comparators;
29 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
31 public final class ChoiceCaseBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
32 AugmentationTargetBuilder {
33 private boolean isBuilt;
34 private final ChoiceCaseNodeImpl instance;
35 private YangNode parent;
37 private SchemaPath schemaPath;
38 private String description;
39 private String reference;
40 private Status status = Status.CURRENT;
41 // DataSchemaNode args
42 private boolean augmenting;
43 private boolean addedByUses;
44 private final ConstraintsBuilder constraints;
45 // AugmentationTarget args
46 private final List<AugmentationSchema> augmentations = new ArrayList<>();
47 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
49 public ChoiceCaseBuilder(final String moduleName, final int line, final QName qname) {
50 super(moduleName, line, qname);
51 instance = new ChoiceCaseNodeImpl(qname);
52 constraints = new ConstraintsBuilder(moduleName, line);
56 public ChoiceCaseNode build(YangNode parent) {
59 instance.setParent(parent);
60 instance.setConstraints(constraints.build());
61 instance.setPath(schemaPath);
62 instance.setDescription(description);
63 instance.setReference(reference);
64 instance.setStatus(status);
65 instance.setAugmenting(augmenting);
66 instance.setAddedByUses(addedByUses);
69 for (DataSchemaNodeBuilder node : addedChildNodes) {
70 DataSchemaNode child = node.build(instance);
71 childNodes.put(child.getQName(), child);
73 instance.setChildNodes(childNodes);
76 for (UsesNodeBuilder builder : addedUsesNodes) {
77 usesNodes.add(builder.build(instance));
79 instance.setUses(usesNodes);
82 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
83 unknownNodes.add(b.build(instance));
85 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
86 instance.setUnknownSchemaNodes(unknownNodes);
89 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
90 augmentations.add(builder.build(instance));
92 instance.setAvailableAugmentations(new HashSet<>(augmentations));
101 public void rebuild() {
107 public void setQName(QName qname) {
112 public SchemaPath getPath() {
117 public void setPath(final SchemaPath schemaPath) {
118 this.schemaPath = schemaPath;
119 instance.setPath(schemaPath);
122 public String getDescription() {
127 public void setDescription(final String description) {
128 this.description = description;
131 public String getReference() {
136 public void setReference(String reference) {
137 this.reference = reference;
140 public Status getStatus() {
145 public void setStatus(Status status) {
146 if (status != null) {
147 this.status = status;
152 public boolean isAugmenting() {
157 public void setAugmenting(boolean augmenting) {
158 this.augmenting = augmenting;
162 public boolean isAddedByUses() {
167 public void setAddedByUses(boolean addedByUses) {
168 this.addedByUses = addedByUses;
172 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
173 return Collections.emptySet();
177 public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
178 throw new YangParseException(moduleName, line, "Can not add type definition to choice case.");
182 public Boolean isConfiguration() {
187 public void setConfiguration(final Boolean configuration) {
188 throw new YangParseException(moduleName, line, "Can not add config statement to choice case.");
192 public ConstraintsBuilder getConstraints() {
197 public void addAugmentation(AugmentationSchemaBuilder augment) {
198 augmentationBuilders.add(augment);
202 public int hashCode() {
203 final int prime = 31;
205 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
210 public boolean equals(Object obj) {
217 if (getClass() != obj.getClass()) {
220 ChoiceCaseBuilder other = (ChoiceCaseBuilder) obj;
221 if (schemaPath == null) {
222 if (other.schemaPath != null) {
225 } else if (!schemaPath.equals(other.schemaPath)) {
228 if (parentBuilder == null) {
229 if (other.parentBuilder != null) {
232 } else if (!parentBuilder.equals(other.parentBuilder)) {
239 public String toString() {
240 return "case " + getQName().getLocalName();
243 public final class ChoiceCaseNodeImpl implements ChoiceCaseNode {
244 private final QName qname;
245 private SchemaPath path;
246 private YangNode parent;
247 private String description;
248 private String reference;
249 private Status status = Status.CURRENT;
250 private boolean augmenting;
251 private boolean addedByUses;
252 private ConstraintDefinition constraints;
253 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
254 private Set<AugmentationSchema> augmentations = Collections.emptySet();
255 private Set<UsesNode> uses = Collections.emptySet();
256 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
258 private ChoiceCaseNodeImpl(QName qname) {
263 public QName getQName() {
268 public SchemaPath getPath() {
272 private void setPath(SchemaPath path) {
277 public YangNode getParent() {
281 private void setParent(YangNode parent) {
282 this.parent = parent;
286 public String getDescription() {
290 private void setDescription(String description) {
291 this.description = description;
295 public String getReference() {
299 private void setReference(String reference) {
300 this.reference = reference;
304 public Status getStatus() {
308 private void setStatus(Status status) {
309 if (status != null) {
310 this.status = status;
315 public boolean isConfiguration() {
320 public ConstraintDefinition getConstraints() {
324 private void setConstraints(ConstraintDefinition constraints) {
325 this.constraints = constraints;
329 public boolean isAugmenting() {
333 private void setAugmenting(boolean augmenting) {
334 this.augmenting = augmenting;
338 public boolean isAddedByUses() {
342 private void setAddedByUses(boolean addedByUses) {
343 this.addedByUses = addedByUses;
347 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
351 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
352 if (unknownNodes != null) {
353 this.unknownNodes = unknownNodes;
358 * Always returns an empty set, because case node can not contains type
362 public Set<TypeDefinition<?>> getTypeDefinitions() {
363 return Collections.emptySet();
367 public Set<DataSchemaNode> getChildNodes() {
368 return new HashSet<>(childNodes.values());
371 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
372 if (childNodes != null) {
373 this.childNodes = childNodes;
378 public Set<GroupingDefinition> getGroupings() {
379 return Collections.emptySet();
383 public DataSchemaNode getDataChildByName(QName name) {
384 return childNodes.get(name);
388 public DataSchemaNode getDataChildByName(String name) {
389 DataSchemaNode result = null;
390 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
391 if (entry.getKey().getLocalName().equals(name)) {
392 result = entry.getValue();
400 public Set<UsesNode> getUses() {
404 private void setUses(Set<UsesNode> uses) {
411 public Set<AugmentationSchema> getAvailableAugmentations() {
412 return augmentations;
415 private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
416 if (augmentations != null) {
417 this.augmentations = augmentations;
421 public ChoiceCaseBuilder toBuilder() {
422 return ChoiceCaseBuilder.this;
426 public int hashCode() {
427 final int prime = 31;
429 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
430 result = prime * result + ((path == null) ? 0 : path.hashCode());
435 public boolean equals(Object obj) {
442 if (getClass() != obj.getClass()) {
445 ChoiceCaseNodeImpl other = (ChoiceCaseNodeImpl) obj;
447 if (other.qname != null) {
450 } else if (!qname.equals(other.qname)) {
454 if (other.path != null) {
457 } else if (!path.equals(other.path)) {
464 public String toString() {
465 StringBuilder sb = new StringBuilder(ChoiceCaseNodeImpl.class.getSimpleName());
470 return sb.toString();