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.DataSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
30 import org.opendaylight.controller.yang.parser.util.Comparators;
31 import org.opendaylight.controller.yang.parser.util.ParserUtils;
33 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
34 AugmentationTargetBuilder, GroupingMember {
35 private boolean isBuilt;
36 private final ChoiceNodeImpl instance;
37 // DataSchemaNode args
38 private boolean augmenting;
39 private boolean addedByUses;
40 private Boolean configuration;
41 private final ConstraintsBuilder constraints;
42 // AugmentationTarget args
43 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
45 private Set<ChoiceCaseNode> cases;
46 private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
47 private String defaultCase;
49 public ChoiceBuilder(final int line, final QName qname) {
51 instance = new ChoiceNodeImpl(qname);
52 constraints = new ConstraintsBuilder(line);
55 public ChoiceBuilder(ChoiceBuilder b) {
56 super(b.getLine(), b.getQName());
57 parent = b.getParent();
58 instance = new ChoiceNodeImpl(qname);
59 constraints = b.getConstraints();
60 schemaPath = b.getPath();
61 description = b.getDescription();
62 reference = b.getReference();
63 status = b.getStatus();
64 unknownNodes = b.unknownNodes;
65 addedUnknownNodes.addAll(b.getUnknownNodes());
66 augmenting = b.isAugmenting();
67 addedByUses = b.isAddedByUses();
68 configuration = b.isConfiguration();
69 addedAugmentations.addAll(b.getAugmentations());
71 addedCases.addAll(b.getCases());
72 defaultCase = b.getDefaultCase();
76 public ChoiceNode build() {
78 instance.setPath(schemaPath);
79 instance.setDescription(description);
80 instance.setReference(reference);
81 instance.setStatus(status);
82 instance.setAugmenting(augmenting);
83 instance.setAddedByUses(addedByUses);
84 instance.setConfiguration(configuration);
85 instance.setConstraints(constraints.build());
86 instance.setDefaultCase(defaultCase);
90 cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
91 for (ChoiceCaseBuilder caseBuilder : addedCases) {
92 cases.add(caseBuilder.build());
95 instance.setCases(cases);
98 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
99 for (AugmentationSchemaBuilder builder : addedAugmentations) {
100 augmentations.add(builder.build());
102 instance.setAvailableAugmentations(augmentations);
105 if (unknownNodes == null) {
106 unknownNodes = new ArrayList<UnknownSchemaNode>();
107 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
108 unknownNodes.add(b.build());
110 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
112 instance.setUnknownSchemaNodes(unknownNodes);
120 public void rebuild() {
125 public Set<ChoiceCaseBuilder> getCases() {
129 public void addChildNode(DataSchemaNodeBuilder childNode) {
130 if (!(childNode instanceof ChoiceCaseBuilder)) {
131 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(childNode.getLine(), childNode.getQName());
132 if (childNode.isAugmenting()) {
133 caseBuilder.setAugmenting(true);
134 childNode.setAugmenting(false);
136 caseBuilder.setPath(childNode.getPath());
137 SchemaPath newPath = ParserUtils.createSchemaPath(childNode.getPath(), childNode.getQName().getLocalName());
138 childNode.setPath(newPath);
139 caseBuilder.addChildNode(childNode);
140 addedCases.add(caseBuilder);
142 addedCases.add((ChoiceCaseBuilder) childNode);
146 public void setCases(Set<ChoiceCaseNode> cases) {
151 public boolean isAugmenting() {
156 public void setAugmenting(boolean augmenting) {
157 this.augmenting = augmenting;
161 public boolean isAddedByUses() {
166 public void setAddedByUses(final boolean addedByUses) {
167 this.addedByUses = addedByUses;
170 public Boolean isConfiguration() {
171 return configuration;
175 public void setConfiguration(Boolean configuration) {
176 this.configuration = configuration;
180 public ConstraintsBuilder getConstraints() {
184 public Set<AugmentationSchemaBuilder> getAugmentations() {
185 return addedAugmentations;
189 public void addAugmentation(AugmentationSchemaBuilder augment) {
190 addedAugmentations.add(augment);
193 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
194 return addedUnknownNodes;
197 public String getDefaultCase() {
201 public void setDefaultCase(String defaultCase) {
202 this.defaultCase = defaultCase;
206 public int hashCode() {
207 final int prime = 31;
209 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
214 public boolean equals(Object obj) {
221 if (getClass() != obj.getClass()) {
224 ChoiceBuilder other = (ChoiceBuilder) obj;
225 if (schemaPath == null) {
226 if (other.schemaPath != null) {
229 } else if (!schemaPath.equals(other.schemaPath)) {
232 if (parent == null) {
233 if (other.parent != null) {
236 } else if (!parent.equals(other.parent)) {
243 public String toString() {
244 return "choice " + qname.getLocalName();
247 public final class ChoiceNodeImpl implements ChoiceNode {
248 private final QName qname;
249 private SchemaPath path;
250 private String description;
251 private String reference;
252 private Status status = Status.CURRENT;
253 private boolean augmenting;
254 private boolean addedByUses;
255 private boolean configuration;
256 private ConstraintDefinition constraints;
257 private Set<ChoiceCaseNode> cases = Collections.emptySet();
258 private Set<AugmentationSchema> augmentations = Collections.emptySet();
259 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
260 private String defaultCase;
262 private ChoiceNodeImpl(QName qname) {
267 public QName getQName() {
272 public SchemaPath getPath() {
276 private void setPath(SchemaPath path) {
281 public String getDescription() {
285 private void setDescription(String description) {
286 this.description = description;
290 public String getReference() {
294 private void setReference(String reference) {
295 this.reference = reference;
299 public Status getStatus() {
303 private void setStatus(Status status) {
304 if (status != null) {
305 this.status = status;
310 public boolean isAugmenting() {
314 private void setAugmenting(boolean augmenting) {
315 this.augmenting = augmenting;
319 public boolean isAddedByUses() {
323 private void setAddedByUses(boolean addedByUses) {
324 this.addedByUses = addedByUses;
328 public boolean isConfiguration() {
329 return configuration;
332 private void setConfiguration(boolean configuration) {
333 this.configuration = configuration;
337 public ConstraintDefinition getConstraints() {
341 private void setConstraints(ConstraintDefinition constraints) {
342 this.constraints = constraints;
346 public Set<AugmentationSchema> getAvailableAugmentations() {
347 return augmentations;
350 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
351 if (availableAugmentations != null) {
352 this.augmentations = availableAugmentations;
357 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
361 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
362 if (unknownSchemaNodes != null) {
363 this.unknownNodes = unknownSchemaNodes;
368 public Set<ChoiceCaseNode> getCases() {
373 public ChoiceCaseNode getCaseNodeByName(final QName name) {
375 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
377 for (final ChoiceCaseNode caseNode : cases) {
378 if (caseNode != null) {
379 if (name.equals(caseNode.getQName())) {
388 public ChoiceCaseNode getCaseNodeByName(final String name) {
390 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
392 for (final ChoiceCaseNode caseNode : cases) {
393 if (caseNode != null && (caseNode.getQName() != null)) {
394 if (name.equals(caseNode.getQName().getLocalName())) {
402 private void setCases(Set<ChoiceCaseNode> cases) {
409 public String getDefaultCase() {
413 private void setDefaultCase(String defaultCase) {
414 this.defaultCase = defaultCase;
417 public ChoiceBuilder toBuilder() {
418 return ChoiceBuilder.this;
422 public int hashCode() {
423 final int prime = 31;
425 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
426 result = prime * result + ((path == null) ? 0 : path.hashCode());
431 public boolean equals(Object obj) {
438 if (getClass() != obj.getClass()) {
441 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
443 if (other.qname != null) {
446 } else if (!qname.equals(other.qname)) {
450 if (other.path != null) {
453 } else if (!path.equals(other.path)) {
460 public String toString() {
461 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
463 sb.append("qname=" + qname);
465 return sb.toString();