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