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.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Objects;
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.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
30 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
32 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
33 DataSchemaNodeBuilder, AugmentationTargetBuilder {
34 private ListSchemaNodeImpl instance;
35 private boolean userOrdered;
36 private Set<String> keys;
37 private List<QName> keyDefinition;
39 private SchemaPath schemaPath;
40 // DataSchemaNode args
41 private boolean augmenting;
42 private boolean addedByUses;
43 private ListSchemaNodeBuilder originalBuilder;
44 private ListSchemaNode originalNode;
45 private boolean configuration;
46 private final ConstraintsBuilder constraints;
47 // AugmentationTarget args
48 private final List<AugmentationSchema> augmentations = new ArrayList<>();
49 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
51 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
52 super(moduleName, line, qname);
53 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
54 constraints = new ConstraintsBuilderImpl(moduleName, line);
57 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
58 final ListSchemaNode base) {
59 super(moduleName, line, qname, path, base);
60 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
61 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
63 keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
64 userOrdered = base.isUserOrdered();
66 augmenting = base.isAugmenting();
67 addedByUses = base.isAddedByUses();
69 configuration = base.isConfiguration();
71 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
73 augmentations.addAll(base.getAvailableAugmentations());
77 public ListSchemaNode build() {
78 if (instance != null) {
82 instance = new ListSchemaNodeImpl(qname, schemaPath, this);
84 instance.augmenting = augmenting;
85 instance.addedByUses = addedByUses;
86 instance.configuration = configuration;
87 instance.constraints = constraints.build();
88 instance.userOrdered = userOrdered;
92 keyDefinition = new ArrayList<>();
93 for (String key : keys) {
94 DataSchemaNode keyPart = instance.getDataChildByName(key);
95 if (keyPart == null) {
96 throw new YangParseException(getModuleName(), getLine(), "Failed to resolve list key for name "
100 if (!(keyPart instanceof LeafSchemaNode)) {
101 throw new YangParseException(getModuleName(), getLine(), "List key : \"" + key
102 + "\" does not reference any Leaf of the List");
105 final QName qname = keyPart.getQName();
106 if (!keyDefinition.contains(qname)) {
107 keyDefinition.add(qname);
110 instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
112 instance.keyDefinition = ImmutableList.of();
116 if (originalNode == null && originalBuilder != null) {
117 originalNode = originalBuilder.build();
119 instance.original = originalNode;
122 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
123 augmentations.add(builder.build());
125 instance.augmentations = ImmutableSet.copyOf(augmentations);
128 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
129 unknownNodes.add(b.build());
131 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
137 protected String getStatementName() {
142 public SchemaPath getPath() {
147 public void setPath(final SchemaPath path) {
148 this.schemaPath = path;
152 public void addAugmentation(final AugmentationSchemaBuilder augment) {
153 augmentationBuilders.add(augment);
156 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
157 return augmentationBuilders;
160 public Set<String> getKeys() {
164 public void setKeys(final Set<String> keys) {
169 public boolean isAugmenting() {
174 public void setAugmenting(final boolean augmenting) {
175 this.augmenting = augmenting;
179 public boolean isAddedByUses() {
184 public void setAddedByUses(final boolean addedByUses) {
185 this.addedByUses = addedByUses;
189 public ListSchemaNodeBuilder getOriginal() {
190 return originalBuilder;
194 public void setOriginal(final SchemaNodeBuilder builder) {
195 Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
196 this.originalBuilder = (ListSchemaNodeBuilder) builder;
200 public boolean isConfiguration() {
201 return configuration;
205 public void setConfiguration(final boolean configuration) {
206 this.configuration = configuration;
210 public ConstraintsBuilder getConstraints() {
214 public boolean isUserOrdered() {
218 public void setUserOrdered(final boolean userOrdered) {
219 this.userOrdered = userOrdered;
223 public int hashCode() {
224 final int prime = 31;
226 result = prime * result + Objects.hashCode(schemaPath);
231 public boolean equals(final Object obj) {
238 if (getClass() != obj.getClass()) {
241 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
242 if (schemaPath == null) {
243 if (other.schemaPath != null) {
246 } else if (!schemaPath.equals(other.schemaPath)) {
249 if (getParent() == null) {
250 if (other.getParent() != null) {
253 } else if (!getParent().equals(other.getParent())) {
260 public String toString() {
261 return "list " + qname.getLocalName();