Fix for bug 24.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / 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.controller.yang.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.util.RevisionAwareXPathImpl;
26 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil;
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     /**
88      * Always returns null.
89      */
90     @Override
91     public SchemaPath getPath() {
92         return null;
93     }
94
95     @Override
96     public AugmentationSchema build() {
97         instance.setTargetPath(finalAugmentTarget);
98
99         RevisionAwareXPath whenStmt;
100         if (whenCondition == null) {
101             whenStmt = null;
102         } else {
103             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
104         }
105         instance.setWhenCondition(whenStmt);
106
107         // CHILD NODES
108         final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
109         for (DataSchemaNodeBuilder node : childNodes) {
110             childs.put(node.getQName(), node.build());
111         }
112         instance.setChildNodes(childs);
113
114         // GROUPINGS
115         final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
116         for (GroupingBuilder builder : groupings) {
117             groupingDefinitions.add(builder.build());
118         }
119         instance.setGroupings(groupingDefinitions);
120
121         // USES
122         final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
123         for (UsesNodeBuilder builder : usesNodes) {
124             usesNodeDefinitions.add(builder.build());
125         }
126         instance.setUses(usesNodeDefinitions);
127
128         return instance;
129     }
130
131     @Override
132     public boolean isResolved() {
133         return resolved;
134     }
135
136     @Override
137     public void setResolved(boolean resolved) {
138         this.resolved = resolved;
139     }
140
141     public String getWhenCondition() {
142         return whenCondition;
143     }
144
145     public void addWhenCondition(String whenCondition) {
146         this.whenCondition = whenCondition;
147     }
148
149     @Override
150     public void addTypedef(TypeDefinitionBuilder type) {
151         throw new UnsupportedOperationException(
152                 "Augmentation can not contains type definitions");
153     }
154
155     @Override
156     public void setDescription(String description) {
157         instance.setDescription(description);
158     }
159
160     @Override
161     public void setReference(String reference) {
162         instance.setReference(reference);
163     }
164
165     @Override
166     public void setStatus(Status status) {
167         instance.setStatus(status);
168     }
169
170     @Override
171     public SchemaPath getTargetPath() {
172         return augmentTarget;
173     }
174
175     @Override
176     public void setTargetPath(SchemaPath path) {
177         this.finalAugmentTarget = path;
178     }
179
180     @Override
181     public String getTargetPathAsString() {
182         return augmentTargetStr;
183     }
184
185     @Override
186     public int hashCode() {
187         final int prime = 17;
188         int result = 1;
189         result = prime * result
190                 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
191         result = prime * result
192                 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
193         result = prime * result
194                 + ((childNodes == null) ? 0 : childNodes.hashCode());
195         return result;
196     }
197
198     @Override
199     public boolean equals(Object obj) {
200         if (this == obj) {
201             return true;
202         }
203         if (obj == null) {
204             return false;
205         }
206         if (getClass() != obj.getClass()) {
207             return false;
208         }
209         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
210         if (augmentTargetStr == null) {
211             if (other.augmentTargetStr != null) {
212                 return false;
213             }
214         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
215             return false;
216         }
217         if (whenCondition == null) {
218             if (other.whenCondition != null) {
219                 return false;
220             }
221         } else if (!whenCondition.equals(other.whenCondition)) {
222             return false;
223         }
224         if (childNodes == null) {
225             if (other.childNodes != null) {
226                 return false;
227             }
228         } else if (!childNodes.equals(other.childNodes)) {
229             return false;
230         }
231         return true;
232     }
233
234
235     private static class AugmentationSchemaImpl implements AugmentationSchema {
236         private SchemaPath targetPath;
237         private RevisionAwareXPath whenCondition;
238         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
239         private Set<GroupingDefinition> groupings = Collections.emptySet();
240         private Set<UsesNode> uses = Collections.emptySet();
241
242         private String description;
243         private String reference;
244         private Status status;
245
246         private AugmentationSchemaImpl(SchemaPath targetPath) {
247             this.targetPath = targetPath;
248         }
249
250         @Override
251         public SchemaPath getTargetPath() {
252             return targetPath;
253         }
254
255         private void setTargetPath(SchemaPath path) {
256             this.targetPath = path;
257         }
258
259         @Override
260         public RevisionAwareXPath getWhenCondition() {
261             return whenCondition;
262         }
263
264         private void setWhenCondition(RevisionAwareXPath whenCondition) {
265             this.whenCondition = whenCondition;
266         }
267
268         @Override
269         public Set<DataSchemaNode> getChildNodes() {
270             return new HashSet<DataSchemaNode>(childNodes.values());
271         }
272
273         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
274             if (childNodes != null) {
275                 this.childNodes = childNodes;
276             }
277         }
278
279         @Override
280         public Set<GroupingDefinition> getGroupings() {
281             return groupings;
282         }
283
284         private void setGroupings(Set<GroupingDefinition> groupings) {
285             if (groupings != null) {
286                 this.groupings = groupings;
287             }
288         }
289
290         @Override
291         public Set<UsesNode> getUses() {
292             return uses;
293         }
294
295         private void setUses(Set<UsesNode> uses) {
296             if (uses != null) {
297                 this.uses = uses;
298             }
299         }
300
301         /**
302          * Always returns an empty set, because augmentation can not contains
303          * type definitions.
304          */
305         @Override
306         public Set<TypeDefinition<?>> getTypeDefinitions() {
307             return Collections.emptySet();
308         }
309
310         @Override
311         public String getDescription() {
312             return description;
313         }
314
315         private void setDescription(String description) {
316             this.description = description;
317         }
318
319         @Override
320         public String getReference() {
321             return reference;
322         }
323
324         private void setReference(String reference) {
325             this.reference = reference;
326         }
327
328         @Override
329         public Status getStatus() {
330             return status;
331         }
332
333         private void setStatus(Status status) {
334             this.status = status;
335         }
336
337         @Override
338         public DataSchemaNode getDataChildByName(QName name) {
339             return childNodes.get(name);
340         }
341
342         @Override
343         public DataSchemaNode getDataChildByName(String name) {
344             DataSchemaNode result = null;
345             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
346                 if (entry.getKey().getLocalName().equals(name)) {
347                     result = entry.getValue();
348                     break;
349                 }
350             }
351             return result;
352         }
353
354         @Override
355         public int hashCode() {
356             final int prime = 17;
357             int result = 1;
358             result = prime * result
359                     + ((targetPath == null) ? 0 : targetPath.hashCode());
360             result = prime * result
361                     + ((whenCondition == null) ? 0 : whenCondition.hashCode());
362             result = prime * result
363                     + ((childNodes == null) ? 0 : childNodes.hashCode());
364             return result;
365         }
366
367         @Override
368         public boolean equals(Object obj) {
369             if (this == obj) {
370                 return true;
371             }
372             if (obj == null) {
373                 return false;
374             }
375             if (getClass() != obj.getClass()) {
376                 return false;
377             }
378             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
379             if (targetPath == null) {
380                 if (other.targetPath != null) {
381                     return false;
382                 }
383             } else if (!targetPath.equals(other.targetPath)) {
384                 return false;
385             }
386             if (whenCondition == null) {
387                 if (other.whenCondition != null) {
388                     return false;
389                 }
390             } else if (!whenCondition.equals(other.whenCondition)) {
391                 return false;
392             }
393             if (childNodes == null) {
394                 if (other.childNodes != null) {
395                     return false;
396                 }
397             } else if (!childNodes.equals(other.childNodes)) {
398                 return false;
399             }
400             return true;
401         }
402
403         @Override
404         public String toString() {
405             StringBuilder sb = new StringBuilder(
406                     AugmentationSchemaImpl.class.getSimpleName());
407             sb.append("[");
408             sb.append("targetPath=" + targetPath);
409             sb.append(", childNodes=" + childNodes.values());
410             sb.append(", groupings=" + groupings);
411             sb.append(", uses=" + uses);
412             sb.append("]");
413             return sb.toString();
414         }
415     }
416
417 }