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 / UsesNodeBuilderImpl.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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.model.api.YangNode;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
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.RefineHolder;
35 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
36
37 public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNodeBuilder {
38     private boolean isBuilt;
39     private UsesNodeImpl instance;
40     private DataNodeContainerBuilder parentBuilder;
41     private final String groupingName;
42     private SchemaPath groupingPath;
43     private GroupingDefinition groupingDefinition;
44     private GroupingBuilder groupingBuilder;
45     private boolean addedByUses;
46     private boolean augmenting;
47     private AugmentationSchemaBuilder parentAugment;
48     private final Set<AugmentationSchema> augments = new HashSet<>();
49     private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<>();
50     private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<>();
51     private final List<RefineHolder> refines = new ArrayList<>();
52
53     /**
54      * Copies of target grouping child nodes.
55      */
56     private final Set<DataSchemaNodeBuilder> targetChildren = new HashSet<>();
57
58     /**
59      * Copies of target grouping groupings.
60      */
61     private final Set<GroupingBuilder> targetGroupings = new HashSet<>();
62
63     /**
64      * Copies of target grouping typedefs.
65      */
66     private final Set<TypeDefinitionBuilder> targetTypedefs = new HashSet<>();
67
68     /**
69      * Copies of target grouping unknown nodes.
70      */
71     private final List<UnknownSchemaNodeBuilder> targetUnknownNodes = new ArrayList<>();
72
73     private final boolean isCopy;
74     private boolean dataCollected;
75
76     @Override
77     public boolean isCopy() {
78         return isCopy;
79     }
80
81     @Override
82     public boolean isDataCollected() {
83         return dataCollected;
84     }
85
86     @Override
87     public void setDataCollected(boolean dataCollected) {
88         this.dataCollected = dataCollected;
89     }
90
91     public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) {
92         super(moduleName, line);
93         this.groupingName = groupingName;
94         isCopy = false;
95     }
96
97     public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName, final boolean isCopy) {
98         super(moduleName, line);
99         this.groupingName = groupingName;
100         this.isCopy = isCopy;
101     }
102
103     @Override
104     public UsesNode build(YangNode parent) {
105         if (!isBuilt) {
106             instance = new UsesNodeImpl(groupingPath);
107             instance.setAddedByUses(addedByUses);
108             instance.setParent(parent);
109
110             // AUGMENTATIONS
111             for (AugmentationSchemaBuilder builder : addedAugments) {
112                 augments.add(builder.build(instance));
113             }
114             instance.setAugmentations(augments);
115
116             // REFINES
117             final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<>();
118             for (SchemaNodeBuilder refineBuilder : refineBuilders) {
119                 SchemaNode refineNode = refineBuilder.build(instance);
120                 refineNodes.put(refineNode.getPath(), refineNode);
121             }
122             instance.setRefines(refineNodes);
123
124             // UNKNOWN NODES
125             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
126                 unknownNodes.add(b.build(instance));
127             }
128             instance.setUnknownSchemaNodes(unknownNodes);
129
130             isBuilt = true;
131         }
132
133         return instance;
134     }
135
136     @Override
137     public DataNodeContainerBuilder getParent() {
138         return parentBuilder;
139     }
140
141     @Override
142     public void setParent(Builder parent) {
143         if (!(parent instanceof DataNodeContainerBuilder)) {
144             throw new YangParseException(moduleName, line,
145                     "Parent of 'uses' has to be instance of DataNodeContainerBuilder, but was: '" + parent + "'.");
146         }
147         this.parentBuilder = (DataNodeContainerBuilder) parent;
148     }
149
150     @Override
151     public SchemaPath getGroupingPath() {
152         return groupingPath;
153     }
154
155     @Override
156     public GroupingDefinition getGroupingDefinition() {
157         return groupingDefinition;
158     }
159
160     @Override
161     public void setGroupingDefinition(GroupingDefinition groupingDefinition) {
162         this.groupingDefinition = groupingDefinition;
163         if (groupingDefinition != null) {
164             this.groupingPath = groupingDefinition.getPath();
165         }
166     }
167
168     @Override
169     public GroupingBuilder getGroupingBuilder() {
170         return groupingBuilder;
171     }
172
173     @Override
174     public void setGrouping(GroupingBuilder grouping) {
175         this.groupingBuilder = grouping;
176         if (groupingBuilder != null) {
177             this.groupingPath = groupingBuilder.getPath();
178         }
179     }
180
181     @Override
182     public String getGroupingPathAsString() {
183         return groupingName;
184     }
185
186     @Override
187     public Set<AugmentationSchemaBuilder> getAugmentations() {
188         return addedAugments;
189     }
190
191     @Override
192     public void addAugment(final AugmentationSchemaBuilder augmentBuilder) {
193         addedAugments.add(augmentBuilder);
194     }
195
196     @Override
197     public boolean isAddedByUses() {
198         return addedByUses;
199     }
200
201     @Override
202     public void setAddedByUses(final boolean addedByUses) {
203         this.addedByUses = addedByUses;
204     }
205
206     @Override
207     public boolean isAugmenting() {
208         return augmenting;
209     }
210
211     @Override
212     public void setAugmenting(boolean augmenting) {
213         this.augmenting = augmenting;
214     }
215
216     @Override
217     public AugmentationSchemaBuilder getParentAugment() {
218         return parentAugment;
219     }
220
221     @Override
222     public void setParentAugment(AugmentationSchemaBuilder augment) {
223         this.parentAugment = augment;
224     }
225
226     @Override
227     public List<SchemaNodeBuilder> getRefineNodes() {
228         return refineBuilders;
229     }
230
231     @Override
232     public void addRefineNode(DataSchemaNodeBuilder refineNode) {
233         refineBuilders.add(refineNode);
234     }
235
236     @Override
237     public List<RefineHolder> getRefines() {
238         return refines;
239     }
240
241     @Override
242     public void addRefine(RefineHolder refine) {
243         refines.add(refine);
244     }
245
246     @Override
247     public Set<DataSchemaNodeBuilder> getTargetChildren() {
248         return targetChildren;
249     }
250
251     @Override
252     public Set<GroupingBuilder> getTargetGroupings() {
253         return targetGroupings;
254     }
255
256     @Override
257     public Set<TypeDefinitionBuilder> getTargetTypedefs() {
258         return targetTypedefs;
259     }
260
261     @Override
262     public List<UnknownSchemaNodeBuilder> getTargetUnknownNodes() {
263         return targetUnknownNodes;
264     }
265
266     @Override
267     public int hashCode() {
268         final int prime = 31;
269         int result = 1;
270         result = prime * result + ((groupingName == null) ? 0 : groupingName.hashCode());
271         result = prime * result + ((parentBuilder == null) ? 0 : parentBuilder.hashCode());
272         return result;
273     }
274
275     @Override
276     public boolean equals(Object obj) {
277         if (this == obj) {
278             return true;
279         }
280         if (obj == null) {
281             return false;
282         }
283         if (getClass() != obj.getClass()) {
284             return false;
285         }
286         UsesNodeBuilderImpl other = (UsesNodeBuilderImpl) obj;
287         if (groupingName == null) {
288             if (other.groupingName != null) {
289                 return false;
290             }
291         } else if (!groupingName.equals(other.groupingName)) {
292             return false;
293         }
294         if (parentBuilder == null) {
295             if (other.parentBuilder != null) {
296                 return false;
297             }
298         } else if (!parentBuilder.equals(other.parentBuilder)) {
299             return false;
300         }
301         return true;
302     }
303
304     @Override
305     public String toString() {
306         return "uses '" + groupingName + "'";
307     }
308
309     public final class UsesNodeImpl implements UsesNode {
310         private YangNode parent;
311         private final SchemaPath groupingPath;
312         private Set<AugmentationSchema> augmentations = Collections.emptySet();
313         private boolean addedByUses;
314         private Map<SchemaPath, SchemaNode> refines = Collections.emptyMap();
315         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
316
317         @Override
318         public YangNode getParent() {
319             return parent;
320         }
321
322         private void setParent(YangNode parent) {
323             this.parent = parent;
324         }
325
326         private UsesNodeImpl(final SchemaPath groupingPath) {
327             this.groupingPath = groupingPath;
328         }
329
330         @Override
331         public SchemaPath getGroupingPath() {
332             return groupingPath;
333         }
334
335         @Override
336         public Set<AugmentationSchema> getAugmentations() {
337             return augmentations;
338         }
339
340         private void setAugmentations(final Set<AugmentationSchema> augmentations) {
341             if (augmentations != null) {
342                 this.augmentations = augmentations;
343             }
344         }
345
346         @Override
347         public boolean isAugmenting() {
348             return false;
349         }
350
351         @Override
352         public boolean isAddedByUses() {
353             return addedByUses;
354         }
355
356         private void setAddedByUses(final boolean addedByUses) {
357             this.addedByUses = addedByUses;
358         }
359
360         @Override
361         public Map<SchemaPath, SchemaNode> getRefines() {
362             return refines;
363         }
364
365         private void setRefines(Map<SchemaPath, SchemaNode> refines) {
366             if (refines != null) {
367                 this.refines = refines;
368             }
369         }
370
371         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
372             return unknownNodes;
373         }
374
375         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
376             if (unknownSchemaNodes != null) {
377                 this.unknownNodes = unknownSchemaNodes;
378             }
379         }
380
381         public UsesNodeBuilder toBuilder() {
382             return UsesNodeBuilderImpl.this;
383         }
384
385         @Override
386         public int hashCode() {
387             final int prime = 31;
388             int result = 1;
389             result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
390             result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
391             return result;
392         }
393
394         @Override
395         public boolean equals(Object obj) {
396             if (this == obj) {
397                 return true;
398             }
399             if (obj == null) {
400                 return false;
401             }
402             if (getClass() != obj.getClass()) {
403                 return false;
404             }
405             final UsesNodeImpl other = (UsesNodeImpl) obj;
406             if (groupingPath == null) {
407                 if (other.groupingPath != null) {
408                     return false;
409                 }
410             } else if (!groupingPath.equals(other.groupingPath)) {
411                 return false;
412             }
413             if (augmentations == null) {
414                 if (other.augmentations != null) {
415                     return false;
416                 }
417             } else if (!augmentations.equals(other.augmentations)) {
418                 return false;
419             }
420             return true;
421         }
422
423         @Override
424         public String toString() {
425             StringBuilder sb = new StringBuilder(UsesNodeImpl.class.getSimpleName());
426             sb.append("[groupingPath=");
427             sb.append(groupingPath);
428             sb.append("]");
429             return sb.toString();
430         }
431     }
432
433 }