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.ListSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
32 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
33 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
35 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
36 DataSchemaNodeBuilder, AugmentationTargetBuilder {
37 private ListSchemaNodeImpl instance;
38 private boolean userOrdered;
39 private List<String> keys;
40 private List<QName> keyDefinition;
42 private SchemaPath schemaPath;
43 // DataSchemaNode args
44 private boolean augmenting;
45 private boolean addedByUses;
46 private ListSchemaNodeBuilder originalBuilder;
47 private ListSchemaNode originalNode;
48 private boolean configuration;
49 private final ConstraintsBuilder constraints;
50 // AugmentationTarget args
51 private final List<AugmentationSchema> augmentations = new ArrayList<>();
52 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
54 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
55 super(moduleName, line, qname);
56 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
57 constraints = new ConstraintsBuilderImpl(moduleName, line);
60 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
61 final ListSchemaNode base) {
62 super(moduleName, line, qname, path, base);
63 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
64 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
66 keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
67 userOrdered = base.isUserOrdered();
69 augmenting = base.isAugmenting();
70 addedByUses = base.isAddedByUses();
72 configuration = base.isConfiguration();
74 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
76 augmentations.addAll(base.getAvailableAugmentations());
80 public ListSchemaNode build() {
81 if (instance != null) {
85 instance = new ListSchemaNodeImpl(qname, schemaPath, this);
87 instance.augmenting = augmenting;
88 instance.addedByUses = addedByUses;
89 instance.configuration = configuration;
90 instance.constraints = constraints.toInstance();
91 instance.userOrdered = userOrdered;
95 instance.keyDefinition = ImmutableList.of();
97 keyDefinition = new ArrayList<>();
98 for (String key : keys) {
99 DataSchemaNode keyPart = instance.getDataChildByName(key);
100 if (keyPart == null) {
101 throw new YangParseException(getModuleName(), getLine(), "Failed to resolve list key for name "
104 keyDefinition.add(keyPart.getQName());
106 instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
110 if (originalNode == null && originalBuilder != null) {
111 originalNode = originalBuilder.build();
113 instance.original = originalNode;
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);
131 protected String getStatementName() {
136 public SchemaPath getPath() {
141 public void setPath(final SchemaPath path) {
142 this.schemaPath = path;
146 public void addAugmentation(final AugmentationSchemaBuilder augment) {
147 augmentationBuilders.add(augment);
150 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
151 return augmentationBuilders;
154 public List<String> getKeys() {
158 public void setKeys(final List<String> keys) {
163 public boolean isAugmenting() {
168 public void setAugmenting(final boolean augmenting) {
169 this.augmenting = augmenting;
173 public boolean isAddedByUses() {
178 public void setAddedByUses(final boolean addedByUses) {
179 this.addedByUses = addedByUses;
183 public ListSchemaNodeBuilder getOriginal() {
184 return originalBuilder;
188 public void setOriginal(final SchemaNodeBuilder builder) {
189 Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
190 this.originalBuilder = (ListSchemaNodeBuilder) builder;
194 public boolean isConfiguration() {
195 return configuration;
199 public void setConfiguration(final boolean configuration) {
200 this.configuration = configuration;
204 public ConstraintsBuilder getConstraints() {
208 public boolean isUserOrdered() {
212 public void setUserOrdered(final boolean userOrdered) {
213 this.userOrdered = userOrdered;
217 public int hashCode() {
218 final int prime = 31;
220 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
225 public boolean equals(final Object obj) {
232 if (getClass() != obj.getClass()) {
235 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
236 if (schemaPath == null) {
237 if (other.schemaPath != null) {
240 } else if (!schemaPath.equals(other.schemaPath)) {
243 if (getParent() == null) {
244 if (other.getParent() != null) {
247 } else if (!getParent().equals(other.getParent())) {
254 public String toString() {
255 return "list " + qname.getLocalName();
258 private static final class ListSchemaNodeImpl extends AbstractDocumentedDataNodeContainer implements
259 ListSchemaNode, DerivableSchemaNode {
260 private final QName qname;
261 private final SchemaPath path;
262 private ImmutableList<QName> keyDefinition;
263 private boolean augmenting;
264 private boolean addedByUses;
265 private ListSchemaNode original;
266 private boolean configuration;
267 private ConstraintDefinition constraints;
268 private ImmutableSet<AugmentationSchema> augmentations;
269 private ImmutableList<UnknownSchemaNode> unknownNodes;
270 private boolean userOrdered;
272 private ListSchemaNodeImpl(final QName qname, final SchemaPath path, final ListSchemaNodeBuilder builder) {
279 public QName getQName() {
284 public SchemaPath getPath() {
289 public List<QName> getKeyDefinition() {
290 return keyDefinition;
294 public boolean isAugmenting() {
299 public boolean isAddedByUses() {
304 public Optional<ListSchemaNode> getOriginal() {
305 return Optional.fromNullable(original);
309 public boolean isConfiguration() {
310 return configuration;
314 public ConstraintDefinition getConstraints() {
319 public Set<AugmentationSchema> getAvailableAugmentations() {
320 return augmentations;
324 public boolean isUserOrdered() {
329 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
334 public int hashCode() {
335 final int prime = 31;
337 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
338 result = prime * result + ((path == null) ? 0 : path.hashCode());
343 public boolean equals(final Object obj) {
350 if (getClass() != obj.getClass()) {
353 final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
355 if (other.qname != null) {
358 } else if (!qname.equals(other.qname)) {
362 if (other.path != null) {
365 } else if (!path.equals(other.path)) {
372 public String toString() {
373 return "list " + qname.getLocalName();