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;
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.controller.yang.model.api.ChoiceNode;
20 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
24 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
26 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
28 public final class ChoiceBuilder implements DataSchemaNodeBuilder, AugmentationTargetBuilder {
29 private boolean built;
30 private final ChoiceNodeImpl instance;
31 private final int line;
33 private final QName qname;
34 private SchemaPath schemaPath;
35 private String description;
36 private String reference;
37 private Status status = Status.CURRENT;
38 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
39 // DataSchemaNode args
40 private boolean augmenting;
41 private boolean configuration;
42 private final ConstraintsBuilder constraints;
43 // AugmentationTarget args
44 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
46 private final Set<ChoiceCaseBuilder> cases = new HashSet<ChoiceCaseBuilder>();
47 private String defaultCase;
49 public ChoiceBuilder(final QName qname, final int line) {
52 instance = new ChoiceNodeImpl(qname);
53 constraints = new ConstraintsBuilder(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.setConfiguration(configuration);
65 instance.setConstraints(constraints.build());
66 instance.setDefaultCase(defaultCase);
69 final Set<ChoiceCaseNode> choiceCases = new HashSet<ChoiceCaseNode>();
70 for (ChoiceCaseBuilder caseBuilder : cases) {
71 choiceCases.add(caseBuilder.build());
73 instance.setCases(choiceCases);
76 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
77 for (AugmentationSchemaBuilder builder : addedAugmentations) {
78 augmentations.add(builder.build());
80 instance.setAvailableAugmentations(augmentations);
83 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
84 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
85 unknownNodes.add(b.build());
87 instance.setUnknownSchemaNodes(unknownNodes);
95 public int getLine() {
99 public Set<ChoiceCaseBuilder> getCases() {
103 public void addChildNode(DataSchemaNodeBuilder childNode) {
104 if (!(childNode instanceof ChoiceCaseBuilder)) {
105 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(
106 childNode.getQName(), childNode.getLine());
107 caseBuilder.addChildNode(childNode);
108 cases.add(caseBuilder);
110 cases.add((ChoiceCaseBuilder) childNode);
115 public QName getQName() {
119 public SchemaPath getPath() {
124 public void setPath(final SchemaPath schemaPath) {
125 this.schemaPath = schemaPath;
128 public String getDescription() {
133 public void setDescription(final String description) {
134 this.description = description;
137 public String getReference() {
142 public void setReference(String reference) {
143 this.reference = reference;
146 public Status getStatus() {
151 public void setStatus(Status status) {
152 if (status != null) {
153 this.status = status;
157 public boolean isAugmenting() {
162 public void setAugmenting(boolean augmenting) {
163 this.augmenting = augmenting;
166 public boolean isConfiguration() {
167 return configuration;
171 public void setConfiguration(boolean configuration) {
172 this.configuration = configuration;
176 public ConstraintsBuilder getConstraints() {
180 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
181 return addedUnknownNodes;
184 public Set<AugmentationSchemaBuilder> getAugmentations() {
185 return addedAugmentations;
189 public void addAugmentation(AugmentationSchemaBuilder augment) {
190 addedAugmentations.add(augment);
194 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
195 addedUnknownNodes.add(unknownNode);
198 public String getDefaultCase() {
202 public void setDefaultCase(String defaultCase) {
203 this.defaultCase = defaultCase;
207 private final class ChoiceNodeImpl implements ChoiceNode {
208 private final QName qname;
209 private SchemaPath path;
210 private String description;
211 private String reference;
212 private Status status = Status.CURRENT;
213 private boolean augmenting;
214 private boolean configuration;
215 private ConstraintDefinition constraints;
216 private Set<ChoiceCaseNode> cases = Collections.emptySet();
217 private Set<AugmentationSchema> augmentations = Collections.emptySet();
218 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
219 private String defaultCase;
221 private ChoiceNodeImpl(QName qname) {
226 public QName getQName() {
231 public SchemaPath getPath() {
235 private void setPath(SchemaPath path) {
240 public String getDescription() {
244 private void setDescription(String description) {
245 this.description = description;
249 public String getReference() {
253 private void setReference(String reference) {
254 this.reference = reference;
258 public Status getStatus() {
262 private void setStatus(Status status) {
263 if (status != null) {
264 this.status = status;
269 public boolean isAugmenting() {
273 private void setAugmenting(boolean augmenting) {
274 this.augmenting = augmenting;
278 public boolean isConfiguration() {
279 return configuration;
282 private void setConfiguration(boolean configuration) {
283 this.configuration = configuration;
287 public ConstraintDefinition getConstraints() {
291 private void setConstraints(ConstraintDefinition constraints) {
292 this.constraints = constraints;
296 public Set<AugmentationSchema> getAvailableAugmentations() {
297 return augmentations;
300 private void setAvailableAugmentations(
301 Set<AugmentationSchema> availableAugmentations) {
302 if (availableAugmentations != null) {
303 this.augmentations = availableAugmentations;
308 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
312 private void setUnknownSchemaNodes(
313 List<UnknownSchemaNode> unknownSchemaNodes) {
314 if (unknownSchemaNodes != null) {
315 this.unknownNodes = unknownSchemaNodes;
320 public Set<ChoiceCaseNode> getCases() {
324 private void setCases(Set<ChoiceCaseNode> cases) {
330 public String getDefaultCase() {
334 private void setDefaultCase(String defaultCase) {
335 this.defaultCase = defaultCase;
339 public int hashCode() {
340 final int prime = 31;
342 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
343 result = prime * result + ((path == null) ? 0 : path.hashCode());
348 public boolean equals(Object obj) {
355 if (getClass() != obj.getClass()) {
358 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
360 if (other.qname != null) {
363 } else if (!qname.equals(other.qname)) {
367 if (other.path != null) {
370 } else if (!path.equals(other.path)) {
377 public String toString() {
378 StringBuilder sb = new StringBuilder(
379 ChoiceNodeImpl.class.getSimpleName());
381 sb.append("qname=" + qname);
383 return sb.toString();