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.DataSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
30 import org.opendaylight.controller.yang.parser.util.Comparators;
31 import org.opendaylight.controller.yang.parser.util.ParserUtils;
32 import org.opendaylight.controller.yang.parser.util.YangParseException;
34 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
35 AugmentationTargetBuilder, GroupingMember {
36 private boolean isBuilt;
37 private final ChoiceNodeImpl instance;
38 // DataSchemaNode args
39 private boolean augmenting;
40 private boolean addedByUses;
41 private Boolean configuration;
42 private final ConstraintsBuilder constraints;
43 // AugmentationTarget args
44 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
46 private Set<ChoiceCaseNode> cases;
47 private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
48 private String defaultCase;
50 public ChoiceBuilder(final String moduleName, final int line, final QName qname) {
51 super(moduleName, line, qname);
52 instance = new ChoiceNodeImpl(qname);
53 constraints = new ConstraintsBuilder(moduleName, line);
56 public ChoiceBuilder(ChoiceBuilder b) {
57 super(b.getModuleName(), b.getLine(), b.getQName());
58 parent = b.getParent();
59 instance = new ChoiceNodeImpl(qname);
60 constraints = b.getConstraints();
61 schemaPath = b.getPath();
62 description = b.getDescription();
63 reference = b.getReference();
64 status = b.getStatus();
65 unknownNodes = b.unknownNodes;
66 addedUnknownNodes.addAll(b.getUnknownNodes());
67 augmenting = b.isAugmenting();
68 addedByUses = b.isAddedByUses();
69 configuration = b.isConfiguration();
70 addedAugmentations.addAll(b.getAugmentations());
72 addedCases.addAll(b.getCases());
73 defaultCase = b.getDefaultCase();
77 public ChoiceNode build() {
79 instance.setPath(schemaPath);
80 instance.setDescription(description);
81 instance.setReference(reference);
82 instance.setStatus(status);
83 instance.setAugmenting(augmenting);
84 instance.setAddedByUses(addedByUses);
85 instance.setConfiguration(configuration);
86 instance.setConstraints(constraints.build());
87 instance.setDefaultCase(defaultCase);
91 cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
92 for (ChoiceCaseBuilder caseBuilder : addedCases) {
93 cases.add(caseBuilder.build());
96 instance.setCases(cases);
99 final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
100 for (AugmentationSchemaBuilder builder : addedAugmentations) {
101 augmentations.add(builder.build());
103 instance.setAvailableAugmentations(augmentations);
106 if (unknownNodes == null) {
107 unknownNodes = new ArrayList<UnknownSchemaNode>();
108 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
109 unknownNodes.add(b.build());
111 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
113 instance.setUnknownSchemaNodes(unknownNodes);
121 public void rebuild() {
126 public Set<ChoiceCaseBuilder> getCases() {
131 * Add case node to this choice.
133 * If node is not declared with 'case' keyword, create new case builder and
134 * make this node child of newly created case.
139 public void addCase(DataSchemaNodeBuilder caseNode) {
140 String newCaseName = caseNode.getQName().getLocalName();
141 for (ChoiceCaseBuilder addedCase : addedCases) {
142 if (addedCase.getQName().getLocalName().equals(newCaseName)) {
143 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
144 + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
145 + "': case with same name already declared at line " + addedCase.getLine());
149 if (caseNode instanceof ChoiceCaseBuilder) {
150 addedCases.add((ChoiceCaseBuilder) caseNode);
152 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
153 caseNode.getQName());
154 if (caseNode.isAugmenting()) {
155 // if node is added by augmentation, set case builder augmenting
156 // as true and node augmenting as false
157 caseBuilder.setAugmenting(true);
158 caseNode.setAugmenting(false);
160 caseBuilder.setPath(caseNode.getPath());
161 SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseNode.getQName().getLocalName());
162 caseNode.setPath(newPath);
163 caseBuilder.addChildNode(caseNode);
164 addedCases.add(caseBuilder);
168 public void setCases(Set<ChoiceCaseNode> cases) {
173 public boolean isAugmenting() {
178 public void setAugmenting(boolean augmenting) {
179 this.augmenting = augmenting;
183 public boolean isAddedByUses() {
188 public void setAddedByUses(final boolean addedByUses) {
189 this.addedByUses = addedByUses;
192 public Boolean isConfiguration() {
193 return configuration;
197 public void setConfiguration(Boolean configuration) {
198 this.configuration = configuration;
202 public ConstraintsBuilder getConstraints() {
206 public Set<AugmentationSchemaBuilder> getAugmentations() {
207 return addedAugmentations;
211 public void addAugmentation(AugmentationSchemaBuilder augment) {
212 addedAugmentations.add(augment);
215 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
216 return addedUnknownNodes;
219 public String getDefaultCase() {
223 public void setDefaultCase(String defaultCase) {
224 this.defaultCase = defaultCase;
228 public int hashCode() {
229 final int prime = 31;
231 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
236 public boolean equals(Object obj) {
243 if (getClass() != obj.getClass()) {
246 ChoiceBuilder other = (ChoiceBuilder) obj;
247 if (schemaPath == null) {
248 if (other.schemaPath != null) {
251 } else if (!schemaPath.equals(other.schemaPath)) {
254 if (parent == null) {
255 if (other.parent != null) {
258 } else if (!parent.equals(other.parent)) {
265 public String toString() {
266 return "choice " + qname.getLocalName();
269 public final class ChoiceNodeImpl implements ChoiceNode {
270 private final QName qname;
271 private SchemaPath path;
272 private String description;
273 private String reference;
274 private Status status = Status.CURRENT;
275 private boolean augmenting;
276 private boolean addedByUses;
277 private boolean configuration;
278 private ConstraintDefinition constraints;
279 private Set<ChoiceCaseNode> cases = Collections.emptySet();
280 private Set<AugmentationSchema> augmentations = Collections.emptySet();
281 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
282 private String defaultCase;
284 private ChoiceNodeImpl(QName qname) {
289 public QName getQName() {
294 public SchemaPath getPath() {
298 private void setPath(SchemaPath path) {
303 public String getDescription() {
307 private void setDescription(String description) {
308 this.description = description;
312 public String getReference() {
316 private void setReference(String reference) {
317 this.reference = reference;
321 public Status getStatus() {
325 private void setStatus(Status status) {
326 if (status != null) {
327 this.status = status;
332 public boolean isAugmenting() {
336 private void setAugmenting(boolean augmenting) {
337 this.augmenting = augmenting;
341 public boolean isAddedByUses() {
345 private void setAddedByUses(boolean addedByUses) {
346 this.addedByUses = addedByUses;
350 public boolean isConfiguration() {
351 return configuration;
354 private void setConfiguration(boolean configuration) {
355 this.configuration = configuration;
359 public ConstraintDefinition getConstraints() {
363 private void setConstraints(ConstraintDefinition constraints) {
364 this.constraints = constraints;
368 public Set<AugmentationSchema> getAvailableAugmentations() {
369 return augmentations;
372 private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
373 if (availableAugmentations != null) {
374 this.augmentations = availableAugmentations;
379 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
383 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
384 if (unknownSchemaNodes != null) {
385 this.unknownNodes = unknownSchemaNodes;
390 public Set<ChoiceCaseNode> getCases() {
395 public ChoiceCaseNode getCaseNodeByName(final QName name) {
397 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
399 for (final ChoiceCaseNode caseNode : cases) {
400 if (caseNode != null) {
401 if (name.equals(caseNode.getQName())) {
410 public ChoiceCaseNode getCaseNodeByName(final String name) {
412 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
414 for (final ChoiceCaseNode caseNode : cases) {
415 if (caseNode != null && (caseNode.getQName() != null)) {
416 if (name.equals(caseNode.getQName().getLocalName())) {
424 private void setCases(Set<ChoiceCaseNode> cases) {
431 public String getDefaultCase() {
435 private void setDefaultCase(String defaultCase) {
436 this.defaultCase = defaultCase;
439 public ChoiceBuilder toBuilder() {
440 return ChoiceBuilder.this;
444 public int hashCode() {
445 final int prime = 31;
447 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
448 result = prime * result + ((path == null) ? 0 : path.hashCode());
453 public boolean equals(Object obj) {
460 if (getClass() != obj.getClass()) {
463 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
465 if (other.qname != null) {
468 } else if (!qname.equals(other.qname)) {
472 if (other.path != null) {
475 } else if (!path.equals(other.path)) {
482 public String toString() {
483 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
485 sb.append("qname=" + qname);
487 return sb.toString();