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.ContainerSchemaNode;
23 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
24 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
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.ConfigNode;
34 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
35 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
36 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
37 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
38 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
39 import org.opendaylight.controller.yang.parser.util.Comparators;
41 public final class ContainerSchemaNodeBuilder extends AbstractDataNodeContainerBuilder implements
42 AugmentationTargetBuilder, DataSchemaNodeBuilder, GroupingMember, ConfigNode {
43 private boolean isBuilt;
44 private final ContainerSchemaNodeImpl instance;
47 private SchemaPath schemaPath;
48 private String description;
49 private String reference;
50 private Status status = Status.CURRENT;
51 private List<UnknownSchemaNode> unknownNodes;
52 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
53 // DataSchemaNode args
54 private boolean augmenting;
55 private boolean addedByUses;
56 private Boolean configuration;
57 private final ConstraintsBuilder constraints;
58 // DataNodeContainer args
59 private Set<TypeDefinition<?>> typedefs;
60 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
61 private Set<UsesNode> usesNodes;
62 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
63 // AugmentationTarget args
64 private Set<AugmentationSchema> augmentations;
65 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
66 // ContainerSchemaNode args
67 private boolean presence;
69 public ContainerSchemaNodeBuilder(final int line, final QName qname, final SchemaPath schemaPath) {
71 this.schemaPath = schemaPath;
72 instance = new ContainerSchemaNodeImpl(qname);
73 constraints = new ConstraintsBuilder(line);
76 public ContainerSchemaNodeBuilder(final ContainerSchemaNodeBuilder b) {
77 super(b.getLine(), b.getQName());
78 instance = new ContainerSchemaNodeImpl(b.getQName());
79 constraints = b.getConstraints();
80 schemaPath = b.getPath();
81 description = b.getDescription();
82 reference = b.getReference();
83 status = b.getStatus();
84 presence = b.isPresence();
85 augmenting = b.isAugmenting();
86 addedByUses = b.isAddedByUses();
87 configuration = b.isConfiguration();
88 childNodes = b.getChildNodes();
89 addedChildNodes.addAll(b.getChildNodeBuilders());
90 groupings = b.getGroupings();
91 addedGroupings.addAll(b.getGroupingBuilders());
92 typedefs = b.typedefs;
93 addedTypedefs.addAll(b.getTypeDefinitionBuilders());
94 usesNodes = b.usesNodes;
95 addedUsesNodes.addAll(b.getUsesNodes());
96 augmentations = b.augmentations;
97 addedAugmentations.addAll(b.getAugmentations());
98 unknownNodes = b.unknownNodes;
99 addedUnknownNodes.addAll(b.getUnknownNodes());
103 public ContainerSchemaNode build() {
105 instance.setPath(schemaPath);
106 instance.setDescription(description);
107 instance.setReference(reference);
108 instance.setStatus(status);
109 instance.setPresenceContainer(presence);
110 instance.setAugmenting(augmenting);
111 instance.setAddedByUses(addedByUses);
113 // if this builder represents rpc input or output, it can has
114 // configuration value set to null
115 if (configuration == null) {
116 configuration = false;
118 instance.setConfiguration(configuration);
121 final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
122 if (childNodes == null) {
123 for (DataSchemaNodeBuilder node : addedChildNodes) {
124 childs.put(node.getQName(), node.build());
127 for (DataSchemaNode node : childNodes) {
128 childs.put(node.getQName(), node);
131 instance.setChildNodes(childs);
134 if (groupings == null) {
135 groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
136 for (GroupingBuilder builder : addedGroupings) {
137 groupings.add(builder.build());
140 instance.setGroupings(groupings);
143 if (typedefs == null) {
144 typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
145 for (TypeDefinitionBuilder entry : addedTypedefs) {
146 typedefs.add(entry.build());
149 instance.setTypeDefinitions(typedefs);
152 if (usesNodes == null) {
153 usesNodes = new HashSet<UsesNode>();
154 for (UsesNodeBuilder builder : addedUsesNodes) {
155 usesNodes.add(builder.build());
158 instance.setUses(usesNodes);
161 if (augmentations == null) {
162 augmentations = new HashSet<AugmentationSchema>();
163 for (AugmentationSchemaBuilder builder : addedAugmentations) {
164 augmentations.add(builder.build());
167 instance.setAvailableAugmentations(augmentations);
170 if (unknownNodes == null) {
171 unknownNodes = new ArrayList<UnknownSchemaNode>();
172 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
173 unknownNodes.add(b.build());
175 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
177 instance.setUnknownSchemaNodes(unknownNodes);
179 instance.setConstraints(constraints.build());
180 instance.setAvailableAugmentations(augmentations);
188 public void rebuild() {
194 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
195 return addedTypedefs;
199 public void addTypedef(final TypeDefinitionBuilder type) {
200 addedTypedefs.add(type);
203 public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
204 this.typedefs = typedefs;
207 public Set<AugmentationSchemaBuilder> getAugmentations() {
208 return addedAugmentations;
212 public void addAugmentation(AugmentationSchemaBuilder augment) {
213 addedAugmentations.add(augment);
216 public void setAugmentations(final Set<AugmentationSchema> augmentations) {
217 this.augmentations = augmentations;
220 public SchemaPath getPath() {
225 public void setPath(final SchemaPath schemaPath) {
226 this.schemaPath = schemaPath;
230 public String getDescription() {
235 public void setDescription(final String description) {
236 this.description = description;
240 public String getReference() {
245 public void setReference(String reference) {
246 this.reference = reference;
250 public Status getStatus() {
255 public void setStatus(Status status) {
256 if (status != null) {
257 this.status = status;
262 public boolean isAugmenting() {
267 public void setAugmenting(boolean augmenting) {
268 this.augmenting = augmenting;
272 public boolean isAddedByUses() {
277 public void setAddedByUses(final boolean addedByUses) {
278 this.addedByUses = addedByUses;
282 public Boolean isConfiguration() {
283 return configuration;
287 public void setConfiguration(Boolean configuration) {
288 this.configuration = configuration;
292 public ConstraintsBuilder getConstraints() {
296 public Set<UsesNodeBuilder> getUsesNodes() {
297 return addedUsesNodes;
301 public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
302 addedUsesNodes.add(usesNodeBuilder);
305 public void setUsesnodes(final Set<UsesNode> usesNodes) {
306 this.usesNodes = usesNodes;
309 public boolean isPresence() {
313 public void setPresence(boolean presence) {
314 this.presence = presence;
317 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
318 return addedUnknownNodes;
322 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
323 addedUnknownNodes.add(unknownNode);
326 public void setUnknownNodes(List<UnknownSchemaNode> unknownNodes) {
327 this.unknownNodes = unknownNodes;
331 public int hashCode() {
332 final int prime = 31;
334 result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
339 public boolean equals(Object obj) {
346 if (getClass() != obj.getClass()) {
349 ContainerSchemaNodeBuilder other = (ContainerSchemaNodeBuilder) obj;
350 if (schemaPath == null) {
351 if (other.schemaPath != null) {
354 } else if (!schemaPath.equals(other.schemaPath)) {
357 if (parent == null) {
358 if (other.parent != null) {
361 } else if (!parent.equals(other.parent)) {
368 public String toString() {
369 return "container " + qname.getLocalName();
372 public final class ContainerSchemaNodeImpl implements ContainerSchemaNode {
373 private final QName qname;
374 private SchemaPath path;
375 private String description;
376 private String reference;
377 private Status status = Status.CURRENT;
378 private boolean augmenting;
379 private boolean addedByUses;
380 private boolean configuration;
381 private ConstraintDefinition constraints;
382 private Set<AugmentationSchema> augmentations = Collections.emptySet();
383 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
384 private Set<GroupingDefinition> groupings = Collections.emptySet();
385 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
386 private Set<UsesNode> uses = Collections.emptySet();
387 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
388 private boolean presence;
390 private ContainerSchemaNodeImpl(QName qname) {
395 public QName getQName() {
400 public SchemaPath getPath() {
404 private void setPath(SchemaPath path) {
409 public String getDescription() {
413 private void setDescription(String description) {
414 this.description = description;
418 public String getReference() {
422 private void setReference(String reference) {
423 this.reference = reference;
427 public Status getStatus() {
431 private void setStatus(Status status) {
432 if (status != null) {
433 this.status = status;
438 public boolean isAugmenting() {
442 private void setAugmenting(boolean augmenting) {
443 this.augmenting = augmenting;
447 public boolean isAddedByUses() {
451 private void setAddedByUses(boolean addedByUses) {
452 this.addedByUses = addedByUses;
456 public boolean isConfiguration() {
457 return configuration;
460 private void setConfiguration(boolean configuration) {
461 this.configuration = configuration;
465 public ConstraintDefinition getConstraints() {
469 private void setConstraints(ConstraintDefinition constraints) {
470 this.constraints = constraints;
474 public Set<AugmentationSchema> getAvailableAugmentations() {
475 return augmentations;
478 private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
479 if (augmentations != null) {
480 this.augmentations = augmentations;
485 public Set<DataSchemaNode> getChildNodes() {
486 return new HashSet<DataSchemaNode>(childNodes.values());
489 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
490 if (childNodes != null) {
491 this.childNodes = childNodes;
496 public Set<GroupingDefinition> getGroupings() {
500 private void setGroupings(Set<GroupingDefinition> groupings) {
501 if (groupings != null) {
502 this.groupings = groupings;
507 public DataSchemaNode getDataChildByName(QName name) {
508 return childNodes.get(name);
512 public DataSchemaNode getDataChildByName(String name) {
513 DataSchemaNode result = null;
514 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
515 if (entry.getKey().getLocalName().equals(name)) {
516 result = entry.getValue();
524 public Set<UsesNode> getUses() {
528 private void setUses(Set<UsesNode> uses) {
535 public boolean isPresenceContainer() {
539 private void setPresenceContainer(boolean presence) {
540 this.presence = presence;
544 public Set<TypeDefinition<?>> getTypeDefinitions() {
545 return typeDefinitions;
548 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
549 if (typeDefinitions != null) {
550 this.typeDefinitions = typeDefinitions;
555 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
559 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
560 if (unknownSchemaNodes != null) {
561 this.unknownNodes = unknownSchemaNodes;
565 public ContainerSchemaNodeBuilder toBuilder() {
566 return ContainerSchemaNodeBuilder.this;
570 public int hashCode() {
571 final int prime = 31;
573 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
574 result = prime * result + ((path == null) ? 0 : path.hashCode());
579 public boolean equals(Object obj) {
586 if (getClass() != obj.getClass()) {
589 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
591 if (other.qname != null) {
594 } else if (!qname.equals(other.qname)) {
598 if (other.path != null) {
601 } else if (!path.equals(other.path)) {
608 public String toString() {
609 StringBuilder sb = new StringBuilder(ContainerSchemaNodeImpl.class.getSimpleName());
611 sb.append("qname=" + qname);
613 return sb.toString();