1cd54cae62ab2a93ba050351fe90388267632bba
[controller.git] / opendaylight / sal / yang-prototype / code-generator / 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         String newCaseName = caseNode.getQName().getLocalName();
141         for (ChoiceCaseBuilder addedCase : addedCases) {
142             if (addedCase.getQName().getLocalName().equals(newCaseName)) {
143                 throw new YangParseException(caseNode.getModuleName(), caseNode.getLine(), "Can not add '" + caseNode
144                         + "' to node '" + qname.getLocalName() + "' in module '" + moduleName
145                         + "': case with same name already declared at line " + addedCase.getLine());
146             }
147         }
148
149         if (caseNode instanceof ChoiceCaseBuilder) {
150             addedCases.add((ChoiceCaseBuilder) caseNode);
151         } else {
152             ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(caseNode.getModuleName(), caseNode.getLine(),
153                     caseNode.getQName());
154             if (caseNode.isAugmenting()) {
155                 // if node is added by augmentation, set case builder augmenting
156                 // as true and node augmenting as false
157                 caseBuilder.setAugmenting(true);
158                 caseNode.setAugmenting(false);
159             }
160             caseBuilder.setPath(caseNode.getPath());
161             SchemaPath newPath = ParserUtils.createSchemaPath(caseNode.getPath(), caseNode.getQName().getLocalName());
162             caseNode.setPath(newPath);
163             caseBuilder.addChildNode(caseNode);
164             addedCases.add(caseBuilder);
165         }
166     }
167
168     public void setCases(Set<ChoiceCaseNode> cases) {
169         this.cases = cases;
170     }
171
172     @Override
173     public boolean isAugmenting() {
174         return augmenting;
175     }
176
177     @Override
178     public void setAugmenting(boolean augmenting) {
179         this.augmenting = augmenting;
180     }
181
182     @Override
183     public boolean isAddedByUses() {
184         return addedByUses;
185     }
186
187     @Override
188     public void setAddedByUses(final boolean addedByUses) {
189         this.addedByUses = addedByUses;
190     }
191
192     public Boolean isConfiguration() {
193         return configuration;
194     }
195
196     @Override
197     public void setConfiguration(Boolean configuration) {
198         this.configuration = configuration;
199     }
200
201     @Override
202     public ConstraintsBuilder getConstraints() {
203         return constraints;
204     }
205
206     public Set<AugmentationSchemaBuilder> getAugmentations() {
207         return addedAugmentations;
208     }
209
210     @Override
211     public void addAugmentation(AugmentationSchemaBuilder augment) {
212         addedAugmentations.add(augment);
213     }
214
215     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
216         return addedUnknownNodes;
217     }
218
219     public String getDefaultCase() {
220         return defaultCase;
221     }
222
223     public void setDefaultCase(String defaultCase) {
224         this.defaultCase = defaultCase;
225     }
226
227     @Override
228     public int hashCode() {
229         final int prime = 31;
230         int result = 1;
231         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
232         return result;
233     }
234
235     @Override
236     public boolean equals(Object obj) {
237         if (this == obj) {
238             return true;
239         }
240         if (obj == null) {
241             return false;
242         }
243         if (getClass() != obj.getClass()) {
244             return false;
245         }
246         ChoiceBuilder other = (ChoiceBuilder) obj;
247         if (schemaPath == null) {
248             if (other.schemaPath != null) {
249                 return false;
250             }
251         } else if (!schemaPath.equals(other.schemaPath)) {
252             return false;
253         }
254         if (parent == null) {
255             if (other.parent != null) {
256                 return false;
257             }
258         } else if (!parent.equals(other.parent)) {
259             return false;
260         }
261         return true;
262     }
263
264     @Override
265     public String toString() {
266         return "choice " + qname.getLocalName();
267     }
268
269     public final class ChoiceNodeImpl implements ChoiceNode {
270         private final QName qname;
271         private SchemaPath path;
272         private String description;
273         private String reference;
274         private Status status = Status.CURRENT;
275         private boolean augmenting;
276         private boolean addedByUses;
277         private boolean configuration;
278         private ConstraintDefinition constraints;
279         private Set<ChoiceCaseNode> cases = Collections.emptySet();
280         private Set<AugmentationSchema> augmentations = Collections.emptySet();
281         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
282         private String defaultCase;
283
284         private ChoiceNodeImpl(QName qname) {
285             this.qname = qname;
286         }
287
288         @Override
289         public QName getQName() {
290             return qname;
291         }
292
293         @Override
294         public SchemaPath getPath() {
295             return path;
296         }
297
298         private void setPath(SchemaPath path) {
299             this.path = path;
300         }
301
302         @Override
303         public String getDescription() {
304             return description;
305         }
306
307         private void setDescription(String description) {
308             this.description = description;
309         }
310
311         @Override
312         public String getReference() {
313             return reference;
314         }
315
316         private void setReference(String reference) {
317             this.reference = reference;
318         }
319
320         @Override
321         public Status getStatus() {
322             return status;
323         }
324
325         private void setStatus(Status status) {
326             if (status != null) {
327                 this.status = status;
328             }
329         }
330
331         @Override
332         public boolean isAugmenting() {
333             return augmenting;
334         }
335
336         private void setAugmenting(boolean augmenting) {
337             this.augmenting = augmenting;
338         }
339
340         @Override
341         public boolean isAddedByUses() {
342             return addedByUses;
343         }
344
345         private void setAddedByUses(boolean addedByUses) {
346             this.addedByUses = addedByUses;
347         }
348
349         @Override
350         public boolean isConfiguration() {
351             return configuration;
352         }
353
354         private void setConfiguration(boolean configuration) {
355             this.configuration = configuration;
356         }
357
358         @Override
359         public ConstraintDefinition getConstraints() {
360             return constraints;
361         }
362
363         private void setConstraints(ConstraintDefinition constraints) {
364             this.constraints = constraints;
365         }
366
367         @Override
368         public Set<AugmentationSchema> getAvailableAugmentations() {
369             return augmentations;
370         }
371
372         private void setAvailableAugmentations(Set<AugmentationSchema> availableAugmentations) {
373             if (availableAugmentations != null) {
374                 this.augmentations = availableAugmentations;
375             }
376         }
377
378         @Override
379         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
380             return unknownNodes;
381         }
382
383         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
384             if (unknownSchemaNodes != null) {
385                 this.unknownNodes = unknownSchemaNodes;
386             }
387         }
388
389         @Override
390         public Set<ChoiceCaseNode> getCases() {
391             return cases;
392         }
393
394         @Override
395         public ChoiceCaseNode getCaseNodeByName(final QName name) {
396             if (name == null) {
397                 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
398             }
399             for (final ChoiceCaseNode caseNode : cases) {
400                 if (caseNode != null) {
401                     if (name.equals(caseNode.getQName())) {
402                         return caseNode;
403                     }
404                 }
405             }
406             return null;
407         }
408
409         @Override
410         public ChoiceCaseNode getCaseNodeByName(final String name) {
411             if (name == null) {
412                 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
413             }
414             for (final ChoiceCaseNode caseNode : cases) {
415                 if (caseNode != null && (caseNode.getQName() != null)) {
416                     if (name.equals(caseNode.getQName().getLocalName())) {
417                         return caseNode;
418                     }
419                 }
420             }
421             return null;
422         }
423
424         private void setCases(Set<ChoiceCaseNode> cases) {
425             if (cases != null) {
426                 this.cases = cases;
427             }
428         }
429
430         @Override
431         public String getDefaultCase() {
432             return defaultCase;
433         }
434
435         private void setDefaultCase(String defaultCase) {
436             this.defaultCase = defaultCase;
437         }
438
439         public ChoiceBuilder toBuilder() {
440             return ChoiceBuilder.this;
441         }
442
443         @Override
444         public int hashCode() {
445             final int prime = 31;
446             int result = 1;
447             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
448             result = prime * result + ((path == null) ? 0 : path.hashCode());
449             return result;
450         }
451
452         @Override
453         public boolean equals(Object obj) {
454             if (this == obj) {
455                 return true;
456             }
457             if (obj == null) {
458                 return false;
459             }
460             if (getClass() != obj.getClass()) {
461                 return false;
462             }
463             ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
464             if (qname == null) {
465                 if (other.qname != null) {
466                     return false;
467                 }
468             } else if (!qname.equals(other.qname)) {
469                 return false;
470             }
471             if (path == null) {
472                 if (other.path != null) {
473                     return false;
474                 }
475             } else if (!path.equals(other.path)) {
476                 return false;
477             }
478             return true;
479         }
480
481         @Override
482         public String toString() {
483             StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
484             sb.append("[");
485             sb.append("qname=" + qname);
486             sb.append("]");
487             return sb.toString();
488         }
489     }
490
491 }