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