When a node is going down, remove edges in both directions associated with the node.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / ChoiceBuilder.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.Set;
15 import java.util.TreeSet;
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.ChoiceCaseNode;
20 import org.opendaylight.controller.yang.model.api.ChoiceNode;
21 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
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.UnknownSchemaNode;
25 import org.opendaylight.controller.yang.parser.builder.api.AbstractSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
30 import org.opendaylight.controller.yang.parser.util.Comparators;
31 import org.opendaylight.controller.yang.parser.util.ParserUtils;
32 import org.opendaylight.controller.yang.parser.util.YangParseException;
33
34 public final class ChoiceBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder,
35         AugmentationTargetBuilder, GroupingMember {
36     private boolean isBuilt;
37     private final ChoiceNodeImpl instance;
38     // DataSchemaNode args
39     private boolean augmenting;
40     private boolean addedByUses;
41     private Boolean configuration;
42     private final ConstraintsBuilder constraints;
43     // AugmentationTarget args
44     private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
45     // ChoiceNode args
46     private Set<ChoiceCaseNode> cases;
47     private final Set<ChoiceCaseBuilder> addedCases = new HashSet<ChoiceCaseBuilder>();
48     private String defaultCase;
49
50     public ChoiceBuilder(final String moduleName, final int line, final QName qname) {
51         super(moduleName, line, qname);
52         instance = new ChoiceNodeImpl(qname);
53         constraints = new ConstraintsBuilder(moduleName, line);
54     }
55
56     public ChoiceBuilder(ChoiceBuilder b) {
57         super(b.getModuleName(), b.getLine(), b.getQName());
58         parent = b.getParent();
59         instance = new ChoiceNodeImpl(qname);
60         constraints = b.getConstraints();
61         schemaPath = b.getPath();
62         description = b.getDescription();
63         reference = b.getReference();
64         status = b.getStatus();
65         unknownNodes = b.unknownNodes;
66         addedUnknownNodes.addAll(b.getUnknownNodes());
67         augmenting = b.isAugmenting();
68         addedByUses = b.isAddedByUses();
69         configuration = b.isConfiguration();
70         addedAugmentations.addAll(b.getAugmentations());
71         cases = b.cases;
72         addedCases.addAll(b.getCases());
73         defaultCase = b.getDefaultCase();
74     }
75
76     @Override
77     public ChoiceNode build() {
78         if (!isBuilt) {
79             instance.setPath(schemaPath);
80             instance.setDescription(description);
81             instance.setReference(reference);
82             instance.setStatus(status);
83             instance.setAugmenting(augmenting);
84             instance.setAddedByUses(addedByUses);
85             instance.setConfiguration(configuration);
86             instance.setConstraints(constraints.build());
87             instance.setDefaultCase(defaultCase);
88
89             // CASES
90             if (cases == null) {
91                 cases = new TreeSet<ChoiceCaseNode>(Comparators.SCHEMA_NODE_COMP);
92                 for (ChoiceCaseBuilder caseBuilder : addedCases) {
93                     cases.add(caseBuilder.build());
94                 }
95             }
96             instance.setCases(cases);
97
98             // AUGMENTATIONS
99             final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
100             for (AugmentationSchemaBuilder builder : addedAugmentations) {
101                 augmentations.add(builder.build());
102             }
103             instance.setAvailableAugmentations(augmentations);
104
105             // UNKNOWN NODES
106             if (unknownNodes == null) {
107                 unknownNodes = new ArrayList<UnknownSchemaNode>();
108                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
109                     unknownNodes.add(b.build());
110                 }
111                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
112             }
113             instance.setUnknownSchemaNodes(unknownNodes);
114
115             isBuilt = true;
116         }
117         return instance;
118     }
119
120     @Override
121     public void rebuild() {
122         isBuilt = false;
123         build();
124     }
125
126     public Set<ChoiceCaseBuilder> getCases() {
127         return addedCases;
128     }
129
130     /**
131      * Add case node to this choice.
132      *
133      * If node is not declared with 'case' keyword, create new case builder and
134      * make this node child of newly created case.
135      *
136      * @param caseNode
137      *            case node
138      */
139     public void addCase(DataSchemaNodeBuilder caseNode) {
140         QName caseQName = caseNode.getQName();
141         String caseName = caseQName.getLocalName();
142         for (ChoiceCaseBuilder addedCase : addedCases) {
143             if (addedCase.getQName().getLocalName().equals(caseName)) {
144                 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
145                         + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
146                         + "': case with same name already declared at line " + addedCase.getLine());
147             }
148         }
149
150         if (caseNode instanceof ChoiceCaseBuilder) {
151             addedCases.add((ChoiceCaseBuilder) caseNode);
152         } else {
153             ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
154                     caseQName);
155             if (caseNode.isAugmenting()) {
156                 // if node is added by augmentation, set case builder augmenting
157                 // as true and node augmenting as false
158                 caseBuilder.setAugmenting(true);
159                 caseNode.setAugmenting(false);
160             }
161             caseBuilder.setPath(caseNode.getPath());
162             SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseQName.getLocalName(),
163                     caseQName.getNamespace(), caseQName.getRevision(), caseQName.getPrefix());
164             caseNode.setPath(newPath);
165             caseBuilder.addChildNode(caseNode);
166             addedCases.add(caseBuilder);
167         }
168     }
169
170     public void setCases(Set<ChoiceCaseNode> cases) {
171         this.cases = cases;
172     }
173
174     @Override
175     public boolean isAugmenting() {
176         return augmenting;
177     }
178
179     @Override
180     public void setAugmenting(boolean augmenting) {
181         this.augmenting = augmenting;
182     }
183
184     @Override
185     public boolean isAddedByUses() {
186         return addedByUses;
187     }
188
189     @Override
190     public void setAddedByUses(final boolean addedByUses) {
191         this.addedByUses = addedByUses;
192     }
193
194     public Boolean isConfiguration() {
195         return configuration;
196     }
197
198     @Override
199     public void setConfiguration(Boolean configuration) {
200         this.configuration = configuration;
201     }
202
203     @Override
204     public ConstraintsBuilder getConstraints() {
205         return constraints;
206     }
207
208     public Set<AugmentationSchemaBuilder> getAugmentations() {
209         return addedAugmentations;
210     }
211
212     @Override
213     public void addAugmentation(AugmentationSchemaBuilder augment) {
214         addedAugmentations.add(augment);
215     }
216
217     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
218         return addedUnknownNodes;
219     }
220
221     public String getDefaultCase() {
222         return defaultCase;
223     }
224
225     public void setDefaultCase(String defaultCase) {
226         this.defaultCase = defaultCase;
227     }
228
229     @Override
230     public int hashCode() {
231         final int prime = 31;
232         int result = 1;
233         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
234         return result;
235     }
236
237     @Override
238     public boolean equals(Object obj) {
239         if (this == obj) {
240             return true;
241         }
242         if (obj == null) {
243             return false;
244         }
245         if (getClass() != obj.getClass()) {
246             return false;
247         }
248         ChoiceBuilder other = (ChoiceBuilder) obj;
249         if (schemaPath == null) {
250             if (other.schemaPath != null) {
251                 return false;
252             }
253         } else if (!schemaPath.equals(other.schemaPath)) {
254             return false;
255         }
256         if (parent == null) {
257             if (other.parent != null) {
258                 return false;
259             }
260         } else if (!parent.equals(other.parent)) {
261             return false;
262         }
263         return true;
264     }
265
266     @Override
267     public String toString() {
268         return "choice " + qname.getLocalName();
269     }
270
271     public final class ChoiceNodeImpl implements ChoiceNode {
272         private final QName qname;
273         private SchemaPath path;
274         private String description;
275         private String reference;
276         private Status status = Status.CURRENT;
277         private boolean augmenting;
278         private boolean addedByUses;
279         private boolean configuration;
280         private ConstraintDefinition constraints;
281         private Set<ChoiceCaseNode> cases = Collections.emptySet();
282         private Set<AugmentationSchema> augmentations = Collections.emptySet();
283         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
284         private String defaultCase;
285
286         private ChoiceNodeImpl(QName qname) {
287             this.qname = qname;
288         }
289
290         @Override
291         public QName getQName() {
292             return qname;
293         }
294
295         @Override
296         public SchemaPath getPath() {
297             return path;
298         }
299
300         private void setPath(SchemaPath path) {
301             this.path = path;
302         }
303
304         @Override
305         public String getDescription() {
306             return description;
307         }
308
309         private void setDescription(String description) {
310             this.description = description;
311         }
312
313         @Override
314         public String getReference() {
315             return reference;
316         }
317
318         private void setReference(String reference) {
319             this.reference = reference;
320         }
321
322         @Override
323         public Status getStatus() {
324             return status;
325         }
326
327         private void setStatus(Status status) {
328             if (status != null) {
329                 this.status = status;
330             }
331         }
332
333         @Override
334         public boolean isAugmenting() {
335             return augmenting;
336         }
337
338         private void setAugmenting(boolean augmenting) {
339             this.augmenting = augmenting;
340         }
341
342         @Override
343         public boolean isAddedByUses() {
344             return addedByUses;
345         }
346
347         private void setAddedByUses(boolean addedByUses) {
348             this.addedByUses = addedByUses;
349         }
350
351         @Override
352         public boolean isConfiguration() {
353             return configuration;
354         }
355
356         private void setConfiguration(boolean configuration) {
357             this.configuration = configuration;
358         }
359
360         @Override
361         public ConstraintDefinition getConstraints() {
362             return constraints;
363         }
364
365         private void setConstraints(ConstraintDefinition constraints) {
366             this.constraints = constraints;
367         }
368
369         @Override
370         public Set<AugmentationSchema> getAvailableAugmentations() {
371             return augmentations;
372         }
373
374         private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
375             if (availableAugmentations != null) {
376                 this.augmentations = availableAugmentations;
377             }
378         }
379
380         @Override
381         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
382             return unknownNodes;
383         }
384
385         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
386             if (unknownSchemaNodes != null) {
387                 this.unknownNodes = unknownSchemaNodes;
388             }
389         }
390
391         @Override
392         public Set<ChoiceCaseNode> getCases() {
393             return cases;
394         }
395
396         @Override
397         public ChoiceCaseNode getCaseNodeByName(final QName name) {
398             if (name == null) {
399                 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
400             }
401             for (final ChoiceCaseNode caseNode : cases) {
402                 if (caseNode != null) {
403                     if (name.equals(caseNode.getQName())) {
404                         return caseNode;
405                     }
406                 }
407             }
408             return null;
409         }
410
411         @Override
412         public ChoiceCaseNode getCaseNodeByName(final String name) {
413             if (name == null) {
414                 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
415             }
416             for (final ChoiceCaseNode caseNode : cases) {
417                 if (caseNode != null && (caseNode.getQName() != null)) {
418                     if (name.equals(caseNode.getQName().getLocalName())) {
419                         return caseNode;
420                     }
421                 }
422             }
423             return null;
424         }
425
426         private void setCases(Set<ChoiceCaseNode> cases) {
427             if (cases != null) {
428                 this.cases = cases;
429             }
430         }
431
432         @Override
433         public String getDefaultCase() {
434             return defaultCase;
435         }
436
437         private void setDefaultCase(String defaultCase) {
438             this.defaultCase = defaultCase;
439         }
440
441         public ChoiceBuilder toBuilder() {
442             return ChoiceBuilder.this;
443         }
444
445         @Override
446         public int hashCode() {
447             final int prime = 31;
448             int result = 1;
449             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
450             result = prime * result + ((path == null) ? 0 : path.hashCode());
451             return result;
452         }
453
454         @Override
455         public boolean equals(Object obj) {
456             if (this == obj) {
457                 return true;
458             }
459             if (obj == null) {
460                 return false;
461             }
462             if (getClass() != obj.getClass()) {
463                 return false;
464             }
465             ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
466             if (qname == null) {
467                 if (other.qname != null) {
468                     return false;
469                 }
470             } else if (!qname.equals(other.qname)) {
471                 return false;
472             }
473             if (path == null) {
474                 if (other.path != null) {
475                     return false;
476                 }
477             } else if (!path.equals(other.path)) {
478                 return false;
479             }
480             return true;
481         }
482
483         @Override
484         public String toString() {
485             StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
486             sb.append("[");
487             sb.append("qname=" + qname);
488             sb.append("]");
489             return sb.toString();
490         }
491     }
492
493 }