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