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 / ListSchemaNodeBuilder.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.AugmentationSchema;
19 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
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.ListSchemaNode;
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.model.parser.builder.api.AbstractChildNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationTargetBuilder;
30 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
31 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
32 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
33 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionAwareBuilder;
34 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
35 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
36
37 public class ListSchemaNodeBuilder extends AbstractChildNodeBuilder implements
38         DataSchemaNodeBuilder, SchemaNodeBuilder, AugmentationTargetBuilder,
39         TypeDefinitionAwareBuilder {
40
41     private final ListSchemaNodeImpl instance;
42     private final ConstraintsBuilder constraintsBuilder;
43
44     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
45     private final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
46     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
47
48     ListSchemaNodeBuilder(QName qname) {
49         super(qname);
50         instance = new ListSchemaNodeImpl(qname);
51         constraintsBuilder = new ConstraintsBuilder();
52     }
53
54     @Override
55     public ListSchemaNode build() {
56         // CHILD NODES
57         Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
58         for (DataSchemaNodeBuilder node : childNodes) {
59             childs.put(node.getQName(), node.build());
60         }
61         instance.setChildNodes(childs);
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> usesNodeDefinitions = new HashSet<UsesNode>();
72         for (UsesNodeBuilder builder : usesNodes) {
73             usesNodeDefinitions.add(builder.build());
74         }
75         instance.setUses(usesNodeDefinitions);
76
77         // GROUPINGS
78         Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
79         for (GroupingBuilder builder : groupings) {
80             groupingDefinitions.add(builder.build());
81         }
82         instance.setGroupings(groupingDefinitions);
83
84         instance.setConstraints(constraintsBuilder.build());
85         instance.setAvailableAugmentations(augmentations);
86
87         return instance;
88     }
89
90     @Override
91     public void addTypedef(TypeDefinitionBuilder type) {
92         addedTypedefs.add(type);
93     }
94
95     @Override
96     public void setPath(SchemaPath path) {
97         instance.setPath(path);
98     }
99
100     @Override
101     public void setDescription(String description) {
102         instance.setDescription(description);
103     }
104
105     @Override
106     public void setReference(String reference) {
107         instance.setReference(reference);
108     }
109
110     @Override
111     public void setStatus(Status status) {
112         if(status != null) {
113             instance.setStatus(status);
114         }
115     }
116
117     @Override
118     public void addUsesNode(UsesNodeBuilder usesBuilder) {
119         usesNodes.add(usesBuilder);
120     }
121
122     @Override
123     public void addAugmentation(AugmentationSchema augmentationSchema) {
124         augmentations.add(augmentationSchema);
125     }
126
127     public void setKeyDefinition(List<QName> keyDefinition) {
128         instance.setKeyDefinition(keyDefinition);
129     }
130
131     @Override
132     public void setAugmenting(boolean augmenting) {
133         instance.setAugmenting(augmenting);
134     }
135
136     @Override
137     public void setConfiguration(boolean configuration) {
138         instance.setConfiguration(configuration);
139     }
140
141     @Override
142     public ConstraintsBuilder getConstraintsBuilder() {
143         return constraintsBuilder;
144     }
145
146     public void setUserOrdered(boolean userOrdered) {
147         instance.setUserOrdered(userOrdered);
148     }
149
150     private class ListSchemaNodeImpl implements ListSchemaNode {
151         private final QName qname;
152         private SchemaPath path;
153         private String description;
154         private String reference;
155         private Status status = Status.CURRENT;
156         private List<QName> keyDefinition = Collections.emptyList();
157         private boolean augmenting;
158         private boolean configuration;
159         private ConstraintDefinition constraints;
160         private Set<AugmentationSchema> augmentations = Collections.emptySet();
161         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
162         private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
163         private Set<GroupingDefinition> groupings = Collections.emptySet();
164         private Set<UsesNode> uses = Collections.emptySet();
165         private boolean userOrdered;
166         private List<UnknownSchemaNode> unknownSchemaNodes = Collections.emptyList();
167
168         private ListSchemaNodeImpl(QName qname) {
169             this.qname = qname;
170         }
171
172         @Override
173         public QName getQName() {
174             return qname;
175         }
176
177         @Override
178         public SchemaPath getPath() {
179             return path;
180         }
181
182         private void setPath(SchemaPath path) {
183             this.path = path;
184         }
185
186         @Override
187         public String getDescription() {
188             return description;
189         }
190
191         private void setDescription(String description) {
192             this.description = description;
193         }
194
195         @Override
196         public String getReference() {
197             return reference;
198         }
199
200         private void setReference(String reference) {
201             this.reference = reference;
202         }
203
204         @Override
205         public Status getStatus() {
206             return status;
207         }
208
209         private void setStatus(Status status) {
210             this.status = status;
211         }
212
213         @Override
214         public List<QName> getKeyDefinition() {
215             if(keyDefinition == null) {
216                 return Collections.emptyList();
217             } else {
218                 return keyDefinition;
219             }
220         }
221
222         private void setKeyDefinition(List<QName> keyDefinition) {
223             if(keyDefinition != null) {
224                 this.keyDefinition = keyDefinition;
225             }
226         }
227
228         @Override
229         public boolean isAugmenting() {
230             return augmenting;
231         }
232
233         private void setAugmenting(boolean augmenting) {
234             this.augmenting = augmenting;
235         }
236
237         @Override
238         public boolean isConfiguration() {
239             return configuration;
240         }
241
242         private void setConfiguration(boolean configuration) {
243             this.configuration = configuration;
244         }
245
246         @Override
247         public ConstraintDefinition getConstraints() {
248             return constraints;
249         }
250
251         private void setConstraints(ConstraintDefinition constraints) {
252             this.constraints = constraints;
253         }
254
255         @Override
256         public Set<AugmentationSchema> getAvailableAugmentations() {
257             return augmentations;
258         }
259
260         private void setAvailableAugmentations(
261                 Set<AugmentationSchema> augmentations) {
262             if(augmentations != null) {
263                 this.augmentations = augmentations;
264             }
265         }
266
267         @Override
268         public Set<DataSchemaNode> getChildNodes() {
269             return new HashSet<DataSchemaNode>(childNodes.values());
270         }
271
272         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
273             if(childNodes != null) {
274                 this.childNodes = childNodes;
275             }
276         }
277
278         @Override
279         public Set<GroupingDefinition> getGroupings() {
280             return groupings;
281         }
282
283         private void setGroupings(Set<GroupingDefinition> groupings) {
284             if(groupings != null) {
285                 this.groupings = groupings;
286             }
287         }
288
289         @Override
290         public Set<TypeDefinition<?>> getTypeDefinitions() {
291             return typeDefinitions;
292         }
293
294         private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
295             if(typeDefinitions != null) {
296                 this.typeDefinitions = typeDefinitions;
297             }
298         }
299
300         @Override
301         public Set<UsesNode> getUses() {
302             return uses;
303         }
304
305         private void setUses(Set<UsesNode> uses) {
306             if(uses != null) {
307                 this.uses = uses;
308             }
309         }
310
311         @Override
312         public DataSchemaNode getDataChildByName(QName name) {
313             return childNodes.get(name);
314         }
315
316         @Override
317         public DataSchemaNode getDataChildByName(String name) {
318             DataSchemaNode result = null;
319             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
320                 if (entry.getKey().getLocalName().equals(name)) {
321                     result = entry.getValue();
322                     break;
323                 }
324             }
325             return result;
326         }
327
328         @Override
329         public boolean isUserOrdered() {
330             return userOrdered;
331         }
332
333         private void setUserOrdered(boolean userOrdered) {
334             this.userOrdered = userOrdered;
335         }
336
337         @Override
338         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
339             return unknownSchemaNodes;
340         }
341
342         @Override
343         public int hashCode() {
344             final int prime = 31;
345             int result = 1;
346             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
347             result = prime * result + ((path == null) ? 0 : path.hashCode());
348             result = prime * result
349                     + ((description == null) ? 0 : description.hashCode());
350             result = prime * result
351                     + ((reference == null) ? 0 : reference.hashCode());
352             result = prime * result
353                     + ((status == null) ? 0 : status.hashCode());
354             result = prime * result
355                     + ((keyDefinition == null) ? 0 : keyDefinition.hashCode());
356             result = prime * result + (augmenting ? 1231 : 1237);
357             result = prime * result + (configuration ? 1231 : 1237);
358             result = prime * result
359                     + ((constraints == null) ? 0 : constraints.hashCode());
360             result = prime * result
361                     + ((augmentations == null) ? 0 : augmentations.hashCode());
362             result = prime * result
363                     + ((childNodes == null) ? 0 : childNodes.hashCode());
364             result = prime
365                     * result
366                     + ((typeDefinitions == null) ? 0 : typeDefinitions
367                             .hashCode());
368             result = prime * result
369                     + ((groupings == null) ? 0 : groupings.hashCode());
370             result = prime * result + ((uses == null) ? 0 : uses.hashCode());
371             result = prime * result + (userOrdered ? 1231 : 1237);
372             return result;
373         }
374
375         @Override
376         public boolean equals(Object obj) {
377             if (this == obj) {
378                 return true;
379             }
380             if (obj == null) {
381                 return false;
382             }
383             if (getClass() != obj.getClass()) {
384                 return false;
385             }
386             ListSchemaNodeImpl other = (ListSchemaNodeImpl) obj;
387             if (qname == null) {
388                 if (other.qname != null) {
389                     return false;
390                 }
391             } else if (!qname.equals(other.qname)) {
392                 return false;
393             }
394             if (path == null) {
395                 if (other.path != null) {
396                     return false;
397                 }
398             } else if (!path.equals(other.path)) {
399                 return false;
400             }
401             if (description == null) {
402                 if (other.description != null) {
403                     return false;
404                 }
405             } else if (!description.equals(other.description)) {
406                 return false;
407             }
408             if (reference == null) {
409                 if (other.reference != null) {
410                     return false;
411                 }
412             } else if (!reference.equals(other.reference)) {
413                 return false;
414             }
415             if (status == null) {
416                 if (other.status != null) {
417                     return false;
418                 }
419             } else if (!status.equals(other.status)) {
420                 return false;
421             }
422             if (keyDefinition == null) {
423                 if (other.keyDefinition != null) {
424                     return false;
425                 }
426             } else if (!keyDefinition.equals(other.keyDefinition)) {
427                 return false;
428             }
429             if (augmenting != other.augmenting) {
430                 return false;
431             }
432             if (configuration != other.configuration) {
433                 return false;
434             }
435             if (constraints == null) {
436                 if (other.constraints != null) {
437                     return false;
438                 }
439             } else if (!constraints.equals(other.constraints)) {
440                 return false;
441             }
442             if (augmentations == null) {
443                 if (other.augmentations != null) {
444                     return false;
445                 }
446             } else if (!augmentations.equals(other.augmentations)) {
447                 return false;
448             }
449             if (childNodes == null) {
450                 if (other.childNodes != null) {
451                     return false;
452                 }
453             } else if (!childNodes.equals(other.childNodes)) {
454                 return false;
455             }
456             if (typeDefinitions == null) {
457                 if (other.typeDefinitions != null) {
458                     return false;
459                 }
460             } else if (!typeDefinitions.equals(other.typeDefinitions)) {
461                 return false;
462             }
463             if (groupings == null) {
464                 if (other.groupings != null) {
465                     return false;
466                 }
467             } else if (!groupings.equals(other.groupings)) {
468                 return false;
469             }
470             if (uses == null) {
471                 if (other.uses != null) {
472                     return false;
473                 }
474             } else if (!uses.equals(other.uses)) {
475                 return false;
476             }
477             if (userOrdered != other.userOrdered) {
478                 return false;
479             }
480             return true;
481         }
482
483         @Override
484         public String toString() {
485             StringBuilder sb = new StringBuilder(
486                     ListSchemaNodeImpl.class.getSimpleName());
487             sb.append("[");
488             sb.append("qname=" + qname);
489             sb.append(", path=" + path);
490             sb.append(", description=" + description);
491             sb.append(", reference=" + reference);
492             sb.append(", status=" + status);
493             sb.append(", keyDefinition=" + keyDefinition);
494             sb.append(", augmenting=" + augmenting);
495             sb.append(", configuration=" + configuration);
496             sb.append(", constraints=" + constraints);
497             sb.append(", augmentations=" + augmentations);
498             sb.append(", childNodes=" + childNodes.values());
499             sb.append(", typedefinitions=" + typeDefinitions);
500             sb.append(", groupings=" + groupings);
501             sb.append(", uses=" + uses);
502             sb.append(", userOrdered=" + userOrdered);
503             sb.append("]");
504             return sb.toString();
505         }
506     }
507
508 }