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.yangtools.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
14 import java.util.TreeSet;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
20 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.Status;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
34 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
36 import com.google.common.base.Optional;
37 import com.google.common.base.Preconditions;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.collect.ImmutableSet;
41 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
42 AugmentationTargetBuilder {
43 private ChoiceNodeImpl instance;
45 // DataSchemaNode args
46 private boolean augmenting;
47 private boolean addedByUses;
48 private boolean configuration;
49 private ChoiceNode originalNode;
50 private ChoiceBuilder originalBuilder;
51 private final ConstraintsBuilder constraints;
52 // AugmentationTarget args
53 private final Set<AugmentationSchema> augmentations = new HashSet<>();
54 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
56 private final Set<ChoiceCaseBuilder> caseBuilders = new HashSet<>();
57 private String defaultCase;
59 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
60 super(moduleName, line, qname);
61 this.schemaPath = path;
62 constraints = new ConstraintsBuilderImpl(moduleName, line);
65 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
66 final ChoiceNode base) {
67 super(moduleName, line, qname);
68 this.schemaPath = path;
69 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
71 description = base.getDescription();
72 reference = base.getReference();
73 status = base.getStatus();
74 augmenting = base.isAugmenting();
75 addedByUses = base.isAddedByUses();
77 configuration = base.isConfiguration();
78 augmentations.addAll(base.getAvailableAugmentations());
80 Set<DataSchemaNodeBuilder> wrapped = BuilderUtils.wrapChildNodes(moduleName, line, new HashSet<DataSchemaNode>(
81 base.getCases()), path, qname);
82 for (DataSchemaNodeBuilder wrap : wrapped) {
83 if (wrap instanceof ChoiceCaseBuilder) {
84 caseBuilders.add((ChoiceCaseBuilder) wrap);
88 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
93 public ChoiceNode build() {
94 if (instance != null) {
98 instance = new ChoiceNodeImpl(qname, schemaPath);
100 instance.description = description;
101 instance.reference = reference;
102 instance.status = status;
103 instance.augmenting = augmenting;
104 instance.addedByUses = addedByUses;
105 instance.configuration = configuration;
107 instance.constraints = constraints.toInstance();
108 instance.defaultCase = defaultCase;
111 if (originalNode == null && originalBuilder != null) {
112 originalNode = originalBuilder.build();
114 instance.original = originalNode;
117 final Set<ChoiceCaseNode> cases = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
118 for (ChoiceCaseBuilder caseBuilder : caseBuilders) {
119 cases.add(caseBuilder.build());
121 instance.cases = ImmutableSet.copyOf(cases);
124 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
125 augmentations.add(builder.build());
127 instance.augmentations = ImmutableSet.copyOf(augmentations);
130 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
131 unknownNodes.add(b.build());
133 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
138 public Set<ChoiceCaseBuilder> getCases() {
146 * name of case to search
147 * @return case with given name if present, null otherwise
149 public ChoiceCaseBuilder getCaseNodeByName(final String caseName) {
150 for (ChoiceCaseBuilder addedCase : caseBuilders) {
151 if (addedCase.getQName().getLocalName().equals(caseName)) {
159 * Add case node to this choice.
161 * If node is not declared with 'case' keyword, create new case builder and
162 * make this node child of newly created case.
167 public void addCase(final DataSchemaNodeBuilder caseNode) {
168 QName caseQName = caseNode.getQName();
169 String caseName = caseQName.getLocalName();
171 for (ChoiceCaseBuilder existingCase : caseBuilders) {
172 if (existingCase.getQName().getLocalName().equals(caseName)) {
173 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
174 + "' to node '" + qname.getLocalName() + "' in module '" + getModuleName()
175 + "': case with same name already declared at line " + existingCase.getLine());
179 if (caseNode instanceof ChoiceCaseBuilder) {
180 caseBuilders.add((ChoiceCaseBuilder) caseNode);
182 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
183 caseQName, caseNode.getPath());
184 if (caseNode.isAugmenting()) {
185 // if node is added by augmentation, set case builder augmenting
186 // as true and node augmenting as false
187 caseBuilder.setAugmenting(true);
188 caseNode.setAugmenting(false);
190 SchemaPath newPath = caseNode.getPath().createChild(caseQName);
191 caseNode.setPath(newPath);
192 caseBuilder.addChildNode(caseNode);
193 caseBuilders.add(caseBuilder);
198 public boolean isAugmenting() {
203 public void setAugmenting(final boolean augmenting) {
204 this.augmenting = augmenting;
208 public boolean isAddedByUses() {
213 public void setAddedByUses(final boolean addedByUses) {
214 this.addedByUses = addedByUses;
218 public ChoiceBuilder getOriginal() {
219 return originalBuilder;
223 public void setOriginal(final SchemaNodeBuilder builder) {
224 Preconditions.checkArgument(builder instanceof ChoiceBuilder, "Original of choice cannot be " + builder);
225 this.originalBuilder = (ChoiceBuilder) builder;
229 public boolean isConfiguration() {
230 return configuration;
234 public void setConfiguration(final boolean configuration) {
235 this.configuration = configuration;
239 public ConstraintsBuilder getConstraints() {
244 public void addAugmentation(final AugmentationSchemaBuilder augment) {
245 augmentationBuilders.add(augment);
248 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
249 return augmentationBuilders;
252 public String getDefaultCase() {
256 public void setDefaultCase(final String defaultCase) {
257 this.defaultCase = defaultCase;
261 public int hashCode() {
262 final int prime = 31;
264 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
269 public boolean equals(final Object obj) {
276 if (getClass() != obj.getClass()) {
279 ChoiceBuilder other = (ChoiceBuilder) obj;
280 if (schemaPath == null) {
281 if (other.schemaPath != null) {
284 } else if (!schemaPath.equals(other.schemaPath)) {
287 if (getParent() == null) {
288 if (other.getParent() != null) {
291 } else if (!getParent().equals(other.getParent())) {
298 public String toString() {
299 return "choice " + qname.getLocalName();
302 private static final class ChoiceNodeImpl implements ChoiceNode, DerivableSchemaNode {
303 private final QName qname;
304 private final SchemaPath path;
305 private String description;
306 private String reference;
307 private Status status;
308 private boolean augmenting;
309 private boolean addedByUses;
310 private ChoiceNode original;
311 private boolean configuration;
312 private ConstraintDefinition constraints;
313 private ImmutableSet<ChoiceCaseNode> cases;
314 private ImmutableSet<AugmentationSchema> augmentations;
315 private ImmutableList<UnknownSchemaNode> unknownNodes;
316 private String defaultCase;
318 private ChoiceNodeImpl(final QName qname, final SchemaPath path) {
324 public QName getQName() {
329 public SchemaPath getPath() {
334 public String getDescription() {
339 public String getReference() {
344 public Status getStatus() {
349 public boolean isAugmenting() {
354 public boolean isAddedByUses() {
359 public Optional<ChoiceNode> getOriginal() {
360 return Optional.fromNullable(original);
364 public boolean isConfiguration() {
365 return configuration;
369 public ConstraintDefinition getConstraints() {
374 public Set<AugmentationSchema> getAvailableAugmentations() {
375 return augmentations;
379 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
384 public Set<ChoiceCaseNode> getCases() {
389 public ChoiceCaseNode getCaseNodeByName(final QName name) {
391 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
393 for (final ChoiceCaseNode caseNode : cases) {
394 if (caseNode != null && name.equals(caseNode.getQName())) {
402 public ChoiceCaseNode getCaseNodeByName(final String name) {
404 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
406 for (final ChoiceCaseNode caseNode : cases) {
407 if (caseNode != null && (caseNode.getQName() != null)
408 && name.equals(caseNode.getQName().getLocalName())) {
416 public String getDefaultCase() {
421 public int hashCode() {
422 final int prime = 31;
424 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
425 result = prime * result + ((path == null) ? 0 : path.hashCode());
430 public boolean equals(final Object obj) {
437 if (getClass() != obj.getClass()) {
440 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
442 if (other.qname != null) {
445 } else if (!qname.equals(other.qname)) {
449 if (other.path != null) {
452 } else if (!path.equals(other.path)) {
459 public String toString() {
460 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
462 sb.append("qname=").append(qname);
464 return sb.toString();