Added YANG typedef support in the YANG parser
[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.SchemaPath;
21 import org.opendaylight.controller.yang.model.api.Status;
22 import org.opendaylight.controller.yang.model.api.TypeDefinition;
23 import org.opendaylight.controller.yang.model.api.UsesNode;
24 import org.opendaylight.controller.yang.model.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.controller.yang.model.parser.builder.api.DataSchemaNodeBuilder;
26 import org.opendaylight.controller.yang.model.parser.builder.api.GroupingBuilder;
27 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
28 import org.opendaylight.controller.yang.model.parser.builder.api.UsesNodeBuilder;
29 import org.opendaylight.controller.yang.model.parser.util.YangModelBuilderUtil;
30
31 public class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
32
33     private final AugmentationSchemaImpl instance;
34     private final SchemaPath augmentTarget;
35     final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
36     final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
37     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
38
39     AugmentationSchemaBuilderImpl(String augmentPath) {
40         SchemaPath targetPath = YangModelBuilderUtil.parseAugmentPath(augmentPath);
41         augmentTarget = targetPath;
42         instance = new AugmentationSchemaImpl(targetPath);
43     }
44
45     @Override
46     public void addChildNode(DataSchemaNodeBuilder childNode) {
47         childNodes.add(childNode);
48     }
49
50     @Override
51     public Set<DataSchemaNodeBuilder> getChildNodes() {
52         return childNodes;
53     }
54
55     @Override
56     public void addGrouping(GroupingBuilder grouping) {
57         groupings.add(grouping);
58     }
59
60     @Override
61     public void addUsesNode(UsesNodeBuilder usesBuilder) {
62         usesNodes.add(usesBuilder);
63     }
64
65     /**
66      * Always returns null.
67      */
68     @Override
69     public QName getQName() {
70         return null;
71     }
72
73     @Override
74     public AugmentationSchema build() {
75
76         // CHILD NODES
77         Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
78         for (DataSchemaNodeBuilder node : childNodes) {
79             childs.put(node.getQName(), node.build());
80         }
81         instance.setChildNodes(childs);
82
83         // GROUPINGS
84         Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
85         for (GroupingBuilder builder : groupings) {
86             groupingDefinitions.add(builder.build());
87         }
88         instance.setGroupings(groupingDefinitions);
89
90         // USES
91         Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
92         for (UsesNodeBuilder builder : usesNodes) {
93             usesNodeDefinitions.add(builder.build());
94         }
95         instance.setUses(usesNodeDefinitions);
96
97         return instance;
98     }
99
100     @Override
101     public void addTypedef(TypeDefinitionBuilder type) {
102         throw new UnsupportedOperationException(
103                 "Augmentation can not contains type definitions");
104     }
105
106     @Override
107     public void setDescription(String description) {
108         instance.setDescription(description);
109     }
110
111     @Override
112     public void setReference(String reference) {
113         instance.setReference(reference);
114     }
115
116     @Override
117     public void setStatus(Status status) {
118         instance.setStatus(status);
119     }
120
121     @Override
122     public SchemaPath getTargetPath() {
123         return augmentTarget;
124     }
125
126     private static class AugmentationSchemaImpl implements AugmentationSchema {
127
128         private final SchemaPath targetPath;
129         private Map<QName, DataSchemaNode> childNodes;
130         private Set<GroupingDefinition> groupings;
131         private Set<UsesNode> uses;
132
133         private String description;
134         private String reference;
135         private Status status;
136
137         private AugmentationSchemaImpl(SchemaPath targetPath) {
138             this.targetPath = targetPath;
139         }
140
141         @Override
142         public SchemaPath getTargetPath() {
143             return targetPath;
144         }
145
146         @Override
147         public Set<DataSchemaNode> getChildNodes() {
148             return new HashSet<DataSchemaNode>(childNodes.values());
149         }
150
151         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
152             this.childNodes = childNodes;
153         }
154
155         @Override
156         public Set<GroupingDefinition> getGroupings() {
157             return groupings;
158         }
159
160         private void setGroupings(Set<GroupingDefinition> groupings) {
161             this.groupings = groupings;
162         }
163
164         @Override
165         public Set<UsesNode> getUses() {
166             return uses;
167         }
168
169         private void setUses(Set<UsesNode> uses) {
170             this.uses = uses;
171         }
172
173         /**
174          * Always returns an empty set, because augmentation can not contains
175          * type definitions.
176          */
177         @Override
178         public Set<TypeDefinition<?>> getTypeDefinitions() {
179             return Collections.emptySet();
180         }
181
182         @Override
183         public String getDescription() {
184             return description;
185         }
186
187         private void setDescription(String description) {
188             this.description = description;
189         }
190
191         @Override
192         public String getReference() {
193             return reference;
194         }
195
196         private void setReference(String reference) {
197             this.reference = reference;
198         }
199
200         @Override
201         public Status getStatus() {
202             return status;
203         }
204
205         private void setStatus(Status status) {
206             this.status = status;
207         }
208
209         @Override
210         public DataSchemaNode getDataChildByName(QName name) {
211             return childNodes.get(name);
212         }
213
214         @Override
215         public DataSchemaNode getDataChildByName(String name) {
216             DataSchemaNode result = null;
217             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
218                 if (entry.getKey().getLocalName().equals(name)) {
219                     result = entry.getValue();
220                     break;
221                 }
222             }
223             return result;
224         }
225
226         @Override
227         public int hashCode() {
228             final int prime = 17;
229             int result = 1;
230             result = prime * result
231                     + ((targetPath == null) ? 0 : targetPath.hashCode());
232             result = prime * result
233                     + ((childNodes == null) ? 0 : childNodes.hashCode());
234             result = prime * result
235                     + ((groupings == null) ? 0 : groupings.hashCode());
236             result = prime * result + ((uses == null) ? 0 : uses.hashCode());
237             result = prime * result
238                     + ((description == null) ? 0 : description.hashCode());
239             result = prime * result
240                     + ((reference == null) ? 0 : reference.hashCode());
241             result = prime * result
242                     + ((status == null) ? 0 : status.hashCode());
243             return result;
244         }
245
246         @Override
247         public boolean equals(Object obj) {
248             if (this == obj) {
249                 return true;
250             }
251             if (obj == null) {
252                 return false;
253             }
254             if (getClass() != obj.getClass()) {
255                 return false;
256             }
257             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
258             if (targetPath == null) {
259                 if (other.targetPath != null) {
260                     return false;
261                 }
262             } else if (!targetPath.equals(other.targetPath)) {
263                 return false;
264             }
265             if (childNodes == null) {
266                 if (other.childNodes != null) {
267                     return false;
268                 }
269             } else if (!childNodes.equals(other.childNodes)) {
270                 return false;
271             }
272             if (groupings == null) {
273                 if (other.groupings != null) {
274                     return false;
275                 }
276             } else if (!groupings.equals(other.groupings)) {
277                 return false;
278             }
279             if (uses == null) {
280                 if (other.uses != null) {
281                     return false;
282                 }
283             } else if (!uses.equals(other.uses)) {
284                 return false;
285             }
286             if (description == null) {
287                 if (other.description != null) {
288                     return false;
289                 }
290             } else if (!description.equals(other.description)) {
291                 return false;
292             }
293             if (reference == null) {
294                 if (other.reference != null) {
295                     return false;
296                 }
297             } else if (!reference.equals(other.reference)) {
298                 return false;
299             }
300             if (status == null) {
301                 if (other.status != null) {
302                     return false;
303                 }
304             } else if (!status.equals(other.status)) {
305                 return false;
306             }
307             return true;
308         }
309
310         @Override
311         public String toString() {
312             StringBuilder sb = new StringBuilder(
313                     AugmentationSchemaImpl.class.getSimpleName());
314             sb.append("[");
315             sb.append("targetPath=" + targetPath);
316             sb.append(", childNodes=" + childNodes.values());
317             sb.append(", groupings=" + groupings);
318             sb.append(", uses=" + uses);
319             sb.append("]");
320             return sb.toString();
321         }
322     }
323
324 }