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 com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
23 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 import org.opendaylight.yangtools.yang.model.api.Status;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
35 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
37 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
38 AugmentationTargetBuilder {
39 private ChoiceNodeImpl instance;
41 // DataSchemaNode args
42 private boolean augmenting;
43 private boolean addedByUses;
44 private boolean configuration;
45 private final ConstraintsBuilder constraints;
46 // AugmentationTarget args
47 private final Set<AugmentationSchema> augmentations = new HashSet<>();
48 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
50 private final Set<ChoiceCaseBuilder> caseBuilders = new HashSet<>();
51 private String defaultCase;
53 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
54 super(moduleName, line, qname);
55 this.schemaPath = path;
56 constraints = new ConstraintsBuilderImpl(moduleName, line);
59 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
60 final ChoiceNode base) {
61 super(moduleName, line, qname);
62 this.schemaPath = path;
63 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
65 description = base.getDescription();
66 reference = base.getReference();
67 status = base.getStatus();
68 augmenting = base.isAugmenting();
69 addedByUses = base.isAddedByUses();
70 configuration = base.isConfiguration();
71 augmentations.addAll(base.getAvailableAugmentations());
73 Set<DataSchemaNodeBuilder> wrapped = BuilderUtils.wrapChildNodes(moduleName, line, new HashSet<DataSchemaNode>(
74 base.getCases()), path, qname);
75 for (DataSchemaNodeBuilder wrap : wrapped) {
76 if (wrap instanceof ChoiceCaseBuilder) {
77 caseBuilders.add((ChoiceCaseBuilder) wrap);
81 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
86 public ChoiceNode build() {
87 if (instance != null) {
91 instance = new ChoiceNodeImpl(qname, schemaPath);
93 instance.description = description;
94 instance.reference = reference;
95 instance.status = status;
96 instance.augmenting = augmenting;
97 instance.addedByUses = addedByUses;
98 instance.configuration = configuration;
100 instance.constraints = constraints.toInstance();
101 instance.defaultCase = defaultCase;
104 final Set<ChoiceCaseNode> cases = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
105 for (ChoiceCaseBuilder caseBuilder : caseBuilders) {
106 cases.add(caseBuilder.build());
108 instance.cases = ImmutableSet.copyOf(cases);
111 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
112 augmentations.add(builder.build());
114 instance.augmentations = ImmutableSet.copyOf(augmentations);
117 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
118 unknownNodes.add(b.build());
120 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
125 public Set<ChoiceCaseBuilder> getCases() {
133 * name of case to search
134 * @return case with given name if present, null otherwise
136 public ChoiceCaseBuilder getCaseNodeByName(final String caseName) {
137 for (ChoiceCaseBuilder addedCase : caseBuilders) {
138 if (addedCase.getQName().getLocalName().equals(caseName)) {
146 * Add case node to this choice.
148 * If node is not declared with 'case' keyword, create new case builder and
149 * make this node child of newly created case.
154 public void addCase(final DataSchemaNodeBuilder caseNode) {
155 QName caseQName = caseNode.getQName();
156 String caseName = caseQName.getLocalName();
158 for (ChoiceCaseBuilder existingCase : caseBuilders) {
159 if (existingCase.getQName().getLocalName().equals(caseName)) {
160 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
161 + "' to node '" + qname.getLocalName() + "' in module '" + getModuleName()
162 + "': case with same name already declared at line " + existingCase.getLine());
166 if (caseNode instanceof ChoiceCaseBuilder) {
167 caseBuilders.add((ChoiceCaseBuilder) caseNode);
169 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
170 caseQName, caseNode.getPath());
171 if (caseNode.isAugmenting()) {
172 // if node is added by augmentation, set case builder augmenting
173 // as true and node augmenting as false
174 caseBuilder.setAugmenting(true);
175 caseNode.setAugmenting(false);
177 SchemaPath newPath = caseNode.getPath().createChild(caseQName);
178 caseNode.setPath(newPath);
179 caseBuilder.addChildNode(caseNode);
180 caseBuilders.add(caseBuilder);
185 public boolean isAugmenting() {
190 public void setAugmenting(final boolean augmenting) {
191 this.augmenting = augmenting;
195 public boolean isAddedByUses() {
200 public void setAddedByUses(final boolean addedByUses) {
201 this.addedByUses = addedByUses;
205 public boolean isConfiguration() {
206 return configuration;
210 public void setConfiguration(final boolean configuration) {
211 this.configuration = configuration;
215 public ConstraintsBuilder getConstraints() {
220 public void addAugmentation(final AugmentationSchemaBuilder augment) {
221 augmentationBuilders.add(augment);
224 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
225 return augmentationBuilders;
228 public String getDefaultCase() {
232 public void setDefaultCase(final String defaultCase) {
233 this.defaultCase = defaultCase;
237 public int hashCode() {
238 final int prime = 31;
240 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
245 public boolean equals(final Object obj) {
252 if (getClass() != obj.getClass()) {
255 ChoiceBuilder other = (ChoiceBuilder) obj;
256 if (schemaPath == null) {
257 if (other.schemaPath != null) {
260 } else if (!schemaPath.equals(other.schemaPath)) {
263 if (getParent() == null) {
264 if (other.getParent() != null) {
267 } else if (!getParent().equals(other.getParent())) {
274 public String toString() {
275 return "choice " + qname.getLocalName();
278 private static final class ChoiceNodeImpl implements ChoiceNode {
279 private final QName qname;
280 private final SchemaPath path;
281 private String description;
282 private String reference;
283 private Status status;
284 private boolean augmenting;
285 private boolean addedByUses;
286 private boolean configuration;
287 private ConstraintDefinition constraints;
288 private ImmutableSet<ChoiceCaseNode> cases;
289 private ImmutableSet<AugmentationSchema> augmentations;
290 private ImmutableList<UnknownSchemaNode> unknownNodes;
291 private String defaultCase;
293 private ChoiceNodeImpl(final QName qname, final SchemaPath path) {
299 public QName getQName() {
304 public SchemaPath getPath() {
309 public String getDescription() {
314 public String getReference() {
319 public Status getStatus() {
324 public boolean isAugmenting() {
329 public boolean isAddedByUses() {
334 public boolean isConfiguration() {
335 return configuration;
339 public ConstraintDefinition getConstraints() {
344 public Set<AugmentationSchema> getAvailableAugmentations() {
345 return augmentations;
349 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
354 public Set<ChoiceCaseNode> getCases() {
359 public ChoiceCaseNode getCaseNodeByName(final QName name) {
361 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
363 for (final ChoiceCaseNode caseNode : cases) {
364 if (caseNode != null && name.equals(caseNode.getQName())) {
372 public ChoiceCaseNode getCaseNodeByName(final String name) {
374 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
376 for (final ChoiceCaseNode caseNode : cases) {
377 if (caseNode != null && (caseNode.getQName() != null)
378 && name.equals(caseNode.getQName().getLocalName())) {
386 public String getDefaultCase() {
391 public int hashCode() {
392 final int prime = 31;
394 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
395 result = prime * result + ((path == null) ? 0 : path.hashCode());
400 public boolean equals(final Object obj) {
407 if (getClass() != obj.getClass()) {
410 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
412 if (other.qname != null) {
415 } else if (!qname.equals(other.qname)) {
419 if (other.path != null) {
422 } else if (!path.equals(other.path)) {
429 public String toString() {
430 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
432 sb.append("qname=").append(qname);
434 return sb.toString();