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.controller.yang.parser.builder.impl;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
19 import org.opendaylight.controller.yang.common.QName;
20 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
21 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
22 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
23 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
24 import org.opendaylight.controller.yang.model.api.ListSchemaNode;
25 import org.opendaylight.controller.yang.model.api.SchemaPath;
26 import org.opendaylight.controller.yang.model.api.Status;
27 import org.opendaylight.controller.yang.model.api.TypeDefinition;
28 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
29 import org.opendaylight.controller.yang.model.api.UsesNode;
30 import org.opendaylight.controller.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
33 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
34 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
35 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
36 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
37 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
38 import org.opendaylight.controller.yang.parser.util.Comparators;
39 import org.opendaylight.controller.yang.parser.util.YangParseException;
41 public final class ListSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
42 AugmentationTargetBuilder, GroupingMember {
43 private boolean isBuilt;
44 private final ListSchemaNodeImpl instance;
46 private SchemaPath schemaPath;
47 private String description;
48 private String reference;
49 private Status status = Status.CURRENT;
50 // DataSchemaNode args
51 private boolean augmenting;
52 private boolean addedByUses;
53 private Boolean configuration;
54 private final ConstraintsBuilder constraints;
55 // DataNodeContainer args
56 private Set<TypeDefinition<?>> typedefs;
57 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
58 private Set<UsesNode> usesNodes;
59 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
60 // AugmentationTarget args
61 private Set<AugmentationSchema> augmentations;
62 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
63 // ListSchemaNode args
64 private List<QName> keyDefinition = Collections.emptyList();
65 private boolean userOrdered;
67 public ListSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
68 super(moduleName, line, qname);
69 this.schemaPath = schemaPath;
70 instance = new ListSchemaNodeImpl(qname);
71 constraints = new ConstraintsBuilder(moduleName, line);
74 public ListSchemaNodeBuilder(final ListSchemaNodeBuilder b) {
75 super(b.getModuleName(), b.getLine(), b.getQName());
76 instance = new ListSchemaNodeImpl(b.getQName());
77 constraints = b.getConstraints();
78 schemaPath = b.getPath();
79 description = b.getDescription();
80 reference = b.getReference();
81 status = b.getStatus();
82 augmenting = b.isAugmenting();
83 addedByUses = b.isAddedByUses();
84 configuration = b.isConfiguration();
85 keyDefinition = b.getKeyDefinition();
86 userOrdered = b.isUserOrdered();
87 childNodes = b.getChildNodes();
88 addedChildNodes.addAll(b.getChildNodeBuilders());
89 groupings = b.getGroupings();
90 addedGroupings.addAll(b.getGroupingBuilders());
91 typedefs = b.typedefs;
92 addedTypedefs.addAll(b.getTypeDefinitionBuilders());
93 usesNodes = b.usesNodes;
94 addedUsesNodes.addAll(b.getUsesNodes());
95 augmentations = b.augmentations;
96 addedAugmentations.addAll(b.getAugmentations());
97 unknownNodes = b.unknownNodes;
98 addedUnknownNodes.addAll(b.getUnknownNodeBuilders());
102 public ListSchemaNode build() {
104 instance.setKeyDefinition(keyDefinition);
105 instance.setPath(schemaPath);
106 instance.setDescription(description);
107 instance.setReference(reference);
108 instance.setStatus(status);
109 instance.setAugmenting(augmenting);
110 instance.setAddedByUses(addedByUses);
111 instance.setConfiguration(configuration);
112 instance.setUserOrdered(userOrdered);
115 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
116 if (childNodes == null || childNodes.isEmpty()) {
117 for (DataSchemaNodeBuilder node : addedChildNodes) {
118 childs.put(node.getQName(), node.build());
121 for (DataSchemaNode node : childNodes) {
122 childs.put(node.getQName(), node);
125 instance.setChildNodes(childs);
128 if (typedefs == null) {
129 typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
130 for (TypeDefinitionBuilder entry : addedTypedefs) {
131 typedefs.add(entry.build());
134 instance.setTypeDefinitions(typedefs);
137 if (usesNodes == null) {
138 usesNodes = new HashSet<UsesNode>();
139 for (UsesNodeBuilder builder : addedUsesNodes) {
140 usesNodes.add(builder.build());
143 instance.setUses(usesNodes);
146 if (groupings == null) {
147 groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
148 for (GroupingBuilder builder : addedGroupings) {
149 groupings.add(builder.build());
152 instance.setGroupings(groupings);
155 if (augmentations == null) {
156 augmentations = new HashSet<AugmentationSchema>();
157 for (AugmentationSchemaBuilder builder : addedAugmentations) {
158 augmentations.add(builder.build());
161 instance.setAvailableAugmentations(augmentations);
164 if (unknownNodes == null) {
165 unknownNodes = new ArrayList<UnknownSchemaNode>();
166 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
167 unknownNodes.add(b.build());
169 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
171 instance.setUnknownSchemaNodes(unknownNodes);
173 instance.setConstraints(constraints.build());
174 instance.setAvailableAugmentations(augmentations);
182 public void rebuild() {
188 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
189 return addedTypedefs;
193 public void addTypedef(final TypeDefinitionBuilder type) {
194 String typeName = type.getQName().getLocalName();
195 for (TypeDefinitionBuilder addedTypedef : addedTypedefs) {
196 throw new YangParseException(moduleName, type.getLine(), "Can not add typedef '" + typeName
197 + "': typedef with same name already declared at line " + addedTypedef.getLine());
199 addedTypedefs.add(type);
202 public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
203 this.typedefs = typedefs;
206 public SchemaPath getPath() {
211 public void setPath(final SchemaPath schemaPath) {
212 this.schemaPath = schemaPath;
215 public String getDescription() {
220 public void setDescription(final String description) {
221 this.description = description;
224 public String getReference() {
229 public void setReference(String reference) {
230 this.reference = reference;
233 public Status getStatus() {
238 public void setStatus(Status status) {
239 if (status != null) {
240 this.status = status;
244 public Set<UsesNodeBuilder> getUsesNodes() {
245 return addedUsesNodes;
249 public void addUsesNode(final UsesNodeBuilder usesBuilder) {
250 addedUsesNodes.add(usesBuilder);
253 public void setUsesnodes(final Set<UsesNode> usesNodes) {
254 this.usesNodes = usesNodes;
257 public Set<AugmentationSchemaBuilder> getAugmentations() {
258 return addedAugmentations;
262 public void addAugmentation(AugmentationSchemaBuilder augment) {
263 addedAugmentations.add(augment);
266 public void setAugmentations(final Set<AugmentationSchema> augmentations) {
267 this.augmentations = augmentations;
270 public List<QName> getKeyDefinition() {
271 return keyDefinition;
274 public void setKeyDefinition(final List<QName> keyDefinition) {
275 if (keyDefinition != null) {
276 this.keyDefinition = keyDefinition;
280 public boolean isAugmenting() {
285 public void setAugmenting(boolean augmenting) {
286 this.augmenting = augmenting;
290 public boolean isAddedByUses() {
295 public void setAddedByUses(final boolean addedByUses) {
296 this.addedByUses = addedByUses;
299 public Boolean isConfiguration() {
300 return configuration;
304 public void setConfiguration(Boolean configuration) {
305 this.configuration = configuration;
309 public ConstraintsBuilder getConstraints() {
313 public boolean isUserOrdered() {
317 public void setUserOrdered(final boolean userOrdered) {
318 this.userOrdered = userOrdered;
322 public int hashCode() {
323 final int prime = 31;
325 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
330 public boolean equals(Object obj) {
337 if (getClass() != obj.getClass()) {
340 ListSchemaNodeBuilder other = (ListSchemaNodeBuilder) obj;
341 if (schemaPath == null) {
342 if (other.schemaPath != null) {
345 } else if (!schemaPath.equals(other.schemaPath)) {
348 if (parent == null) {
349 if (other.parent != null) {
352 } else if (!parent.equals(other.parent)) {
359 public String toString() {
360 return "list " + qname.getLocalName();
363 public final class ListSchemaNodeImpl implements ListSchemaNode {
364 private final QName qname;
365 private SchemaPath path;
366 private String description;
367 private String reference;
368 private Status status = Status.CURRENT;
369 private List<QName> keyDefinition = Collections.emptyList();
370 private boolean augmenting;
371 private boolean addedByUses;
372 private boolean configuration;
373 private ConstraintDefinition constraints;
374 private Set<AugmentationSchema> augmentations = Collections.emptySet();
375 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
376 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
377 private Set<GroupingDefinition> groupings = Collections.emptySet();
378 private Set<UsesNode> uses = Collections.emptySet();
379 private boolean userOrdered;
380 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
382 private ListSchemaNodeImpl(final QName qname) {
387 public QName getQName() {
392 public SchemaPath getPath() {
396 private void setPath(final SchemaPath path) {
401 public String getDescription() {
405 private void setDescription(final String description) {
406 this.description = description;
410 public String getReference() {
414 private void setReference(final String reference) {
415 this.reference = reference;
419 public Status getStatus() {
423 private void setStatus(Status status) {
424 this.status = status;
428 public List<QName> getKeyDefinition() {
429 return keyDefinition;
432 private void setKeyDefinition(List<QName> keyDefinition) {
433 if (keyDefinition != null) {
434 this.keyDefinition = keyDefinition;
439 public boolean isAugmenting() {
443 private void setAugmenting(boolean augmenting) {
444 this.augmenting = augmenting;
448 public boolean isAddedByUses() {
452 private void setAddedByUses(final boolean addedByUses) {
453 this.addedByUses = addedByUses;
457 public boolean isConfiguration() {
458 return configuration;
461 private void setConfiguration(boolean configuration) {
462 this.configuration = configuration;
466 public ConstraintDefinition getConstraints() {
470 private void setConstraints(ConstraintDefinition constraints) {
471 this.constraints = constraints;
475 public Set<AugmentationSchema> getAvailableAugmentations() {
476 return augmentations;
479 private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
480 if (augmentations != null) {
481 this.augmentations = augmentations;
486 public Set<DataSchemaNode> getChildNodes() {
487 return new HashSet<DataSchemaNode>(childNodes.values());
490 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
491 if (childNodes != null) {
492 this.childNodes = childNodes;
497 public Set<GroupingDefinition> getGroupings() {
501 private void setGroupings(Set<GroupingDefinition> groupings) {
502 if (groupings != null) {
503 this.groupings = groupings;
508 public Set<TypeDefinition<?>> getTypeDefinitions() {
509 return typeDefinitions;
512 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
513 if (typeDefinitions != null) {
514 this.typeDefinitions = typeDefinitions;
519 public Set<UsesNode> getUses() {
523 private void setUses(Set<UsesNode> uses) {
530 public DataSchemaNode getDataChildByName(QName name) {
531 return childNodes.get(name);
535 public DataSchemaNode getDataChildByName(String name) {
536 DataSchemaNode result = null;
537 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
538 if (entry.getKey().getLocalName().equals(name)) {
539 result = entry.getValue();
547 public boolean isUserOrdered() {
551 private void setUserOrdered(boolean userOrdered) {
552 this.userOrdered = userOrdered;
556 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
560 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
561 if (unknownNodes != null) {
562 this.unknownNodes = unknownNodes;
566 public ListSchemaNodeBuilder toBuilder() {
567 return ListSchemaNodeBuilder.this;
571 public int hashCode() {
572 final int prime = 31;
574 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
575 result = prime * result + ((path == null) ? 0 : path.hashCode());
580 public boolean equals(Object obj) {
587 if (getClass() != obj.getClass()) {
590 final ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
592 if (other.qname != null) {
595 } else if (!qname.equals(other.qname)) {
599 if (other.path != null) {
602 } else if (!path.equals(other.path)) {
609 public String toString() {
610 StringBuilder sb = new StringBuilder(ListSchemaNodeImpl.class.getSimpleName());
612 sb.append("qname=" + qname);
613 sb.append(", path=" + path);
614 sb.append(", keyDefinition=" + keyDefinition);
616 return sb.toString();