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