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