blob: b8f12e69f5ab9b2d6cb08534e1cc51168175a7b4 [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 saraswal64e7e232016-07-14 23:33:55 +053032import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
33import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
34import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16;
35import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053036import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
37import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
38import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
39import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
41import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053042import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053043import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
44import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswal64e7e232016-07-14 23:33:55 +053045import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053046import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
47import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswal64e7e232016-07-14 23:33:55 +053048import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT;
49import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053050import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
51import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal64e7e232016-07-14 23:33:55 +053052import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
53import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053054
55/**
Bharat saraswal64e7e232016-07-14 23:33:55 +053056 * Represents implementation of java data type code fragments temporary implementations. Maintains the temp files
57 * required specific for user defined data type java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053058 */
59public class TempJavaTypeFragmentFiles
60 extends TempJavaFragmentFiles {
61
62 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053063 * File name for of string method.
64 */
65 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
66
67 /**
68 * File name for construction for special type like union, typedef.
69 */
70 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053071
72 /**
73 * File name for typedef class file name suffix.
74 */
75 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
76
77 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053078 * File name for generated class file for special type like union, typedef suffix.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053079 */
80 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
81
82 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053083 * Integer index in type list.
84 */
85 private int intIndex = 0;
86
87 /**
88 * UInt index in type list.
89 */
90 private int uIntIndex = 0;
91
92 /**
93 * long index in type list.
94 */
95 private int longIndex = 0;
96
97 /**
98 * ULong index in type list.
99 */
100 private int uLongIndex = 0;
101
102 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530103 * Temporary file handle for of string method of class.
104 */
105 private File ofStringImplTempFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530106
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530107 /**
108 * Temporary file handle for constructor for type class.
109 */
110 private File constructorForTypeTempFileHandle;
111
112 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530113 * Java file handle for typedef class file.
114 */
115 private File typedefClassJavaFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530116
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530117 /**
118 * Java file handle for type class like union, typedef file.
119 */
120 private File typeClassJavaFileHandle;
121
122 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530123 * Java attribute for int.
124 */
125 private JavaAttributeInfo intAttribute;
126
127 /**
128 * Java attribute for long.
129 */
130 private JavaAttributeInfo longAttribute;
131
132 /**
133 * Java attribute for uInt.
134 */
135 private JavaAttributeInfo uIntAttribute;
136
137 /**
138 * Java attribute for uLong.
139 */
140 private JavaAttributeInfo uLongAttribute;
141
142 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530143 * Creates an instance of temporary java code fragment.
144 *
145 * @param javaFileInfo generated java file info
146 * @throws IOException when fails to create new file handle
147 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530148 TempJavaTypeFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530149 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530150
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530151 super(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530152
153 /*
154 * Initialize getterImpl, attributes, hash code, equals and to strings
155 * when generation file type matches to typeDef class mask.
156 */
157 addGeneratedTempFile(OF_STRING_IMPL_MASK);
158 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
159 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
160
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530161 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
162 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530163
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530164 }
165
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530166 /**
167 * Returns type class constructor method's temporary file handle.
168 *
169 * @return type class constructor method's temporary file handle
170 */
171
172 public File getConstructorForTypeTempFileHandle() {
173 return constructorForTypeTempFileHandle;
174 }
175
176 /**
177 * Sets type class constructor method's temporary file handle.
178 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530179 * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530180 */
181 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
182 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
183 }
184
185 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530186 * Returns java file handle for typedef class file.
187 *
188 * @return java file handle for typedef class file
189 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530190 private File getTypedefClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530191 return typedefClassJavaFileHandle;
192 }
193
194 /**
195 * Sets the java file handle for typedef class file.
196 *
197 * @param typedefClassJavaFileHandle java file handle
198 */
199 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
200 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
201 }
202
203 /**
204 * Returns java file handle for type class file.
205 *
206 * @return java file handle for type class file
207 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530208 private File getTypeClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530209 return typeClassJavaFileHandle;
210 }
211
212 /**
213 * Sets the java file handle for type class file.
214 *
215 * @param typeClassJavaFileHandle type file handle
216 */
217 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
218 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
219 }
220
221 /**
222 * Returns of string method's temporary file handle.
223 *
224 * @return of string method's temporary file handle
225 */
226
227 public File getOfStringImplTempFileHandle() {
228 return ofStringImplTempFileHandle;
229 }
230
231 /**
232 * Set of string method's temporary file handle.
233 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530234 * @param ofStringImplTempFileHandle of string method's temporary file handle
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530235 */
236 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
237 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
238 }
239
240 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530241 * Adds all the type in the current data model node as part of the generated temporary file.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530242 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530243 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
244 * @param pluginConfig plugin configurations for naming conventions
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530245 * @throws IOException IO operation fail
246 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530247 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530248 throws IOException {
249
250 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
251 if (typeList != null) {
252 for (YangType<?> yangType : typeList) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530253 if (!(yangType instanceof YangJavaTypeTranslator)) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530254 throw new TranslatorException("Type does not have Java info");
255 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530256 JavaAttributeInfo javaAttributeInfo = getAttributeForType(yangType, pluginConfig);
257 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo,
258 pluginConfig, typeList);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530259 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530260 addTypeConstructor(pluginConfig);
261 addMethodsInConflictCase(pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530262 }
263 }
264
265 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530266 * Returns java attribute.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530267 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530268 * @param yangType YANG type
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530269 * @param pluginConfig plugin configurations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530270 * @return java attribute
271 */
272 private JavaAttributeInfo getAttributeForType(YangType yangType, YangPluginConfig pluginConfig) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530273 YangJavaTypeTranslator<?> javaType = (YangJavaTypeTranslator<?>) yangType;
Bharat saraswal64e7e232016-07-14 23:33:55 +0530274 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
275 String typeName = javaType.getDataTypeName();
276 typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
277 return getAttributeInfoForTheData(
278 javaType.getJavaQualifiedInfo(),
279 typeName, javaType,
280 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
281 false);
282 }
283
284 /**
285 * Adds the new attribute info to the target generated temporary files for union class.
286 *
287 * @param javaAttributeInfo the attribute info that needs to be added to temporary files
288 * @param pluginConfig plugin configurations
289 * @param typeList type list
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530290 * @throws IOException IO operation fail
291 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530292 private void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfo,
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530293 YangPluginConfig pluginConfig, List<YangType<?>> typeList)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530294 throws IOException {
295
Bharat saraswal64e7e232016-07-14 23:33:55 +0530296 YangDataTypes attrType = javaAttributeInfo.getAttributeType().getDataType();
297
298 if (attrType == INT32 || attrType == UINT16) {
299 boolean isIntConflict = validateForConflictingIntTypes(typeList);
300 javaAttributeInfo.setIntConflict(isIntConflict);
301 updateAttributeCondition(javaAttributeInfo);
302 if (!isIntConflict) {
303 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
304 }
305 } else if (attrType == INT64 || attrType == UINT32) {
306 boolean isLongConflict = validateForConflictingLongTypes(typeList);
307 javaAttributeInfo.setLongConflict(isLongConflict);
308 updateAttributeCondition(javaAttributeInfo);
309 if (!isLongConflict) {
310 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
311 }
312 } else {
313 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
314 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530315 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530316
Bharat saraswal64e7e232016-07-14 23:33:55 +0530317 }
318
319 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530320 * Adds of method and constructor when there is no conflicting types.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530321 *
322 * @param javaAttributeInfo java attribute info
323 * @param pluginConfig plugin configurations
Shankara-Huaweib7564772016-08-02 18:13:13 +0530324 * @throws IOException when fails to do IO operations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530325 */
326 private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo,
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530327 YangPluginConfig pluginConfig)
328 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530329 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530330 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530331 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530332
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530333 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530334 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530335 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530336 }
337
338 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530339 * Adds of, getter and from string method in conflict cases.
340 *
341 * @param pluginConfig plugin configurations
342 * @throws IOException when fails to do IO operations
343 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530344 private void addMethodsInConflictCase(YangPluginConfig pluginConfig)
345 throws IOException {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530346 JavaAttributeInfo attr = getIntAttribute();
347 if (attr != null) {
348 attr = getUIntAttribute();
349 }
350 if (attr != null) {
351 if (attr.isIntConflict()) {
352 if (getIntIndex() < getUIntIndex()) {
353 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(),
354 getGeneratedJavaClassName(), pluginConfig)
355 + NEW_LINE);
356 addGetterImpl(getIntAttribute(), pluginConfig);
357 addFromStringMethod(getIntAttribute(), pluginConfig);
358 } else {
359 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(),
360 getGeneratedJavaClassName(), pluginConfig)
361 + NEW_LINE);
362 addGetterImpl(getUIntAttribute(), pluginConfig);
363 addFromStringMethod(getUIntAttribute(), pluginConfig);
364 }
365 }
366 }
367 attr = getLongAttribute();
368 if (attr != null) {
369 attr = getULongAttribute();
370 }
371 if (attr != null) {
372 if (attr.isLongConflict()) {
373 if (getLongIndex() < getULongIndex()) {
374 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(),
375 getGeneratedJavaClassName(), pluginConfig)
376 + NEW_LINE);
377 addGetterImpl(getLongAttribute(), pluginConfig);
378 addFromStringMethod(getLongAttribute(), pluginConfig);
379 } else {
380 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(),
381 getGeneratedJavaClassName(), pluginConfig)
382 + NEW_LINE);
383 addGetterImpl(getULongAttribute(), pluginConfig);
384 addFromStringMethod(getULongAttribute(), pluginConfig);
385 }
386 }
387 }
388 }
389
390 /**
391 * Adds from string method for conflict case.
392 *
393 * @param newAttrInfo new attribute
394 * @param pluginConfig plugin configurations
395 * @throws IOException when fails to do IO operations
396 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530397 private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
398 throws IOException {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530399
Shankara-Huaweib7564772016-08-02 18:13:13 +0530400 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +0530401 pluginConfig.getConflictResolver());
402 /*
403 * Create a new java attribute info with qualified information of
404 * wrapper classes.
405 */
406 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
407 newAttrInfo.getAttributeName(),
408 newAttrInfo.getAttributeType(),
409 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
410
411 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
412 }
413
414 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530415 * Adds type constructor.
416 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530417 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530418 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530419 * @throws IOException when fails to append to temporary file
420 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530421 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530422 throws IOException {
423 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530424 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530425 }
426
427 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530428 * Adds type constructor.
429 *
430 * @param pluginConfig plugin configurations
431 * @throws IOException when fails to append to temporary file
432 */
433 private void addTypeConstructor(YangPluginConfig pluginConfig)
434 throws IOException {
435 JavaAttributeInfo attr = getIntAttribute();
436 if (attr != null) {
437 attr = getUIntAttribute();
438 }
439 if (attr != null) {
440 if (attr.isIntConflict()) {
441 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
442 getIntAttribute(),
443 getUIntAttribute(), getGeneratedJavaClassName(), pluginConfig, INT_TYPE_CONFLICT,
444 getIntIndex()
445 < getUIntIndex()) + NEW_LINE);
446 }
447 }
448 attr = getLongAttribute();
449 if (attr != null) {
450 attr = getULongAttribute();
451 }
452 if (attr != null) {
453 if (attr.isLongConflict()) {
454 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
455 getLongAttribute(),
456 getULongAttribute(), getGeneratedJavaClassName(), pluginConfig, LONG_TYPE_CONFLICT,
457 getLongIndex()
458 < getULongIndex()) + NEW_LINE);
459 }
460 }
461 }
462
463 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530464 * Adds of string for type.
465 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530466 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530467 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530468 * @throws IOException when fails to append to temporary file
469 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530470 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530471 throws IOException {
472 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530473 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530474 + NEW_LINE);
475 }
476
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530477 /**
478 * Removes all temporary file handles.
479 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530480 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530481 * @throws IOException when failed to delete the temporary files
482 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530483 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530484 public void freeTemporaryResources(boolean isErrorOccurred)
485 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530486
487 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530488 closeFile(getTypedefClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530489 }
490
491 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530492 closeFile(getTypeClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530493 }
494
495 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
496 closeFile(getConstructorForTypeTempFileHandle(), true);
497 }
498 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
499 closeFile(getOfStringImplTempFileHandle(), true);
500 }
501 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
502 closeFile(getFromStringImplTempFileHandle(), true);
503 }
504
505 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswal250a7472016-05-12 13:16:57 +0530506
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530507 }
508
509 /**
510 * Constructs java code exit.
511 *
512 * @param fileType generated file type
Bharat saraswal64e7e232016-07-14 23:33:55 +0530513 * @param curNode current YANG node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530514 * @throws IOException when fails to generate java files
515 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530516 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530517 public void generateJavaFile(int fileType, YangNode curNode)
518 throws IOException {
519 List<String> imports = new ArrayList<>();
520 if (isAttributePresent()) {
521 imports = getJavaImportData().getImports();
522 }
523
524 createPackage(curNode);
525
526 /*
527 * Creates type def class file.
528 */
529 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530530 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530531 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
Gaurav Agrawal8a147522016-08-10 13:43:01 +0530532
533 // In case if data type is binary, MoreObjects is not required and should be removed.
534 if (curNode instanceof YangTypeHolder) {
535 YangType yangType = ((YangTypeHolder) curNode).getTypeList().get(0);
536 if (yangType.getDataType() == YangDataTypes.BINARY) {
537 imports.remove(getJavaImportData().getImportForToString());
538 }
539 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530540 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
541 }
542 /*
543 * Creates type class file.
544 */
545 if ((fileType & GENERATE_UNION_CLASS) != 0) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530546 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530547 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
548 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
549 }
550
551 /*
552 * Close all the file handles.
553 */
554 freeTemporaryResources(false);
555 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530556
557 /**
558 * Returns int type index from type list.
559 *
560 * @return int type index from type list
561 */
562 public int getIntIndex() {
563 return intIndex;
564 }
565
566 /**
567 * Sets int type index from type list.
568 *
569 * @param intIndex int type index from type list.
570 */
571 private void setIntIndex(int intIndex) {
572 this.intIndex = intIndex;
573 }
574
575 /**
576 * Returns uInt type index from type list.
577 *
578 * @return uInt type index from type list
579 */
580 public int getUIntIndex() {
581 return uIntIndex;
582 }
583
584 /**
585 * Sets uInt type index from type list.
586 *
587 * @param uIntIndex uInt type index from type list.
588 */
589 private void setUIntIndex(int uIntIndex) {
590 this.uIntIndex = uIntIndex;
591 }
592
593 /**
594 * Returns long type index from type list.
595 *
596 * @return long type index from type list
597 */
598 public int getLongIndex() {
599 return longIndex;
600 }
601
602 /**
603 * Sets long type index from type list.
604 *
605 * @param longIndex long type index from type list.
606 */
607 private void setLongIndex(int longIndex) {
608 this.longIndex = longIndex;
609 }
610
611 /**
612 * Returns uLong type index from type list.
613 *
614 * @return uLong type index from type list
615 */
616 public int getULongIndex() {
617 return uLongIndex;
618 }
619
620 /**
621 * Sets uLong type index from type list.
622 *
623 * @param uLongIndex uLong type index from type list.
624 */
625 private void setULongIndex(int uLongIndex) {
626 this.uLongIndex = uLongIndex;
627 }
628
629 /**
630 * Validates conflict for int and uInt.
631 *
632 * @param typeList type list
633 * @return true if conflict is there
634 */
635 private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) {
636 boolean isIntPresent = false;
637 boolean isUIntPresent = false;
638 for (YangType type : typeList) {
639 if (type.getDataType().equals(INT32)) {
640 setIntIndex(typeList.indexOf(type));
641 isIntPresent = true;
642 }
643 if (type.getDataType().equals(UINT16)) {
644 setUIntIndex(typeList.indexOf(type));
645 isUIntPresent = true;
646 }
647 }
648
649 return isIntPresent && isUIntPresent;
650 }
651
652 /**
653 * Validates conflict for long and uLong.
654 *
655 * @param typeList type list
656 * @return true if conflict is there
657 */
658 private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) {
659 boolean isLongPresent = false;
660 boolean isULongPresent = false;
661 for (YangType type : typeList) {
662 if (type.getDataType().equals(INT64)) {
663 setLongIndex(typeList.indexOf(type));
664 isLongPresent = true;
665 }
666 if (type.getDataType().equals(UINT32)) {
667 setULongIndex(typeList.indexOf(type));
668 isULongPresent = true;
669 }
670 }
671
672 return isLongPresent && isULongPresent;
673 }
674
675 /**
676 * Updates attribute info in case of conflicts.
677 *
678 * @param javaAttributeInfo java attribute info
679 */
680 private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) {
681
682 if (javaAttributeInfo.isIntConflict()) {
683 if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) {
684 setUIntAttribute(javaAttributeInfo);
685 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) {
686 setIntAttribute(javaAttributeInfo);
687 }
688
689 }
690 if (javaAttributeInfo.isLongConflict()) {
691 if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) {
692 setULongAttribute(javaAttributeInfo);
693 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) {
694 setLongAttribute(javaAttributeInfo);
695 }
696
697 }
698 }
699
700 /**
701 * Returns attribute for int.
702 *
703 * @return attribute for int
704 */
705 public JavaAttributeInfo getIntAttribute() {
706 return intAttribute;
707 }
708
709 /**
710 * Sets attribute for int.
711 *
712 * @param intAttribute attribute for int
713 */
714 private void setIntAttribute(JavaAttributeInfo intAttribute) {
715 this.intAttribute = intAttribute;
716 }
717
718 /**
719 * Returns attribute for long.
720 *
721 * @return attribute for long
722 */
723 public JavaAttributeInfo getLongAttribute() {
724 return longAttribute;
725 }
726
727 /**
728 * Sets attribute for long.
729 *
730 * @param longAttribute attribute for long
731 */
732 private void setLongAttribute(JavaAttributeInfo longAttribute) {
733 this.longAttribute = longAttribute;
734 }
735
736 /**
737 * Returns attribute for uInt.
738 *
739 * @return attribute for uInt
740 */
741 public JavaAttributeInfo getUIntAttribute() {
742 return uIntAttribute;
743 }
744
745 /**
746 * Sets attribute for uInt.
747 *
748 * @param uIntAttribute attribute for uInt
749 */
750 private void setUIntAttribute(JavaAttributeInfo uIntAttribute) {
751 this.uIntAttribute = uIntAttribute;
752 }
753
754 /**
755 * Returns attribute for uLong.
756 *
757 * @return attribute for uLong
758 */
759 public JavaAttributeInfo getULongAttribute() {
760 return uLongAttribute;
761 }
762
763 /**
764 * Sets attribute for uLong.
765 *
766 * @param uLongAttribute attribute for uLong
767 */
768 private void setULongAttribute(JavaAttributeInfo uLongAttribute) {
769 this.uLongAttribute = uLongAttribute;
770 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530771}