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