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