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