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);
57 public ChoiceNode build() {
59 instance.setPath(schemaPath);
60 instance.setDescription(description);
61 instance.setReference(reference);
62 instance.setStatus(status);
63 instance.setAugmenting(augmenting);
64 instance.setAddedByUses(addedByUses);
65 instance.setConfiguration(configuration);
66 instance.setConstraints(constraints.build());
67 instance.setDefaultCase(defaultCase);
71 cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
72 for (ChoiceCaseBuilder caseBuilder : addedCases) {
73 cases.add(caseBuilder.build());
76 instance.setCases(cases);
79 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
80 for (AugmentationSchemaBuilder builder : addedAugmentations) {
81 augmentations.add(builder.build());
83 instance.setAvailableAugmentations(augmentations);
86 if (unknownNodes == null) {
87 unknownNodes = new ArrayList<UnknownSchemaNode>();
88 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
89 unknownNodes.add(b.build());
91 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
93 instance.setUnknownSchemaNodes(unknownNodes);
101 public void rebuild() {
107 public void setQName(QName qname) {
111 public Set<ChoiceCaseBuilder> getCases() {
119 * name of case to search
120 * @return case with given name if present, null otherwise
122 public ChoiceCaseBuilder getCaseNodeByName(String caseName) {
123 for (ChoiceCaseBuilder addedCase : addedCases) {
124 if (addedCase.getQName().getLocalName().equals(caseName)) {
132 * Add case node to this choice.
134 * If node is not declared with 'case' keyword, create new case builder and
135 * make this node child of newly created case.
140 public void addCase(DataSchemaNodeBuilder caseNode) {
141 QName caseQName = caseNode.getQName();
142 String caseName = caseQName.getLocalName();
143 for (ChoiceCaseBuilder addedCase : addedCases) {
144 if (addedCase.getQName().getLocalName().equals(caseName)) {
145 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
146 + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
147 + "': case with same name already declared at line " + addedCase.getLine());
151 if (caseNode instanceof ChoiceCaseBuilder) {
152 addedCases.add((ChoiceCaseBuilder) caseNode);
154 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
156 if (caseNode.isAugmenting()) {
157 // if node is added by augmentation, set case builder augmenting
158 // as true and node augmenting as false
159 caseBuilder.setAugmenting(true);
160 caseNode.setAugmenting(false);
162 caseBuilder.setPath(caseNode.getPath());
163 SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseQName);
164 caseNode.setPath(newPath);
165 caseBuilder.addChildNode(caseNode);
166 addedCases.add(caseBuilder);
170 public void setCases(Set<ChoiceCaseNode> cases) {
175 public boolean isAugmenting() {
180 public void setAugmenting(boolean augmenting) {
181 this.augmenting = augmenting;
185 public boolean isAddedByUses() {
190 public void setAddedByUses(final boolean addedByUses) {
191 this.addedByUses = addedByUses;
194 public Boolean isConfiguration() {
195 return configuration;
199 public void setConfiguration(Boolean configuration) {
200 this.configuration = configuration;
204 public ConstraintsBuilder getConstraints() {
208 public Set<AugmentationSchemaBuilder> getAugmentations() {
209 return addedAugmentations;
213 public void addAugmentation(AugmentationSchemaBuilder augment) {
214 addedAugmentations.add(augment);
217 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
218 return addedUnknownNodes;
221 public String getDefaultCase() {
225 public void setDefaultCase(String defaultCase) {
226 this.defaultCase = defaultCase;
230 public int hashCode() {
231 final int prime = 31;
233 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
238 public boolean equals(Object obj) {
245 if (getClass() != obj.getClass()) {
248 ChoiceBuilder other = (ChoiceBuilder) obj;
249 if (schemaPath == null) {
250 if (other.schemaPath != null) {
253 } else if (!schemaPath.equals(other.schemaPath)) {
256 if (parent == null) {
257 if (other.parent != null) {
260 } else if (!parent.equals(other.parent)) {
267 public String toString() {
268 return "choice " + qname.getLocalName();
271 public final class ChoiceNodeImpl implements ChoiceNode {
272 private final QName qname;
273 private SchemaPath path;
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() {
296 public SchemaPath getPath() {
300 private void setPath(SchemaPath path) {
305 public String getDescription() {
309 private void setDescription(String description) {
310 this.description = description;
314 public String getReference() {
318 private void setReference(String reference) {
319 this.reference = reference;
323 public Status getStatus() {
327 private void setStatus(Status status) {
328 if (status != null) {
329 this.status = status;
334 public boolean isAugmenting() {
338 private void setAugmenting(boolean augmenting) {
339 this.augmenting = augmenting;
343 public boolean isAddedByUses() {
347 private void setAddedByUses(boolean addedByUses) {
348 this.addedByUses = addedByUses;
352 public boolean isConfiguration() {
353 return configuration;
356 private void setConfiguration(boolean configuration) {
357 this.configuration = configuration;
361 public ConstraintDefinition getConstraints() {
365 private void setConstraints(ConstraintDefinition constraints) {
366 this.constraints = constraints;
370 public Set<AugmentationSchema> getAvailableAugmentations() {
371 return augmentations;
374 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
375 if (availableAugmentations != null) {
376 this.augmentations = availableAugmentations;
381 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
385 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
386 if (unknownSchemaNodes != null) {
387 this.unknownNodes = unknownSchemaNodes;
392 public Set<ChoiceCaseNode> getCases() {
397 public ChoiceCaseNode getCaseNodeByName(final QName name) {
399 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
401 for (final ChoiceCaseNode caseNode : cases) {
402 if (caseNode != null && name.equals(caseNode.getQName())) {
410 public ChoiceCaseNode getCaseNodeByName(final String name) {
412 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
414 for (final ChoiceCaseNode caseNode : cases) {
415 if (caseNode != null && (caseNode.getQName() != null)
416 && name.equals(caseNode.getQName().getLocalName())) {
423 private void setCases(Set<ChoiceCaseNode> cases) {
430 public String getDefaultCase() {
434 private void setDefaultCase(String defaultCase) {
435 this.defaultCase = defaultCase;
438 public ChoiceBuilder toBuilder() {
439 return ChoiceBuilder.this;
443 public int hashCode() {
444 final int prime = 31;
446 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
447 result = prime * result + ((path == null) ? 0 : path.hashCode());
452 public boolean equals(Object obj) {
459 if (getClass() != obj.getClass()) {
462 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
464 if (other.qname != null) {
467 } else if (!qname.equals(other.qname)) {
471 if (other.path != null) {
474 } else if (!path.equals(other.path)) {
481 public String toString() {
482 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
484 sb.append("qname=" + qname);
486 return sb.toString();