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