blob: 62c63905bd730d72eb1f5db41c033650342586f5 [file] [log] [blame]
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301/*
2 * Copyright 2016-present 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;
18
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053019import java.io.File;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053020import java.io.IOException;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053021import java.util.ArrayList;
22import java.util.List;
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +053023
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053024import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangType;
26import org.onosproject.yangutils.datamodel.YangTypeHolder;
Bharat saraswal64e7e232016-07-14 23:33:55 +053027import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053028import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053029import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053030import org.onosproject.yangutils.utils.io.YangPluginConfig;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053031
Bharat saraswal2da23bf2016-08-25 15:28:39 +053032import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16;
Bharat saraswal64e7e232016-07-14 23:33:55 +053033import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
34import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
35import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16;
36import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053037import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT8;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
39import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
40import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
42import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
43import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053044import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053045import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
46import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswal64e7e232016-07-14 23:33:55 +053047import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053048import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
49import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswal64e7e232016-07-14 23:33:55 +053050import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT;
51import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053052import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.SHORT_TYPE_CONFLICT;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053053import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
54import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal64e7e232016-07-14 23:33:55 +053055import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
56import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053057
58/**
Bharat saraswal64e7e232016-07-14 23:33:55 +053059 * Represents implementation of java data type code fragments temporary implementations. Maintains the temp files
60 * required specific for user defined data type java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053061 */
62public class TempJavaTypeFragmentFiles
63 extends TempJavaFragmentFiles {
64
65 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053066 * File name for of string method.
67 */
68 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
69
70 /**
71 * File name for construction for special type like union, typedef.
72 */
73 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053074
75 /**
76 * File name for typedef class file name suffix.
77 */
78 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
79
80 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053081 * File name for generated class file for special type like union, typedef suffix.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053082 */
83 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
84
85 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053086 * Integer index in type list.
87 */
88 private int intIndex = 0;
89
90 /**
91 * UInt index in type list.
92 */
93 private int uIntIndex = 0;
94
95 /**
96 * long index in type list.
97 */
98 private int longIndex = 0;
99
100 /**
101 * ULong index in type list.
102 */
103 private int uLongIndex = 0;
104
105 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530106 * short index in type list.
107 */
108 private int shortIndex = 0;
109
110 /**
111 * Uint8 index in type list.
112 */
113 private int uInt8Index = 0;
114
115 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530116 * Temporary file handle for of string method of class.
117 */
118 private File ofStringImplTempFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530119
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530120 /**
121 * Temporary file handle for constructor for type class.
122 */
123 private File constructorForTypeTempFileHandle;
124
125 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530126 * Java file handle for typedef class file.
127 */
128 private File typedefClassJavaFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530129
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530130 /**
131 * Java file handle for type class like union, typedef file.
132 */
133 private File typeClassJavaFileHandle;
134
135 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530136 * Java attribute for int.
137 */
138 private JavaAttributeInfo intAttribute;
139
140 /**
141 * Java attribute for long.
142 */
143 private JavaAttributeInfo longAttribute;
144
145 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530146 * Java attribute for short.
147 */
148 private JavaAttributeInfo shortAttribute;
149
150 /**
151 * Java attribute for uint8.
152 */
153 private JavaAttributeInfo uInt8Attribute;
154
155 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530156 * Java attribute for uInt.
157 */
158 private JavaAttributeInfo uIntAttribute;
159
160 /**
161 * Java attribute for uLong.
162 */
163 private JavaAttributeInfo uLongAttribute;
164
165 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530166 * Creates an instance of temporary java code fragment.
167 *
168 * @param javaFileInfo generated java file info
169 * @throws IOException when fails to create new file handle
170 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530171 TempJavaTypeFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530172 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530173
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530174 super(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530175
176 /*
177 * Initialize getterImpl, attributes, hash code, equals and to strings
178 * when generation file type matches to typeDef class mask.
179 */
180 addGeneratedTempFile(OF_STRING_IMPL_MASK);
181 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
182 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
183
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530184 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
185 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530186
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530187 }
188
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530189 /**
190 * Returns type class constructor method's temporary file handle.
191 *
192 * @return type class constructor method's temporary file handle
193 */
194
195 public File getConstructorForTypeTempFileHandle() {
196 return constructorForTypeTempFileHandle;
197 }
198
199 /**
200 * Sets type class constructor method's temporary file handle.
201 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530202 * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530203 */
204 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
205 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
206 }
207
208 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530209 * Returns java file handle for typedef class file.
210 *
211 * @return java file handle for typedef class file
212 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530213 private File getTypedefClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530214 return typedefClassJavaFileHandle;
215 }
216
217 /**
218 * Sets the java file handle for typedef class file.
219 *
220 * @param typedefClassJavaFileHandle java file handle
221 */
222 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
223 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
224 }
225
226 /**
227 * Returns java file handle for type class file.
228 *
229 * @return java file handle for type class file
230 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530231 private File getTypeClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530232 return typeClassJavaFileHandle;
233 }
234
235 /**
236 * Sets the java file handle for type class file.
237 *
238 * @param typeClassJavaFileHandle type file handle
239 */
240 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
241 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
242 }
243
244 /**
245 * Returns of string method's temporary file handle.
246 *
247 * @return of string method's temporary file handle
248 */
249
250 public File getOfStringImplTempFileHandle() {
251 return ofStringImplTempFileHandle;
252 }
253
254 /**
255 * Set of string method's temporary file handle.
256 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530257 * @param ofStringImplTempFileHandle of string method's temporary file handle
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530258 */
259 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
260 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
261 }
262
263 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530264 * Adds all the type in the current data model node as part of the generated temporary file.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530265 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530266 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
267 * @param pluginConfig plugin configurations for naming conventions
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530268 * @throws IOException IO operation fail
269 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530270 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530271 throws IOException {
272
273 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
274 if (typeList != null) {
275 for (YangType<?> yangType : typeList) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530276 if (!(yangType instanceof YangJavaTypeTranslator)) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530277 throw new TranslatorException("Type does not have Java info");
278 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530279 JavaAttributeInfo javaAttributeInfo = getAttributeForType(yangType, pluginConfig);
280 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo,
281 pluginConfig, typeList);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530282 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530283 addTypeConstructor(pluginConfig);
284 addMethodsInConflictCase(pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530285 }
286 }
287
288 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530289 * Returns java attribute.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530290 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530291 * @param yangType YANG type
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530292 * @param pluginConfig plugin configurations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530293 * @return java attribute
294 */
295 private JavaAttributeInfo getAttributeForType(YangType yangType, YangPluginConfig pluginConfig) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530296 YangJavaTypeTranslator javaType = (YangJavaTypeTranslator) yangType;
Bharat saraswal64e7e232016-07-14 23:33:55 +0530297 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
298 String typeName = javaType.getDataTypeName();
299 typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
300 return getAttributeInfoForTheData(
301 javaType.getJavaQualifiedInfo(),
302 typeName, javaType,
303 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
304 false);
305 }
306
307 /**
308 * Adds the new attribute info to the target generated temporary files for union class.
309 *
310 * @param javaAttributeInfo the attribute info that needs to be added to temporary files
311 * @param pluginConfig plugin configurations
312 * @param typeList type list
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530313 * @throws IOException IO operation fail
314 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530315 private void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfo,
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530316 YangPluginConfig pluginConfig, List<YangType<?>> typeList)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530317 throws IOException {
318
Bharat saraswal64e7e232016-07-14 23:33:55 +0530319 YangDataTypes attrType = javaAttributeInfo.getAttributeType().getDataType();
320
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530321 if (attrType == INT16 || attrType == UINT8) {
322 boolean isShortConflict = validateForConflictingShortTypes(typeList);
323 javaAttributeInfo.setShortConflict(isShortConflict);
324 updateAttributeCondition(javaAttributeInfo);
325 if (!isShortConflict) {
326 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
327 }
328 } else if (attrType == INT32 || attrType == UINT16) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530329 boolean isIntConflict = validateForConflictingIntTypes(typeList);
330 javaAttributeInfo.setIntConflict(isIntConflict);
331 updateAttributeCondition(javaAttributeInfo);
332 if (!isIntConflict) {
333 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
334 }
335 } else if (attrType == INT64 || attrType == UINT32) {
336 boolean isLongConflict = validateForConflictingLongTypes(typeList);
337 javaAttributeInfo.setLongConflict(isLongConflict);
338 updateAttributeCondition(javaAttributeInfo);
339 if (!isLongConflict) {
340 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
341 }
342 } else {
343 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
344 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530345 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530346
Bharat saraswal64e7e232016-07-14 23:33:55 +0530347 }
348
349 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530350 * Adds of method and constructor when there is no conflicting types.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530351 *
352 * @param javaAttributeInfo java attribute info
353 * @param pluginConfig plugin configurations
Shankara-Huaweib7564772016-08-02 18:13:13 +0530354 * @throws IOException when fails to do IO operations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530355 */
356 private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo,
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530357 YangPluginConfig pluginConfig)
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530358 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530359 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530360 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530361 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530362
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530363 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530364 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530365 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530366 }
367
368 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530369 * Adds of, getter and from string method in conflict cases.
370 *
371 * @param pluginConfig plugin configurations
372 * @throws IOException when fails to do IO operations
373 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530374 private void addMethodsInConflictCase(YangPluginConfig pluginConfig)
375 throws IOException {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530376 JavaAttributeInfo attr = getIntAttribute();
377 if (attr != null) {
378 attr = getUIntAttribute();
379 }
380 if (attr != null) {
381 if (attr.isIntConflict()) {
382 if (getIntIndex() < getUIntIndex()) {
383 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(),
384 getGeneratedJavaClassName(), pluginConfig)
385 + NEW_LINE);
386 addGetterImpl(getIntAttribute(), pluginConfig);
387 addFromStringMethod(getIntAttribute(), pluginConfig);
388 } else {
389 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(),
390 getGeneratedJavaClassName(), pluginConfig)
391 + NEW_LINE);
392 addGetterImpl(getUIntAttribute(), pluginConfig);
393 addFromStringMethod(getUIntAttribute(), pluginConfig);
394 }
395 }
396 }
397 attr = getLongAttribute();
398 if (attr != null) {
399 attr = getULongAttribute();
400 }
401 if (attr != null) {
402 if (attr.isLongConflict()) {
403 if (getLongIndex() < getULongIndex()) {
404 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(),
405 getGeneratedJavaClassName(), pluginConfig)
406 + NEW_LINE);
407 addGetterImpl(getLongAttribute(), pluginConfig);
408 addFromStringMethod(getLongAttribute(), pluginConfig);
409 } else {
410 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(),
411 getGeneratedJavaClassName(), pluginConfig)
412 + NEW_LINE);
413 addGetterImpl(getULongAttribute(), pluginConfig);
414 addFromStringMethod(getULongAttribute(), pluginConfig);
415 }
416 }
417 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530418
419 attr = getShortAttribute();
420 if (attr != null) {
421 attr = getUInt8Attribute();
422 }
423 if (attr != null) {
424 if (attr.isShortConflict()) {
425 if (getShortIndex() < getUInt8Index()) {
426 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getShortAttribute(),
427 getGeneratedJavaClassName(), pluginConfig)
428 + NEW_LINE);
429 addGetterImpl(getShortAttribute(), pluginConfig);
430 addFromStringMethod(getShortAttribute(), pluginConfig);
431 } else {
432 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUInt8Attribute(),
433 getGeneratedJavaClassName(), pluginConfig)
434 + NEW_LINE);
435 addGetterImpl(getUInt8Attribute(), pluginConfig);
436 addFromStringMethod(getUInt8Attribute(), pluginConfig);
437 }
438 }
439 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530440 }
441
442 /**
443 * Adds from string method for conflict case.
444 *
445 * @param newAttrInfo new attribute
446 * @param pluginConfig plugin configurations
447 * @throws IOException when fails to do IO operations
448 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530449 private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
450 throws IOException {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530451
Shankara-Huaweib7564772016-08-02 18:13:13 +0530452 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +0530453 pluginConfig.getConflictResolver());
454 /*
455 * Create a new java attribute info with qualified information of
456 * wrapper classes.
457 */
458 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
459 newAttrInfo.getAttributeName(),
460 newAttrInfo.getAttributeType(),
461 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
462
463 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
464 }
465
466 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530467 * Adds type constructor.
468 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530469 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530470 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530471 * @throws IOException when fails to append to temporary file
472 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530473 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530474 throws IOException {
475 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530476 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530477 }
478
479 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530480 * Adds type constructor.
481 *
482 * @param pluginConfig plugin configurations
483 * @throws IOException when fails to append to temporary file
484 */
485 private void addTypeConstructor(YangPluginConfig pluginConfig)
486 throws IOException {
487 JavaAttributeInfo attr = getIntAttribute();
488 if (attr != null) {
489 attr = getUIntAttribute();
490 }
491 if (attr != null) {
492 if (attr.isIntConflict()) {
493 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
494 getIntAttribute(),
495 getUIntAttribute(), getGeneratedJavaClassName(), pluginConfig, INT_TYPE_CONFLICT,
496 getIntIndex()
497 < getUIntIndex()) + NEW_LINE);
498 }
499 }
500 attr = getLongAttribute();
501 if (attr != null) {
502 attr = getULongAttribute();
503 }
504 if (attr != null) {
505 if (attr.isLongConflict()) {
506 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
507 getLongAttribute(),
508 getULongAttribute(), getGeneratedJavaClassName(), pluginConfig, LONG_TYPE_CONFLICT,
509 getLongIndex()
510 < getULongIndex()) + NEW_LINE);
511 }
512 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530513 attr = getShortAttribute();
514 if (attr != null) {
515 attr = getUInt8Attribute();
516 }
517 if (attr != null) {
518 if (attr.isShortConflict()) {
519 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
520 getShortAttribute(),
521 getUInt8Attribute(), getGeneratedJavaClassName(), pluginConfig, SHORT_TYPE_CONFLICT,
522 getShortIndex()
523 < getUInt8Index()) + NEW_LINE);
524 }
525 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530526 }
527
528 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530529 * Adds of string for type.
530 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530531 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530532 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530533 * @throws IOException when fails to append to temporary file
534 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530535 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530536 throws IOException {
537 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530538 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530539 + NEW_LINE);
540 }
541
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530542 /**
543 * Removes all temporary file handles.
544 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530545 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530546 * @throws IOException when failed to delete the temporary files
547 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530548 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530549 public void freeTemporaryResources(boolean isErrorOccurred)
550 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530551
552 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530553 closeFile(getTypedefClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530554 }
555
556 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530557 closeFile(getTypeClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530558 }
559
560 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
561 closeFile(getConstructorForTypeTempFileHandle(), true);
562 }
563 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
564 closeFile(getOfStringImplTempFileHandle(), true);
565 }
566 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
567 closeFile(getFromStringImplTempFileHandle(), true);
568 }
569
570 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswal250a7472016-05-12 13:16:57 +0530571
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530572 }
573
574 /**
575 * Constructs java code exit.
576 *
577 * @param fileType generated file type
Bharat saraswal64e7e232016-07-14 23:33:55 +0530578 * @param curNode current YANG node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530579 * @throws IOException when fails to generate java files
580 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530581 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530582 public void generateJavaFile(int fileType, YangNode curNode)
583 throws IOException {
584 List<String> imports = new ArrayList<>();
585 if (isAttributePresent()) {
586 imports = getJavaImportData().getImports();
587 }
588
589 createPackage(curNode);
590
591 /*
592 * Creates type def class file.
593 */
594 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530595 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530596 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
Gaurav Agrawal8a147522016-08-10 13:43:01 +0530597
598 // In case if data type is binary, MoreObjects is not required and should be removed.
599 if (curNode instanceof YangTypeHolder) {
600 YangType yangType = ((YangTypeHolder) curNode).getTypeList().get(0);
601 if (yangType.getDataType() == YangDataTypes.BINARY) {
602 imports.remove(getJavaImportData().getImportForToString());
603 }
604 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530605 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
606 }
607 /*
608 * Creates type class file.
609 */
610 if ((fileType & GENERATE_UNION_CLASS) != 0) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530611 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530612 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
613 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
614 }
615
616 /*
617 * Close all the file handles.
618 */
619 freeTemporaryResources(false);
620 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530621
622 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530623 * Returns short type index from type list.
624 *
625 * @return short type index from type list
626 */
627 public int getShortIndex() {
628 return shortIndex;
629 }
630
631 /**
632 * Sets short type index from type list.
633 *
634 * @param shortIndex short type index from type list.
635 */
636 private void setShortIndex(int shortIndex) {
637 this.shortIndex = shortIndex;
638 }
639
640 /**
641 * Returns uInt type index from type list.
642 *
643 * @return uInt type index from type list
644 */
645 public int getUInt8Index() {
646 return uInt8Index;
647 }
648
649 /**
650 * Sets uInt8 type index from type list.
651 *
652 * @param uInt8Index uInt8 type index from type list.
653 */
654 private void setUInt8Index(int uInt8Index) {
655 this.uInt8Index = uInt8Index;
656 }
657
658 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530659 * Returns int type index from type list.
660 *
661 * @return int type index from type list
662 */
663 public int getIntIndex() {
664 return intIndex;
665 }
666
667 /**
668 * Sets int type index from type list.
669 *
670 * @param intIndex int type index from type list.
671 */
672 private void setIntIndex(int intIndex) {
673 this.intIndex = intIndex;
674 }
675
676 /**
677 * Returns uInt type index from type list.
678 *
679 * @return uInt type index from type list
680 */
681 public int getUIntIndex() {
682 return uIntIndex;
683 }
684
685 /**
686 * Sets uInt type index from type list.
687 *
688 * @param uIntIndex uInt type index from type list.
689 */
690 private void setUIntIndex(int uIntIndex) {
691 this.uIntIndex = uIntIndex;
692 }
693
694 /**
695 * Returns long type index from type list.
696 *
697 * @return long type index from type list
698 */
699 public int getLongIndex() {
700 return longIndex;
701 }
702
703 /**
704 * Sets long type index from type list.
705 *
706 * @param longIndex long type index from type list.
707 */
708 private void setLongIndex(int longIndex) {
709 this.longIndex = longIndex;
710 }
711
712 /**
713 * Returns uLong type index from type list.
714 *
715 * @return uLong type index from type list
716 */
717 public int getULongIndex() {
718 return uLongIndex;
719 }
720
721 /**
722 * Sets uLong type index from type list.
723 *
724 * @param uLongIndex uLong type index from type list.
725 */
726 private void setULongIndex(int uLongIndex) {
727 this.uLongIndex = uLongIndex;
728 }
729
730 /**
731 * Validates conflict for int and uInt.
732 *
733 * @param typeList type list
734 * @return true if conflict is there
735 */
736 private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) {
737 boolean isIntPresent = false;
738 boolean isUIntPresent = false;
739 for (YangType type : typeList) {
740 if (type.getDataType().equals(INT32)) {
741 setIntIndex(typeList.indexOf(type));
742 isIntPresent = true;
743 }
744 if (type.getDataType().equals(UINT16)) {
745 setUIntIndex(typeList.indexOf(type));
746 isUIntPresent = true;
747 }
748 }
749
750 return isIntPresent && isUIntPresent;
751 }
752
753 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530754 * Validates conflict for int and uInt.
755 *
756 * @param typeList type list
757 * @return true if conflict is there
758 */
759 private boolean validateForConflictingShortTypes(List<YangType<?>> typeList) {
760 boolean isShortPresent = false;
761 boolean isUInt8Present = false;
762 for (YangType type : typeList) {
763 if (type.getDataType().equals(INT16)) {
764 setShortIndex(typeList.indexOf(type));
765 isShortPresent = true;
766 }
767 if (type.getDataType().equals(UINT8)) {
768 setUInt8Index(typeList.indexOf(type));
769 isUInt8Present = true;
770 }
771 }
772
773 return isShortPresent && isUInt8Present;
774 }
775
776 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530777 * Validates conflict for long and uLong.
778 *
779 * @param typeList type list
780 * @return true if conflict is there
781 */
782 private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) {
783 boolean isLongPresent = false;
784 boolean isULongPresent = false;
785 for (YangType type : typeList) {
786 if (type.getDataType().equals(INT64)) {
787 setLongIndex(typeList.indexOf(type));
788 isLongPresent = true;
789 }
790 if (type.getDataType().equals(UINT32)) {
791 setULongIndex(typeList.indexOf(type));
792 isULongPresent = true;
793 }
794 }
795
796 return isLongPresent && isULongPresent;
797 }
798
799 /**
800 * Updates attribute info in case of conflicts.
801 *
802 * @param javaAttributeInfo java attribute info
803 */
804 private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) {
805
806 if (javaAttributeInfo.isIntConflict()) {
807 if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) {
808 setUIntAttribute(javaAttributeInfo);
809 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) {
810 setIntAttribute(javaAttributeInfo);
811 }
812
813 }
814 if (javaAttributeInfo.isLongConflict()) {
815 if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) {
816 setULongAttribute(javaAttributeInfo);
817 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) {
818 setLongAttribute(javaAttributeInfo);
819 }
820
821 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530822 if (javaAttributeInfo.isShortConflict()) {
823 if (javaAttributeInfo.getAttributeType().getDataType() == UINT8) {
824 setUInt8Attribute(javaAttributeInfo);
825 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT16) {
826 setShortAttribute(javaAttributeInfo);
827 }
828
829 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530830 }
831
832 /**
833 * Returns attribute for int.
834 *
835 * @return attribute for int
836 */
837 public JavaAttributeInfo getIntAttribute() {
838 return intAttribute;
839 }
840
841 /**
842 * Sets attribute for int.
843 *
844 * @param intAttribute attribute for int
845 */
846 private void setIntAttribute(JavaAttributeInfo intAttribute) {
847 this.intAttribute = intAttribute;
848 }
849
850 /**
851 * Returns attribute for long.
852 *
853 * @return attribute for long
854 */
855 public JavaAttributeInfo getLongAttribute() {
856 return longAttribute;
857 }
858
859 /**
860 * Sets attribute for long.
861 *
862 * @param longAttribute attribute for long
863 */
864 private void setLongAttribute(JavaAttributeInfo longAttribute) {
865 this.longAttribute = longAttribute;
866 }
867
868 /**
869 * Returns attribute for uInt.
870 *
871 * @return attribute for uInt
872 */
873 public JavaAttributeInfo getUIntAttribute() {
874 return uIntAttribute;
875 }
876
877 /**
878 * Sets attribute for uInt.
879 *
880 * @param uIntAttribute attribute for uInt
881 */
882 private void setUIntAttribute(JavaAttributeInfo uIntAttribute) {
883 this.uIntAttribute = uIntAttribute;
884 }
885
886 /**
887 * Returns attribute for uLong.
888 *
889 * @return attribute for uLong
890 */
891 public JavaAttributeInfo getULongAttribute() {
892 return uLongAttribute;
893 }
894
895 /**
896 * Sets attribute for uLong.
897 *
898 * @param uLongAttribute attribute for uLong
899 */
900 private void setULongAttribute(JavaAttributeInfo uLongAttribute) {
901 this.uLongAttribute = uLongAttribute;
902 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530903
904 /**
905 * Returns attribute for uInt8.
906 *
907 * @return attribute for uInt8
908 */
909 public JavaAttributeInfo getUInt8Attribute() {
910 return uInt8Attribute;
911 }
912
913 /**
914 * Sets attribute for uInt8.
915 *
916 * @param uInt8Attribute attribute for uInt8
917 */
918 private void setUInt8Attribute(JavaAttributeInfo uInt8Attribute) {
919 this.uInt8Attribute = uInt8Attribute;
920 }
921
922 /**
923 * Returns attribute for short.
924 *
925 * @return attribute for short
926 */
927 public JavaAttributeInfo getShortAttribute() {
928 return shortAttribute;
929 }
930
931 /**
932 * Sets attribute for short.
933 *
934 * @param shortAttribute attribute for short
935 */
936 private void setShortAttribute(JavaAttributeInfo shortAttribute) {
937 this.shortAttribute = shortAttribute;
938 }
939
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530940}