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;
15 import java.util.TreeSet;
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.ChoiceCaseNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
21 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.YangNode;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.util.Comparators;
31 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
32 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
34 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
35 AugmentationTargetBuilder {
36 private boolean isBuilt;
37 private final ChoiceNodeImpl instance;
38 private YangNode parent;
39 // DataSchemaNode args
40 private boolean augmenting;
41 private boolean addedByUses;
42 private Boolean configuration;
43 private final ConstraintsBuilder constraints;
44 // AugmentationTarget args
45 private final List<AugmentationSchema> augmentations = new ArrayList<>();
46 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
48 private Set<ChoiceCaseNode> cases = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
49 private final Set<ChoiceCaseBuilder> caseBuilders = new HashSet<>();
50 private String defaultCase;
52 public ChoiceBuilder(final String moduleName, final int line, final QName qname) {
53 super(moduleName, line, qname);
54 instance = new ChoiceNodeImpl(qname);
55 constraints = new ConstraintsBuilder(moduleName, line);
59 public ChoiceNode build(YangNode parent) {
62 instance.setParent(parent);
63 instance.setPath(schemaPath);
64 instance.setDescription(description);
65 instance.setReference(reference);
66 instance.setStatus(status);
67 instance.setAugmenting(augmenting);
68 instance.setAddedByUses(addedByUses);
69 instance.setConfiguration(configuration);
70 instance.setConstraints(constraints.build());
71 instance.setDefaultCase(defaultCase);
74 for (ChoiceCaseBuilder caseBuilder : caseBuilders) {
75 cases.add(caseBuilder.build(instance));
77 instance.setCases(cases);
80 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
81 augmentations.add(builder.build(instance));
83 instance.setAvailableAugmentations(new HashSet<>(augmentations));
86 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
87 unknownNodes.add(b.build(instance));
89 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
90 instance.setUnknownSchemaNodes(unknownNodes);
98 public void rebuild() {
104 public void setQName(QName qname) {
106 instance.setQName(qname);
109 public Set<ChoiceCaseBuilder> getCases() {
117 * name of case to search
118 * @return case with given name if present, null otherwise
120 public ChoiceCaseBuilder getCaseNodeByName(String caseName) {
121 for (ChoiceCaseBuilder addedCase : caseBuilders) {
122 if (addedCase.getQName().getLocalName().equals(caseName)) {
130 * Add case node to this choice.
132 * If node is not declared with 'case' keyword, create new case builder and
133 * make this node child of newly created case.
138 public void addCase(DataSchemaNodeBuilder caseNode) {
139 QName caseQName = caseNode.getQName();
140 String caseName = caseQName.getLocalName();
142 for (ChoiceCaseBuilder addedCase : caseBuilders) {
143 if (addedCase.getQName().getLocalName().equals(caseName)) {
144 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
145 + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
146 + "': case with same name already declared at line " + addedCase.getLine());
150 if (caseNode instanceof ChoiceCaseBuilder) {
151 caseBuilders.add((ChoiceCaseBuilder) caseNode);
153 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
155 if (caseNode.isAugmenting()) {
156 // if node is added by augmentation, set case builder augmenting
157 // as true and node augmenting as false
158 caseBuilder.setAugmenting(true);
159 caseNode.setAugmenting(false);
161 caseBuilder.setPath(caseNode.getPath());
162 SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseQName);
163 caseNode.setPath(newPath);
164 caseBuilder.addChildNode(caseNode);
165 caseBuilders.add(caseBuilder);
169 public void setCases(Set<ChoiceCaseNode> cases) {
174 public boolean isAugmenting() {
179 public void setAugmenting(boolean augmenting) {
180 this.augmenting = augmenting;
184 public boolean isAddedByUses() {
189 public void setAddedByUses(final boolean addedByUses) {
190 this.addedByUses = addedByUses;
193 public Boolean isConfiguration() {
194 return configuration;
198 public void setConfiguration(Boolean configuration) {
199 this.configuration = configuration;
203 public ConstraintsBuilder getConstraints() {
208 public void addAugmentation(AugmentationSchemaBuilder augment) {
209 augmentationBuilders.add(augment);
212 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
213 return augmentationBuilders;
216 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
217 return addedUnknownNodes;
220 public String getDefaultCase() {
224 public void setDefaultCase(String defaultCase) {
225 this.defaultCase = defaultCase;
229 public int hashCode() {
230 final int prime = 31;
232 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
237 public boolean equals(Object obj) {
244 if (getClass() != obj.getClass()) {
247 ChoiceBuilder other = (ChoiceBuilder) obj;
248 if (schemaPath == null) {
249 if (other.schemaPath != null) {
252 } else if (!schemaPath.equals(other.schemaPath)) {
255 if (parentBuilder == null) {
256 if (other.parentBuilder != null) {
259 } else if (!parentBuilder.equals(other.parentBuilder)) {
266 public String toString() {
267 return "choice " + qname.getLocalName();
270 public final class ChoiceNodeImpl implements ChoiceNode {
272 private SchemaPath path;
273 private YangNode parent;
274 private String description;
275 private String reference;
276 private Status status = Status.CURRENT;
277 private boolean augmenting;
278 private boolean addedByUses;
279 private boolean configuration;
280 private ConstraintDefinition constraints;
281 private Set<ChoiceCaseNode> cases = Collections.emptySet();
282 private Set<AugmentationSchema> augmentations = Collections.emptySet();
283 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
284 private String defaultCase;
286 private ChoiceNodeImpl(QName qname) {
291 public QName getQName() {
295 private void setQName(QName qname) {
300 public SchemaPath getPath() {
304 private void setPath(SchemaPath path) {
309 public YangNode getParent() {
313 private void setParent(YangNode parent) {
314 this.parent = parent;
318 public String getDescription() {
322 private void setDescription(String description) {
323 this.description = description;
327 public String getReference() {
331 private void setReference(String reference) {
332 this.reference = reference;
336 public Status getStatus() {
340 private void setStatus(Status status) {
341 if (status != null) {
342 this.status = status;
347 public boolean isAugmenting() {
351 private void setAugmenting(boolean augmenting) {
352 this.augmenting = augmenting;
356 public boolean isAddedByUses() {
360 private void setAddedByUses(boolean addedByUses) {
361 this.addedByUses = addedByUses;
365 public boolean isConfiguration() {
366 return configuration;
369 private void setConfiguration(boolean configuration) {
370 this.configuration = configuration;
374 public ConstraintDefinition getConstraints() {
378 private void setConstraints(ConstraintDefinition constraints) {
379 this.constraints = constraints;
383 public Set<AugmentationSchema> getAvailableAugmentations() {
384 return augmentations;
387 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
388 if (availableAugmentations != null) {
389 this.augmentations = availableAugmentations;
394 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
398 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
399 if (unknownSchemaNodes != null) {
400 this.unknownNodes = unknownSchemaNodes;
405 public Set<ChoiceCaseNode> getCases() {
410 public ChoiceCaseNode getCaseNodeByName(final QName name) {
412 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
414 for (final ChoiceCaseNode caseNode : cases) {
415 if (caseNode != null && name.equals(caseNode.getQName())) {
423 public ChoiceCaseNode getCaseNodeByName(final String name) {
425 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
427 for (final ChoiceCaseNode caseNode : cases) {
428 if (caseNode != null && (caseNode.getQName() != null)
429 && name.equals(caseNode.getQName().getLocalName())) {
436 private void setCases(Set<ChoiceCaseNode> cases) {
443 public String getDefaultCase() {
447 private void setDefaultCase(String defaultCase) {
448 this.defaultCase = defaultCase;
451 public ChoiceBuilder toBuilder() {
452 return ChoiceBuilder.this;
456 public int hashCode() {
457 final int prime = 31;
459 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
460 result = prime * result + ((path == null) ? 0 : path.hashCode());
465 public boolean equals(Object obj) {
472 if (getClass() != obj.getClass()) {
475 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
477 if (other.qname != null) {
480 } else if (!qname.equals(other.qname)) {
484 if (other.path != null) {
487 } else if (!path.equals(other.path)) {
494 public String toString() {
495 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
497 sb.append("qname=" + qname);
499 return sb.toString();