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.AugmentationSchemaBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
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.collect.ImmutableList;
37 import com.google.common.collect.ImmutableSet;
39 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
40 AugmentationTargetBuilder {
41 private ChoiceNodeImpl instance;
43 // DataSchemaNode args
44 private boolean augmenting;
45 private boolean addedByUses;
46 private boolean configuration;
47 private final ConstraintsBuilder constraints;
48 // AugmentationTarget args
49 private final Set<AugmentationSchema> augmentations = new HashSet<>();
50 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
52 private final Set<ChoiceCaseBuilder> caseBuilders = new HashSet<>();
53 private String defaultCase;
55 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
56 super(moduleName, line, qname);
57 this.schemaPath = path;
58 constraints = new ConstraintsBuilderImpl(moduleName, line);
61 public ChoiceBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
62 final ChoiceNode base) {
63 super(moduleName, line, qname);
64 this.schemaPath = path;
65 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
67 description = base.getDescription();
68 reference = base.getReference();
69 status = base.getStatus();
70 augmenting = base.isAugmenting();
71 addedByUses = base.isAddedByUses();
72 configuration = base.isConfiguration();
73 augmentations.addAll(base.getAvailableAugmentations());
75 URI ns = qname.getNamespace();
76 Date rev = qname.getRevision();
77 String pref = qname.getPrefix();
78 Set<DataSchemaNodeBuilder> wrapped = BuilderUtils.wrapChildNodes(moduleName, line, new HashSet<DataSchemaNode>(
79 base.getCases()), path, ns, rev, pref);
80 for (DataSchemaNodeBuilder wrap : wrapped) {
81 if (wrap instanceof ChoiceCaseBuilder) {
82 caseBuilders.add((ChoiceCaseBuilder) wrap);
86 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
91 public ChoiceNode build() {
92 if (instance != null) {
96 instance = new ChoiceNodeImpl(qname, schemaPath);
98 instance.description = description;
99 instance.reference = reference;
100 instance.status = status;
101 instance.augmenting = augmenting;
102 instance.addedByUses = addedByUses;
103 instance.configuration = configuration;
105 instance.constraints = constraints.toInstance();
106 instance.defaultCase = defaultCase;
109 final Set<ChoiceCaseNode> cases = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
110 for (ChoiceCaseBuilder caseBuilder : caseBuilders) {
111 cases.add(caseBuilder.build());
113 instance.cases = ImmutableSet.copyOf(cases);
116 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
117 augmentations.add(builder.build());
119 instance.augmentations = ImmutableSet.copyOf(augmentations);
122 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
123 unknownNodes.add(b.build());
125 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
130 public Set<ChoiceCaseBuilder> getCases() {
138 * name of case to search
139 * @return case with given name if present, null otherwise
141 public ChoiceCaseBuilder getCaseNodeByName(final String caseName) {
142 for (ChoiceCaseBuilder addedCase : caseBuilders) {
143 if (addedCase.getQName().getLocalName().equals(caseName)) {
151 * Add case node to this choice.
153 * If node is not declared with 'case' keyword, create new case builder and
154 * make this node child of newly created case.
159 public void addCase(final DataSchemaNodeBuilder caseNode) {
160 QName caseQName = caseNode.getQName();
161 String caseName = caseQName.getLocalName();
163 for (ChoiceCaseBuilder existingCase : caseBuilders) {
164 if (existingCase.getQName().getLocalName().equals(caseName)) {
165 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
166 + "' to node '" + qname.getLocalName() + "' in module '" + getModuleName()
167 + "': case with same name already declared at line " + existingCase.getLine());
171 if (caseNode instanceof ChoiceCaseBuilder) {
172 caseBuilders.add((ChoiceCaseBuilder) caseNode);
174 ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
175 caseQName, caseNode.getPath());
176 if (caseNode.isAugmenting()) {
177 // if node is added by augmentation, set case builder augmenting
178 // as true and node augmenting as false
179 caseBuilder.setAugmenting(true);
180 caseNode.setAugmenting(false);
182 SchemaPath newPath = caseNode.getPath().createChild(caseQName);
183 caseNode.setPath(newPath);
184 caseBuilder.addChildNode(caseNode);
185 caseBuilders.add(caseBuilder);
190 public boolean isAugmenting() {
195 public void setAugmenting(final boolean augmenting) {
196 this.augmenting = augmenting;
200 public boolean isAddedByUses() {
205 public void setAddedByUses(final boolean addedByUses) {
206 this.addedByUses = addedByUses;
210 public boolean isConfiguration() {
211 return configuration;
215 public void setConfiguration(final boolean configuration) {
216 this.configuration = configuration;
220 public ConstraintsBuilder getConstraints() {
225 public void addAugmentation(final AugmentationSchemaBuilder augment) {
226 augmentationBuilders.add(augment);
229 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
230 return augmentationBuilders;
233 public String getDefaultCase() {
237 public void setDefaultCase(final String defaultCase) {
238 this.defaultCase = defaultCase;
242 public int hashCode() {
243 final int prime = 31;
245 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
250 public boolean equals(final Object obj) {
257 if (getClass() != obj.getClass()) {
260 ChoiceBuilder other = (ChoiceBuilder) obj;
261 if (schemaPath == null) {
262 if (other.schemaPath != null) {
265 } else if (!schemaPath.equals(other.schemaPath)) {
268 if (getParent() == null) {
269 if (other.getParent() != null) {
272 } else if (!getParent().equals(other.getParent())) {
279 public String toString() {
280 return "choice " + qname.getLocalName();
283 private static final class ChoiceNodeImpl implements ChoiceNode {
284 private final QName qname;
285 private final SchemaPath path;
286 private String description;
287 private String reference;
288 private Status status;
289 private boolean augmenting;
290 private boolean addedByUses;
291 private boolean configuration;
292 private ConstraintDefinition constraints;
293 private ImmutableSet<ChoiceCaseNode> cases;
294 private ImmutableSet<AugmentationSchema> augmentations;
295 private ImmutableList<UnknownSchemaNode> unknownNodes;
296 private String defaultCase;
298 private ChoiceNodeImpl(final QName qname, final SchemaPath path) {
304 public QName getQName() {
309 public SchemaPath getPath() {
314 public String getDescription() {
319 public String getReference() {
324 public Status getStatus() {
329 public boolean isAugmenting() {
334 public boolean isAddedByUses() {
339 public boolean isConfiguration() {
340 return configuration;
344 public ConstraintDefinition getConstraints() {
349 public Set<AugmentationSchema> getAvailableAugmentations() {
350 return augmentations;
354 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
359 public Set<ChoiceCaseNode> getCases() {
364 public ChoiceCaseNode getCaseNodeByName(final QName name) {
366 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
368 for (final ChoiceCaseNode caseNode : cases) {
369 if (caseNode != null && name.equals(caseNode.getQName())) {
377 public ChoiceCaseNode getCaseNodeByName(final String name) {
379 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
381 for (final ChoiceCaseNode caseNode : cases) {
382 if (caseNode != null && (caseNode.getQName() != null)
383 && name.equals(caseNode.getQName().getLocalName())) {
391 public String getDefaultCase() {
396 public int hashCode() {
397 final int prime = 31;
399 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
400 result = prime * result + ((path == null) ? 0 : path.hashCode());
405 public boolean equals(final Object obj) {
412 if (getClass() != obj.getClass()) {
415 ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
417 if (other.qname != null) {
420 } else if (!qname.equals(other.qname)) {
424 if (other.path != null) {
427 } else if (!path.equals(other.path)) {
434 public String toString() {
435 StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
437 sb.append("qname=" + qname);
439 return sb.toString();