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.parser.builder.api.AbstractSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingMember;
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, GroupingMember {
36 private boolean isBuilt;
37 private final ChoiceNodeImpl instance;
38 // DataSchemaNode args
39 private boolean augmenting;
40 private boolean addedByUses;
41 private Boolean configuration;
42 private final ConstraintsBuilder constraints;
43 // AugmentationTarget args
44 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
46 private Set<ChoiceCaseNode> cases;
47 private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
48 private String defaultCase;
50 public ChoiceBuilder(final String moduleName, final int line, final QName qname) {
51 super(moduleName, line, qname);
52 instance = new ChoiceNodeImpl(qname);
53 constraints = new ConstraintsBuilder(moduleName, line);
56 public ChoiceBuilder(ChoiceBuilder b, QName qname) {
57 super(b.getModuleName(), b.getLine(), qname);
58 parent = b.getParent();
59 instance = new ChoiceNodeImpl(qname);
60 constraints = new ConstraintsBuilder(b.getConstraints());
61 schemaPath = b.getPath();
62 description = b.getDescription();
63 reference = b.getReference();
64 status = b.getStatus();
65 unknownNodes = b.unknownNodes;
66 addedUnknownNodes.addAll(b.getUnknownNodes());
67 augmenting = b.isAugmenting();
68 addedByUses = b.isAddedByUses();
69 configuration = b.isConfiguration();
70 addedAugmentations.addAll(b.getAugmentations());
72 addedCases.addAll(b.getCases());
73 defaultCase = b.getDefaultCase();
77 public ChoiceNode build() {
79 instance.setPath(schemaPath);
80 instance.setDescription(description);
81 instance.setReference(reference);
82 instance.setStatus(status);
83 instance.setAugmenting(augmenting);
84 instance.setAddedByUses(addedByUses);
85 instance.setConfiguration(configuration);
86 instance.setConstraints(constraints.build());
87 instance.setDefaultCase(defaultCase);
91 cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
92 for (ChoiceCaseBuilder caseBuilder : addedCases) {
93 cases.add(caseBuilder.build());
96 instance.setCases(cases);
99 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
100 for (AugmentationSchemaBuilder builder : addedAugmentations) {
101 augmentations.add(builder.build());
103 instance.setAvailableAugmentations(augmentations);
106 if (unknownNodes == null) {
107 unknownNodes = new ArrayList<UnknownSchemaNode>();
108 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
109 unknownNodes.add(b.build());
111 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
113 instance.setUnknownSchemaNodes(unknownNodes);
121 public void rebuild() {
126 public Set<ChoiceCaseBuilder> getCases() {
131 * Add case node to this choice.
133 * If node is not declared with 'case' keyword, create new case builder and
134 * make this node child of newly created case.
139 public void addCase(DataSchemaNodeBuilder caseNode) {
140 QName caseQName = caseNode.getQName();
141 String caseName = caseQName.getLocalName();
142 for (ChoiceCaseBuilder addedCase : addedCases) {
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 addedCases.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 addedCases.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() {
207 public Set<AugmentationSchemaBuilder> getAugmentations() {
208 return addedAugmentations;
212 public void addAugmentation(AugmentationSchemaBuilder augment) {
213 addedAugmentations.add(augment);
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 (parent == null) {
256 if (other.parent != null) {
259 } else if (!parent.equals(other.parent)) {
266 public String toString() {
267 return "choice " + qname.getLocalName();
270 public final class ChoiceNodeImpl implements ChoiceNode {
271 private final QName qname;
272 private SchemaPath path;
273 private String description;
274 private String reference;
275 private Status status = Status.CURRENT;
276 private boolean augmenting;
277 private boolean addedByUses;
278 private boolean configuration;
279 private ConstraintDefinition constraints;
280 private Set<ChoiceCaseNode> cases = Collections.emptySet();
281 private Set<AugmentationSchema> augmentations = Collections.emptySet();
282 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
283 private String defaultCase;
285 private ChoiceNodeImpl(QName qname) {
290 public QName getQName() {
295 public SchemaPath getPath() {
299 private void setPath(SchemaPath path) {
304 public String getDescription() {
308 private void setDescription(String description) {
309 this.description = description;
313 public String getReference() {
317 private void setReference(String reference) {
318 this.reference = reference;
322 public Status getStatus() {
326 private void setStatus(Status status) {
327 if (status != null) {
328 this.status = status;
333 public boolean isAugmenting() {
337 private void setAugmenting(boolean augmenting) {
338 this.augmenting = augmenting;
342 public boolean isAddedByUses() {
346 private void setAddedByUses(boolean addedByUses) {
347 this.addedByUses = addedByUses;
351 public boolean isConfiguration() {
352 return configuration;
355 private void setConfiguration(boolean configuration) {
356 this.configuration = configuration;
360 public ConstraintDefinition getConstraints() {
364 private void setConstraints(ConstraintDefinition constraints) {
365 this.constraints = constraints;
369 public Set<AugmentationSchema> getAvailableAugmentations() {
370 return augmentations;
373 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
374 if (availableAugmentations != null) {
375 this.augmentations = availableAugmentations;
380 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
384 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
385 if (unknownSchemaNodes != null) {
386 this.unknownNodes = unknownSchemaNodes;
391 public Set<ChoiceCaseNode> getCases() {
396 public ChoiceCaseNode getCaseNodeByName(final QName name) {
398 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
400 for (final ChoiceCaseNode caseNode : cases) {
401 if (caseNode != null) {
402 if (name.equals(caseNode.getQName())) {
411 public ChoiceCaseNode getCaseNodeByName(final String name) {
413 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
415 for (final ChoiceCaseNode caseNode : cases) {
416 if (caseNode != null && (caseNode.getQName() != null)) {
417 if (name.equals(caseNode.getQName().getLocalName())) {
425 private void setCases(Set<ChoiceCaseNode> cases) {
432 public String getDefaultCase() {
436 private void setDefaultCase(String defaultCase) {
437 this.defaultCase = defaultCase;
440 public ChoiceBuilder toBuilder() {
441 return ChoiceBuilder.this;
445 public int hashCode() {
446 final int prime = 31;
448 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
449 result = prime * result + ((path == null) ? 0 : path.hashCode());
454 public boolean equals(Object obj) {
461 if (getClass() != obj.getClass()) {
464 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
466 if (other.qname != null) {
469 } else if (!qname.equals(other.qname)) {
473 if (other.path != null) {
476 } else if (!path.equals(other.path)) {
483 public String toString() {
484 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
486 sb.append("qname=" + qname);
488 return sb.toString();