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