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