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.DataSchemaNode;
21 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
22 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
23 import org.opendaylight.controller.yang.model.api.SchemaPath;
24 import org.opendaylight.controller.yang.model.api.Status;
25 import org.opendaylight.controller.yang.model.api.TypeDefinition;
26 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
27 import org.opendaylight.controller.yang.model.api.UsesNode;
28 import org.opendaylight.controller.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
31 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
32 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
33 import org.opendaylight.controller.yang.parser.builder.api.SchemaNodeBuilder;
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 NotificationBuilder extends AbstractDataNodeContainerBuilder implements TypeDefinitionAwareBuilder,
39 SchemaNodeBuilder, AugmentationTargetBuilder {
40 private boolean isBuilt;
41 private final NotificationDefinitionImpl instance;
42 private final int line;
43 private SchemaPath schemaPath;
44 private String description;
45 private String reference;
46 private Status status = Status.CURRENT;
47 private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
48 private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
49 private Set<AugmentationSchema> augmentations;
50 private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
51 private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
53 NotificationBuilder(final QName qname, final int line) {
56 instance = new NotificationDefinitionImpl(qname);
60 public NotificationDefinition build() {
62 instance.setPath(schemaPath);
63 instance.setDescription(description);
64 instance.setReference(reference);
65 instance.setStatus(status);
68 final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
69 for (DataSchemaNodeBuilder node : addedChildNodes) {
70 childs.put(node.getQName(), node.build());
72 instance.setChildNodes(childs);
75 final Set<GroupingDefinition> groupingDefs = new HashSet<GroupingDefinition>();
76 for (GroupingBuilder builder : addedGroupings) {
77 groupingDefs.add(builder.build());
79 instance.setGroupings(groupingDefs);
82 final Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
83 for (TypeDefinitionBuilder entry : addedTypedefs) {
84 typedefs.add(entry.build());
86 instance.setTypeDefinitions(typedefs);
89 final Set<UsesNode> uses = new HashSet<UsesNode>();
90 for (UsesNodeBuilder builder : addedUsesNodes) {
91 uses.add(builder.build());
93 instance.setUses(uses);
96 if (augmentations == null) {
97 augmentations = new HashSet<AugmentationSchema>();
98 for (AugmentationSchemaBuilder builder : addedAugmentations) {
99 augmentations.add(builder.build());
102 instance.setAvailableAugmentations(augmentations);
105 final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
106 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
107 unknownNodes.add(b.build());
109 instance.setUnknownSchemaNodes(unknownNodes);
118 public void rebuild() {
124 public int getLine() {
129 public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
130 return addedTypedefs;
134 public void addTypedef(final TypeDefinitionBuilder type) {
135 addedTypedefs.add(type);
139 public void addUsesNode(final UsesNodeBuilder usesNodeBuilder) {
140 addedUsesNodes.add(usesNodeBuilder);
144 public SchemaPath getPath() {
149 public void setPath(SchemaPath schemaPath) {
150 this.schemaPath = schemaPath;
154 public String getDescription() {
159 public void setDescription(final String description) {
160 this.description = description;
164 public String getReference() {
169 public void setReference(final String reference) {
170 this.reference = reference;
174 public Status getStatus() {
179 public void setStatus(final Status status) {
180 if (status != null) {
181 this.status = status;
185 public Set<AugmentationSchemaBuilder> getAugmentations() {
186 return addedAugmentations;
190 public void addAugmentation(AugmentationSchemaBuilder augment) {
191 addedAugmentations.add(augment);
194 public void setAugmentations(final Set<AugmentationSchema> augmentations) {
195 this.augmentations = augmentations;
199 public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
200 addedUnknownNodes.add(unknownNode);
204 public String toString() {
205 return "notification " + getQName().getLocalName();
208 public final class NotificationDefinitionImpl implements NotificationDefinition {
209 private final QName qname;
210 private SchemaPath path;
211 private String description;
212 private String reference;
213 private Status status = Status.CURRENT;
214 private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
215 private Set<GroupingDefinition> groupings = Collections.emptySet();
216 private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
217 private Set<UsesNode> uses = Collections.emptySet();
218 private Set<AugmentationSchema> augmentations = Collections.emptySet();
219 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
221 private NotificationDefinitionImpl(final QName qname) {
226 public QName getQName() {
231 public SchemaPath getPath() {
235 private void setPath(final SchemaPath path) {
240 public String getDescription() {
244 private void setDescription(final String description) {
245 this.description = description;
249 public String getReference() {
253 private void setReference(String reference) {
254 this.reference = reference;
258 public Status getStatus() {
262 private void setStatus(Status status) {
263 if (status != null) {
264 this.status = status;
269 public Set<DataSchemaNode> getChildNodes() {
270 return new HashSet<DataSchemaNode>(childNodes.values());
273 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
274 if (childNodes != null) {
275 this.childNodes = childNodes;
280 public Set<GroupingDefinition> getGroupings() {
284 private void setGroupings(Set<GroupingDefinition> groupings) {
285 if (groupings != null) {
286 this.groupings = groupings;
291 public Set<UsesNode> getUses() {
295 private void setUses(Set<UsesNode> uses) {
302 public Set<TypeDefinition<?>> getTypeDefinitions() {
303 return typeDefinitions;
306 private void setTypeDefinitions(final Set<TypeDefinition<?>> typeDefinitions) {
307 if (typeDefinitions != null) {
308 this.typeDefinitions = typeDefinitions;
313 public Set<AugmentationSchema> getAvailableAugmentations() {
314 return augmentations;
317 private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
318 if (augmentations != null) {
319 this.augmentations = augmentations;
324 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
328 private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
329 if (unknownNodes != null) {
330 this.unknownNodes = unknownNodes;
335 public DataSchemaNode getDataChildByName(QName name) {
336 return childNodes.get(name);
340 public DataSchemaNode getDataChildByName(String name) {
341 DataSchemaNode result = null;
342 for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
343 if (entry.getKey().getLocalName().equals(name)) {
344 result = entry.getValue();
351 public NotificationBuilder toBuilder() {
352 return NotificationBuilder.this;
356 public int hashCode() {
357 final int prime = 31;
359 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
360 result = prime * result + ((path == null) ? 0 : path.hashCode());
365 public boolean equals(Object obj) {
372 if (getClass() != obj.getClass()) {
375 final NotificationDefinitionImpl other = (NotificationDefinitionImpl) obj;
377 if (other.qname != null) {
380 } else if (!qname.equals(other.qname)) {
384 if (other.path != null) {
387 } else if (!path.equals(other.path)) {
394 public String toString() {
395 StringBuilder sb = new StringBuilder(NotificationDefinitionImpl.class.getSimpleName());
396 sb.append("[qname=" + qname + ", path=" + path + "]");
397 return sb.toString();