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;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.HashSet;
14 import java.util.List;
16 import java.util.TreeSet;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
22 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractSchemaNodeBuilder;
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.DataSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.util.Comparators;
32 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
33 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
35 import com.google.common.collect.ImmutableList;
36 import com.google.common.collect.ImmutableSet;
38 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
39 AugmentationTargetBuilder {
40 private ChoiceNodeImpl instance;
42 // DataSchemaNode args
43 private boolean augmenting;
44 private boolean addedByUses;
45 private boolean configuration;
46 private final ConstraintsBuilder constraints;
47 // AugmentationTarget args
48 private final Set<AugmentationSchema> augmentations = new HashSet<>();
49 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
51 private final Set<ChoiceCaseBuilder> caseBuilders = new HashSet<>();
52 private String defaultCase;
54 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
55 super(moduleName, line, qname);
56 this.schemaPath = path;
57 constraints = new ConstraintsBuilder(moduleName, line);
60 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
61 final ChoiceNode base) {
62 super(moduleName, line, qname);
63 this.schemaPath = path;
64 constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints());
66 description = base.getDescription();
67 reference = base.getReference();
68 status = base.getStatus();
69 augmenting = base.isAugmenting();
70 addedByUses = base.isAddedByUses();
71 configuration = base.isConfiguration();
72 augmentations.addAll(base.getAvailableAugmentations());
74 URI ns = qname.getNamespace();
75 Date rev = qname.getRevision();
76 String pref = qname.getPrefix();
77 Set<DataSchemaNodeBuilder> wrapped = ParserUtils.wrapChildNodes(moduleName, line, new HashSet<DataSchemaNode>(
78 base.getCases()), path, ns, rev, pref);
79 for (DataSchemaNodeBuilder wrap : wrapped) {
80 if (wrap instanceof ChoiceCaseBuilder) {
81 caseBuilders.add((ChoiceCaseBuilder) wrap);
85 addedUnknownNodes.addAll(ParserUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
90 public ChoiceNode build() {
91 if (instance != null) {
95 instance = new ChoiceNodeImpl(qname, schemaPath);
97 instance.description = description;
98 instance.reference = reference;
99 instance.status = status;
100 instance.augmenting = augmenting;
101 instance.addedByUses = addedByUses;
102 instance.configuration = configuration;
104 instance.constraints = constraints.build();
105 instance.defaultCase = defaultCase;
108 final Set<ChoiceCaseNode> cases = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
109 for (ChoiceCaseBuilder caseBuilder : caseBuilders) {
110 cases.add(caseBuilder.build());
112 instance.cases = ImmutableSet.copyOf(cases);
115 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
116 augmentations.add(builder.build());
118 instance.augmentations = ImmutableSet.copyOf(augmentations);
121 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
122 unknownNodes.add(b.build());
124 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
129 public Set<ChoiceCaseBuilder> getCases() {
137 * name of case to search
138 * @return case with given name if present, null otherwise
140 public ChoiceCaseBuilder getCaseNodeByName(final String caseName) {
141 for (ChoiceCaseBuilder addedCase : caseBuilders) {
142 if (addedCase.getQName().getLocalName().equals(caseName)) {
150 * Add case node to this choice.
152 * If node is not declared with 'case' keyword, create new case builder and
153 * make this node child of newly created case.
158 public void addCase(final DataSchemaNodeBuilder caseNode) {
159 QName caseQName = caseNode.getQName();
160 String caseName = caseQName.getLocalName();
162 for (ChoiceCaseBuilder existingCase : caseBuilders) {
163 if (existingCase.getQName().getLocalName().equals(caseName)) {
164 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
165 + "' to node '" + qname.getLocalName() + "' in module '" + getModuleName()
166 + "': case with same name already declared at line " + existingCase.getLine());
170 if (caseNode instanceof ChoiceCaseBuilder) {
171 caseBuilders.add((ChoiceCaseBuilder) caseNode);
173 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
174 caseQName, caseNode.getPath());
175 if (caseNode.isAugmenting()) {
176 // if node is added by augmentation, set case builder augmenting
177 // as true and node augmenting as false
178 caseBuilder.setAugmenting(true);
179 caseNode.setAugmenting(false);
181 SchemaPath newPath = caseNode.getPath().createChild(caseQName);
182 caseNode.setPath(newPath);
183 caseBuilder.addChildNode(caseNode);
184 caseBuilders.add(caseBuilder);
189 public boolean isAugmenting() {
194 public void setAugmenting(final boolean augmenting) {
195 this.augmenting = augmenting;
199 public boolean isAddedByUses() {
204 public void setAddedByUses(final boolean addedByUses) {
205 this.addedByUses = addedByUses;
209 public boolean isConfiguration() {
210 return configuration;
214 public void setConfiguration(final boolean configuration) {
215 this.configuration = configuration;
219 public ConstraintsBuilder getConstraints() {
224 public void addAugmentation(final AugmentationSchemaBuilder augment) {
225 augmentationBuilders.add(augment);
228 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
229 return augmentationBuilders;
232 public String getDefaultCase() {
236 public void setDefaultCase(final String defaultCase) {
237 this.defaultCase = defaultCase;
241 public int hashCode() {
242 final int prime = 31;
244 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
249 public boolean equals(final Object obj) {
256 if (getClass() != obj.getClass()) {
259 ChoiceBuilder other = (ChoiceBuilder) obj;
260 if (schemaPath == null) {
261 if (other.schemaPath != null) {
264 } else if (!schemaPath.equals(other.schemaPath)) {
267 if (getParent() == null) {
268 if (other.getParent() != null) {
271 } else if (!getParent().equals(other.getParent())) {
278 public String toString() {
279 return "choice " + qname.getLocalName();
282 private static final class ChoiceNodeImpl implements ChoiceNode {
283 private final QName qname;
284 private final SchemaPath path;
285 private String description;
286 private String reference;
287 private Status status;
288 private boolean augmenting;
289 private boolean addedByUses;
290 private boolean configuration;
291 private ConstraintDefinition constraints;
292 private ImmutableSet<ChoiceCaseNode> cases;
293 private ImmutableSet<AugmentationSchema> augmentations;
294 private ImmutableList<UnknownSchemaNode> unknownNodes;
295 private String defaultCase;
297 private ChoiceNodeImpl(final QName qname, final SchemaPath path) {
303 public QName getQName() {
308 public SchemaPath getPath() {
313 public String getDescription() {
318 public String getReference() {
323 public Status getStatus() {
328 public boolean isAugmenting() {
333 public boolean isAddedByUses() {
338 public boolean isConfiguration() {
339 return configuration;
343 public ConstraintDefinition getConstraints() {
348 public Set<AugmentationSchema> getAvailableAugmentations() {
349 return augmentations;
353 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
358 public Set<ChoiceCaseNode> getCases() {
363 public ChoiceCaseNode getCaseNodeByName(final QName name) {
365 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
367 for (final ChoiceCaseNode caseNode : cases) {
368 if (caseNode != null && name.equals(caseNode.getQName())) {
376 public ChoiceCaseNode getCaseNodeByName(final String name) {
378 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
380 for (final ChoiceCaseNode caseNode : cases) {
381 if (caseNode != null && (caseNode.getQName() != null)
382 && name.equals(caseNode.getQName().getLocalName())) {
390 public String getDefaultCase() {
395 public int hashCode() {
396 final int prime = 31;
398 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
399 result = prime * result + ((path == null) ? 0 : path.hashCode());
404 public boolean equals(final Object obj) {
411 if (getClass() != obj.getClass()) {
414 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
416 if (other.qname != null) {
419 } else if (!qname.equals(other.qname)) {
423 if (other.path != null) {
426 } else if (!path.equals(other.path)) {
433 public String toString() {
434 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
436 sb.append("qname=" + qname);
438 return sb.toString();