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