blob: 97b47587814ef495724ec0305e6326ef581f73c9 [file] [log] [blame]
Bharat saraswal4bf8b152016-02-25 02:26:43 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.translator.tojava.utils;
18
Vinod Kumar S38046502016-03-23 15:30:27 +053019import java.util.Set;
20import java.util.TreeSet;
21
Bharat saraswal4bf8b152016-02-25 02:26:43 +053022import org.onosproject.yangutils.datamodel.YangDataTypes;
23import org.onosproject.yangutils.datamodel.YangType;
Vinod Kumar S38046502016-03-23 15:30:27 +053024import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053025import org.onosproject.yangutils.utils.UtilConstants;
26
27/**
28 * Provides java data types corresponding to YANG type.
29 */
30public final class AttributesJavaDataType {
31
Vinod Kumar S38046502016-03-23 15:30:27 +053032 private static Set<JavaQualifiedTypeInfo> importInfo = new TreeSet<>();
33
Bharat saraswal4bf8b152016-02-25 02:26:43 +053034 /**
35 * Default constructor.
36 */
37 private AttributesJavaDataType() {
38 }
39
40 /**
Vinod Kumar S38046502016-03-23 15:30:27 +053041 * Returns import info.
42 *
43 * @return import info
44 */
45 public static Set<JavaQualifiedTypeInfo> getImportInfo() {
46 return importInfo;
47 }
48
49 /**
50 * Adds import info to the import info set.
51 *
52 * @param importData import info
53 */
54 public static void addImportInfo(JavaQualifiedTypeInfo importData) {
55 getImportInfo().add(importData);
56 }
57
58 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053059 * Returns java type.
Bharat saraswal4bf8b152016-02-25 02:26:43 +053060 *
61 * @param yangType YANG type
Vinod Kumar Sc4216002016-03-03 19:55:30 +053062 * @return java type
Bharat saraswal4bf8b152016-02-25 02:26:43 +053063 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053064 public static String getJavaDataType(YangType<?> yangType) {
65 YangDataTypes type = yangType.getDataType();
Bharat saraswal4bf8b152016-02-25 02:26:43 +053066
Bharat saraswal4bf8b152016-02-25 02:26:43 +053067 if (type.equals(YangDataTypes.INT8)) {
68 return UtilConstants.BYTE;
69 } else if (type.equals(YangDataTypes.INT16)) {
70 return UtilConstants.SHORT;
71 } else if (type.equals(YangDataTypes.INT32)) {
72 return UtilConstants.INT;
73 } else if (type.equals(YangDataTypes.INT64)) {
74 return UtilConstants.LONG;
75 } else if (type.equals(YangDataTypes.UINT8)) {
76 return UtilConstants.SHORT;
77 } else if (type.equals(YangDataTypes.UINT16)) {
78 return UtilConstants.INT;
79 } else if (type.equals(YangDataTypes.UINT32)) {
80 return UtilConstants.LONG;
81 } else if (type.equals(YangDataTypes.UINT64)) {
82 //TODO: BIGINTEGER.
83 } else if (type.equals(YangDataTypes.DECIMAL64)) {
84 //TODO: DECIMAL64.
85 } else if (type.equals(YangDataTypes.STRING)) {
86 return UtilConstants.STRING;
87 } else if (type.equals(YangDataTypes.BOOLEAN)) {
88 return UtilConstants.BOOLEAN;
89 } else if (type.equals(YangDataTypes.ENUMERATION)) {
90 //TODO: ENUMERATION.
91 } else if (type.equals(YangDataTypes.BITS)) {
92 //TODO:BITS
93 } else if (type.equals(YangDataTypes.BINARY)) {
94 //TODO:BINARY
95 } else if (type.equals(YangDataTypes.LEAFREF)) {
96 //TODO:LEAFREF
97 } else if (type.equals(YangDataTypes.IDENTITYREF)) {
98 //TODO:IDENTITYREF
99 } else if (type.equals(YangDataTypes.EMPTY)) {
100 //TODO:EMPTY
101 } else if (type.equals(YangDataTypes.UNION)) {
102 //TODO:UNION
103 } else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
104 //TODO:INSTANCE_IDENTIFIER
105 } else if (type.equals(YangDataTypes.DERIVED)) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530106 return yangType.getDataTypeName();
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530107 }
108 return null;
109 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530110
111 /**
112 * Returns java import class.
113 *
114 * @param yangType YANG type
115 * @param isListAttr if the attribute need to be a list
116 * @return java import class
117 */
118 public static String getJavaImportClass(YangType<?> yangType, boolean isListAttr) {
119 YangDataTypes type = yangType.getDataType();
120
121 if (isListAttr) {
122 if (type.equals(YangDataTypes.INT8)) {
123 return UtilConstants.BYTE_WRAPPER;
124 } else if (type.equals(YangDataTypes.INT16)) {
125 return UtilConstants.SHORT_WRAPPER;
126 } else if (type.equals(YangDataTypes.INT32)) {
127 return UtilConstants.INTEGER_WRAPPER;
128 } else if (type.equals(YangDataTypes.INT64)) {
129 return UtilConstants.LONG_WRAPPER;
130 } else if (type.equals(YangDataTypes.UINT8)) {
131 return UtilConstants.SHORT_WRAPPER;
132 } else if (type.equals(YangDataTypes.UINT16)) {
133 return UtilConstants.INTEGER_WRAPPER;
134 } else if (type.equals(YangDataTypes.UINT32)) {
135 return UtilConstants.LONG_WRAPPER;
136 } else if (type.equals(YangDataTypes.UINT64)) {
137 //TODO: BIGINTEGER.
138 } else if (type.equals(YangDataTypes.DECIMAL64)) {
139 //TODO: DECIMAL64.
140 } else if (type.equals(YangDataTypes.STRING)) {
141 return UtilConstants.STRING;
142 } else if (type.equals(YangDataTypes.BOOLEAN)) {
143 return UtilConstants.BOOLEAN_WRAPPER;
144 } else if (type.equals(YangDataTypes.ENUMERATION)) {
145 //TODO: ENUMERATION.
146 } else if (type.equals(YangDataTypes.BITS)) {
147 //TODO:BITS
148 } else if (type.equals(YangDataTypes.BINARY)) {
149 //TODO:BINARY
150 } else if (type.equals(YangDataTypes.LEAFREF)) {
151 //TODO:LEAFREF
152 } else if (type.equals(YangDataTypes.IDENTITYREF)) {
153 //TODO:IDENTITYREF
154 } else if (type.equals(YangDataTypes.EMPTY)) {
155 //TODO:EMPTY
156 } else if (type.equals(YangDataTypes.UNION)) {
157 //TODO:UNION
158 } else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
159 //TODO:INSTANCE_IDENTIFIER
160 } else if (type.equals(YangDataTypes.DERIVED)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530161 return JavaIdentifierSyntax
162 .getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530163 }
164 } else {
165 if (type.equals(YangDataTypes.UINT64)) {
166 //TODO: BIGINTEGER.
167 } else if (type.equals(YangDataTypes.DECIMAL64)) {
168 //TODO: DECIMAL64.
169 } else if (type.equals(YangDataTypes.STRING)) {
170 return UtilConstants.STRING;
171 } else if (type.equals(YangDataTypes.ENUMERATION)) {
172 //TODO: ENUMERATION.
173 } else if (type.equals(YangDataTypes.BITS)) {
174 //TODO:BITS
175 } else if (type.equals(YangDataTypes.BINARY)) {
176 //TODO:BINARY
177 } else if (type.equals(YangDataTypes.LEAFREF)) {
178 //TODO:LEAFREF
179 } else if (type.equals(YangDataTypes.IDENTITYREF)) {
180 //TODO:IDENTITYREF
181 } else if (type.equals(YangDataTypes.EMPTY)) {
182 //TODO:EMPTY
183 } else if (type.equals(YangDataTypes.UNION)) {
184 //TODO:UNION
185 } else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
186 //TODO:INSTANCE_IDENTIFIER
187 } else if (type.equals(YangDataTypes.DERIVED)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530188 return JavaIdentifierSyntax
189 .getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530190 }
191 }
192 return null;
193 }
194
195 /**
196 * Returns java import package.
197 *
198 * @param yangType YANG type
199 * @param isListAttr if the attribute is of list type
Vinod Kumar S38046502016-03-23 15:30:27 +0530200 * @param classInfo java import class info
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530201 * @return java import package
202 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530203 public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530204 YangDataTypes type = yangType.getDataType();
205
206 if (isListAttr) {
207 if (type.equals(YangDataTypes.INT8)
208 || type.equals(YangDataTypes.INT16)
209 || type.equals(YangDataTypes.INT32)
210 || type.equals(YangDataTypes.INT64)
211 || type.equals(YangDataTypes.UINT8)
212 || type.equals(YangDataTypes.UINT16)
213 || type.equals(YangDataTypes.UINT32)
214 || type.equals(YangDataTypes.STRING)
215 || type.equals(YangDataTypes.BOOLEAN)) {
216 return UtilConstants.JAVA_LANG;
217 } else if (type.equals(YangDataTypes.UINT64)) {
218 //TODO: BIGINTEGER.
219 } else if (type.equals(YangDataTypes.DECIMAL64)) {
220 //TODO: DECIMAL64.
221 } else if (type.equals(YangDataTypes.ENUMERATION)) {
222 //TODO: ENUMERATION.
223 } else if (type.equals(YangDataTypes.BITS)) {
224 //TODO:BITS
225 } else if (type.equals(YangDataTypes.BINARY)) {
226 //TODO:BINARY
227 } else if (type.equals(YangDataTypes.LEAFREF)) {
228 //TODO:LEAFREF
229 } else if (type.equals(YangDataTypes.IDENTITYREF)) {
230 //TODO:IDENTITYREF
231 } else if (type.equals(YangDataTypes.EMPTY)) {
232 //TODO:EMPTY
233 } else if (type.equals(YangDataTypes.UNION)) {
234 //TODO:UNION
235 } else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
236 //TODO:INSTANCE_IDENTIFIER
237 } else if (type.equals(YangDataTypes.DERIVED)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530238 for (JavaQualifiedTypeInfo imports : getImportInfo()) {
239 if (imports.getClassInfo().equals(classInfo)) {
240 return imports.getPkgInfo();
241 }
242 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530243 }
244 } else {
245
246 if (type.equals(YangDataTypes.UINT64)) {
247 //TODO: BIGINTEGER.
248 } else if (type.equals(YangDataTypes.DECIMAL64)) {
249 //TODO: DECIMAL64.
250 } else if (type.equals(YangDataTypes.STRING)) {
251 return UtilConstants.JAVA_LANG;
252 } else if (type.equals(YangDataTypes.ENUMERATION)) {
253 //TODO: ENUMERATION.
254 } else if (type.equals(YangDataTypes.BITS)) {
255 //TODO:BITS
256 } else if (type.equals(YangDataTypes.BINARY)) {
257 //TODO:BINARY
258 } else if (type.equals(YangDataTypes.LEAFREF)) {
259 //TODO:LEAFREF
260 } else if (type.equals(YangDataTypes.IDENTITYREF)) {
261 //TODO:IDENTITYREF
262 } else if (type.equals(YangDataTypes.EMPTY)) {
263 //TODO:EMPTY
264 } else if (type.equals(YangDataTypes.UNION)) {
265 //TODO:UNION
266 } else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
267 //TODO:INSTANCE_IDENTIFIER
268 } else if (type.equals(YangDataTypes.DERIVED)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530269 for (JavaQualifiedTypeInfo imports : getImportInfo()) {
270 if (imports.getClassInfo().equals(classInfo)) {
271 return imports.getPkgInfo();
272 }
273 }
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530274 }
275 }
276 return null;
277 }
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530278}