A race condition occurs between ARPHandler and HostTracker if the ARP
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / ChoiceCaseBuilder.java
1 package org.opendaylight.controller.yang.parser.builder.impl;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.TreeMap;
10
11 import org.opendaylight.controller.yang.common.QName;
12 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
13 import org.opendaylight.controller.yang.model.api.ChoiceCaseNode;
14 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
16 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
17 import org.opendaylight.controller.yang.model.api.SchemaPath;
18 import org.opendaylight.controller.yang.model.api.Status;
19 import org.opendaylight.controller.yang.model.api.TypeDefinition;
20 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.controller.yang.model.api.UsesNode;
22 import org.opendaylight.controller.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
23 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
24 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
25 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
28 import org.opendaylight.controller.yang.parser.util.Comparators;
29 import org.opendaylight.controller.yang.parser.util.YangParseException;
30
31 public final class ChoiceCaseBuilder extends AbstractDataNodeContainerBuilder implements DataSchemaNodeBuilder,
32         AugmentationTargetBuilder {
33     private boolean isBuilt;
34     private final ChoiceCaseNodeImpl instance;
35     // SchemaNode args
36     private SchemaPath schemaPath;
37     private String description;
38     private String reference;
39     private Status status = Status.CURRENT;
40     // DataSchemaNode args
41     private boolean augmenting;
42     private final ConstraintsBuilder constraints;
43     // DataNodeContainer args
44     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
45     // AugmentationTarget args
46     private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
47
48     ChoiceCaseBuilder(final String moduleName, final int line, final QName qname) {
49         super(moduleName, line, qname);
50         instance = new ChoiceCaseNodeImpl(qname);
51         constraints = new ConstraintsBuilder(moduleName, line);
52     }
53
54     @Override
55     public ChoiceCaseNode build() {
56         if (!isBuilt) {
57             instance.setConstraints(constraints.build());
58             instance.setPath(schemaPath);
59             instance.setDescription(description);
60             instance.setReference(reference);
61             instance.setStatus(status);
62             instance.setAugmenting(augmenting);
63
64             // CHILD NODES
65             final Map<QName, DataSchemaNode> childs = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
66             for (DataSchemaNodeBuilder node : addedChildNodes) {
67                 childs.put(node.getQName(), node.build());
68             }
69             instance.setChildNodes(childs);
70
71             // USES
72             final Set<UsesNode> uses = new HashSet<UsesNode>();
73             for (UsesNodeBuilder builder : addedUsesNodes) {
74                 uses.add(builder.build());
75             }
76             instance.setUses(uses);
77
78             // UNKNOWN NODES
79             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
80             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
81                 unknownNodes.add(b.build());
82             }
83             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
84             instance.setUnknownSchemaNodes(unknownNodes);
85
86             // AUGMENTATIONS
87             final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
88             for (AugmentationSchemaBuilder builder : addedAugmentations) {
89                 augmentations.add(builder.build());
90             }
91             instance.setAvailableAugmentations(augmentations);
92
93             isBuilt = true;
94         }
95
96         return instance;
97     }
98
99     @Override
100     public void rebuild() {
101         isBuilt = false;
102         build();
103     }
104
105     public SchemaPath getPath() {
106         return schemaPath;
107     }
108
109     @Override
110     public void setPath(final SchemaPath schemaPath) {
111         this.schemaPath = schemaPath;
112     }
113
114     public String getDescription() {
115         return description;
116     }
117
118     @Override
119     public void setDescription(final String description) {
120         this.description = description;
121     }
122
123     public String getReference() {
124         return reference;
125     }
126
127     @Override
128     public void setReference(String reference) {
129         this.reference = reference;
130     }
131
132     public Status getStatus() {
133         return status;
134     }
135
136     @Override
137     public void setStatus(Status status) {
138         if (status != null) {
139             this.status = status;
140         }
141     }
142
143     public boolean isAugmenting() {
144         return augmenting;
145     }
146
147     @Override
148     public void setAugmenting(boolean augmenting) {
149         this.augmenting = augmenting;
150     }
151
152     public Set<UsesNodeBuilder> getUsesNodes() {
153         return addedUsesNodes;
154     }
155
156     @Override
157     public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
158         addedUsesNodes.add(usesNodeBuilder);
159     }
160
161     @Override
162     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
163         return Collections.emptySet();
164     }
165
166     @Override
167     public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
168         throw new YangParseException(moduleName, line, "Can not add type definition to choice case.");
169     }
170
171     @Override
172     public Boolean isConfiguration() {
173         return false;
174     }
175
176     @Override
177     public void setConfiguration(final Boolean configuration) {
178         throw new YangParseException(moduleName, line, "Can not add config statement to choice case.");
179     }
180
181     @Override
182     public ConstraintsBuilder getConstraints() {
183         return constraints;
184     }
185
186     @Override
187     public void addAugmentation(AugmentationSchemaBuilder augment) {
188         addedAugmentations.add(augment);
189     }
190
191     @Override
192     public int hashCode() {
193         final int prime = 31;
194         int result = 1;
195         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
196         return result;
197     }
198
199     @Override
200     public boolean equals(Object obj) {
201         if (this == obj) {
202             return true;
203         }
204         if (obj == null) {
205             return false;
206         }
207         if (getClass() != obj.getClass()) {
208             return false;
209         }
210         ChoiceCaseBuilder other = (ChoiceCaseBuilder) obj;
211         if (schemaPath == null) {
212             if (other.schemaPath != null) {
213                 return false;
214             }
215         } else if (!schemaPath.equals(other.schemaPath)) {
216             return false;
217         }
218         if (parent == null) {
219             if (other.parent != null) {
220                 return false;
221             }
222         } else if (!parent.equals(other.parent)) {
223             return false;
224         }
225         return true;
226     }
227
228     @Override
229     public String toString() {
230         return "case " + getQName().getLocalName();
231     }
232
233     public final class ChoiceCaseNodeImpl implements ChoiceCaseNode {
234         private final QName qname;
235         private SchemaPath path;
236         private String description;
237         private String reference;
238         private Status status = Status.CURRENT;
239         private boolean augmenting;
240         private ConstraintDefinition constraints;
241         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
242         private Set<AugmentationSchema> augmentations = Collections.emptySet();
243         private Set<UsesNode> uses = Collections.emptySet();
244         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
245
246         private ChoiceCaseNodeImpl(QName qname) {
247             this.qname = qname;
248         }
249
250         @Override
251         public QName getQName() {
252             return qname;
253         }
254
255         @Override
256         public SchemaPath getPath() {
257             return path;
258         }
259
260         private void setPath(SchemaPath path) {
261             this.path = path;
262         }
263
264         @Override
265         public String getDescription() {
266             return description;
267         }
268
269         private void setDescription(String description) {
270             this.description = description;
271         }
272
273         @Override
274         public String getReference() {
275             return reference;
276         }
277
278         private void setReference(String reference) {
279             this.reference = reference;
280         }
281
282         @Override
283         public Status getStatus() {
284             return status;
285         }
286
287         private void setStatus(Status status) {
288             if (status != null) {
289                 this.status = status;
290             }
291         }
292
293         @Override
294         public boolean isConfiguration() {
295             return false;
296         }
297
298         @Override
299         public ConstraintDefinition getConstraints() {
300             return constraints;
301         }
302
303         private void setConstraints(ConstraintDefinition constraints) {
304             this.constraints = constraints;
305         }
306
307         @Override
308         public boolean isAugmenting() {
309             return augmenting;
310         }
311
312         private void setAugmenting(boolean augmenting) {
313             this.augmenting = augmenting;
314         }
315
316         @Override
317         public boolean isAddedByUses() {
318             return false;
319         }
320
321         @Override
322         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
323             return unknownNodes;
324         }
325
326         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
327             if (unknownNodes != null) {
328                 this.unknownNodes = unknownNodes;
329             }
330         }
331
332         /**
333          * Always returns an empty set, because case node can not contains type
334          * definitions.
335          */
336         @Override
337         public Set<TypeDefinition<?>> getTypeDefinitions() {
338             return Collections.emptySet();
339         }
340
341         @Override
342         public Set<DataSchemaNode> getChildNodes() {
343             return new HashSet<DataSchemaNode>(childNodes.values());
344         }
345
346         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
347             if (childNodes != null) {
348                 this.childNodes = childNodes;
349             }
350         }
351
352         @Override
353         public Set<GroupingDefinition> getGroupings() {
354             return Collections.emptySet();
355         }
356
357         @Override
358         public DataSchemaNode getDataChildByName(QName name) {
359             return childNodes.get(name);
360         }
361
362         @Override
363         public DataSchemaNode getDataChildByName(String name) {
364             DataSchemaNode result = null;
365             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
366                 if (entry.getKey().getLocalName().equals(name)) {
367                     result = entry.getValue();
368                     break;
369                 }
370             }
371             return result;
372         }
373
374         @Override
375         public Set<UsesNode> getUses() {
376             return uses;
377         }
378
379         private void setUses(Set<UsesNode> uses) {
380             if (uses != null) {
381                 this.uses = uses;
382             }
383         }
384
385         @Override
386         public Set<AugmentationSchema> getAvailableAugmentations() {
387             return augmentations;
388         }
389
390         private void setAvailableAugmentations(Set<AugmentationSchema> augmentations) {
391             if (augmentations != null) {
392                 this.augmentations = augmentations;
393             }
394         }
395
396         public ChoiceCaseBuilder toBuilder() {
397             return ChoiceCaseBuilder.this;
398         }
399
400         @Override
401         public int hashCode() {
402             final int prime = 31;
403             int result = 1;
404             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
405             result = prime * result + ((path == null) ? 0 : path.hashCode());
406             return result;
407         }
408
409         @Override
410         public boolean equals(Object obj) {
411             if (this == obj) {
412                 return true;
413             }
414             if (obj == null) {
415                 return false;
416             }
417             if (getClass() != obj.getClass()) {
418                 return false;
419             }
420             ChoiceCaseNodeImpl other = (ChoiceCaseNodeImpl) obj;
421             if (qname == null) {
422                 if (other.qname != null) {
423                     return false;
424                 }
425             } else if (!qname.equals(other.qname)) {
426                 return false;
427             }
428             if (path == null) {
429                 if (other.path != null) {
430                     return false;
431                 }
432             } else if (!path.equals(other.path)) {
433                 return false;
434             }
435             return true;
436         }
437
438         @Override
439         public String toString() {
440             StringBuilder sb = new StringBuilder(ChoiceCaseNodeImpl.class.getSimpleName());
441             sb.append("[");
442             sb.append("qname=" + qname);
443             sb.append("]");
444             return sb.toString();
445         }
446     }
447
448 }