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() {
106 public Set<ChoiceCaseBuilder> getCases() {
114 * name of case to search
115 * @return case with given name if present, null otherwise
117 public ChoiceCaseBuilder getCaseNodeByName(String caseName) {
118 for (ChoiceCaseBuilder addedCase : addedCases) {
119 if (addedCase.getQName().getLocalName().equals(caseName)) {
127 * Add case node to this choice.
129 * If node is not declared with 'case' keyword, create new case builder and
130 * make this node child of newly created case.
135 public void addCase(DataSchemaNodeBuilder caseNode) {
136 QName caseQName = caseNode.getQName();
137 String caseName = caseQName.getLocalName();
138 for (ChoiceCaseBuilder addedCase : addedCases) {
139 if (addedCase.getQName().getLocalName().equals(caseName)) {
140 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
141 + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
142 + "': case with same name already declared at line " + addedCase.getLine());
146 if (caseNode instanceof ChoiceCaseBuilder) {
147 addedCases.add((ChoiceCaseBuilder) caseNode);
149 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
151 if (caseNode.isAugmenting()) {
152 // if node is added by augmentation, set case builder augmenting
153 // as true and node augmenting as false
154 caseBuilder.setAugmenting(true);
155 caseNode.setAugmenting(false);
157 caseBuilder.setPath(caseNode.getPath());
158 SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseQName);
159 caseNode.setPath(newPath);
160 caseBuilder.addChildNode(caseNode);
161 addedCases.add(caseBuilder);
165 public void setCases(Set<ChoiceCaseNode> cases) {
170 public boolean isAugmenting() {
175 public void setAugmenting(boolean augmenting) {
176 this.augmenting = augmenting;
180 public boolean isAddedByUses() {
185 public void setAddedByUses(final boolean addedByUses) {
186 this.addedByUses = addedByUses;
189 public Boolean isConfiguration() {
190 return configuration;
194 public void setConfiguration(Boolean configuration) {
195 this.configuration = configuration;
199 public ConstraintsBuilder getConstraints() {
203 public Set<AugmentationSchemaBuilder> getAugmentations() {
204 return addedAugmentations;
208 public void addAugmentation(AugmentationSchemaBuilder augment) {
209 addedAugmentations.add(augment);
212 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
213 return addedUnknownNodes;
216 public String getDefaultCase() {
220 public void setDefaultCase(String defaultCase) {
221 this.defaultCase = defaultCase;
225 public int hashCode() {
226 final int prime = 31;
228 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
233 public boolean equals(Object obj) {
240 if (getClass() != obj.getClass()) {
243 ChoiceBuilder other = (ChoiceBuilder) obj;
244 if (schemaPath == null) {
245 if (other.schemaPath != null) {
248 } else if (!schemaPath.equals(other.schemaPath)) {
251 if (parent == null) {
252 if (other.parent != null) {
255 } else if (!parent.equals(other.parent)) {
262 public String toString() {
263 return "choice " + qname.getLocalName();
266 public final class ChoiceNodeImpl implements ChoiceNode {
267 private final QName qname;
268 private SchemaPath path;
269 private String description;
270 private String reference;
271 private Status status = Status.CURRENT;
272 private boolean augmenting;
273 private boolean addedByUses;
274 private boolean configuration;
275 private ConstraintDefinition constraints;
276 private Set<ChoiceCaseNode> cases = Collections.emptySet();
277 private Set<AugmentationSchema> augmentations = Collections.emptySet();
278 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
279 private String defaultCase;
281 private ChoiceNodeImpl(QName qname) {
286 public QName getQName() {
291 public SchemaPath getPath() {
295 private void setPath(SchemaPath path) {
300 public String getDescription() {
304 private void setDescription(String description) {
305 this.description = description;
309 public String getReference() {
313 private void setReference(String reference) {
314 this.reference = reference;
318 public Status getStatus() {
322 private void setStatus(Status status) {
323 if (status != null) {
324 this.status = status;
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 boolean isConfiguration() {
348 return configuration;
351 private void setConfiguration(boolean configuration) {
352 this.configuration = configuration;
356 public ConstraintDefinition getConstraints() {
360 private void setConstraints(ConstraintDefinition constraints) {
361 this.constraints = constraints;
365 public Set<AugmentationSchema> getAvailableAugmentations() {
366 return augmentations;
369 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
370 if (availableAugmentations != null) {
371 this.augmentations = availableAugmentations;
376 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
380 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
381 if (unknownSchemaNodes != null) {
382 this.unknownNodes = unknownSchemaNodes;
387 public Set<ChoiceCaseNode> getCases() {
392 public ChoiceCaseNode getCaseNodeByName(final QName name) {
394 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
396 for (final ChoiceCaseNode caseNode : cases) {
397 if (caseNode != null && name.equals(caseNode.getQName())) {
405 public ChoiceCaseNode getCaseNodeByName(final String name) {
407 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
409 for (final ChoiceCaseNode caseNode : cases) {
410 if (caseNode != null && (caseNode.getQName() != null)
411 && name.equals(caseNode.getQName().getLocalName())) {
418 private void setCases(Set<ChoiceCaseNode> cases) {
425 public String getDefaultCase() {
429 private void setDefaultCase(String defaultCase) {
430 this.defaultCase = defaultCase;
433 public ChoiceBuilder toBuilder() {
434 return ChoiceBuilder.this;
438 public int hashCode() {
439 final int prime = 31;
441 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
442 result = prime * result + ((path == null) ? 0 : path.hashCode());
447 public boolean equals(Object obj) {
454 if (getClass() != obj.getClass()) {
457 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
459 if (other.qname != null) {
462 } else if (!qname.equals(other.qname)) {
466 if (other.path != null) {
469 } else if (!path.equals(other.path)) {
476 public String toString() {
477 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
479 sb.append("qname=" + qname);
481 return sb.toString();