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