Fixed bug in QName of augmented nodes added by uses statement.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AnyXmlBuilder.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.Collections;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.YangNode;
20 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.util.Comparators;
23
24 public final class AnyXmlBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder {
25     private boolean built;
26     private final AnyXmlSchemaNodeImpl instance;
27     private final ConstraintsBuilder constraints;
28
29     private Boolean configuration;
30     private boolean augmenting;
31     private boolean addedByUses;
32
33     public AnyXmlBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
34         super(moduleName, line, qname);
35         this.schemaPath = schemaPath;
36         instance = new AnyXmlSchemaNodeImpl(qname);
37         constraints = new ConstraintsBuilder(moduleName, line);
38     }
39
40     @Override
41     public AnyXmlSchemaNode build(YangNode parent) {
42         if (!built) {
43             instance.setParent(parent);
44             instance.setPath(schemaPath);
45             instance.setConstraints(constraints.build());
46             instance.setDescription(description);
47             instance.setReference(reference);
48             instance.setStatus(status);
49             instance.setConfiguration(configuration);
50             instance.setAugmenting(augmenting);
51             instance.setAddedByUses(addedByUses);
52
53             // UNKNOWN NODES
54             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
55                 unknownNodes.add(b.build(instance));
56             }
57             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
58             instance.setUnknownSchemaNodes(unknownNodes);
59
60             built = true;
61         }
62         return instance;
63     }
64
65     @Override
66     public void setQName(QName qname) {
67         this.qname = qname;
68         instance.setQName(qname);
69     }
70
71     @Override
72     public ConstraintsBuilder getConstraints() {
73         return constraints;
74     }
75
76     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
77         return addedUnknownNodes;
78     }
79
80     @Override
81     public boolean isAugmenting() {
82         return augmenting;
83     }
84
85     @Override
86     public void setAugmenting(final boolean augmenting) {
87         this.augmenting = augmenting;
88     }
89
90     @Override
91     public boolean isAddedByUses() {
92         return addedByUses;
93     }
94
95     @Override
96     public void setAddedByUses(final boolean addedByUses) {
97         this.addedByUses = addedByUses;
98     }
99
100     @Override
101     public Boolean isConfiguration() {
102         return configuration;
103     }
104
105     @Override
106     public void setConfiguration(final Boolean configuration) {
107         this.configuration = configuration;
108     }
109
110     @Override
111     public int hashCode() {
112         final int prime = 31;
113         int result = 1;
114         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
115         return result;
116     }
117
118     @Override
119     public boolean equals(Object obj) {
120         if (this == obj) {
121             return true;
122         }
123         if (obj == null) {
124             return false;
125         }
126         if (getClass() != obj.getClass()) {
127             return false;
128         }
129         AnyXmlBuilder other = (AnyXmlBuilder) obj;
130         if (schemaPath == null) {
131             if (other.schemaPath != null) {
132                 return false;
133             }
134         } else if (!schemaPath.equals(other.schemaPath)) {
135             return false;
136         }
137         if (parentBuilder == null) {
138             if (other.parentBuilder != null) {
139                 return false;
140             }
141         } else if (!parentBuilder.equals(other.parentBuilder)) {
142             return false;
143         }
144         return true;
145     }
146
147     @Override
148     public String toString() {
149         return "anyxml " + qname.getLocalName();
150     }
151
152     private final class AnyXmlSchemaNodeImpl implements AnyXmlSchemaNode {
153         private QName qname;
154         private SchemaPath path;
155         private YangNode parent;
156         private String description;
157         private String reference;
158         private Status status = Status.CURRENT;
159         private boolean configuration;
160         private ConstraintDefinition constraintsDef;
161         private boolean augmenting;
162         private boolean addedByUses;
163         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
164
165         private AnyXmlSchemaNodeImpl(final QName qname) {
166             this.qname = qname;
167         }
168
169         @Override
170         public QName getQName() {
171             return qname;
172         }
173
174         private void setQName(QName qname) {
175             this.qname = qname;
176         }
177
178         @Override
179         public SchemaPath getPath() {
180             return path;
181         }
182
183         private void setPath(final SchemaPath path) {
184             this.path = path;
185         }
186
187         @Override
188         public YangNode getParent() {
189             return parent;
190         }
191
192         private void setParent(YangNode parent) {
193             this.parent = parent;
194         }
195
196         @Override
197         public String getDescription() {
198             return description;
199         }
200
201         private void setDescription(String description) {
202             this.description = description;
203         }
204
205         @Override
206         public String getReference() {
207             return reference;
208         }
209
210         private void setReference(String reference) {
211             this.reference = reference;
212         }
213
214         @Override
215         public Status getStatus() {
216             return status;
217         }
218
219         private void setStatus(Status status) {
220             if (status != null) {
221                 this.status = status;
222             }
223         }
224
225         @Override
226         public boolean isAugmenting() {
227             return augmenting;
228         }
229
230         private void setAugmenting(boolean augmenting) {
231             this.augmenting = augmenting;
232         }
233
234         @Override
235         public boolean isAddedByUses() {
236             return addedByUses;
237         }
238
239         private void setAddedByUses(boolean addedByUses) {
240             this.addedByUses = addedByUses;
241         }
242
243         @Override
244         public boolean isConfiguration() {
245             return configuration;
246         }
247
248         private void setConfiguration(boolean configuration) {
249             this.configuration = configuration;
250         }
251
252         @Override
253         public ConstraintDefinition getConstraints() {
254             return constraintsDef;
255         }
256
257         private void setConstraints(ConstraintDefinition constraintsDef) {
258             this.constraintsDef = constraintsDef;
259         }
260
261         @Override
262         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
263             return unknownNodes;
264         }
265
266         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
267             if (unknownNodes != null) {
268                 this.unknownNodes = unknownNodes;
269             }
270         }
271
272         @Override
273         public int hashCode() {
274             final int prime = 31;
275             int result = 1;
276             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
277             result = prime * result + ((path == null) ? 0 : path.hashCode());
278             return result;
279         }
280
281         @Override
282         public boolean equals(Object obj) {
283             if (this == obj) {
284                 return true;
285             }
286             if (obj == null) {
287                 return false;
288             }
289             if (getClass() != obj.getClass()) {
290                 return false;
291             }
292             AnyXmlSchemaNodeImpl other = (AnyXmlSchemaNodeImpl) obj;
293             if (qname == null) {
294                 if (other.qname != null) {
295                     return false;
296                 }
297             } else if (!qname.equals(other.qname)) {
298                 return false;
299             }
300             if (path == null) {
301                 if (other.path != null) {
302                     return false;
303                 }
304             } else if (!path.equals(other.path)) {
305                 return false;
306             }
307             return true;
308         }
309
310         @Override
311         public String toString() {
312             StringBuilder sb = new StringBuilder(AnyXmlSchemaNodeImpl.class.getSimpleName());
313             sb.append("[");
314             sb.append("qname=" + qname);
315             sb.append(", path=" + path);
316             sb.append("]");
317             return sb.toString();
318         }
319     }
320
321 }