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.controller.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.controller.yang.common.QName;
18 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
19 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
20 import org.opendaylight.controller.yang.model.api.ChoiceNode;
21 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.controller.yang.model.api.SchemaPath;
23 import org.opendaylight.controller.yang.model.api.Status;
24 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.controller.yang.parser.builder.api.AbstractSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.ConfigNode;
29 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
31 import org.opendaylight.controller.yang.parser.util.Comparators;
32 import org.opendaylight.controller.yang.parser.util.ParserUtils;
34 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
35 AugmentationTargetBuilder, GroupingMember, ConfigNode {
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 int line, final QName qname) {
52 instance = new ChoiceNodeImpl(qname);
53 constraints = new ConstraintsBuilder(line);
56 public ChoiceBuilder(ChoiceBuilder b) {
57 super(b.getLine(), b.getQName());
58 parent = b.getParent();
59 instance = new ChoiceNodeImpl(qname);
60 constraints = 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() {
130 public void addChildNode(DataSchemaNodeBuilder childNode) {
131 if (!(childNode instanceof ChoiceCaseBuilder)) {
132 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(childNode.getLine(), childNode.getQName());
133 if(childNode.isAugmenting()) {
134 caseBuilder.setAugmenting(true);
135 childNode.setAugmenting(false);
137 caseBuilder.setPath(childNode.getPath());
138 SchemaPath newPath = ParserUtils.createSchemaPath(childNode.getPath(), childNode.getQName().getLocalName());
139 childNode.setPath(newPath);
140 caseBuilder.addChildNode(childNode);
141 addedCases.add(caseBuilder);
143 addedCases.add((ChoiceCaseBuilder) childNode);
147 public void setCases(Set<ChoiceCaseNode> cases) {
152 public boolean isAugmenting() {
157 public void setAugmenting(boolean augmenting) {
158 this.augmenting = augmenting;
162 public boolean isAddedByUses() {
167 public void setAddedByUses(final boolean addedByUses) {
168 this.addedByUses = addedByUses;
171 public Boolean isConfiguration() {
172 return configuration;
176 public void setConfiguration(Boolean configuration) {
177 this.configuration = configuration;
181 public ConstraintsBuilder getConstraints() {
185 public Set<AugmentationSchemaBuilder> getAugmentations() {
186 return addedAugmentations;
190 public void addAugmentation(AugmentationSchemaBuilder augment) {
191 addedAugmentations.add(augment);
194 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
195 return addedUnknownNodes;
198 public String getDefaultCase() {
202 public void setDefaultCase(String defaultCase) {
203 this.defaultCase = defaultCase;
207 public int hashCode() {
208 final int prime = 31;
210 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
215 public boolean equals(Object obj) {
222 if (getClass() != obj.getClass()) {
225 ChoiceBuilder other = (ChoiceBuilder) obj;
226 if (schemaPath == null) {
227 if (other.schemaPath != null) {
230 } else if (!schemaPath.equals(other.schemaPath)) {
233 if (parent == null) {
234 if (other.parent != null) {
237 } else if (!parent.equals(other.parent)) {
244 public String toString() {
245 return "choice " + qname.getLocalName();
248 public final class ChoiceNodeImpl implements ChoiceNode {
249 private final QName qname;
250 private SchemaPath path;
251 private String description;
252 private String reference;
253 private Status status = Status.CURRENT;
254 private boolean augmenting;
255 private boolean addedByUses;
256 private boolean configuration;
257 private ConstraintDefinition constraints;
258 private Set<ChoiceCaseNode> cases = Collections.emptySet();
259 private Set<AugmentationSchema> augmentations = Collections.emptySet();
260 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
261 private String defaultCase;
263 private ChoiceNodeImpl(QName qname) {
268 public QName getQName() {
273 public SchemaPath getPath() {
277 private void setPath(SchemaPath path) {
282 public String getDescription() {
286 private void setDescription(String description) {
287 this.description = description;
291 public String getReference() {
295 private void setReference(String reference) {
296 this.reference = reference;
300 public Status getStatus() {
304 private void setStatus(Status status) {
305 if (status != null) {
306 this.status = status;
311 public boolean isAugmenting() {
315 private void setAugmenting(boolean augmenting) {
316 this.augmenting = augmenting;
320 public boolean isAddedByUses() {
324 private void setAddedByUses(boolean addedByUses) {
325 this.addedByUses = addedByUses;
329 public boolean isConfiguration() {
330 return configuration;
333 private void setConfiguration(boolean configuration) {
334 this.configuration = configuration;
338 public ConstraintDefinition getConstraints() {
342 private void setConstraints(ConstraintDefinition constraints) {
343 this.constraints = constraints;
347 public Set<AugmentationSchema> getAvailableAugmentations() {
348 return augmentations;
351 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
352 if (availableAugmentations != null) {
353 this.augmentations = availableAugmentations;
358 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
362 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
363 if (unknownSchemaNodes != null) {
364 this.unknownNodes = unknownSchemaNodes;
369 public Set<ChoiceCaseNode> getCases() {
374 public ChoiceCaseNode getCaseNodeByName(final QName name) {
376 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
378 for (final ChoiceCaseNode caseNode : cases) {
379 if (caseNode != null) {
380 if (name.equals(caseNode.getQName())) {
389 public ChoiceCaseNode getCaseNodeByName(final String name) {
391 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
393 for (final ChoiceCaseNode caseNode : cases) {
394 if (caseNode != null && (caseNode.getQName() != null)) {
395 if (name.equals(caseNode.getQName().getLocalName())) {
403 private void setCases(Set<ChoiceCaseNode> cases) {
410 public String getDefaultCase() {
414 private void setDefaultCase(String defaultCase) {
415 this.defaultCase = defaultCase;
418 public ChoiceBuilder toBuilder() {
419 return ChoiceBuilder.this;
423 public int hashCode() {
424 final int prime = 31;
426 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
427 result = prime * result + ((path == null) ? 0 : path.hashCode());
432 public boolean equals(Object obj) {
439 if (getClass() != obj.getClass()) {
442 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
444 if (other.qname != null) {
447 } else if (!qname.equals(other.qname)) {
451 if (other.path != null) {
454 } else if (!path.equals(other.path)) {
461 public String toString() {
462 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
464 sb.append("qname=" + qname);
466 return sb.toString();