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