Refactored parsing of YANG uses statement.
[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 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>();
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> getTypeDefinitionBuilders() {
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(final Set<TypeDefinition<?>> typeDefinitions) {
307             if (typeDefinitions != null) {
308                 this.typeDefinitions = typeDefinitions;
309             }
310         }
311
312         @Override
313         public Set<AugmentationSchema> getAvailableAugmentations() {
314             return augmentations;
315         }
316
317         private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
318             if (augmentations != null) {
319                 this.augmentations = augmentations;
320             }
321         }
322
323         @Override
324         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
325             return unknownNodes;
326         }
327
328         private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
329             if (unknownNodes != null) {
330                 this.unknownNodes = unknownNodes;
331             }
332         }
333
334         @Override
335         public DataSchemaNode getDataChildByName(QName name) {
336             return childNodes.get(name);
337         }
338
339         @Override
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();
345                     break;
346                 }
347             }
348             return result;
349         }
350
351         public NotificationBuilder toBuilder() {
352             return NotificationBuilder.this;
353         }
354
355         @Override
356         public int hashCode() {
357             final int prime = 31;
358             int result = 1;
359             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
360             result = prime * result + ((path == null) ? 0 : path.hashCode());
361             return result;
362         }
363
364         @Override
365         public boolean equals(Object obj) {
366             if (this == obj) {
367                 return true;
368             }
369             if (obj == null) {
370                 return false;
371             }
372             if (getClass() != obj.getClass()) {
373                 return false;
374             }
375             final NotificationDefinitionImpl other = (NotificationDefinitionImpl) obj;
376             if (qname == null) {
377                 if (other.qname != null) {
378                     return false;
379                 }
380             } else if (!qname.equals(other.qname)) {
381                 return false;
382             }
383             if (path == null) {
384                 if (other.path != null) {
385                     return false;
386                 }
387             } else if (!path.equals(other.path)) {
388                 return false;
389             }
390             return true;
391         }
392
393         @Override
394         public String toString() {
395             StringBuilder sb = new StringBuilder(NotificationDefinitionImpl.class.getSimpleName());
396             sb.append("[qname=" + qname + ", path=" + path + "]");
397             return sb.toString();
398         }
399     }
400
401 }