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;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
31 public final class ListSchemaNodeBuilder extends AbstractDocumentedDataNodeContainerBuilder implements
32 DataSchemaNodeBuilder, AugmentationTargetBuilder {
33 private ListSchemaNodeImpl instance;
34 private boolean userOrdered;
35 private Set<String> keys;
36 private List<QName> keyDefinition;
38 private SchemaPath schemaPath;
39 // DataSchemaNode args
40 private boolean augmenting;
41 private boolean addedByUses;
42 private ListSchemaNodeBuilder originalBuilder;
43 private ListSchemaNode originalNode;
44 private boolean configuration;
45 private final ConstraintsBuilder constraints;
46 // AugmentationTarget args
47 private final List<AugmentationSchema> augmentations = new ArrayList<>();
48 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
50 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
51 super(moduleName, line, qname);
52 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
53 constraints = new ConstraintsBuilderImpl(moduleName, line);
56 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
57 final ListSchemaNode base) {
58 super(moduleName, line, qname, path, base);
59 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
60 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
62 keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
63 userOrdered = base.isUserOrdered();
65 augmenting = base.isAugmenting();
66 addedByUses = base.isAddedByUses();
68 configuration = base.isConfiguration();
70 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
72 augmentations.addAll(base.getAvailableAugmentations());
76 public ListSchemaNode build() {
77 if (instance != null) {
81 instance = new ListSchemaNodeImpl(qname, schemaPath, this);
83 instance.augmenting = augmenting;
84 instance.addedByUses = addedByUses;
85 instance.configuration = configuration;
86 instance.constraints = constraints.toInstance();
87 instance.userOrdered = userOrdered;
91 keyDefinition = new ArrayList<>();
92 for (String key : keys) {
93 DataSchemaNode keyPart = instance.getDataChildByName(key);
94 if (keyPart == null) {
95 throw new YangParseException(getModuleName(), getLine(), "Failed to resolve list key for name "
99 if (!(keyPart instanceof LeafSchemaNode)) {
100 throw new YangParseException(getModuleName(), getLine(), "List key : \"" + key
101 + "\" does not reference any Leaf of the List");
104 final QName qname = keyPart.getQName();
105 if (!keyDefinition.contains(qname)) {
106 keyDefinition.add(qname);
109 instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
111 instance.keyDefinition = ImmutableList.of();
115 if (originalNode == null && originalBuilder != null) {
116 originalNode = originalBuilder.build();
118 instance.original = originalNode;
121 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
122 augmentations.add(builder.build());
124 instance.augmentations = ImmutableSet.copyOf(augmentations);
127 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
128 unknownNodes.add(b.build());
130 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
136 protected String getStatementName() {
141 public SchemaPath getPath() {
146 public void setPath(final SchemaPath path) {
147 this.schemaPath = path;
151 public void addAugmentation(final AugmentationSchemaBuilder augment) {
152 augmentationBuilders.add(augment);
155 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
156 return augmentationBuilders;
159 public Set<String> getKeys() {
163 public void setKeys(final Set<String> keys) {
168 public boolean isAugmenting() {
173 public void setAugmenting(final boolean augmenting) {
174 this.augmenting = augmenting;
178 public boolean isAddedByUses() {
183 public void setAddedByUses(final boolean addedByUses) {
184 this.addedByUses = addedByUses;
188 public ListSchemaNodeBuilder getOriginal() {
189 return originalBuilder;
193 public void setOriginal(final SchemaNodeBuilder builder) {
194 Preconditions.checkArgument(builder instanceof ListSchemaNodeBuilder, "Original of list cannot be " + builder);
195 this.originalBuilder = (ListSchemaNodeBuilder) builder;
199 public boolean isConfiguration() {
200 return configuration;
204 public void setConfiguration(final boolean configuration) {
205 this.configuration = configuration;
209 public ConstraintsBuilder getConstraints() {
213 public boolean isUserOrdered() {
217 public void setUserOrdered(final boolean userOrdered) {
218 this.userOrdered = userOrdered;
222 public int hashCode() {
223 final int prime = 31;
225 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
230 public boolean equals(final Object obj) {
237 if (getClass() != obj.getClass()) {
240 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
241 if (schemaPath == null) {
242 if (other.schemaPath != null) {
245 } else if (!schemaPath.equals(other.schemaPath)) {
248 if (getParent() == null) {
249 if (other.getParent() != null) {
252 } else if (!getParent().equals(other.getParent())) {
259 public String toString() {
260 return "list " + qname.getLocalName();