Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / model / parser / builder / ContainerSchemaNodeBuilder.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.model.parser.builder;\r
9 \r
10 import java.util.HashMap;\r
11 import java.util.HashSet;\r
12 import java.util.List;\r
13 import java.util.Map;\r
14 import java.util.Set;\r
15 \r
16 import org.opendaylight.controller.model.parser.api.AbstractChildNodeBuilder;\r
17 import org.opendaylight.controller.model.parser.api.AugmentationTargetBuilder;\r
18 import org.opendaylight.controller.model.parser.api.DataSchemaNodeBuilder;\r
19 import org.opendaylight.controller.model.parser.api.GroupingBuilder;\r
20 import org.opendaylight.controller.model.parser.api.TypeDefinitionAwareBuilder;\r
21 import org.opendaylight.controller.model.parser.api.TypeDefinitionBuilder;\r
22 import org.opendaylight.controller.model.parser.api.UsesNodeBuilder;\r
23 import org.opendaylight.controller.yang.common.QName;\r
24 import org.opendaylight.controller.yang.model.api.AugmentationSchema;\r
25 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;\r
26 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;\r
27 import org.opendaylight.controller.yang.model.api.DataSchemaNode;\r
28 import org.opendaylight.controller.yang.model.api.ExtensionDefinition;\r
29 import org.opendaylight.controller.yang.model.api.GroupingDefinition;\r
30 import org.opendaylight.controller.yang.model.api.MustDefinition;\r
31 import org.opendaylight.controller.yang.model.api.SchemaPath;\r
32 import org.opendaylight.controller.yang.model.api.Status;\r
33 import org.opendaylight.controller.yang.model.api.TypeDefinition;\r
34 import org.opendaylight.controller.yang.model.api.UsesNode;\r
35 \r
36 \r
37 public class ContainerSchemaNodeBuilder extends AbstractChildNodeBuilder implements TypeDefinitionAwareBuilder, MustAwareBuilder, AugmentationTargetBuilder, DataSchemaNodeBuilder {\r
38 \r
39         private final ContainerSchemaNodeImpl instance;\r
40 \r
41         private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();\r
42         private final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();\r
43         private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();\r
44         private MustDefinitionBuilder mustDefinitionBuilder;\r
45 \r
46         ContainerSchemaNodeBuilder(QName qname) {\r
47                 super(qname);\r
48                 instance = new ContainerSchemaNodeImpl(qname);\r
49         }\r
50 \r
51         @Override\r
52         public ContainerSchemaNode build() {\r
53                 // CHILD NODES\r
54                 Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();\r
55                 for (DataSchemaNodeBuilder node : childNodes) {\r
56                         childs.put(node.getQName(), node.build());\r
57                 }\r
58                 instance.setChildNodes(childs);\r
59 \r
60                 // GROUPINGS\r
61                 Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();\r
62                 for (GroupingBuilder builder : groupings) {\r
63                         groupingDefinitions.add(builder.build());\r
64                 }\r
65                 instance.setGroupings(groupingDefinitions);\r
66 \r
67                 // TYPEDEFS\r
68                 Set<TypeDefinition<?>> typedefs = new HashSet<TypeDefinition<?>>();\r
69                 for(TypeDefinitionBuilder entry : addedTypedefs) {\r
70                         typedefs.add(entry.build());\r
71                 }\r
72                 instance.setTypeDefinitions(typedefs);\r
73 \r
74                 // USES\r
75                 Set<UsesNode> uses = new HashSet<UsesNode>();\r
76                 for (UsesNodeBuilder builder : addedUsesNodes) {\r
77                         uses.add(builder.build());\r
78                 }\r
79                 instance.setUses(uses);\r
80 \r
81                 // MUST definition\r
82                 if(mustDefinitionBuilder != null) {\r
83                         MustDefinition md = mustDefinitionBuilder.build();\r
84                         instance.setMustStatement(md);\r
85                 }\r
86 \r
87                 instance.setAvailableAugmentations(augmentations);\r
88 \r
89                 return instance;\r
90         }\r
91 \r
92 \r
93         @Override\r
94         public void addTypedef(TypeDefinitionBuilder type) {\r
95                 addedTypedefs.add(type);\r
96         }\r
97 \r
98         @Override\r
99         public void addAugmentation(AugmentationSchema augment) {\r
100                 augmentations.add(augment);\r
101         }\r
102 \r
103         @Override\r
104         public void setPath(SchemaPath schemaPath) {\r
105                 instance.setPath(schemaPath);\r
106         }\r
107 \r
108         @Override\r
109         public void setDescription(String description) {\r
110                 instance.setDescription(description);\r
111         }\r
112 \r
113         @Override\r
114         public void setReference(String reference) {\r
115                 instance.setReference(reference);\r
116         }\r
117 \r
118         @Override\r
119         public void setStatus(Status status) {\r
120                 instance.setStatus(status);\r
121         }\r
122 \r
123         @Override\r
124         public void setAugmenting(boolean augmenting) {\r
125                 instance.setAugmenting(augmenting);\r
126         }\r
127 \r
128         @Override\r
129         public void setConfiguration(boolean configuration) {\r
130                 instance.setConfiguration(configuration);\r
131         }\r
132 \r
133         public void setConstraints(ConstraintDefinition constraints) {\r
134                 instance.setConstraints(constraints);\r
135         }\r
136 \r
137         @Override\r
138         public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {\r
139                 addedUsesNodes.add(usesNodeBuilder);\r
140         }\r
141 \r
142         public void setPresenceContainer(boolean presence) {\r
143                 instance.setPresenceContainer(presence);\r
144         }\r
145 \r
146         @Override\r
147         public void setMustDefinitionBuilder(MustDefinitionBuilder mustDefinitionBuilder) {\r
148                 this.mustDefinitionBuilder = mustDefinitionBuilder;\r
149         }\r
150 \r
151 \r
152         private class ContainerSchemaNodeImpl implements ContainerSchemaNode {\r
153 \r
154                 private final QName qname;\r
155                 private SchemaPath path;\r
156                 private String description;\r
157                 private String reference;\r
158                 private Status status = Status.CURRENT;\r
159 \r
160                 private boolean augmenting;\r
161                 private boolean configuration;\r
162                 private ConstraintDefinition constraints;\r
163 \r
164                 private Set<AugmentationSchema> augmentations;\r
165 \r
166                 private Map<QName, DataSchemaNode> childNodes;\r
167                 private Set<GroupingDefinition> groupings;\r
168                 private Set<TypeDefinition<?>> typeDefinitions;\r
169 \r
170                 private Set<UsesNode> uses;\r
171                 private boolean presence;\r
172 \r
173                 private MustDefinition mustDefinition;\r
174 \r
175                 private ContainerSchemaNodeImpl(QName qname) {\r
176                         this.qname = qname;\r
177                 }\r
178 \r
179                 @Override\r
180                 public QName getQName() {\r
181                         return qname;\r
182                 }\r
183 \r
184                 @Override\r
185                 public SchemaPath getPath() {\r
186                         return path;\r
187                 }\r
188                 private void setPath(SchemaPath path) {\r
189                         this.path = path;\r
190                 }\r
191 \r
192                 @Override\r
193                 public String getDescription() {\r
194                         return description;\r
195                 }\r
196                 private void setDescription(String description) {\r
197                         this.description = description;\r
198                 }\r
199 \r
200                 @Override\r
201                 public String getReference() {\r
202                         return reference;\r
203                 }\r
204                 private void setReference(String reference) {\r
205                         this.reference = reference;\r
206                 }\r
207 \r
208                 @Override\r
209                 public Status getStatus() {\r
210                         return status;\r
211                 }\r
212                 private void setStatus(Status status) {\r
213                         this.status = status;\r
214                 }\r
215 \r
216                 @Override\r
217                 public boolean isAugmenting() {\r
218                         return augmenting;\r
219                 }\r
220                 private void setAugmenting(boolean augmenting) {\r
221                         this.augmenting = augmenting;\r
222                 }\r
223 \r
224                 @Override\r
225                 public boolean isConfiguration() {\r
226                         return configuration;\r
227                 }\r
228                 private void setConfiguration(boolean configuration) {\r
229                         this.configuration = configuration;\r
230                 }\r
231 \r
232                 @Override\r
233                 public ConstraintDefinition getConstraints() {\r
234                         return constraints;\r
235                 }\r
236                 private void setConstraints(ConstraintDefinition constraints) {\r
237                         this.constraints = constraints;\r
238                 }\r
239 \r
240                 @Override\r
241                 public Set<AugmentationSchema> getAvailableAugmentations() {\r
242                         return augmentations;\r
243                 }\r
244                 private void setAvailableAugmentations(\r
245                                 Set<AugmentationSchema> augmentations) {\r
246                         this.augmentations = augmentations;\r
247                 }\r
248 \r
249                 @Override\r
250                 public Set<DataSchemaNode> getChildNodes() {\r
251                         return new HashSet<DataSchemaNode>(childNodes.values());\r
252                 }\r
253                 private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {\r
254                         this.childNodes = childNodes;\r
255                 }\r
256 \r
257                 @Override\r
258                 public Set<GroupingDefinition> getGroupings() {\r
259                         return groupings;\r
260                 }\r
261                 private void setGroupings(Set<GroupingDefinition> groupings) {\r
262                         this.groupings = groupings;\r
263                 }\r
264 \r
265                 @Override\r
266                 public DataSchemaNode getDataChildByName(QName name) {\r
267                         return childNodes.get(name);\r
268                 }\r
269 \r
270                 @Override\r
271                 public DataSchemaNode getDataChildByName(String name) {\r
272                         DataSchemaNode result = null;\r
273                         for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {\r
274                                 if (entry.getKey().getLocalName().equals(name)) {\r
275                                         result = entry.getValue();\r
276                                         break;\r
277                                 }\r
278                         }\r
279                         return result;\r
280                 }\r
281 \r
282                 @Override\r
283                 public Set<UsesNode> getUses() {\r
284                         return uses;\r
285                 }\r
286 \r
287                 private void setUses(Set<UsesNode> uses) {\r
288                         this.uses = uses;\r
289                 }\r
290 \r
291                 @Override\r
292                 public boolean isPresenceContainer() {\r
293                         return presence;\r
294                 }\r
295 \r
296                 private void setPresenceContainer(boolean presence) {\r
297                         this.presence = presence;\r
298                 }\r
299 \r
300                 @Override\r
301                 public MustDefinition getMustDefinition() {\r
302                         return mustDefinition;\r
303                 }\r
304                 private void setMustStatement(MustDefinition mustDefinition) {\r
305                         this.mustDefinition = mustDefinition;\r
306                 }\r
307 \r
308                 @Override\r
309                 public Set<TypeDefinition<?>> getTypeDefinitions() {\r
310                         return typeDefinitions;\r
311                 }\r
312                 private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {\r
313                         this.typeDefinitions = typeDefinitions;\r
314                 }\r
315 \r
316                 @Override\r
317                 public List<ExtensionDefinition> getExtensionSchemaNodes() {\r
318                         // TODO Auto-generated method stub\r
319                         return null;\r
320                 }\r
321 \r
322 \r
323                 @Override\r
324         public int hashCode() {\r
325             final int prime = 31;\r
326             int result = 1;\r
327             result = prime * result + ((qname == null) ? 0 : qname.hashCode());\r
328             result = prime * result + ((path == null) ? 0 : path.hashCode());\r
329             result = prime * result + ((description == null) ? 0 : description.hashCode());\r
330             result = prime * result + ((reference == null) ? 0 : reference.hashCode());\r
331             result = prime * result + ((status == null) ? 0 : status.hashCode());\r
332             result = prime * result + (augmenting ? 1231 : 1237);\r
333             result = prime * result + (configuration ? 1231 : 1237);\r
334             result = prime * result + ((constraints == null) ? 0 : constraints.hashCode());\r
335             result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());\r
336             result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());\r
337             result = prime * result + ((groupings == null) ? 0 : groupings.hashCode());\r
338             result = prime * result + ((uses == null) ? 0 : uses.hashCode());\r
339             result = prime * result + (presence ? 1231 : 1237);\r
340             result = prime * result + ((mustDefinition == null) ? 0 : mustDefinition.hashCode());\r
341             return result;\r
342         }\r
343 \r
344         @Override\r
345         public boolean equals(Object obj) {\r
346             if (this == obj) {\r
347                 return true;\r
348             }\r
349             if (obj == null) {\r
350                 return false;\r
351             }\r
352             if (getClass() != obj.getClass()) {\r
353                 return false;\r
354             }\r
355             ContainerSchemaNodeImpl other = (ContainerSchemaNodeImpl) obj;\r
356             if (qname == null) {\r
357                 if (other.qname != null) {\r
358                     return false;\r
359                 }\r
360             } else if (!qname.equals(other.qname)) {\r
361                 return false;\r
362             }\r
363             if (path == null) {\r
364                 if (other.path != null) {\r
365                     return false;\r
366                 }\r
367             } else if (!path.equals(other.path)) {\r
368                 return false;\r
369             }\r
370             if (description == null) {\r
371                 if (other.description != null) {\r
372                     return false;\r
373                 }\r
374             } else if (!description.equals(other.description)) {\r
375                 return false;\r
376             }\r
377             if (reference == null) {\r
378                 if (other.reference != null) {\r
379                     return false;\r
380                 }\r
381             } else if (!reference.equals(other.reference)) {\r
382                 return false;\r
383             }\r
384             if (status == null) {\r
385                 if (other.status != null) {\r
386                     return false;\r
387                 }\r
388             } else if (!status.equals(other.status)) {\r
389                 return false;\r
390             }\r
391             if(augmenting != other.augmenting) {\r
392                 return false;\r
393             }\r
394             if(configuration != other.configuration) {\r
395                 return false;\r
396             }\r
397             if (constraints == null) {\r
398                 if (other.constraints != null) {\r
399                     return false;\r
400                 }\r
401             } else if (!constraints.equals(other.constraints)) {\r
402                 return false;\r
403             }\r
404             if (augmentations == null) {\r
405                 if (other.augmentations != null) {\r
406                     return false;\r
407                 }\r
408             } else if (!augmentations.equals(other.augmentations)) {\r
409                 return false;\r
410             }\r
411             if (childNodes == null) {\r
412                 if (other.childNodes != null) {\r
413                     return false;\r
414                 }\r
415             } else if (!childNodes.equals(other.childNodes)) {\r
416                 return false;\r
417             }\r
418             if (groupings == null) {\r
419                 if (other.groupings != null) {\r
420                     return false;\r
421                 }\r
422             } else if (!groupings.equals(other.groupings)) {\r
423                 return false;\r
424             }\r
425             if (uses == null) {\r
426                 if (other.uses != null) {\r
427                     return false;\r
428                 }\r
429             } else if (!uses.equals(other.uses)) {\r
430                 return false;\r
431             }\r
432                 if(presence != other.presence) {\r
433                 return false;\r
434             }\r
435                 if (mustDefinition == null) {\r
436                 if (other.mustDefinition != null) {\r
437                     return false;\r
438                 }\r
439             } else if (!mustDefinition.equals(other.mustDefinition)) {\r
440                 return false;\r
441             }\r
442             return true;\r
443         }\r
444 \r
445                 @Override\r
446                 public String toString() {\r
447                         StringBuilder sb = new StringBuilder(\r
448                                         ContainerSchemaNodeImpl.class.getSimpleName());\r
449                         sb.append("[");\r
450                         sb.append("qname=" + qname);\r
451                         sb.append(", path=" + path);\r
452                         sb.append(", description=" + description);\r
453                         sb.append(", reference=" + reference);\r
454                         sb.append(", status=" + status);\r
455                         sb.append(", augmenting=" + augmenting);\r
456                         sb.append(", constraints=" + constraints);\r
457                         sb.append(", augmentations=" + augmentations);\r
458                         sb.append(", childNodes=" + childNodes.values());\r
459                         sb.append(", groupings=" + groupings);\r
460                         sb.append(", uses=" + uses);\r
461                         sb.append(", presence=" + presence);\r
462                         sb.append(", mustDefinition="+ mustDefinition);\r
463                         sb.append("]");\r
464                         return sb.toString();\r
465                 }\r
466         }\r
467 \r
468 }