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