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