Fixed path correction process of nodes defined by uses statement.
[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     public SchemaPath getPath() {
93         return schemaPath;
94     }
95
96     @Override
97     public void setPath(final SchemaPath path) {
98         this.schemaPath = path;
99     }
100
101     @Override
102     public ConstraintsBuilder getConstraints() {
103         return constraints;
104     }
105
106     public String getDescription() {
107         return description;
108     }
109
110     @Override
111     public void setDescription(final String description) {
112         this.description = description;
113     }
114
115     public String getReference() {
116         return reference;
117     }
118
119     @Override
120     public void setReference(final String reference) {
121         this.reference = reference;
122     }
123
124     public Status getStatus() {
125         return status;
126     }
127
128     @Override
129     public void setStatus(final Status status) {
130         if (status != null) {
131             this.status = status;
132         }
133     }
134
135     public boolean isAugmenting() {
136         return augmenting;
137     }
138
139     @Override
140     public void setAugmenting(final boolean augmenting) {
141         this.augmenting = augmenting;
142     }
143
144     @Override
145     public boolean isAddedByUses() {
146         return addedByUses;
147     }
148
149     @Override
150     public void setAddedByUses(final boolean addedByUses) {
151         this.addedByUses = addedByUses;
152     }
153
154     public Boolean isConfiguration() {
155         return configuration;
156     }
157
158     @Override
159     public void setConfiguration(final Boolean configuration) {
160         this.configuration = configuration;
161     }
162
163     public String getDefaultStr() {
164         return defaultStr;
165     }
166
167     public void setDefaultStr(String defaultStr) {
168         this.defaultStr = defaultStr;
169     }
170
171     public String getUnits() {
172         return unitsStr;
173     }
174
175     public void setUnits(String unitsStr) {
176         this.unitsStr = unitsStr;
177     }
178
179     @Override
180     public int hashCode() {
181         final int prime = 31;
182         int result = 1;
183         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
184         return result;
185     }
186
187     @Override
188     public boolean equals(Object obj) {
189         if (this == obj) {
190             return true;
191         }
192         if (obj == null) {
193             return false;
194         }
195         if (getClass() != obj.getClass()) {
196             return false;
197         }
198         LeafSchemaNodeBuilder other = (LeafSchemaNodeBuilder) obj;
199         if (schemaPath == null) {
200             if (other.schemaPath != null) {
201                 return false;
202             }
203         } else if (!schemaPath.equals(other.schemaPath)) {
204             return false;
205         }
206         if (parent == null) {
207             if (other.parent != null) {
208                 return false;
209             }
210         } else if (!parent.equals(other.parent)) {
211             return false;
212         }
213         return true;
214     }
215
216     @Override
217     public String toString() {
218         return "leaf " + qname.getLocalName();
219     }
220
221     private final class LeafSchemaNodeImpl implements LeafSchemaNode {
222         private final QName qname;
223         private SchemaPath path;
224         private String description;
225         private String reference;
226         private Status status = Status.CURRENT;
227         private boolean augmenting;
228         private boolean addedByUses;
229         private boolean configuration;
230         private ConstraintDefinition constraintsDef;
231         private TypeDefinition<?> type;
232         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
233         private String defaultStr;
234         private String unitsStr;
235
236         private LeafSchemaNodeImpl(final QName qname) {
237             this.qname = qname;
238         }
239
240         @Override
241         public QName getQName() {
242             return qname;
243         }
244
245         @Override
246         public SchemaPath getPath() {
247             return path;
248         }
249
250         private void setPath(final SchemaPath path) {
251             this.path = path;
252         }
253
254         @Override
255         public String getDescription() {
256             return description;
257         }
258
259         private void setDescription(String description) {
260             this.description = description;
261         }
262
263         @Override
264         public String getReference() {
265             return reference;
266         }
267
268         private void setReference(String reference) {
269             this.reference = reference;
270         }
271
272         @Override
273         public Status getStatus() {
274             return status;
275         }
276
277         private void setStatus(Status status) {
278             if (status != null) {
279                 this.status = status;
280             }
281         }
282
283         @Override
284         public boolean isAugmenting() {
285             return augmenting;
286         }
287
288         private void setAugmenting(boolean augmenting) {
289             this.augmenting = augmenting;
290         }
291
292         @Override
293         public boolean isAddedByUses() {
294             return addedByUses;
295         }
296
297         private void setAddedByUses(final boolean addedByUses) {
298             this.addedByUses = addedByUses;
299         }
300
301         @Override
302         public boolean isConfiguration() {
303             return configuration;
304         }
305
306         private void setConfiguration(boolean configuration) {
307             this.configuration = configuration;
308         }
309
310         @Override
311         public ConstraintDefinition getConstraints() {
312             return constraintsDef;
313         }
314
315         private void setConstraints(ConstraintDefinition constraintsDef) {
316             this.constraintsDef = constraintsDef;
317         }
318
319         @Override
320         public TypeDefinition<?> getType() {
321             return type;
322         }
323
324         private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
325             this.type = type;
326         }
327
328         @Override
329         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
330             return unknownNodes;
331         }
332
333         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
334             if (unknownNodes != null) {
335                 this.unknownNodes = unknownNodes;
336             }
337         }
338
339         @Override
340         public String getDefault() {
341             return defaultStr;
342         }
343
344         private void setDefault(String defaultStr) {
345             this.defaultStr = defaultStr;
346         }
347
348         @Override
349         public String getUnits() {
350             return unitsStr;
351         }
352
353         public void setUnits(String unitsStr) {
354             this.unitsStr = unitsStr;
355         }
356
357         @Override
358         public int hashCode() {
359             final int prime = 31;
360             int result = 1;
361             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
362             result = prime * result + ((path == null) ? 0 : path.hashCode());
363             return result;
364         }
365
366         @Override
367         public boolean equals(Object obj) {
368             if (this == obj) {
369                 return true;
370             }
371             if (obj == null) {
372                 return false;
373             }
374             if (getClass() != obj.getClass()) {
375                 return false;
376             }
377             LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
378             if (qname == null) {
379                 if (other.qname != null) {
380                     return false;
381                 }
382             } else if (!qname.equals(other.qname)) {
383                 return false;
384             }
385             if (path == null) {
386                 if (other.path != null) {
387                     return false;
388                 }
389             } else if (!path.equals(other.path)) {
390                 return false;
391             }
392             return true;
393         }
394
395         @Override
396         public String toString() {
397             StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
398             sb.append("[");
399             sb.append("qname=" + qname);
400             sb.append(", path=" + path);
401             sb.append("]");
402             return sb.toString();
403         }
404     }
405
406 }