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.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.AbstractDocumentedDataNodeContainer;
33 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
34 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
36 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
37 DataSchemaNodeBuilder, AugmentationTargetBuilder {
38 private ListSchemaNodeImpl instance;
39 private boolean userOrdered;
40 private Set<String> keys;
41 private List<QName> keyDefinition;
43 private SchemaPath schemaPath;
44 // DataSchemaNode args
45 private boolean augmenting;
46 private boolean addedByUses;
47 private ListSchemaNodeBuilder originalBuilder;
48 private ListSchemaNode originalNode;
49 private boolean configuration;
50 private final ConstraintsBuilder constraints;
51 // AugmentationTarget args
52 private final List<AugmentationSchema> augmentations = new ArrayList<>();
53 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
55 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
56 super(moduleName, line, qname);
57 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
58 constraints = new ConstraintsBuilderImpl(moduleName, line);
61 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
62 final ListSchemaNode base) {
63 super(moduleName, line, qname, path, base);
64 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
65 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
67 keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
68 userOrdered = base.isUserOrdered();
70 augmenting = base.isAugmenting();
71 addedByUses = base.isAddedByUses();
73 configuration = base.isConfiguration();
75 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
77 augmentations.addAll(base.getAvailableAugmentations());
81 public ListSchemaNode build() {
82 if (instance != null) {
86 instance = new ListSchemaNodeImpl(qname, schemaPath, this);
88 instance.augmenting = augmenting;
89 instance.addedByUses = addedByUses;
90 instance.configuration = configuration;
91 instance.constraints = constraints.toInstance();
92 instance.userOrdered = userOrdered;
96 keyDefinition = new ArrayList<>();
97 for (String key : keys) {
98 DataSchemaNode keyPart = instance.getDataChildByName(key);
99 if (keyPart == null) {
100 throw new YangParseException(getModuleName(), getLine(), "Failed to resolve list key for name "
104 if (!(keyPart instanceof LeafSchemaNode)) {
105 throw new YangParseException(getModuleName(), getLine(), "List key : \"" + key
106 + "\" does not reference any Leaf of the List");
109 final QName qname = keyPart.getQName();
110 if (!keyDefinition.contains(qname)) {
111 keyDefinition.add(qname);
114 instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
116 instance.keyDefinition = ImmutableList.of();
120 if (originalNode == null && originalBuilder != null) {
121 originalNode = originalBuilder.build();
123 instance.original = originalNode;
126 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
127 augmentations.add(builder.build());
129 instance.augmentations = ImmutableSet.copyOf(augmentations);
132 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
133 unknownNodes.add(b.build());
135 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
141 protected String getStatementName() {
146 public SchemaPath getPath() {
151 public void setPath(final SchemaPath path) {
152 this.schemaPath = path;
156 public void addAugmentation(final AugmentationSchemaBuilder augment) {
157 augmentationBuilders.add(augment);
160 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
161 return augmentationBuilders;
164 public Set<String> getKeys() {
168 public void setKeys(final Set<String> keys) {
173 public boolean isAugmenting() {
178 public void setAugmenting(final boolean augmenting) {
179 this.augmenting = augmenting;
183 public boolean isAddedByUses() {
188 public void setAddedByUses(final boolean addedByUses) {
189 this.addedByUses = addedByUses;
193 public ListSchemaNodeBuilder getOriginal() {
194 return originalBuilder;
198 public void setOriginal(final SchemaNodeBuilder builder) {
199 Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
200 this.originalBuilder = (ListSchemaNodeBuilder) builder;
204 public boolean isConfiguration() {
205 return configuration;
209 public void setConfiguration(final boolean configuration) {
210 this.configuration = configuration;
214 public ConstraintsBuilder getConstraints() {
218 public boolean isUserOrdered() {
222 public void setUserOrdered(final boolean userOrdered) {
223 this.userOrdered = userOrdered;
227 public int hashCode() {
228 final int prime = 31;
230 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
235 public boolean equals(final Object obj) {
242 if (getClass() != obj.getClass()) {
245 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
246 if (schemaPath == null) {
247 if (other.schemaPath != null) {
250 } else if (!schemaPath.equals(other.schemaPath)) {
253 if (getParent() == null) {
254 if (other.getParent() != null) {
257 } else if (!getParent().equals(other.getParent())) {
264 public String toString() {
265 return "list " + qname.getLocalName();
268 private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
269 ListSchemaNode, DerivableSchemaNode {
270 private final QName qname;
271 private final SchemaPath path;
272 private ImmutableList<QName> keyDefinition;
273 private boolean augmenting;
274 private boolean addedByUses;
275 private ListSchemaNode original;
276 private boolean configuration;
277 private ConstraintDefinition constraints;
278 private ImmutableSet<AugmentationSchema> augmentations;
279 private ImmutableList<UnknownSchemaNode> unknownNodes;
280 private boolean userOrdered;
282 private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
289 public QName getQName() {
294 public SchemaPath getPath() {
299 public List<QName> getKeyDefinition() {
300 return keyDefinition;
304 public boolean isAugmenting() {
309 public boolean isAddedByUses() {
314 public Optional<ListSchemaNode> getOriginal() {
315 return Optional.fromNullable(original);
319 public boolean isConfiguration() {
320 return configuration;
324 public ConstraintDefinition getConstraints() {
329 public Set<AugmentationSchema> getAvailableAugmentations() {
330 return augmentations;
334 public boolean isUserOrdered() {
339 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
344 public int hashCode() {
345 final int prime = 31;
347 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
348 result = prime * result + ((path == null) ? 0 : path.hashCode());
353 public boolean equals(final Object obj) {
360 if (getClass() != obj.getClass()) {
363 final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
365 if (other.qname != null) {
368 } else if (!qname.equals(other.qname)) {
372 if (other.path != null) {
375 } else if (!path.equals(other.path)) {
382 public String toString() {
383 return "list " + qname.getLocalName();