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.model.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.model.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.ChildNodeBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
32 public class ChoiceBuilder implements DataSchemaNodeBuilder, ChildNodeBuilder,
33 AugmentationTargetBuilder {
34 private final ChoiceNodeImpl instance;
36 private final QName qname;
37 private SchemaPath schemaPath;
38 private String description;
39 private String reference;
40 private Status status = Status.CURRENT;
41 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
42 // DataSchemaNode args
43 private boolean augmenting;
44 private boolean configuration;
45 private final ConstraintsBuilder constraints;
46 // DataNodeContainer args
47 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
48 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
49 // AugmentationTarget args
50 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
52 private final Set<ChoiceCaseBuilder> cases = new HashSet<ChoiceCaseBuilder>();
53 private String defaultCase;
55 public ChoiceBuilder(QName qname) {
57 instance = new ChoiceNodeImpl(qname);
58 constraints = new ConstraintsBuilder();
62 public ChoiceNode build() {
63 instance.setPath(schemaPath);
64 instance.setDescription(description);
65 instance.setReference(reference);
66 instance.setStatus(status);
67 instance.setAugmenting(augmenting);
68 instance.setConfiguration(configuration);
69 instance.setConstraints(constraints.build());
70 instance.setDefaultCase(defaultCase);
73 final Set<ChoiceCaseNode> choiceCases = new HashSet<ChoiceCaseNode>();
74 for (ChoiceCaseBuilder caseBuilder : cases) {
75 choiceCases.add(caseBuilder.build());
77 instance.setCases(choiceCases);
80 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
81 for (AugmentationSchemaBuilder builder : addedAugmentations) {
82 augmentations.add(builder.build());
84 instance.setAvailableAugmentations(augmentations);
87 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
88 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
89 unknownNodes.add(b.build());
91 instance.setUnknownSchemaNodes(unknownNodes);
96 public Set<ChoiceCaseBuilder> getCases() {
101 public void addChildNode(DataSchemaNodeBuilder childNode) {
102 if (!(childNode instanceof ChoiceCaseBuilder)) {
103 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(
104 childNode.getQName());
105 caseBuilder.addChildNode(childNode);
106 cases.add(caseBuilder);
108 cases.add((ChoiceCaseBuilder) childNode);
113 public QName getQName() {
118 * Choice can not contains grouping statements, so this method always
119 * returns an empty set.
123 public Set<GroupingBuilder> getGroupings() {
124 return Collections.emptySet();
128 public void addGrouping(GroupingBuilder groupingBuilder) {
129 throw new IllegalStateException(
130 "Can not add grouping to 'choice' node.");
133 public Set<TypeDefinitionBuilder> getTypedefs() {
134 return addedTypedefs;
138 public void addTypedef(final TypeDefinitionBuilder type) {
139 addedTypedefs.add(type);
142 public SchemaPath getPath() {
147 public void setPath(final SchemaPath schemaPath) {
148 this.schemaPath = schemaPath;
151 public String getDescription() {
156 public void setDescription(final String description) {
157 this.description = description;
160 public String getReference() {
165 public void setReference(String reference) {
166 this.reference = reference;
169 public Status getStatus() {
174 public void setStatus(Status status) {
175 if (status != null) {
176 this.status = status;
180 public boolean isAugmenting() {
185 public void setAugmenting(boolean augmenting) {
186 this.augmenting = augmenting;
189 public boolean isConfiguration() {
190 return configuration;
194 public void setConfiguration(boolean configuration) {
195 this.configuration = configuration;
199 public ConstraintsBuilder getConstraints() {
203 public Set<UsesNodeBuilder> getUsesNodes() {
204 return addedUsesNodes;
208 public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
209 addedUsesNodes.add(usesNodeBuilder);
212 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
213 return addedUnknownNodes;
216 public Set<AugmentationSchemaBuilder> getAugmentations() {
217 return addedAugmentations;
221 public void addAugmentation(AugmentationSchemaBuilder augment) {
222 addedAugmentations.add(augment);
226 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
227 addedUnknownNodes.add(unknownNode);
230 public String getDefaultCase() {
234 public void setDefaultCase(String defaultCase) {
235 this.defaultCase = defaultCase;
239 public Set<DataSchemaNodeBuilder> getChildNodes() {
240 return new HashSet<DataSchemaNodeBuilder>(cases);
243 private static class ChoiceNodeImpl implements ChoiceNode {
244 private final QName qname;
245 private SchemaPath path;
246 private String description;
247 private String reference;
248 private Status status = Status.CURRENT;
249 private boolean augmenting;
250 private boolean configuration;
251 private ConstraintDefinition constraints;
252 private Set<ChoiceCaseNode> cases = Collections.emptySet();
253 private Set<AugmentationSchema> augmentations = Collections.emptySet();
254 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
255 private String defaultCase;
257 private ChoiceNodeImpl(QName qname) {
262 public QName getQName() {
267 public SchemaPath getPath() {
271 private void setPath(SchemaPath path) {
276 public String getDescription() {
280 private void setDescription(String description) {
281 this.description = description;
285 public String getReference() {
289 private void setReference(String reference) {
290 this.reference = reference;
294 public Status getStatus() {
298 private void setStatus(Status status) {
299 if (status != null) {
300 this.status = status;
305 public boolean isAugmenting() {
309 private void setAugmenting(boolean augmenting) {
310 this.augmenting = augmenting;
314 public boolean isConfiguration() {
315 return configuration;
318 private void setConfiguration(boolean configuration) {
319 this.configuration = configuration;
323 public ConstraintDefinition getConstraints() {
327 private void setConstraints(ConstraintDefinition constraints) {
328 this.constraints = constraints;
332 public Set<AugmentationSchema> getAvailableAugmentations() {
333 return augmentations;
336 private void setAvailableAugmentations(
337 Set<AugmentationSchema> availableAugmentations) {
338 if (availableAugmentations != null) {
339 this.augmentations = availableAugmentations;
344 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
348 private void setUnknownSchemaNodes(
349 List<UnknownSchemaNode> unknownSchemaNodes) {
350 if (unknownSchemaNodes != null) {
351 this.unknownNodes = unknownSchemaNodes;
356 public Set<ChoiceCaseNode> getCases() {
360 private void setCases(Set<ChoiceCaseNode> cases) {
366 public String getDefaultCase() {
370 private void setDefaultCase(String defaultCase) {
371 this.defaultCase = defaultCase;
375 public int hashCode() {
376 final int prime = 31;
378 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
379 result = prime * result + ((path == null) ? 0 : path.hashCode());
384 public boolean equals(Object obj) {
391 if (getClass() != obj.getClass()) {
394 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
396 if (other.qname != null) {
399 } else if (!qname.equals(other.qname)) {
403 if (other.path != null) {
406 } else if (!path.equals(other.path)) {
413 public String toString() {
414 StringBuilder sb = new StringBuilder(
415 ChoiceNodeImpl.class.getSimpleName());
417 sb.append("qname=" + qname);
419 return sb.toString();