Merge from development repository.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / 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.model.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.opendaylight.controller.yang.common.QName;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.NotificationDefinition;
21 import org.opendaylight.controller.yang.model.api.SchemaNode;
22 import org.opendaylight.controller.yang.model.api.SchemaPath;
23 import org.opendaylight.controller.yang.model.api.Status;
24 import org.opendaylight.controller.yang.model.api.TypeDefinition;
25 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.controller.yang.model.api.UsesNode;
27 import org.opendaylight.controller.yang.model.parser.builder.api.AbstractChildNodeBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
31 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;
32 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
33 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
34
35 public class NotificationBuilder extends AbstractChildNodeBuilder implements
36         TypeDefinitionAwareBuilder, SchemaNodeBuilder {
37
38     private final NotificationDefinitionImpl instance;
39     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
40     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
41
42     NotificationBuilder(QName qname) {
43         super(qname);
44         instance = new NotificationDefinitionImpl(qname);
45     }
46
47     @Override
48     public SchemaNode build() {
49         // CHILD NODES
50         Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
51         for (DataSchemaNodeBuilder node : childNodes) {
52             childs.put(node.getQName(), node.build());
53         }
54         instance.setChildNodes(childs);
55
56         // GROUPINGS
57         Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
58         for (GroupingBuilder builder : groupings) {
59             groupingDefinitions.add(builder.build());
60         }
61         instance.setGroupings(groupingDefinitions);
62
63         // TYPEDEFS
64         Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();
65         for (TypeDefinitionBuilder entry : addedTypedefs) {
66             typedefs.add(entry.build());
67         }
68         instance.setTypeDefinitions(typedefs);
69
70         // USES
71         Set<UsesNode> uses = new HashSet<UsesNode>();
72         for (UsesNodeBuilder builder : addedUsesNodes) {
73             uses.add(builder.build());
74         }
75         instance.setUses(uses);
76
77         return instance;
78     }
79
80     @Override
81     public void addTypedef(TypeDefinitionBuilder type) {
82         addedTypedefs.add(type);
83     }
84
85     @Override
86     public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
87         addedUsesNodes.add(usesNodeBuilder);
88     }
89
90     @Override
91     public void setPath(SchemaPath schemaPath) {
92         instance.setPath(schemaPath);
93     }
94
95     @Override
96     public void setDescription(String description) {
97         instance.setDescription(description);
98     }
99
100     @Override
101     public void setReference(String reference) {
102         instance.setReference(reference);
103     }
104
105     @Override
106     public void setStatus(Status status) {
107         instance.setStatus(status);
108     }
109
110     private class NotificationDefinitionImpl implements NotificationDefinition {
111
112         private final QName qname;
113         private SchemaPath path;
114         private String description;
115         private String reference;
116         private Status status;
117         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
118         private Set<GroupingDefinition> groupings = Collections.emptySet();
119         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
120         private Set<UsesNode> uses = Collections.emptySet();
121         private List<UnknownSchemaNode> unknownSchemaNodes = Collections.emptyList();
122
123         private NotificationDefinitionImpl(QName qname) {
124             this.qname = qname;
125         }
126
127         @Override
128         public QName getQName() {
129             return qname;
130         }
131
132         @Override
133         public SchemaPath getPath() {
134             return path;
135         }
136
137         private void setPath(SchemaPath path) {
138             this.path = path;
139         }
140
141         @Override
142         public String getDescription() {
143             return description;
144         }
145
146         private void setDescription(String description) {
147             this.description = description;
148         }
149
150         @Override
151         public String getReference() {
152             return reference;
153         }
154
155         private void setReference(String reference) {
156             this.reference = reference;
157         }
158
159         @Override
160         public Status getStatus() {
161             return status;
162         }
163
164         private void setStatus(Status status) {
165             this.status = status;
166         }
167
168         @Override
169         public Set<DataSchemaNode> getChildNodes() {
170             return new HashSet<DataSchemaNode>(childNodes.values());
171         }
172
173         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
174             if(childNodes != null) {
175                 this.childNodes = childNodes;
176             }
177         }
178
179         @Override
180         public Set<GroupingDefinition> getGroupings() {
181             return groupings;
182         }
183
184         private void setGroupings(Set<GroupingDefinition> groupings) {
185             if(groupings != null) {
186                 this.groupings = groupings;
187             }
188         }
189
190         @Override
191         public Set<UsesNode> getUses() {
192             return uses;
193         }
194
195         private void setUses(Set<UsesNode> uses) {
196             if(uses != null) {
197                 this.uses = uses;
198             }
199         }
200
201         @Override
202         public Set<TypeDefinition<?>> getTypeDefinitions() {
203             return typeDefinitions;
204         }
205
206         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
207             if(typeDefinitions != null) {
208                 this.typeDefinitions = typeDefinitions;
209             }
210         }
211
212         @Override
213         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
214             return unknownSchemaNodes;
215         }
216
217         @Override
218         public DataSchemaNode getDataChildByName(QName name) {
219             return childNodes.get(name);
220         }
221
222         @Override
223         public DataSchemaNode getDataChildByName(String name) {
224             DataSchemaNode result = null;
225             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
226                 if (entry.getKey().getLocalName().equals(name)) {
227                     result = entry.getValue();
228                     break;
229                 }
230             }
231             return result;
232         }
233
234         @Override
235         public int hashCode() {
236             final int prime = 31;
237             int result = 1;
238             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
239             result = prime * result + ((path == null) ? 0 : path.hashCode());
240             result = prime * result
241                     + ((description == null) ? 0 : description.hashCode());
242             result = prime * result
243                     + ((reference == null) ? 0 : reference.hashCode());
244             result = prime * result
245                     + ((status == null) ? 0 : status.hashCode());
246             result = prime * result
247                     + ((childNodes == null) ? 0 : childNodes.hashCode());
248             result = prime * result
249                     + ((groupings == null) ? 0 : groupings.hashCode());
250             result = prime * result
251                     + ((typeDefinitions == null) ? 0 : typeDefinitions.hashCode());
252             result = prime * result + ((uses == null) ? 0 : uses.hashCode());
253             return result;
254         }
255
256         @Override
257         public boolean equals(Object obj) {
258             if (this == obj) {
259                 return true;
260             }
261             if (obj == null) {
262                 return false;
263             }
264             if (getClass() != obj.getClass()) {
265                 return false;
266             }
267             NotificationDefinitionImpl other = (NotificationDefinitionImpl) obj;
268             if (qname == null) {
269                 if (other.qname != null) {
270                     return false;
271                 }
272             } else if (!qname.equals(other.qname)) {
273                 return false;
274             }
275             if (path == null) {
276                 if (other.path != null) {
277                     return false;
278                 }
279             } else if (!path.equals(other.path)) {
280                 return false;
281             }
282             if (description == null) {
283                 if (other.description != null) {
284                     return false;
285                 }
286             } else if (!description.equals(other.description)) {
287                 return false;
288             }
289             if (reference == null) {
290                 if (other.reference != null) {
291                     return false;
292                 }
293             } else if (!reference.equals(other.reference)) {
294                 return false;
295             }
296             if (status == null) {
297                 if (other.status != null) {
298                     return false;
299                 }
300             } else if (!status.equals(other.status)) {
301                 return false;
302             }
303             if (childNodes == null) {
304                 if (other.childNodes != null) {
305                     return false;
306                 }
307             } else if (!childNodes.equals(other.childNodes)) {
308                 return false;
309             }
310             if (groupings == null) {
311                 if (other.groupings != null) {
312                     return false;
313                 }
314             } else if (!groupings.equals(other.groupings)) {
315                 return false;
316             }
317             if (typeDefinitions == null) {
318                 if (other.typeDefinitions != null) {
319                     return false;
320                 }
321             } else if (!typeDefinitions.equals(other.typeDefinitions)) {
322                 return false;
323             }
324             if (uses == null) {
325                 if (other.uses != null) {
326                     return false;
327                 }
328             } else if (!uses.equals(other.uses)) {
329                 return false;
330             }
331             return true;
332         }
333
334         @Override
335         public String toString() {
336             StringBuilder sb = new StringBuilder(
337                     NotificationDefinitionImpl.class.getSimpleName());
338             sb.append("[qname=" + qname + "]");
339             return sb.toString();
340         }
341     }
342
343 }