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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
18 import org.opendaylight.controller.yang.common.QName;
19 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
20 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
21 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
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.SchemaPath;
25 import org.opendaylight.controller.yang.model.api.Status;
26 import org.opendaylight.controller.yang.model.api.TypeDefinition;
27 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.controller.yang.model.api.UsesNode;
29 import org.opendaylight.controller.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
31 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
32 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
33 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
34 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionAwareBuilder;
35 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
36 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
38 public final class ContainerSchemaNodeBuilder extends AbstractDataNodeContainerBuilder
39 implements TypeDefinitionAwareBuilder, AugmentationTargetBuilder,
40 DataSchemaNodeBuilder {
41 private boolean isBuilt;
42 private final ContainerSchemaNodeImpl instance;
43 private final int line;
44 private final ConstraintsBuilder constraints;
45 private SchemaPath schemaPath;
46 private String description;
47 private String reference;
48 private Status status = Status.CURRENT;
49 private boolean presence;
50 private boolean augmenting;
51 private boolean configuration;
52 private Set<TypeDefinition<?>> typedefs;
53 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
54 private Set<UsesNode> usesNodes;
55 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
56 private Set<AugmentationSchema> augmentations;
57 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
58 private List<UnknownSchemaNode> unknownNodes;
59 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
61 public ContainerSchemaNodeBuilder(final QName qname, final int line) {
64 instance = new ContainerSchemaNodeImpl(qname);
65 constraints = new ConstraintsBuilder(line);
69 public ContainerSchemaNode build() {
71 instance.setPath(schemaPath);
72 instance.setDescription(description);
73 instance.setReference(reference);
74 instance.setStatus(status);
75 instance.setPresenceContainer(presence);
76 instance.setAugmenting(augmenting);
77 instance.setConfiguration(configuration);
80 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
81 if(childNodes == null) {
82 for (DataSchemaNodeBuilder node : addedChildNodes) {
83 childs.put(node.getQName(), node.build());
86 for(DataSchemaNode node : childNodes) {
87 childs.put(node.getQName(), node);
90 instance.setChildNodes(childs);
93 if(groupings == null) {
94 groupings = new HashSet<GroupingDefinition>();
95 for (GroupingBuilder builder : addedGroupings) {
96 groupings.add(builder.build());
99 instance.setGroupings(groupings);
102 if(typedefs == null) {
103 typedefs = new HashSet<TypeDefinition<?>>();
104 for (TypeDefinitionBuilder entry : addedTypedefs) {
105 typedefs.add(entry.build());
108 instance.setTypeDefinitions(typedefs);
111 if(usesNodes == null) {
112 usesNodes = new HashSet<UsesNode>();
113 for (UsesNodeBuilder builder : addedUsesNodes) {
114 usesNodes.add(builder.build());
117 instance.setUses(usesNodes);
120 if(augmentations == null) {
121 augmentations = new HashSet<AugmentationSchema>();
122 for (AugmentationSchemaBuilder builder : addedAugmentations) {
123 augmentations.add(builder.build());
126 instance.setAvailableAugmentations(augmentations);
129 if(unknownNodes == null) {
130 unknownNodes = new ArrayList<UnknownSchemaNode>();
131 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
132 unknownNodes.add(b.build());
135 instance.setUnknownSchemaNodes(unknownNodes);
137 instance.setConstraints(constraints.build());
138 instance.setAvailableAugmentations(augmentations);
146 public void rebuild() {
152 public int getLine() {
157 public Set<TypeDefinitionBuilder> getTypeDefinitions() {
158 return addedTypedefs;
162 public void addTypedef(final TypeDefinitionBuilder type) {
163 addedTypedefs.add(type);
166 public void setTypedefs(final Set<TypeDefinition<?>> typedefs) {
167 this.typedefs = typedefs;
170 public Set<AugmentationSchemaBuilder> getAugmentations() {
171 return addedAugmentations;
175 public void addAugmentation(AugmentationSchemaBuilder augment) {
176 addedAugmentations.add(augment);
179 public void setAugmentations(final Set<AugmentationSchema> augmentations) {
180 this.augmentations = augmentations;
183 public SchemaPath getPath() {
188 public void setPath(final SchemaPath schemaPath) {
189 this.schemaPath = schemaPath;
193 public String getDescription() {
198 public void setDescription(final String description) {
199 this.description = description;
203 public String getReference() {
208 public void setReference(String reference) {
209 this.reference = reference;
213 public Status getStatus() {
218 public void setStatus(Status status) {
219 if (status != null) {
220 this.status = status;
225 public boolean isAugmenting() {
230 public void setAugmenting(boolean augmenting) {
231 this.augmenting = augmenting;
235 public boolean isConfiguration() {
236 return configuration;
240 public void setConfiguration(boolean configuration) {
241 this.configuration = configuration;
245 public ConstraintsBuilder getConstraints() {
249 public Set<UsesNodeBuilder> getUsesNodes() {
250 return addedUsesNodes;
254 public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
255 addedUsesNodes.add(usesNodeBuilder);
258 public void setUsesnodes(final Set<UsesNode> usesNodes) {
259 this.usesNodes = usesNodes;
262 public boolean isPresence() {
266 public void setPresence(boolean presence) {
267 this.presence = presence;
270 public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
271 return addedUnknownNodes;
275 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
276 addedUnknownNodes.add(unknownNode);
279 public void setUnknownNodes(List<UnknownSchemaNode> unknownNodes) {
280 this.unknownNodes = unknownNodes;
284 public String toString() {
285 return "container " + getQName().getLocalName();
288 public final class ContainerSchemaNodeImpl implements ContainerSchemaNode {
289 private final QName qname;
290 private SchemaPath path;
291 private String description;
292 private String reference;
293 private Status status = Status.CURRENT;
294 private boolean augmenting;
295 private boolean configuration;
296 private ConstraintDefinition constraints;
297 private Set<AugmentationSchema> augmentations = Collections.emptySet();
298 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
299 private Set<GroupingDefinition> groupings = Collections.emptySet();
300 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
301 private Set<UsesNode> uses = Collections.emptySet();
302 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
303 private boolean presence;
305 private ContainerSchemaNodeImpl(QName qname) {
310 public QName getQName() {
315 public SchemaPath getPath() {
319 private void setPath(SchemaPath path) {
324 public String getDescription() {
328 private void setDescription(String description) {
329 this.description = description;
333 public String getReference() {
337 private void setReference(String reference) {
338 this.reference = reference;
342 public Status getStatus() {
346 private void setStatus(Status status) {
347 if (status != null) {
348 this.status = status;
353 public boolean isAugmenting() {
357 private void setAugmenting(boolean augmenting) {
358 this.augmenting = augmenting;
362 public boolean isConfiguration() {
363 return configuration;
366 private void setConfiguration(boolean configuration) {
367 this.configuration = configuration;
371 public ConstraintDefinition getConstraints() {
375 private void setConstraints(ConstraintDefinition constraints) {
376 this.constraints = constraints;
380 public Set<AugmentationSchema> getAvailableAugmentations() {
381 return augmentations;
384 private void setAvailableAugmentations(
385 Set<AugmentationSchema> augmentations) {
386 if (augmentations != null) {
387 this.augmentations = augmentations;
392 public Set<DataSchemaNode> getChildNodes() {
393 return new HashSet<DataSchemaNode>(childNodes.values());
396 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
397 if (childNodes != null) {
398 this.childNodes = childNodes;
403 public Set<GroupingDefinition> getGroupings() {
407 private void setGroupings(Set<GroupingDefinition> groupings) {
408 if (groupings != null) {
409 this.groupings = groupings;
414 public DataSchemaNode getDataChildByName(QName name) {
415 return childNodes.get(name);
419 public DataSchemaNode getDataChildByName(String name) {
420 DataSchemaNode result = null;
421 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
422 if (entry.getKey().getLocalName().equals(name)) {
423 result = entry.getValue();
431 public Set<UsesNode> getUses() {
435 private void setUses(Set<UsesNode> uses) {
442 public boolean isPresenceContainer() {
446 private void setPresenceContainer(boolean presence) {
447 this.presence = presence;
451 public Set<TypeDefinition<?>> getTypeDefinitions() {
452 return typeDefinitions;
455 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
456 if (typeDefinitions != null) {
457 this.typeDefinitions = typeDefinitions;
462 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
466 private void setUnknownSchemaNodes(
467 List<UnknownSchemaNode> unknownSchemaNodes) {
468 if (unknownSchemaNodes != null) {
469 this.unknownNodes = unknownSchemaNodes;
473 public ContainerSchemaNodeBuilder toBuilder() {
474 return ContainerSchemaNodeBuilder.this;
478 public int hashCode() {
479 final int prime = 31;
481 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
482 result = prime * result + ((path == null) ? 0 : path.hashCode());
487 public boolean equals(Object obj) {
494 if (getClass() != obj.getClass()) {
497 ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;
499 if (other.qname != null) {
502 } else if (!qname.equals(other.qname)) {
506 if (other.path != null) {
509 } else if (!path.equals(other.path)) {
516 public String toString() {
517 StringBuilder sb = new StringBuilder(
518 ContainerSchemaNodeImpl.class.getSimpleName());
520 sb.append("qname=" + qname);
522 return sb.toString();