Minor code refactoring.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / 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.yangtools.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.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.Status;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.UsesNode;
29 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
30 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
34 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
35 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
36 import org.opendaylight.yangtools.yang.parser.util.Comparators;
37 import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
38 import org.opendaylight.yangtools.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 targetPath;
52     private SchemaPath targetNodeSchemaPath;
53
54     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
55     private boolean resolved;
56
57     public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
58         super(moduleName, line, null);
59         this.augmentTargetStr = augmentTargetStr;
60         targetPath = ParserUtils.parseXPathString(augmentTargetStr);
61         instance = new AugmentationSchemaImpl(targetPath);
62     }
63
64     @Override
65     public Set<GroupingDefinition> getGroupings() {
66         return Collections.emptySet();
67     }
68
69     @Override
70     public Set<GroupingBuilder> getGroupingBuilders() {
71         return Collections.emptySet();
72     }
73
74     @Override
75     public void addGrouping(GroupingBuilder grouping) {
76         throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
77     }
78
79     @Override
80     public Set<UsesNodeBuilder> getUsesNodes() {
81         return usesNodes;
82     }
83
84     @Override
85     public void addUsesNode(UsesNodeBuilder usesBuilder) {
86         usesNodes.add(usesBuilder);
87     }
88
89     @Override
90     public SchemaPath getPath() {
91         return targetNodeSchemaPath;
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(targetNodeSchemaPath);
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(moduleName, line, "Augmentation can not contains typedef statement.");
163     }
164
165     @Override
166     public String getDescription() {
167         return description;
168     }
169
170     @Override
171     public void setDescription(String description) {
172         this.description = description;
173     }
174
175     @Override
176     public String getReference() {
177         return reference;
178     }
179
180     @Override
181     public void setReference(String reference) {
182         this.reference = reference;
183     }
184
185     @Override
186     public Status getStatus() {
187         return status;
188     }
189
190     @Override
191     public void setStatus(Status status) {
192         if (status != null) {
193             this.status = status;
194         }
195     }
196
197     @Override
198     public String getTargetPathAsString() {
199         return augmentTargetStr;
200     }
201
202     @Override
203     public SchemaPath getTargetPath() {
204         return targetPath;
205     }
206
207     @Override
208     public SchemaPath getTargetNodeSchemaPath() {
209         return targetNodeSchemaPath;
210     }
211
212     @Override
213     public void setTargetNodeSchemaPath(SchemaPath path) {
214         this.targetNodeSchemaPath = path;
215     }
216
217     @Override
218     public int hashCode() {
219         final int prime = 17;
220         int result = 1;
221         result = prime * result + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
222         result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
223         result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
224         return result;
225     }
226
227     @Override
228     public boolean equals(Object obj) {
229         if (this == obj) {
230             return true;
231         }
232         if (obj == null) {
233             return false;
234         }
235         if (getClass() != obj.getClass()) {
236             return false;
237         }
238         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
239         if (augmentTargetStr == null) {
240             if (other.augmentTargetStr != null) {
241                 return false;
242             }
243         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
244             return false;
245         }
246         if (whenCondition == null) {
247             if (other.whenCondition != null) {
248                 return false;
249             }
250         } else if (!whenCondition.equals(other.whenCondition)) {
251             return false;
252         }
253         if (childNodes == null) {
254             if (other.childNodes != null) {
255                 return false;
256             }
257         } else if (!childNodes.equals(other.childNodes)) {
258             return false;
259         }
260         return true;
261     }
262
263     public String toString() {
264         return "augment " + augmentTargetStr;
265     }
266
267     private final class AugmentationSchemaImpl implements AugmentationSchema {
268         private SchemaPath targetPath;
269         private RevisionAwareXPath whenCondition;
270         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
271         private Set<UsesNode> uses = Collections.emptySet();
272         private String description;
273         private String reference;
274         private Status status;
275         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
276
277         private AugmentationSchemaImpl(SchemaPath targetPath) {
278             this.targetPath = targetPath;
279         }
280
281         @Override
282         public SchemaPath getTargetPath() {
283             return targetPath;
284         }
285
286         private void setTargetPath(SchemaPath path) {
287             this.targetPath = path;
288         }
289
290         @Override
291         public RevisionAwareXPath getWhenCondition() {
292             return whenCondition;
293         }
294
295         private void setWhenCondition(RevisionAwareXPath whenCondition) {
296             this.whenCondition = whenCondition;
297         }
298
299         @Override
300         public Set<DataSchemaNode> getChildNodes() {
301             final Set<DataSchemaNode> result = new TreeSet<DataSchemaNode>(Comparators.SCHEMA_NODE_COMP);
302             result.addAll(childNodes.values());
303             return result;
304         }
305
306         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
307             if (childNodes != null) {
308                 this.childNodes = childNodes;
309             }
310         }
311
312         /**
313          * Always returns an empty set, because augment can not contains
314          * grouping statement.
315          */
316         @Override
317         public Set<GroupingDefinition> getGroupings() {
318             return Collections.emptySet();
319         }
320
321         @Override
322         public Set<UsesNode> getUses() {
323             return uses;
324         }
325
326         private void setUses(Set<UsesNode> uses) {
327             if (uses != null) {
328                 this.uses = uses;
329             }
330         }
331
332         /**
333          * Always returns an empty set, because augment can not contains type
334          * definitions.
335          */
336         @Override
337         public Set<TypeDefinition<?>> getTypeDefinitions() {
338             return Collections.emptySet();
339         }
340
341         @Override
342         public String getDescription() {
343             return description;
344         }
345
346         private void setDescription(String description) {
347             this.description = description;
348         }
349
350         @Override
351         public String getReference() {
352             return reference;
353         }
354
355         private void setReference(String reference) {
356             this.reference = reference;
357         }
358
359         @Override
360         public Status getStatus() {
361             return status;
362         }
363
364         private void setStatus(Status status) {
365             this.status = status;
366         }
367
368         @Override
369         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
370             return unknownNodes;
371         }
372
373         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
374             if (unknownSchemaNodes != null) {
375                 this.unknownNodes = unknownSchemaNodes;
376             }
377         }
378
379         @Override
380         public DataSchemaNode getDataChildByName(QName name) {
381             return childNodes.get(name);
382         }
383
384         @Override
385         public DataSchemaNode getDataChildByName(String name) {
386             DataSchemaNode result = null;
387             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
388                 if (entry.getKey().getLocalName().equals(name)) {
389                     result = entry.getValue();
390                     break;
391                 }
392             }
393             return result;
394         }
395
396         @Override
397         public int hashCode() {
398             final int prime = 17;
399             int result = 1;
400             result = prime * result + ((targetPath == null) ? 0 : targetPath.hashCode());
401             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
402             result = prime * result + ((childNodes == null) ? 0 : childNodes.hashCode());
403             return result;
404         }
405
406         @Override
407         public boolean equals(Object obj) {
408             if (this == obj) {
409                 return true;
410             }
411             if (obj == null) {
412                 return false;
413             }
414             if (getClass() != obj.getClass()) {
415                 return false;
416             }
417             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
418             if (targetPath == null) {
419                 if (other.targetPath != null) {
420                     return false;
421                 }
422             } else if (!targetPath.equals(other.targetPath)) {
423                 return false;
424             }
425             if (whenCondition == null) {
426                 if (other.whenCondition != null) {
427                     return false;
428                 }
429             } else if (!whenCondition.equals(other.whenCondition)) {
430                 return false;
431             }
432             if (childNodes == null) {
433                 if (other.childNodes != null) {
434                     return false;
435                 }
436             } else if (!childNodes.equals(other.childNodes)) {
437                 return false;
438             }
439             return true;
440         }
441
442         @Override
443         public String toString() {
444             StringBuilder sb = new StringBuilder(AugmentationSchemaImpl.class.getSimpleName());
445             sb.append("[");
446             sb.append("targetPath=" + targetPath);
447             sb.append(", when=" + whenCondition);
448             sb.append("]");
449             return sb.toString();
450         }
451     }
452
453 }