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 / TypedefBuilder.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.List;
12
13 import org.opendaylight.controller.model.util.UnknownType;
14 import org.opendaylight.controller.model.util.YangTypesConverter;
15 import org.opendaylight.controller.yang.common.QName;
16 import org.opendaylight.controller.yang.model.api.SchemaPath;
17 import org.opendaylight.controller.yang.model.api.Status;
18 import org.opendaylight.controller.yang.model.api.TypeDefinition;
19 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
21 import org.opendaylight.controller.yang.model.parser.builder.api.TypeAwareBuilder;
22 import org.opendaylight.controller.yang.model.parser.builder.api.TypeDefinitionBuilder;
23
24 public class TypedefBuilder implements TypeDefinitionBuilder,
25         SchemaNodeBuilder, TypeAwareBuilder {
26
27     private final QName qname;
28     private SchemaPath schemaPath;
29     private TypeDefinition<?> baseType;
30
31     private String description;
32     private String reference;
33     private Status status;
34     private String units;
35     private Object defaultValue;
36
37     TypedefBuilder(QName qname) {
38         this.qname = qname;
39     }
40
41     @Override
42     public TypeDefinition<? extends TypeDefinition<?>> build() {
43         final TypeDefinition<?> type = YangTypesConverter
44                 .javaTypeForBaseYangType(qname);
45         if (type != null) {
46             return type;
47         } else {
48             if (baseType != null) {
49                 // typedef
50                 TypeDefinitionImpl instance = new TypeDefinitionImpl(qname);
51                 instance.setDescription(description);
52                 instance.setReference(reference);
53                 instance.setStatus(status);
54                 instance.setPath(schemaPath);
55                 instance.setBaseType(baseType);
56                 instance.setUnits(units);
57                 instance.setDefaultValue(defaultValue);
58                 return instance;
59             } else {
60                 // type
61                 final UnknownType.Builder unknownBuilder = new UnknownType.Builder(
62                         qname, description, reference);
63                 unknownBuilder.status(status);
64                 return unknownBuilder.build();
65             }
66         }
67     }
68
69     @Override
70     public QName getQName() {
71         return qname;
72     }
73
74     @Override
75     public void setPath(final SchemaPath schemaPath) {
76         this.schemaPath = schemaPath;
77     }
78
79     @Override
80     public void setDescription(final String description) {
81         this.description = description;
82     }
83
84     @Override
85     public void setReference(final String reference) {
86         this.reference = reference;
87     }
88
89     @Override
90     public void setStatus(final Status status) {
91         if (status != null) {
92             this.status = status;
93         }
94     }
95
96     @Override
97     public void setUnits(String units) {
98         this.units = units;
99     }
100
101     @Override
102     public void setDefaultValue(Object defaultValue) {
103         this.defaultValue = defaultValue;
104     }
105
106     @Override
107     public TypeDefinition<?> getType() {
108         return baseType;
109     }
110
111     @Override
112     public void setType(TypeDefinition<?> baseType) {
113         this.baseType = baseType;
114     }
115
116     @Override
117     public TypeDefinition<?> getBaseType() {
118         return baseType;
119     }
120
121     @Override
122     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownSchemaNodeBuilder) {
123         // TODO
124     }
125
126     private static class TypeDefinitionImpl<T extends TypeDefinition<T>>
127             implements TypeDefinition<T> {
128
129         private final QName qname;
130         private SchemaPath path;
131         private String description;
132         private String reference;
133         private Status status = Status.CURRENT;
134         private Object defaultValue;
135         private T baseType;
136         private String units;
137         private List<UnknownSchemaNode> unknownSchemaNodes = Collections.emptyList();
138
139         private TypeDefinitionImpl(QName qname) {
140             this.qname = qname;
141         }
142
143         @Override
144         public QName getQName() {
145             return qname;
146         }
147
148         @Override
149         public SchemaPath getPath() {
150             return path;
151         }
152
153         private void setPath(SchemaPath path) {
154             this.path = path;
155         }
156
157         @Override
158         public String getDescription() {
159             return description;
160         }
161
162         private void setDescription(String description) {
163             this.description = description;
164         }
165
166         @Override
167         public String getReference() {
168             return reference;
169         }
170
171         private void setReference(String reference) {
172             this.reference = reference;
173         }
174
175         @Override
176         public Status getStatus() {
177             return status;
178         }
179
180         private void setStatus(Status status) {
181             this.status = status;
182         }
183
184         @Override
185         public T getBaseType() {
186             return baseType;
187         }
188
189         private void setBaseType(T type) {
190             this.baseType = type;
191         }
192
193         @Override
194         public String getUnits() {
195             return units;
196         }
197
198         private void setUnits(String units) {
199             this.units = units;
200         }
201
202         @Override
203         public Object getDefaultValue() {
204             return defaultValue;
205         }
206
207         private void setDefaultValue(Object defaultValue) {
208             this.defaultValue = defaultValue;
209         }
210
211         @Override
212         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
213             return unknownSchemaNodes;
214         }
215
216         @Override
217         public int hashCode() {
218             final int prime = 31;
219             int result = 1;
220             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
221             result = prime * result + ((path == null) ? 0 : path.hashCode());
222             result = prime * result
223                     + ((description == null) ? 0 : description.hashCode());
224             result = prime * result
225                     + ((reference == null) ? 0 : reference.hashCode());
226             result = prime * result
227                     + ((status == null) ? 0 : status.hashCode());
228             result = prime * result
229                     + ((baseType == null) ? 0 : baseType.hashCode());
230             result = prime * result + ((units == null) ? 0 : units.hashCode());
231             return result;
232         }
233
234         @Override
235         public boolean equals(Object obj) {
236             if (this == obj) {
237                 return true;
238             }
239             if (obj == null) {
240                 return false;
241             }
242             if (getClass() != obj.getClass()) {
243                 return false;
244             }
245             TypeDefinitionImpl other = (TypeDefinitionImpl) obj;
246             if (qname == null) {
247                 if (other.qname != null) {
248                     return false;
249                 }
250             } else if (!qname.equals(other.qname)) {
251                 return false;
252             }
253             if (path == null) {
254                 if (other.path != null) {
255                     return false;
256                 }
257             } else if (!path.equals(other.path)) {
258                 return false;
259             }
260             if (description == null) {
261                 if (other.description != null) {
262                     return false;
263                 }
264             } else if (!description.equals(other.description)) {
265                 return false;
266             }
267             if (reference == null) {
268                 if (other.reference != null) {
269                     return false;
270                 }
271             } else if (!reference.equals(other.reference)) {
272                 return false;
273             }
274             if (status == null) {
275                 if (other.status != null) {
276                     return false;
277                 }
278             } else if (!status.equals(other.status)) {
279                 return false;
280             }
281             if (baseType == null) {
282                 if (other.baseType != null) {
283                     return false;
284                 }
285             } else if (!baseType.equals(other.baseType)) {
286                 return false;
287             }
288             if (units == null) {
289                 if (other.units != null) {
290                     return false;
291                 }
292             } else if (!units.equals(other.units)) {
293                 return false;
294             }
295             return true;
296         }
297
298         @Override
299         public String toString() {
300             final StringBuilder sb = new StringBuilder(
301                     TypeDefinitionImpl.class.getSimpleName());
302             sb.append("[");
303             sb.append("qname=" + qname);
304             sb.append(", path=" + path);
305             sb.append(", description=" + description);
306             sb.append(", reference=" + reference);
307             sb.append(", status=" + status);
308             sb.append(", baseType=" + baseType + "]");
309             return sb.toString();
310         }
311     }
312
313 }