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