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 isBuilt;
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 List<UnknownSchemaNode> unknownNodes;
39 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
40 // DataSchemaNode args
41 private boolean augmenting;
42 private boolean configuration;
43 private final ConstraintsBuilder constraints;
44 // AugmentationTarget args
45 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
47 private Set<ChoiceCaseNode> cases;
48 private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
49 private String defaultCase;
51 public ChoiceBuilder(final QName qname, final int line) {
54 instance = new ChoiceNodeImpl(qname);
55 constraints = new ConstraintsBuilder(line);
59 public ChoiceNode build() {
61 instance.setPath(schemaPath);
62 instance.setDescription(description);
63 instance.setReference(reference);
64 instance.setStatus(status);
65 instance.setAugmenting(augmenting);
66 instance.setConfiguration(configuration);
67 instance.setConstraints(constraints.build());
68 instance.setDefaultCase(defaultCase);
72 cases = new HashSet<ChoiceCaseNode>();
73 for (ChoiceCaseBuilder caseBuilder : addedCases) {
74 cases.add(caseBuilder.build());
77 instance.setCases(cases);
80 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
81 for (AugmentationSchemaBuilder builder : addedAugmentations) {
82 augmentations.add(builder.build());
84 instance.setAvailableAugmentations(augmentations);
87 if(unknownNodes == null) {
88 unknownNodes = new ArrayList<UnknownSchemaNode>();
89 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
90 unknownNodes.add(b.build());
93 instance.setUnknownSchemaNodes(unknownNodes);
101 public void rebuild() {
107 public int getLine() {
111 public Set<ChoiceCaseBuilder> getCases() {
115 public void addChildNode(DataSchemaNodeBuilder childNode) {
116 if (!(childNode instanceof ChoiceCaseBuilder)) {
117 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(childNode.getQName(), childNode.getLine());
118 caseBuilder.addChildNode(childNode);
119 addedCases.add(caseBuilder);
121 addedCases.add((ChoiceCaseBuilder) childNode);
125 public void setCases(Set<ChoiceCaseNode> cases) {
130 public QName getQName() {
134 public SchemaPath getPath() {
139 public void setPath(final SchemaPath schemaPath) {
140 this.schemaPath = schemaPath;
143 public String getDescription() {
148 public void setDescription(final String description) {
149 this.description = description;
152 public String getReference() {
157 public void setReference(String reference) {
158 this.reference = reference;
161 public Status getStatus() {
166 public void setStatus(Status status) {
167 if (status != null) {
168 this.status = status;
172 public boolean isAugmenting() {
177 public void setAugmenting(boolean augmenting) {
178 this.augmenting = augmenting;
181 public boolean isConfiguration() {
182 return configuration;
186 public void setConfiguration(boolean configuration) {
187 this.configuration = configuration;
191 public ConstraintsBuilder getConstraints() {
195 public Set<AugmentationSchemaBuilder> getAugmentations() {
196 return addedAugmentations;
200 public void addAugmentation(AugmentationSchemaBuilder augment) {
201 addedAugmentations.add(augment);
204 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
205 return addedUnknownNodes;
209 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
210 addedUnknownNodes.add(unknownNode);
213 public void setUnknownNodes(List<UnknownSchemaNode> unknownNodes) {
214 this.unknownNodes = unknownNodes;
217 public String getDefaultCase() {
221 public void setDefaultCase(String defaultCase) {
222 this.defaultCase = defaultCase;
225 public final class ChoiceNodeImpl implements ChoiceNode {
226 private final QName qname;
227 private SchemaPath path;
228 private String description;
229 private String reference;
230 private Status status = Status.CURRENT;
231 private boolean augmenting;
232 private boolean configuration;
233 private ConstraintDefinition constraints;
234 private Set<ChoiceCaseNode> cases = Collections.emptySet();
235 private Set<AugmentationSchema> augmentations = Collections.emptySet();
236 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
237 private String defaultCase;
239 private ChoiceNodeImpl(QName qname) {
244 public QName getQName() {
249 public SchemaPath getPath() {
253 private void setPath(SchemaPath path) {
258 public String getDescription() {
262 private void setDescription(String description) {
263 this.description = description;
267 public String getReference() {
271 private void setReference(String reference) {
272 this.reference = reference;
276 public Status getStatus() {
280 private void setStatus(Status status) {
281 if (status != null) {
282 this.status = status;
287 public boolean isAugmenting() {
291 private void setAugmenting(boolean augmenting) {
292 this.augmenting = augmenting;
296 public boolean isConfiguration() {
297 return configuration;
300 private void setConfiguration(boolean configuration) {
301 this.configuration = configuration;
305 public ConstraintDefinition getConstraints() {
309 private void setConstraints(ConstraintDefinition constraints) {
310 this.constraints = constraints;
314 public Set<AugmentationSchema> getAvailableAugmentations() {
315 return augmentations;
318 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
319 if (availableAugmentations != null) {
320 this.augmentations = availableAugmentations;
325 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
329 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
330 if (unknownSchemaNodes != null) {
331 this.unknownNodes = unknownSchemaNodes;
336 public Set<ChoiceCaseNode> getCases() {
340 private void setCases(Set<ChoiceCaseNode> cases) {
347 public String getDefaultCase() {
351 private void setDefaultCase(String defaultCase) {
352 this.defaultCase = defaultCase;
355 public ChoiceBuilder toBuilder() {
356 return ChoiceBuilder.this;
360 public int hashCode() {
361 final int prime = 31;
363 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
364 result = prime * result + ((path == null) ? 0 : path.hashCode());
369 public boolean equals(Object obj) {
376 if (getClass() != obj.getClass()) {
379 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
381 if (other.qname != null) {
384 } else if (!qname.equals(other.qname)) {
388 if (other.path != null) {
391 } else if (!path.equals(other.path)) {
398 public String toString() {
399 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
401 sb.append("qname=" + qname);
403 return sb.toString();