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.ConfigNode;
29 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
31 import org.opendaylight.controller.yang.parser.util.Comparators;
32 import org.opendaylight.controller.yang.parser.util.ParserUtils;
34 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
35 AugmentationTargetBuilder, GroupingMember, ConfigNode {
36 private boolean isBuilt;
37 private final ChoiceNodeImpl instance;
39 private List<UnknownSchemaNode> unknownNodes;
40 // DataSchemaNode args
41 private boolean augmenting;
42 private boolean addedByUses;
43 private Boolean configuration;
44 private final ConstraintsBuilder constraints;
45 // AugmentationTarget args
46 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
48 private Set<ChoiceCaseNode> cases;
49 private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
50 private String defaultCase;
52 public ChoiceBuilder(final int line, final QName qname) {
54 instance = new ChoiceNodeImpl(qname);
55 constraints = new ConstraintsBuilder(line);
58 public ChoiceBuilder(ChoiceBuilder b) {
59 super(b.getLine(), b.getQName());
60 parent = b.getParent();
61 instance = new ChoiceNodeImpl(qname);
62 constraints = b.getConstraints();
63 schemaPath = b.getPath();
64 description = b.getDescription();
65 reference = b.getReference();
66 status = b.getStatus();
67 unknownNodes = b.unknownNodes;
68 addedUnknownNodes.addAll(b.getUnknownNodes());
69 augmenting = b.isAugmenting();
70 addedByUses = b.isAddedByUses();
71 configuration = b.isConfiguration();
72 addedAugmentations.addAll(b.getAugmentations());
74 addedCases.addAll(b.getCases());
75 defaultCase = b.getDefaultCase();
79 public ChoiceNode build() {
81 instance.setPath(schemaPath);
82 instance.setDescription(description);
83 instance.setReference(reference);
84 instance.setStatus(status);
85 instance.setAugmenting(augmenting);
86 instance.setAddedByUses(addedByUses);
87 instance.setConfiguration(configuration);
88 instance.setConstraints(constraints.build());
89 instance.setDefaultCase(defaultCase);
93 cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
94 for (ChoiceCaseBuilder caseBuilder : addedCases) {
95 cases.add(caseBuilder.build());
98 instance.setCases(cases);
101 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
102 for (AugmentationSchemaBuilder builder : addedAugmentations) {
103 augmentations.add(builder.build());
105 instance.setAvailableAugmentations(augmentations);
108 if (unknownNodes == null) {
109 unknownNodes = new ArrayList<UnknownSchemaNode>();
110 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
111 unknownNodes.add(b.build());
113 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
115 instance.setUnknownSchemaNodes(unknownNodes);
123 public void rebuild() {
128 public Set<ChoiceCaseBuilder> getCases() {
132 public void addChildNode(DataSchemaNodeBuilder childNode) {
133 if (!(childNode instanceof ChoiceCaseBuilder)) {
134 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(childNode.getLine(), childNode.getQName());
135 if(childNode.isAugmenting()) {
136 caseBuilder.setAugmenting(true);
137 childNode.setAugmenting(false);
139 caseBuilder.setPath(childNode.getPath());
140 SchemaPath newPath = ParserUtils.createSchemaPath(childNode.getPath(), childNode.getQName().getLocalName());
141 childNode.setPath(newPath);
142 caseBuilder.addChildNode(childNode);
143 addedCases.add(caseBuilder);
145 addedCases.add((ChoiceCaseBuilder) childNode);
149 public void setCases(Set<ChoiceCaseNode> cases) {
154 public boolean isAugmenting() {
159 public void setAugmenting(boolean augmenting) {
160 this.augmenting = augmenting;
164 public boolean isAddedByUses() {
169 public void setAddedByUses(final boolean addedByUses) {
170 this.addedByUses = addedByUses;
173 public Boolean isConfiguration() {
174 return configuration;
178 public void setConfiguration(Boolean configuration) {
179 this.configuration = configuration;
183 public ConstraintsBuilder getConstraints() {
187 public Set<AugmentationSchemaBuilder> getAugmentations() {
188 return addedAugmentations;
192 public void addAugmentation(AugmentationSchemaBuilder augment) {
193 addedAugmentations.add(augment);
196 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
197 return addedUnknownNodes;
200 public void setUnknownNodes(List<UnknownSchemaNode> unknownNodes) {
201 this.unknownNodes = unknownNodes;
204 public String getDefaultCase() {
208 public void setDefaultCase(String defaultCase) {
209 this.defaultCase = defaultCase;
213 public int hashCode() {
214 final int prime = 31;
216 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
221 public boolean equals(Object obj) {
228 if (getClass() != obj.getClass()) {
231 ChoiceBuilder other = (ChoiceBuilder) obj;
232 if (schemaPath == null) {
233 if (other.schemaPath != null) {
236 } else if (!schemaPath.equals(other.schemaPath)) {
239 if (parent == null) {
240 if (other.parent != null) {
243 } else if (!parent.equals(other.parent)) {
250 public String toString() {
251 return "choice " + qname.getLocalName();
254 public final class ChoiceNodeImpl implements ChoiceNode {
255 private final QName qname;
256 private SchemaPath path;
257 private String description;
258 private String reference;
259 private Status status = Status.CURRENT;
260 private boolean augmenting;
261 private boolean addedByUses;
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 isAddedByUses() {
330 private void setAddedByUses(boolean addedByUses) {
331 this.addedByUses = addedByUses;
335 public boolean isConfiguration() {
336 return configuration;
339 private void setConfiguration(boolean configuration) {
340 this.configuration = configuration;
344 public ConstraintDefinition getConstraints() {
348 private void setConstraints(ConstraintDefinition constraints) {
349 this.constraints = constraints;
353 public Set<AugmentationSchema> getAvailableAugmentations() {
354 return augmentations;
357 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
358 if (availableAugmentations != null) {
359 this.augmentations = availableAugmentations;
364 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
368 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
369 if (unknownSchemaNodes != null) {
370 this.unknownNodes = unknownSchemaNodes;
375 public Set<ChoiceCaseNode> getCases() {
379 private void setCases(Set<ChoiceCaseNode> cases) {
386 public String getDefaultCase() {
390 private void setDefaultCase(String defaultCase) {
391 this.defaultCase = defaultCase;
394 public ChoiceBuilder toBuilder() {
395 return ChoiceBuilder.this;
399 public int hashCode() {
400 final int prime = 31;
402 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
403 result = prime * result + ((path == null) ? 0 : path.hashCode());
408 public boolean equals(Object obj) {
415 if (getClass() != obj.getClass()) {
418 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
420 if (other.qname != null) {
423 } else if (!qname.equals(other.qname)) {
427 if (other.path != null) {
430 } else if (!path.equals(other.path)) {
437 public String toString() {
438 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
440 sb.append("qname=" + qname);
442 return sb.toString();