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