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.ChildNodeBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.controller.yang.parser.util.YangParseException;
33 public final class ChoiceBuilder implements DataSchemaNodeBuilder,
34 ChildNodeBuilder, AugmentationTargetBuilder {
35 private boolean built;
36 private final ChoiceNodeImpl instance;
37 private final int line;
39 private final QName qname;
40 private SchemaPath schemaPath;
41 private String description;
42 private String reference;
43 private Status status = Status.CURRENT;
44 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
45 // DataSchemaNode args
46 private boolean augmenting;
47 private boolean configuration;
48 private final ConstraintsBuilder constraints;
49 // DataNodeContainer args
50 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
51 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
52 // AugmentationTarget args
53 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
55 private final Set<ChoiceCaseBuilder> cases = new HashSet<ChoiceCaseBuilder>();
56 private String defaultCase;
58 public ChoiceBuilder(final QName qname, final int line) {
61 instance = new ChoiceNodeImpl(qname);
62 constraints = new ConstraintsBuilder(line);
66 public ChoiceNode build() {
68 instance.setPath(schemaPath);
69 instance.setDescription(description);
70 instance.setReference(reference);
71 instance.setStatus(status);
72 instance.setAugmenting(augmenting);
73 instance.setConfiguration(configuration);
74 instance.setConstraints(constraints.build());
75 instance.setDefaultCase(defaultCase);
78 final Set<ChoiceCaseNode> choiceCases = new HashSet<ChoiceCaseNode>();
79 for (ChoiceCaseBuilder caseBuilder : cases) {
80 choiceCases.add(caseBuilder.build());
82 instance.setCases(choiceCases);
85 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
86 for (AugmentationSchemaBuilder builder : addedAugmentations) {
87 augmentations.add(builder.build());
89 instance.setAvailableAugmentations(augmentations);
92 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
93 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
94 unknownNodes.add(b.build());
96 instance.setUnknownSchemaNodes(unknownNodes);
104 public int getLine() {
108 public Set<ChoiceCaseBuilder> getCases() {
113 public void addChildNode(DataSchemaNodeBuilder childNode) {
114 if (!(childNode instanceof ChoiceCaseBuilder)) {
115 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(
116 childNode.getQName(), childNode.getLine());
117 caseBuilder.addChildNode(childNode);
118 cases.add(caseBuilder);
120 cases.add((ChoiceCaseBuilder) childNode);
125 public QName getQName() {
130 * Choice can not contains grouping statements, so this method always
131 * returns an empty set.
135 public Set<GroupingBuilder> getGroupings() {
136 return Collections.emptySet();
140 public void addGrouping(GroupingBuilder groupingBuilder) {
141 throw new YangParseException(line,
142 "Can not add grouping to 'choice' node.");
145 public Set<TypeDefinitionBuilder> getTypedefs() {
146 return addedTypedefs;
150 public void addTypedef(final TypeDefinitionBuilder type) {
151 addedTypedefs.add(type);
154 public SchemaPath getPath() {
159 public void setPath(final SchemaPath schemaPath) {
160 this.schemaPath = schemaPath;
163 public String getDescription() {
168 public void setDescription(final String description) {
169 this.description = description;
172 public String getReference() {
177 public void setReference(String reference) {
178 this.reference = reference;
181 public Status getStatus() {
186 public void setStatus(Status status) {
187 if (status != null) {
188 this.status = status;
192 public boolean isAugmenting() {
197 public void setAugmenting(boolean augmenting) {
198 this.augmenting = augmenting;
201 public boolean isConfiguration() {
202 return configuration;
206 public void setConfiguration(boolean configuration) {
207 this.configuration = configuration;
211 public ConstraintsBuilder getConstraints() {
215 public Set<UsesNodeBuilder> getUsesNodes() {
216 return addedUsesNodes;
220 public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
221 addedUsesNodes.add(usesNodeBuilder);
224 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
225 return addedUnknownNodes;
228 public Set<AugmentationSchemaBuilder> getAugmentations() {
229 return addedAugmentations;
233 public void addAugmentation(AugmentationSchemaBuilder augment) {
234 addedAugmentations.add(augment);
238 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
239 addedUnknownNodes.add(unknownNode);
242 public String getDefaultCase() {
246 public void setDefaultCase(String defaultCase) {
247 this.defaultCase = defaultCase;
251 public Set<DataSchemaNodeBuilder> getChildNodes() {
252 return new HashSet<DataSchemaNodeBuilder>(cases);
255 private final class ChoiceNodeImpl implements ChoiceNode {
256 private final QName qname;
257 private SchemaPath path;
258 private String description;
259 private String reference;
260 private Status status = Status.CURRENT;
261 private boolean augmenting;
262 private boolean configuration;
263 private ConstraintDefinition constraints;
264 private Set<ChoiceCaseNode> cases = Collections.emptySet();
265 private Set<AugmentationSchema> augmentations = Collections.emptySet();
266 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
267 private String defaultCase;
269 private ChoiceNodeImpl(QName qname) {
274 public QName getQName() {
279 public SchemaPath getPath() {
283 private void setPath(SchemaPath path) {
288 public String getDescription() {
292 private void setDescription(String description) {
293 this.description = description;
297 public String getReference() {
301 private void setReference(String reference) {
302 this.reference = reference;
306 public Status getStatus() {
310 private void setStatus(Status status) {
311 if (status != null) {
312 this.status = status;
317 public boolean isAugmenting() {
321 private void setAugmenting(boolean augmenting) {
322 this.augmenting = augmenting;
326 public boolean isConfiguration() {
327 return configuration;
330 private void setConfiguration(boolean configuration) {
331 this.configuration = configuration;
335 public ConstraintDefinition getConstraints() {
339 private void setConstraints(ConstraintDefinition constraints) {
340 this.constraints = constraints;
344 public Set<AugmentationSchema> getAvailableAugmentations() {
345 return augmentations;
348 private void setAvailableAugmentations(
349 Set<AugmentationSchema> availableAugmentations) {
350 if (availableAugmentations != null) {
351 this.augmentations = availableAugmentations;
356 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
360 private void setUnknownSchemaNodes(
361 List<UnknownSchemaNode> unknownSchemaNodes) {
362 if (unknownSchemaNodes != null) {
363 this.unknownNodes = unknownSchemaNodes;
368 public Set<ChoiceCaseNode> getCases() {
372 private void setCases(Set<ChoiceCaseNode> cases) {
378 public String getDefaultCase() {
382 private void setDefaultCase(String defaultCase) {
383 this.defaultCase = defaultCase;
387 public int hashCode() {
388 final int prime = 31;
390 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
391 result = prime * result + ((path == null) ? 0 : path.hashCode());
396 public boolean equals(Object obj) {
403 if (getClass() != obj.getClass()) {
406 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
408 if (other.qname != null) {
411 } else if (!qname.equals(other.qname)) {
415 if (other.path != null) {
418 } else if (!path.equals(other.path)) {
425 public String toString() {
426 StringBuilder sb = new StringBuilder(
427 ChoiceNodeImpl.class.getSimpleName());
429 sb.append("qname=" + qname);
431 return sb.toString();