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.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.ConstraintDefinition;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.Status;
24 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
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.ConstraintsBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDataNodeContainerBuilder;
36 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
38 import com.google.common.base.Preconditions;
39 import com.google.common.collect.ImmutableList;
40 import com.google.common.collect.ImmutableSet;
42 public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
43 AugmentationTargetBuilder {
44 private ListSchemaNodeImpl instance;
45 private boolean userOrdered;
46 private List<String> keys;
47 private List<QName> keyDefinition;
49 private SchemaPath schemaPath;
50 private String description;
51 private String reference;
52 private Status status = Status.CURRENT;
53 // DataSchemaNode args
54 private boolean augmenting;
55 private boolean addedByUses;
56 private boolean configuration;
57 private final ConstraintsBuilder constraints;
58 // AugmentationTarget args
59 private final List<AugmentationSchema> augmentations = new ArrayList<>();
60 private final List<AugmentationSchemaBuilder> augmentationBuilders = new ArrayList<>();
62 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
63 super(moduleName, line, qname);
64 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
65 constraints = new ConstraintsBuilderImpl(moduleName, line);
68 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
69 final ListSchemaNode base) {
70 super(moduleName, line, qname);
71 this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
72 constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
74 keyDefinition = ImmutableList.copyOf(base.getKeyDefinition());
75 userOrdered = base.isUserOrdered();
77 description = base.getDescription();
78 reference = base.getReference();
79 status = base.getStatus();
80 augmenting = base.isAugmenting();
81 addedByUses = base.isAddedByUses();
82 configuration = base.isConfiguration();
84 URI ns = qname.getNamespace();
85 Date rev = qname.getRevision();
86 String pref = qname.getPrefix();
87 addedChildNodes.addAll(BuilderUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
88 addedGroupings.addAll(BuilderUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
89 addedTypedefs.addAll(BuilderUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
90 addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
93 augmentations.addAll(base.getAvailableAugmentations());
94 usesNodes.addAll(base.getUses());
98 public ListSchemaNode build() {
99 if (instance != null) {
103 instance = new ListSchemaNodeImpl(qname, schemaPath);
105 instance.description = description;
106 instance.reference = reference;
107 instance.status = status;
108 instance.augmenting = augmenting;
109 instance.addedByUses = addedByUses;
110 instance.configuration = configuration;
111 instance.constraints = constraints.toInstance();
112 instance.userOrdered = userOrdered;
115 for (DataSchemaNodeBuilder node : addedChildNodes) {
116 childNodes.put(node.getQName(), node.build());
118 instance.childNodes = ImmutableSet.copyOf(childNodes.values());
122 instance.keyDefinition = ImmutableList.of();
124 keyDefinition = new ArrayList<>();
125 for (String key : keys) {
126 keyDefinition.add(instance.getDataChildByName(key).getQName());
128 instance.keyDefinition = ImmutableList.copyOf(keyDefinition);
132 for (GroupingBuilder builder : addedGroupings) {
133 groupings.add(builder.build());
135 instance.groupings = ImmutableSet.copyOf(groupings);
138 for (TypeDefinitionBuilder entry : addedTypedefs) {
139 typedefs.add(entry.build());
141 instance.typeDefinitions = ImmutableSet.copyOf(typedefs);
144 for (UsesNodeBuilder builder : addedUsesNodes) {
145 usesNodes.add(builder.build());
147 instance.uses = ImmutableSet.copyOf(usesNodes);
150 for (AugmentationSchemaBuilder builder : augmentationBuilders) {
151 augmentations.add(builder.build());
153 instance.augmentations = ImmutableSet.copyOf(augmentations);
156 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
157 unknownNodes.add(b.build());
159 instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
165 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
166 return addedTypedefs;
170 public void addTypedef(final TypeDefinitionBuilder type) {
171 String typeName = type.getQName().getLocalName();
172 for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
173 if (addedTypedef.getQName().getLocalName().equals(typeName)) {
174 throw new YangParseException(getModuleName(), type.getLine(), "Can not add typedef '" + typeName
175 + "': typedef with same name already declared at line " + addedTypedef.getLine());
178 addedTypedefs.add(type);
182 public SchemaPath getPath() {
187 public void setPath(final SchemaPath path) {
188 this.schemaPath = path;
192 public String getDescription() {
197 public void setDescription(final String description) {
198 this.description = description;
202 public String getReference() {
207 public void setReference(final String reference) {
208 this.reference = reference;
212 public Status getStatus() {
217 public void setStatus(final Status status) {
218 this.status = Preconditions.checkNotNull(status, "status cannot be null");
222 public void addAugmentation(final AugmentationSchemaBuilder augment) {
223 augmentationBuilders.add(augment);
226 public List<AugmentationSchemaBuilder> getAugmentationBuilders() {
227 return augmentationBuilders;
230 public List<String> getKeys() {
234 public void setKeys(final List<String> keys) {
239 public boolean isAugmenting() {
244 public void setAugmenting(final boolean augmenting) {
245 this.augmenting = augmenting;
249 public boolean isAddedByUses() {
254 public void setAddedByUses(final boolean addedByUses) {
255 this.addedByUses = addedByUses;
259 public boolean isConfiguration() {
260 return configuration;
264 public void setConfiguration(final boolean configuration) {
265 this.configuration = configuration;
269 public ConstraintsBuilder getConstraints() {
273 public boolean isUserOrdered() {
277 public void setUserOrdered(final boolean userOrdered) {
278 this.userOrdered = userOrdered;
282 public int hashCode() {
283 final int prime = 31;
285 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
290 public boolean equals(final Object obj) {
297 if (getClass() != obj.getClass()) {
300 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
301 if (schemaPath == null) {
302 if (other.schemaPath != null) {
305 } else if (!schemaPath.equals(other.schemaPath)) {
308 if (getParent() == null) {
309 if (other.getParent() != null) {
312 } else if (!getParent().equals(other.getParent())) {
319 public String toString() {
320 return "list " + qname.getLocalName();
323 private static final class ListSchemaNodeImpl implements ListSchemaNode {
324 private final QName qname;
325 private final SchemaPath path;
326 private String description;
327 private String reference;
328 private Status status;
329 private ImmutableList<QName> keyDefinition;
330 private boolean augmenting;
331 private boolean addedByUses;
332 private boolean configuration;
333 private ConstraintDefinition constraints;
334 private ImmutableSet<AugmentationSchema> augmentations;
335 private ImmutableSet<DataSchemaNode> childNodes;
336 private ImmutableSet<GroupingDefinition> groupings;
337 private ImmutableSet<TypeDefinition<?>> typeDefinitions;
338 private ImmutableSet<UsesNode> uses;
339 private ImmutableList<UnknownSchemaNode> unknownNodes;
340 private boolean userOrdered;
342 private ListSchemaNodeImpl(final QName qname, final SchemaPath path) {
348 public QName getQName() {
353 public SchemaPath getPath() {
358 public String getDescription() {
363 public String getReference() {
368 public Status getStatus() {
373 public List<QName> getKeyDefinition() {
374 return keyDefinition;
378 public boolean isAugmenting() {
383 public boolean isAddedByUses() {
388 public boolean isConfiguration() {
389 return configuration;
393 public ConstraintDefinition getConstraints() {
398 public Set<AugmentationSchema> getAvailableAugmentations() {
399 return augmentations;
403 public Set<DataSchemaNode> getChildNodes() {
408 public Set<GroupingDefinition> getGroupings() {
413 public Set<TypeDefinition<?>> getTypeDefinitions() {
414 return typeDefinitions;
418 public Set<UsesNode> getUses() {
423 public DataSchemaNode getDataChildByName(final QName name) {
424 return getChildNode(childNodes, name);
428 public DataSchemaNode getDataChildByName(final String name) {
429 return getChildNode(childNodes, name);
433 public boolean isUserOrdered() {
438 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
443 public int hashCode() {
444 final int prime = 31;
446 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
447 result = prime * result + ((path == null) ? 0 : path.hashCode());
452 public boolean equals(final Object obj) {
459 if (getClass() != obj.getClass()) {
462 final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
464 if (other.qname != null) {
467 } else if (!qname.equals(other.qname)) {
471 if (other.path != null) {
474 } else if (!path.equals(other.path)) {
481 public String toString() {
482 return "list " + qname.getLocalName();