Implemented ordering of yang module data nodes. Added Comparators utility class.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / LeafSchemaNodeBuilder.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.List;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
16 import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
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.parser.builder.api.AbstractTypeAwareBuilder;
22 import org.opendaylight.controller.yang.parser.builder.api.ConfigNode;
23 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
24 import org.opendaylight.controller.yang.parser.builder.api.GroupingMember;
25 import org.opendaylight.controller.yang.parser.util.Comparators;
26
27 public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder,
28         GroupingMember, ConfigNode {
29     private boolean isBuilt;
30     private final LeafSchemaNodeImpl instance;
31     // SchemaNode args
32     private SchemaPath schemaPath;
33     private String description;
34     private String reference;
35     private Status status = Status.CURRENT;
36     private List<UnknownSchemaNode> unknownNodes;
37     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
38     // DataSchemaNode args
39     private boolean augmenting;
40     private boolean addedByUses;
41     private Boolean configuration;
42     private final ConstraintsBuilder constraints;
43     // leaf args
44     private String defaultStr;
45     private String unitsStr;
46
47     public LeafSchemaNodeBuilder(final QName qname, final SchemaPath schemaPath, final int line) {
48         super(line, qname);
49         this.schemaPath = schemaPath;
50         instance = new LeafSchemaNodeImpl(qname);
51         constraints = new ConstraintsBuilder(line);
52     }
53
54     public LeafSchemaNodeBuilder(final LeafSchemaNodeBuilder b) {
55         super(b.getLine(), b.getQName());
56         instance = new LeafSchemaNodeImpl(qname);
57         constraints = b.getConstraints();
58         schemaPath = b.getPath();
59
60         type = b.getType();
61         typedef = b.getTypedef();
62
63         description = b.getDescription();
64         reference = b.getReference();
65         status = b.getStatus();
66         augmenting = b.isAugmenting();
67         addedByUses = b.isAddedByUses();
68         configuration = b.isConfiguration();
69         unknownNodes = b.unknownNodes;
70         addedUnknownNodes.addAll(b.getUnknownNodes());
71
72         defaultStr = b.getDefaultStr();
73         unitsStr = b.getUnits();
74     }
75
76     @Override
77     public LeafSchemaNode build() {
78         if (!isBuilt) {
79             instance.setPath(schemaPath);
80             instance.setConstraints(constraints.build());
81             instance.setDescription(description);
82             instance.setReference(reference);
83             instance.setStatus(status);
84             instance.setAugmenting(augmenting);
85             instance.setAddedByUses(addedByUses);
86             instance.setConfiguration(configuration);
87             instance.setDefault(defaultStr);
88             instance.setUnits(unitsStr);
89
90             // TYPE
91             if (type == null) {
92                 instance.setType(typedef.build());
93             } else {
94                 instance.setType(type);
95             }
96
97             // UNKNOWN NODES
98             if (unknownNodes == null) {
99                 unknownNodes = new ArrayList<UnknownSchemaNode>();
100                 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
101                     unknownNodes.add(b.build());
102                 }
103                 Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
104             }
105             instance.setUnknownSchemaNodes(unknownNodes);
106
107             isBuilt = true;
108         }
109         return instance;
110     }
111
112     public SchemaPath getPath() {
113         return schemaPath;
114     }
115
116     @Override
117     public void setPath(final SchemaPath path) {
118         this.schemaPath = path;
119     }
120
121     @Override
122     public ConstraintsBuilder getConstraints() {
123         return constraints;
124     }
125
126     @Override
127     public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
128         addedUnknownNodes.add(unknownNode);
129     }
130
131     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
132         return addedUnknownNodes;
133     }
134
135     public void setUnknownNodes(List<UnknownSchemaNode> unknownNodes) {
136         this.unknownNodes = unknownNodes;
137     }
138
139     public String getDescription() {
140         return description;
141     }
142
143     @Override
144     public void setDescription(final String description) {
145         this.description = description;
146     }
147
148     public String getReference() {
149         return reference;
150     }
151
152     @Override
153     public void setReference(final String reference) {
154         this.reference = reference;
155     }
156
157     public Status getStatus() {
158         return status;
159     }
160
161     @Override
162     public void setStatus(final Status status) {
163         if (status != null) {
164             this.status = status;
165         }
166     }
167
168     public boolean isAugmenting() {
169         return augmenting;
170     }
171
172     @Override
173     public void setAugmenting(final boolean augmenting) {
174         this.augmenting = augmenting;
175     }
176
177     @Override
178     public boolean isAddedByUses() {
179         return addedByUses;
180     }
181
182     @Override
183     public void setAddedByUses(final boolean addedByUses) {
184         this.addedByUses = addedByUses;
185     }
186
187     public Boolean isConfiguration() {
188         return configuration;
189     }
190
191     @Override
192     public void setConfiguration(final Boolean configuration) {
193         this.configuration = configuration;
194     }
195
196     public String getDefaultStr() {
197         return defaultStr;
198     }
199
200     public void setDefaultStr(String defaultStr) {
201         this.defaultStr = defaultStr;
202     }
203
204     public String getUnits() {
205         return unitsStr;
206     }
207
208     public void setUnits(String unitsStr) {
209         this.unitsStr = unitsStr;
210     }
211
212     @Override
213     public int hashCode() {
214         final int prime = 31;
215         int result = 1;
216         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
217         return result;
218     }
219
220     @Override
221     public boolean equals(Object obj) {
222         if (this == obj) {
223             return true;
224         }
225         if (obj == null) {
226             return false;
227         }
228         if (getClass() != obj.getClass()) {
229             return false;
230         }
231         LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
232         if (schemaPath == null) {
233             if (other.schemaPath != null) {
234                 return false;
235             }
236         } else if (!schemaPath.equals(other.schemaPath)) {
237             return false;
238         }
239         if (parent == null) {
240             if (other.parent != null) {
241                 return false;
242             }
243         } else if (!parent.equals(other.parent)) {
244             return false;
245         }
246         return true;
247     }
248
249     @Override
250     public String toString() {
251         return "leaf " + qname.getLocalName();
252     }
253
254     private final class LeafSchemaNodeImpl implements LeafSchemaNode {
255         private final QName qname;
256         private SchemaPath path;
257         private String description;
258         private String reference;
259         private Status status = Status.CURRENT;
260         private boolean augmenting;
261         private boolean addedByUses;
262         private boolean configuration;
263         private ConstraintDefinition constraintsDef;
264         private TypeDefinition<?> type;
265         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
266         private String defaultStr;
267         private String unitsStr;
268
269         private LeafSchemaNodeImpl(final QName qname) {
270             this.qname = qname;
271         }
272
273         @Override
274         public QName getQName() {
275             return qname;
276         }
277
278         @Override
279         public SchemaPath getPath() {
280             return path;
281         }
282
283         private void setPath(final SchemaPath path) {
284             this.path = path;
285         }
286
287         @Override
288         public String getDescription() {
289             return description;
290         }
291
292         private void setDescription(String description) {
293             this.description = description;
294         }
295
296         @Override
297         public String getReference() {
298             return reference;
299         }
300
301         private void setReference(String reference) {
302             this.reference = reference;
303         }
304
305         @Override
306         public Status getStatus() {
307             return status;
308         }
309
310         private void setStatus(Status status) {
311             if (status != null) {
312                 this.status = status;
313             }
314         }
315
316         @Override
317         public boolean isAugmenting() {
318             return augmenting;
319         }
320
321         private void setAugmenting(boolean augmenting) {
322             this.augmenting = augmenting;
323         }
324
325         @Override
326         public boolean isAddedByUses() {
327             return addedByUses;
328         }
329
330         private void setAddedByUses(final boolean addedByUses) {
331             this.addedByUses = addedByUses;
332         }
333
334         @Override
335         public boolean isConfiguration() {
336             return configuration;
337         }
338
339         private void setConfiguration(boolean configuration) {
340             this.configuration = configuration;
341         }
342
343         @Override
344         public ConstraintDefinition getConstraints() {
345             return constraintsDef;
346         }
347
348         private void setConstraints(ConstraintDefinition constraintsDef) {
349             this.constraintsDef = constraintsDef;
350         }
351
352         @Override
353         public TypeDefinition<?> getType() {
354             return type;
355         }
356
357         private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
358             this.type = type;
359         }
360
361         @Override
362         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
363             return unknownNodes;
364         }
365
366         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
367             if (unknownNodes != null) {
368                 this.unknownNodes = unknownNodes;
369             }
370         }
371
372         @Override
373         public String getDefault() {
374             return defaultStr;
375         }
376
377         private void setDefault(String defaultStr) {
378             this.defaultStr = defaultStr;
379         }
380
381         @Override
382         public String getUnits() {
383             return unitsStr;
384         }
385
386         public void setUnits(String unitsStr) {
387             this.unitsStr = unitsStr;
388         }
389
390         @Override
391         public int hashCode() {
392             final int prime = 31;
393             int result = 1;
394             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
395             result = prime * result + ((path == null) ? 0 : path.hashCode());
396             return result;
397         }
398
399         @Override
400         public boolean equals(Object obj) {
401             if (this == obj) {
402                 return true;
403             }
404             if (obj == null) {
405                 return false;
406             }
407             if (getClass() != obj.getClass()) {
408                 return false;
409             }
410             LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
411             if (qname == null) {
412                 if (other.qname != null) {
413                     return false;
414                 }
415             } else if (!qname.equals(other.qname)) {
416                 return false;
417             }
418             if (path == null) {
419                 if (other.path != null) {
420                     return false;
421                 }
422             } else if (!path.equals(other.path)) {
423                 return false;
424             }
425             return true;
426         }
427
428         @Override
429         public String toString() {
430             StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
431             sb.append("[");
432             sb.append("qname=" + qname);
433             sb.append(", path=" + path);
434             sb.append("]");
435             return sb.toString();
436         }
437     }
438
439 }