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