Moved parsing of unknown nodes from implementation to abstract classes.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / NotificationBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.parser.builder.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.TreeMap;
17 import java.util.TreeSet;
18
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.DataSchemaNode;
22 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
23 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
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.SchemaNodeBuilder;
35 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
36 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
37 import org.opendaylight.controller.yang.parser.util.Comparators;
38
39 public final class NotificationBuilder extends AbstractDataNodeContainerBuilder implements SchemaNodeBuilder,
40         AugmentationTargetBuilder {
41     private boolean isBuilt;
42     private final NotificationDefinitionImpl instance;
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
52     NotificationBuilder(final int line, final QName qname) {
53         super(line, qname);
54         instance = new NotificationDefinitionImpl(qname);
55     }
56
57     @Override
58     public NotificationDefinition build() {
59         if (!isBuilt) {
60             instance.setPath(schemaPath);
61             instance.setDescription(description);
62             instance.setReference(reference);
63             instance.setStatus(status);
64
65             // CHILD NODES
66             final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
67             for (DataSchemaNodeBuilder node : addedChildNodes) {
68                 childs.put(node.getQName(), node.build());
69             }
70             instance.setChildNodes(childs);
71
72             // GROUPINGS
73             final Set<GroupingDefinition> groupingDefs = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
74             for (GroupingBuilder builder : addedGroupings) {
75                 groupingDefs.add(builder.build());
76             }
77             instance.setGroupings(groupingDefs);
78
79             // TYPEDEFS
80             final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
81             for (TypeDefinitionBuilder entry : addedTypedefs) {
82                 typedefs.add(entry.build());
83             }
84             instance.setTypeDefinitions(typedefs);
85
86             // USES
87             final Set<UsesNode> uses = new HashSet<UsesNode>();
88             for (UsesNodeBuilder builder : addedUsesNodes) {
89                 uses.add(builder.build());
90             }
91             instance.setUses(uses);
92
93             // AUGMENTATIONS
94             if (augmentations == null) {
95                 augmentations = new HashSet<AugmentationSchema>();
96                 for (AugmentationSchemaBuilder builder : addedAugmentations) {
97                     augmentations.add(builder.build());
98                 }
99             }
100             instance.setAvailableAugmentations(augmentations);
101
102             // UNKNOWN NODES
103             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
104             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
105                 unknownNodes.add(b.build());
106             }
107             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
108             instance.setUnknownSchemaNodes(unknownNodes);
109
110             isBuilt = true;
111         }
112
113         return instance;
114     }
115
116     @Override
117     public void rebuild() {
118         isBuilt = false;
119         build();
120     }
121
122     @Override
123     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
124         return addedTypedefs;
125     }
126
127     @Override
128     public void addTypedef(final TypeDefinitionBuilder type) {
129         addedTypedefs.add(type);
130     }
131
132     @Override
133     public void addUsesNode(final UsesNodeBuilder usesNodeBuilder) {
134         addedUsesNodes.add(usesNodeBuilder);
135     }
136
137     @Override
138     public SchemaPath getPath() {
139         return schemaPath;
140     }
141
142     @Override
143     public void setPath(SchemaPath schemaPath) {
144         this.schemaPath = schemaPath;
145     }
146
147     @Override
148     public String getDescription() {
149         return description;
150     }
151
152     @Override
153     public void setDescription(final String description) {
154         this.description = description;
155     }
156
157     @Override
158     public String getReference() {
159         return reference;
160     }
161
162     @Override
163     public void setReference(final String reference) {
164         this.reference = reference;
165     }
166
167     @Override
168     public Status getStatus() {
169         return status;
170     }
171
172     @Override
173     public void setStatus(final Status status) {
174         if (status != null) {
175             this.status = status;
176         }
177     }
178
179     public Set<AugmentationSchemaBuilder> getAugmentations() {
180         return addedAugmentations;
181     }
182
183     @Override
184     public void addAugmentation(AugmentationSchemaBuilder augment) {
185         addedAugmentations.add(augment);
186     }
187
188     public void setAugmentations(final Set<AugmentationSchema> augmentations) {
189         this.augmentations = augmentations;
190     }
191
192     @Override
193     public String toString() {
194         return "notification " + getQName().getLocalName();
195     }
196
197     public final class NotificationDefinitionImpl implements NotificationDefinition {
198         private final QName qname;
199         private SchemaPath path;
200         private String description;
201         private String reference;
202         private Status status = Status.CURRENT;
203         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
204         private Set<GroupingDefinition> groupings = Collections.emptySet();
205         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
206         private Set<UsesNode> uses = Collections.emptySet();
207         private Set<AugmentationSchema> augmentations = Collections.emptySet();
208         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
209
210         private NotificationDefinitionImpl(final QName qname) {
211             this.qname = qname;
212         }
213
214         @Override
215         public QName getQName() {
216             return qname;
217         }
218
219         @Override
220         public SchemaPath getPath() {
221             return path;
222         }
223
224         private void setPath(final SchemaPath path) {
225             this.path = path;
226         }
227
228         @Override
229         public String getDescription() {
230             return description;
231         }
232
233         private void setDescription(final String description) {
234             this.description = description;
235         }
236
237         @Override
238         public String getReference() {
239             return reference;
240         }
241
242         private void setReference(String reference) {
243             this.reference = reference;
244         }
245
246         @Override
247         public Status getStatus() {
248             return status;
249         }
250
251         private void setStatus(Status status) {
252             if (status != null) {
253                 this.status = status;
254             }
255         }
256
257         @Override
258         public Set<DataSchemaNode> getChildNodes() {
259             return new HashSet<DataSchemaNode>(childNodes.values());
260         }
261
262         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
263             if (childNodes != null) {
264                 this.childNodes = childNodes;
265             }
266         }
267
268         @Override
269         public Set<GroupingDefinition> getGroupings() {
270             return groupings;
271         }
272
273         private void setGroupings(Set<GroupingDefinition> groupings) {
274             if (groupings != null) {
275                 this.groupings = groupings;
276             }
277         }
278
279         @Override
280         public Set<UsesNode> getUses() {
281             return uses;
282         }
283
284         private void setUses(Set<UsesNode> uses) {
285             if (uses != null) {
286                 this.uses = uses;
287             }
288         }
289
290         @Override
291         public Set<TypeDefinition<?>> getTypeDefinitions() {
292             return typeDefinitions;
293         }
294
295         private void setTypeDefinitions(final Set<TypeDefinition<?>> typeDefinitions) {
296             if (typeDefinitions != null) {
297                 this.typeDefinitions = typeDefinitions;
298             }
299         }
300
301         @Override
302         public Set<AugmentationSchema> getAvailableAugmentations() {
303             return augmentations;
304         }
305
306         private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
307             if (augmentations != null) {
308                 this.augmentations = augmentations;
309             }
310         }
311
312         @Override
313         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
314             return unknownNodes;
315         }
316
317         private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
318             if (unknownNodes != null) {
319                 this.unknownNodes = unknownNodes;
320             }
321         }
322
323         @Override
324         public DataSchemaNode getDataChildByName(QName name) {
325             return childNodes.get(name);
326         }
327
328         @Override
329         public DataSchemaNode getDataChildByName(String name) {
330             DataSchemaNode result = null;
331             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
332                 if (entry.getKey().getLocalName().equals(name)) {
333                     result = entry.getValue();
334                     break;
335                 }
336             }
337             return result;
338         }
339
340         public NotificationBuilder toBuilder() {
341             return NotificationBuilder.this;
342         }
343
344         @Override
345         public int hashCode() {
346             final int prime = 31;
347             int result = 1;
348             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
349             result = prime * result + ((path == null) ? 0 : path.hashCode());
350             return result;
351         }
352
353         @Override
354         public boolean equals(Object obj) {
355             if (this == obj) {
356                 return true;
357             }
358             if (obj == null) {
359                 return false;
360             }
361             if (getClass() != obj.getClass()) {
362                 return false;
363             }
364             final NotificationDefinitionImpl other = (NotificationDefinitionImpl) obj;
365             if (qname == null) {
366                 if (other.qname != null) {
367                     return false;
368                 }
369             } else if (!qname.equals(other.qname)) {
370                 return false;
371             }
372             if (path == null) {
373                 if (other.path != null) {
374                     return false;
375                 }
376             } else if (!path.equals(other.path)) {
377                 return false;
378             }
379             return true;
380         }
381
382         @Override
383         public String toString() {
384             StringBuilder sb = new StringBuilder(NotificationDefinitionImpl.class.getSimpleName());
385             sb.append("[qname=" + qname + ", path=" + path + "]");
386             return sb.toString();
387         }
388     }
389
390 }