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 / AugmentationSchemaBuilderImpl.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 import java.util.Map;
13 import java.util.Set;
14 import java.util.TreeSet;
15
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.Status;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.UsesNode;
26 import org.opendaylight.yangtools.yang.model.api.YangNode;
27 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
28 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.util.Comparators;
35 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
36 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
37
38 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
39         AugmentationSchemaBuilder {
40     private boolean built;
41     private final AugmentationSchemaImpl instance;
42
43     private String whenCondition;
44     private String description;
45     private String reference;
46     private Status status = Status.CURRENT;
47
48     private final String augmentTargetStr;
49     private SchemaPath targetPath;
50     private SchemaPath targetNodeSchemaPath;
51
52     private boolean resolved;
53
54     public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
55         super(moduleName, line, null);
56         this.augmentTargetStr = augmentTargetStr;
57         targetPath = ParserUtils.parseXPathString(augmentTargetStr);
58         instance = new AugmentationSchemaImpl(targetPath);
59     }
60
61     @Override
62     public Set<GroupingDefinition> getGroupings() {
63         return Collections.emptySet();
64     }
65
66     @Override
67     public Set<GroupingBuilder> getGroupingBuilders() {
68         return Collections.emptySet();
69     }
70
71     @Override
72     public void addGrouping(GroupingBuilder grouping) {
73         throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
74     }
75
76     @Override
77     public SchemaPath getPath() {
78         return targetNodeSchemaPath;
79     }
80
81     @Override
82     public AugmentationSchema build(YangNode parent) {
83         if (!built) {
84             instance.setParent(parent);
85             instance.setDescription(description);
86             instance.setReference(reference);
87             instance.setStatus(status);
88             instance.setTargetPath(targetNodeSchemaPath);
89
90             RevisionAwareXPath whenStmt;
91             if (whenCondition == null) {
92                 whenStmt = null;
93             } else {
94                 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
95             }
96             instance.setWhenCondition(whenStmt);
97
98             // CHILD NODES
99             for (DataSchemaNodeBuilder node : addedChildNodes) {
100                 DataSchemaNode child = node.build(instance);
101                 childNodes.put(child.getQName(), child);
102             }
103             instance.setChildNodes(childNodes);
104
105             // USES
106             for (UsesNodeBuilder builder : addedUsesNodes) {
107                 usesNodes.add(builder.build(instance));
108             }
109             instance.setUses(usesNodes);
110
111             // UNKNOWN NODES
112             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
113                 unknownNodes.add(b.build(instance));
114             }
115             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
116             instance.setUnknownSchemaNodes(unknownNodes);
117
118             built = true;
119         }
120         return instance;
121     }
122
123     @Override
124     public boolean isResolved() {
125         return resolved;
126     }
127
128     @Override
129     public void setResolved(boolean resolved) {
130         this.resolved = resolved;
131     }
132
133     public String getWhenCondition() {
134         return whenCondition;
135     }
136
137     public void addWhenCondition(String whenCondition) {
138         this.whenCondition = whenCondition;
139     }
140
141     @Override
142     public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
143         return Collections.emptySet();
144     }
145
146     @Override
147     public void addTypedef(TypeDefinitionBuilder type) {
148         throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
149     }
150
151     @Override
152     public String getDescription() {
153         return description;
154     }
155
156     @Override
157     public void setDescription(String description) {
158         this.description = description;
159     }
160
161     @Override
162     public String getReference() {
163         return reference;
164     }
165
166     @Override
167     public void setReference(String reference) {
168         this.reference = reference;
169     }
170
171     @Override
172     public Status getStatus() {
173         return status;
174     }
175
176     @Override
177     public void setStatus(Status status) {
178         if (status != null) {
179             this.status = status;
180         }
181     }
182
183     @Override
184     public String getTargetPathAsString() {
185         return augmentTargetStr;
186     }
187
188     @Override
189     public SchemaPath getTargetPath() {
190         return targetPath;
191     }
192
193     @Override
194     public SchemaPath getTargetNodeSchemaPath() {
195         return targetNodeSchemaPath;
196     }
197
198     @Override
199     public void setTargetNodeSchemaPath(SchemaPath path) {
200         this.targetNodeSchemaPath = path;
201     }
202
203     @Override
204     public int hashCode() {
205         final int prime = 17;
206         int result = 1;
207         result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
208         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
209         result = prime * result + ((addedChildNodes == null) ? 0 : addedChildNodes.hashCode());
210         return result;
211     }
212
213     @Override
214     public boolean equals(Object obj) {
215         if (this == obj) {
216             return true;
217         }
218         if (obj == null) {
219             return false;
220         }
221         if (getClass() != obj.getClass()) {
222             return false;
223         }
224         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
225         if (augmentTargetStr == null) {
226             if (other.augmentTargetStr != null) {
227                 return false;
228             }
229         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
230             return false;
231         }
232         if (whenCondition == null) {
233             if (other.whenCondition != null) {
234                 return false;
235             }
236         } else if (!whenCondition.equals(other.whenCondition)) {
237             return false;
238         }
239         if (addedChildNodes == null) {
240             if (other.addedChildNodes != null) {
241                 return false;
242             }
243         } else if (!addedChildNodes.equals(other.addedChildNodes)) {
244             return false;
245         }
246         return true;
247     }
248
249     public String toString() {
250         return "augment " + augmentTargetStr;
251     }
252
253     private final class AugmentationSchemaImpl implements AugmentationSchema {
254         private YangNode parent;
255         private SchemaPath targetPath;
256         private RevisionAwareXPath whenCondition;
257         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
258         private Set<UsesNode> uses = Collections.emptySet();
259         private String description;
260         private String reference;
261         private Status status;
262         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
263
264         private AugmentationSchemaImpl(SchemaPath targetPath) {
265             this.targetPath = targetPath;
266         }
267
268         @Override
269         public YangNode getParent() {
270             return parent;
271         }
272
273         private void setParent(YangNode parent) {
274             this.parent = parent;
275         }
276
277         @Override
278         public SchemaPath getTargetPath() {
279             return targetPath;
280         }
281
282         private void setTargetPath(SchemaPath path) {
283             this.targetPath = path;
284         }
285
286         @Override
287         public RevisionAwareXPath getWhenCondition() {
288             return whenCondition;
289         }
290
291         private void setWhenCondition(RevisionAwareXPath whenCondition) {
292             this.whenCondition = whenCondition;
293         }
294
295         @Override
296         public Set<DataSchemaNode> getChildNodes() {
297             final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
298             result.addAll(childNodes.values());
299             return result;
300         }
301
302         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
303             if (childNodes != null) {
304                 this.childNodes = childNodes;
305             }
306         }
307
308         /**
309          * Always returns an empty set, because augment can not contains
310          * grouping statement.
311          */
312         @Override
313         public Set<GroupingDefinition> getGroupings() {
314             return Collections.emptySet();
315         }
316
317         @Override
318         public Set<UsesNode> getUses() {
319             return uses;
320         }
321
322         private void setUses(Set<UsesNode> uses) {
323             if (uses != null) {
324                 this.uses = uses;
325             }
326         }
327
328         /**
329          * Always returns an empty set, because augment can not contains type
330          * definitions.
331          */
332         @Override
333         public Set<TypeDefinition<?>> getTypeDefinitions() {
334             return Collections.emptySet();
335         }
336
337         @Override
338         public String getDescription() {
339             return description;
340         }
341
342         private void setDescription(String description) {
343             this.description = description;
344         }
345
346         @Override
347         public String getReference() {
348             return reference;
349         }
350
351         private void setReference(String reference) {
352             this.reference = reference;
353         }
354
355         @Override
356         public Status getStatus() {
357             return status;
358         }
359
360         private void setStatus(Status status) {
361             this.status = status;
362         }
363
364         @Override
365         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
366             return unknownNodes;
367         }
368
369         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
370             if (unknownSchemaNodes != null) {
371                 this.unknownNodes = unknownSchemaNodes;
372             }
373         }
374
375         @Override
376         public DataSchemaNode getDataChildByName(QName name) {
377             return childNodes.get(name);
378         }
379
380         @Override
381         public DataSchemaNode getDataChildByName(String name) {
382             DataSchemaNode result = null;
383             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
384                 if (entry.getKey().getLocalName().equals(name)) {
385                     result = entry.getValue();
386                     break;
387                 }
388             }
389             return result;
390         }
391
392         @Override
393         public int hashCode() {
394             final int prime = 17;
395             int result = 1;
396             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
397             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
398             result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
399             return result;
400         }
401
402         @Override
403         public boolean equals(Object obj) {
404             if (this == obj) {
405                 return true;
406             }
407             if (obj == null) {
408                 return false;
409             }
410             if (getClass() != obj.getClass()) {
411                 return false;
412             }
413             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
414             if (targetPath == null) {
415                 if (other.targetPath != null) {
416                     return false;
417                 }
418             } else if (!targetPath.equals(other.targetPath)) {
419                 return false;
420             }
421             if (whenCondition == null) {
422                 if (other.whenCondition != null) {
423                     return false;
424                 }
425             } else if (!whenCondition.equals(other.whenCondition)) {
426                 return false;
427             }
428             if (childNodes == null) {
429                 if (other.childNodes != null) {
430                     return false;
431                 }
432             } else if (!childNodes.equals(other.childNodes)) {
433                 return false;
434             }
435             return true;
436         }
437
438         @Override
439         public String toString() {
440             StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
441             sb.append("[");
442             sb.append("targetPath=" + targetPath);
443             sb.append(", when=" + whenCondition);
444             sb.append("]");
445             return sb.toString();
446         }
447     }
448
449 }