blob: e6a5379f6b89c7840f8b14416f3a1156f5a9be95 [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-Huawei6266db32016-05-10 17:58:57 +053023import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.YangType;
25import org.onosproject.yangutils.datamodel.YangTypeHolder;
Bharat saraswal64e7e232016-07-14 23:33:55 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053028import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053029import org.onosproject.yangutils.utils.io.YangPluginConfig;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053030
Bharat saraswal64e7e232016-07-14 23:33:55 +053031import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
32import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
33import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16;
34import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053035import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
36import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
37import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
38import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
39import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
40import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053041import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053042import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
43import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswal64e7e232016-07-14 23:33:55 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053045import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
46import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswal64e7e232016-07-14 23:33:55 +053047import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT;
48import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053049import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
50import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal64e7e232016-07-14 23:33:55 +053051import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
52import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053053
54/**
Bharat saraswal64e7e232016-07-14 23:33:55 +053055 * Represents implementation of java data type code fragments temporary implementations. Maintains the temp files
56 * required specific for user defined data type java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053057 */
58public class TempJavaTypeFragmentFiles
59 extends TempJavaFragmentFiles {
60
61 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053062 * File name for of string method.
63 */
64 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
65
66 /**
67 * File name for construction for special type like union, typedef.
68 */
69 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053070
71 /**
72 * File name for typedef class file name suffix.
73 */
74 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
75
76 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053077 * File name for generated class file for special type like union, typedef suffix.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053078 */
79 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
80
81 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053082 * Integer index in type list.
83 */
84 private int intIndex = 0;
85
86 /**
87 * UInt index in type list.
88 */
89 private int uIntIndex = 0;
90
91 /**
92 * long index in type list.
93 */
94 private int longIndex = 0;
95
96 /**
97 * ULong index in type list.
98 */
99 private int uLongIndex = 0;
100
101 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530102 * Temporary file handle for of string method of class.
103 */
104 private File ofStringImplTempFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530105
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530106 /**
107 * Temporary file handle for constructor for type class.
108 */
109 private File constructorForTypeTempFileHandle;
110
111 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530112 * Java file handle for typedef class file.
113 */
114 private File typedefClassJavaFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530115
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530116 /**
117 * Java file handle for type class like union, typedef file.
118 */
119 private File typeClassJavaFileHandle;
120
121 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530122 * Java attribute for int.
123 */
124 private JavaAttributeInfo intAttribute;
125
126 /**
127 * Java attribute for long.
128 */
129 private JavaAttributeInfo longAttribute;
130
131 /**
132 * Java attribute for uInt.
133 */
134 private JavaAttributeInfo uIntAttribute;
135
136 /**
137 * Java attribute for uLong.
138 */
139 private JavaAttributeInfo uLongAttribute;
140
141 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530142 * Creates an instance of temporary java code fragment.
143 *
144 * @param javaFileInfo generated java file info
145 * @throws IOException when fails to create new file handle
146 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530147 TempJavaTypeFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530148 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530149
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530150 super(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530151
152 /*
153 * Initialize getterImpl, attributes, hash code, equals and to strings
154 * when generation file type matches to typeDef class mask.
155 */
156 addGeneratedTempFile(OF_STRING_IMPL_MASK);
157 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
158 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
159
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530160 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
161 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530162
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530163 }
164
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530165 /**
166 * Returns type class constructor method's temporary file handle.
167 *
168 * @return type class constructor method's temporary file handle
169 */
170
171 public File getConstructorForTypeTempFileHandle() {
172 return constructorForTypeTempFileHandle;
173 }
174
175 /**
176 * Sets type class constructor method's temporary file handle.
177 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530178 * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530179 */
180 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
181 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
182 }
183
184 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530185 * Returns java file handle for typedef class file.
186 *
187 * @return java file handle for typedef class file
188 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530189 private File getTypedefClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530190 return typedefClassJavaFileHandle;
191 }
192
193 /**
194 * Sets the java file handle for typedef class file.
195 *
196 * @param typedefClassJavaFileHandle java file handle
197 */
198 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
199 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
200 }
201
202 /**
203 * Returns java file handle for type class file.
204 *
205 * @return java file handle for type class file
206 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530207 private File getTypeClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530208 return typeClassJavaFileHandle;
209 }
210
211 /**
212 * Sets the java file handle for type class file.
213 *
214 * @param typeClassJavaFileHandle type file handle
215 */
216 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
217 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
218 }
219
220 /**
221 * Returns of string method's temporary file handle.
222 *
223 * @return of string method's temporary file handle
224 */
225
226 public File getOfStringImplTempFileHandle() {
227 return ofStringImplTempFileHandle;
228 }
229
230 /**
231 * Set of string method's temporary file handle.
232 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530233 * @param ofStringImplTempFileHandle of string method's temporary file handle
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530234 */
235 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
236 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
237 }
238
239 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530240 * Adds all the type in the current data model node as part of the generated temporary file.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530241 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530242 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
243 * @param pluginConfig plugin configurations for naming conventions
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530244 * @throws IOException IO operation fail
245 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530246 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530247 throws IOException {
248
249 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
250 if (typeList != null) {
251 for (YangType<?> yangType : typeList) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530252 if (!(yangType instanceof YangJavaTypeTranslator)) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530253 throw new TranslatorException("Type does not have Java info");
254 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530255 JavaAttributeInfo javaAttributeInfo = getAttributeForType(yangType, pluginConfig);
256 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo,
257 pluginConfig, typeList);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530258 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530259 addTypeConstructor(pluginConfig);
260 addMethodsInConflictCase(pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530261 }
262 }
263
264 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530265 * Returns java attribute.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530266 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530267 * @param yangType YANG type
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530268 * @param pluginConfig plugin configurations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530269 * @return java attribute
270 */
271 private JavaAttributeInfo getAttributeForType(YangType yangType, YangPluginConfig pluginConfig) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530272 YangJavaTypeTranslator<?> javaType = (YangJavaTypeTranslator<?>) yangType;
Bharat saraswal64e7e232016-07-14 23:33:55 +0530273 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
274 String typeName = javaType.getDataTypeName();
275 typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
276 return getAttributeInfoForTheData(
277 javaType.getJavaQualifiedInfo(),
278 typeName, javaType,
279 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
280 false);
281 }
282
283 /**
284 * Adds the new attribute info to the target generated temporary files for union class.
285 *
286 * @param javaAttributeInfo the attribute info that needs to be added to temporary files
287 * @param pluginConfig plugin configurations
288 * @param typeList type list
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530289 * @throws IOException IO operation fail
290 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530291 private void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfo,
292 YangPluginConfig pluginConfig, List<YangType<?>> typeList)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530293 throws IOException {
294
Bharat saraswal64e7e232016-07-14 23:33:55 +0530295 YangDataTypes attrType = javaAttributeInfo.getAttributeType().getDataType();
296
297 if (attrType == INT32 || attrType == UINT16) {
298 boolean isIntConflict = validateForConflictingIntTypes(typeList);
299 javaAttributeInfo.setIntConflict(isIntConflict);
300 updateAttributeCondition(javaAttributeInfo);
301 if (!isIntConflict) {
302 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
303 }
304 } else if (attrType == INT64 || attrType == UINT32) {
305 boolean isLongConflict = validateForConflictingLongTypes(typeList);
306 javaAttributeInfo.setLongConflict(isLongConflict);
307 updateAttributeCondition(javaAttributeInfo);
308 if (!isLongConflict) {
309 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
310 }
311 } else {
312 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
313 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530314 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530315
Bharat saraswal64e7e232016-07-14 23:33:55 +0530316 }
317
318 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530319 * Adds of method and constructor when there is no conflicting types.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530320 *
321 * @param javaAttributeInfo java attribute info
322 * @param pluginConfig plugin configurations
Shankara-Huaweib7564772016-08-02 18:13:13 +0530323 * @throws IOException when fails to do IO operations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530324 */
325 private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo,
326 YangPluginConfig pluginConfig) throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530327 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530328 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530329 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530330
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530331 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530332 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530333 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530334 }
335
336 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530337 * Adds of, getter and from string method in conflict cases.
338 *
339 * @param pluginConfig plugin configurations
340 * @throws IOException when fails to do IO operations
341 */
342 private void addMethodsInConflictCase(YangPluginConfig pluginConfig) throws IOException {
343 JavaAttributeInfo attr = getIntAttribute();
344 if (attr != null) {
345 attr = getUIntAttribute();
346 }
347 if (attr != null) {
348 if (attr.isIntConflict()) {
349 if (getIntIndex() < getUIntIndex()) {
350 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(),
351 getGeneratedJavaClassName(), pluginConfig)
352 + NEW_LINE);
353 addGetterImpl(getIntAttribute(), pluginConfig);
354 addFromStringMethod(getIntAttribute(), pluginConfig);
355 } else {
356 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(),
357 getGeneratedJavaClassName(), pluginConfig)
358 + NEW_LINE);
359 addGetterImpl(getUIntAttribute(), pluginConfig);
360 addFromStringMethod(getUIntAttribute(), pluginConfig);
361 }
362 }
363 }
364 attr = getLongAttribute();
365 if (attr != null) {
366 attr = getULongAttribute();
367 }
368 if (attr != null) {
369 if (attr.isLongConflict()) {
370 if (getLongIndex() < getULongIndex()) {
371 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(),
372 getGeneratedJavaClassName(), pluginConfig)
373 + NEW_LINE);
374 addGetterImpl(getLongAttribute(), pluginConfig);
375 addFromStringMethod(getLongAttribute(), pluginConfig);
376 } else {
377 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(),
378 getGeneratedJavaClassName(), pluginConfig)
379 + NEW_LINE);
380 addGetterImpl(getULongAttribute(), pluginConfig);
381 addFromStringMethod(getULongAttribute(), pluginConfig);
382 }
383 }
384 }
385 }
386
387 /**
388 * Adds from string method for conflict case.
389 *
390 * @param newAttrInfo new attribute
391 * @param pluginConfig plugin configurations
392 * @throws IOException when fails to do IO operations
393 */
394 private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig) throws IOException {
395
Shankara-Huaweib7564772016-08-02 18:13:13 +0530396 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswal64e7e232016-07-14 23:33:55 +0530397 pluginConfig.getConflictResolver());
398 /*
399 * Create a new java attribute info with qualified information of
400 * wrapper classes.
401 */
402 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
403 newAttrInfo.getAttributeName(),
404 newAttrInfo.getAttributeType(),
405 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
406
407 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
408 }
409
410 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530411 * Adds type constructor.
412 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530413 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530414 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530415 * @throws IOException when fails to append to temporary file
416 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530417 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530418 throws IOException {
419 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530420 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530421 }
422
423 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530424 * Adds type constructor.
425 *
426 * @param pluginConfig plugin configurations
427 * @throws IOException when fails to append to temporary file
428 */
429 private void addTypeConstructor(YangPluginConfig pluginConfig)
430 throws IOException {
431 JavaAttributeInfo attr = getIntAttribute();
432 if (attr != null) {
433 attr = getUIntAttribute();
434 }
435 if (attr != null) {
436 if (attr.isIntConflict()) {
437 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
438 getIntAttribute(),
439 getUIntAttribute(), getGeneratedJavaClassName(), pluginConfig, INT_TYPE_CONFLICT,
440 getIntIndex()
441 < getUIntIndex()) + NEW_LINE);
442 }
443 }
444 attr = getLongAttribute();
445 if (attr != null) {
446 attr = getULongAttribute();
447 }
448 if (attr != null) {
449 if (attr.isLongConflict()) {
450 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
451 getLongAttribute(),
452 getULongAttribute(), getGeneratedJavaClassName(), pluginConfig, LONG_TYPE_CONFLICT,
453 getLongIndex()
454 < getULongIndex()) + NEW_LINE);
455 }
456 }
457 }
458
459 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530460 * Adds of string for type.
461 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530462 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530463 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530464 * @throws IOException when fails to append to temporary file
465 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530466 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530467 throws IOException {
468 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530469 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530470 + NEW_LINE);
471 }
472
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530473 /**
474 * Removes all temporary file handles.
475 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530476 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530477 * @throws IOException when failed to delete the temporary files
478 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530479 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530480 public void freeTemporaryResources(boolean isErrorOccurred)
481 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530482
483 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530484 closeFile(getTypedefClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530485 }
486
487 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530488 closeFile(getTypeClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530489 }
490
491 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
492 closeFile(getConstructorForTypeTempFileHandle(), true);
493 }
494 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
495 closeFile(getOfStringImplTempFileHandle(), true);
496 }
497 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
498 closeFile(getFromStringImplTempFileHandle(), true);
499 }
500
501 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswal250a7472016-05-12 13:16:57 +0530502
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530503 }
504
505 /**
506 * Constructs java code exit.
507 *
508 * @param fileType generated file type
Bharat saraswal64e7e232016-07-14 23:33:55 +0530509 * @param curNode current YANG node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530510 * @throws IOException when fails to generate java files
511 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530512 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530513 public void generateJavaFile(int fileType, YangNode curNode)
514 throws IOException {
515 List<String> imports = new ArrayList<>();
516 if (isAttributePresent()) {
517 imports = getJavaImportData().getImports();
518 }
519
520 createPackage(curNode);
521
522 /*
523 * Creates type def class file.
524 */
525 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530526 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530527 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
Gaurav Agrawal8a147522016-08-10 13:43:01 +0530528
529 // In case if data type is binary, MoreObjects is not required and should be removed.
530 if (curNode instanceof YangTypeHolder) {
531 YangType yangType = ((YangTypeHolder) curNode).getTypeList().get(0);
532 if (yangType.getDataType() == YangDataTypes.BINARY) {
533 imports.remove(getJavaImportData().getImportForToString());
534 }
535 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530536 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
537 }
538 /*
539 * Creates type class file.
540 */
541 if ((fileType & GENERATE_UNION_CLASS) != 0) {
Shankara-Huaweia1039e52016-07-14 16:53:09 +0530542 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530543 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
544 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
545 }
546
547 /*
548 * Close all the file handles.
549 */
550 freeTemporaryResources(false);
551 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530552
553 /**
554 * Returns int type index from type list.
555 *
556 * @return int type index from type list
557 */
558 public int getIntIndex() {
559 return intIndex;
560 }
561
562 /**
563 * Sets int type index from type list.
564 *
565 * @param intIndex int type index from type list.
566 */
567 private void setIntIndex(int intIndex) {
568 this.intIndex = intIndex;
569 }
570
571 /**
572 * Returns uInt type index from type list.
573 *
574 * @return uInt type index from type list
575 */
576 public int getUIntIndex() {
577 return uIntIndex;
578 }
579
580 /**
581 * Sets uInt type index from type list.
582 *
583 * @param uIntIndex uInt type index from type list.
584 */
585 private void setUIntIndex(int uIntIndex) {
586 this.uIntIndex = uIntIndex;
587 }
588
589 /**
590 * Returns long type index from type list.
591 *
592 * @return long type index from type list
593 */
594 public int getLongIndex() {
595 return longIndex;
596 }
597
598 /**
599 * Sets long type index from type list.
600 *
601 * @param longIndex long type index from type list.
602 */
603 private void setLongIndex(int longIndex) {
604 this.longIndex = longIndex;
605 }
606
607 /**
608 * Returns uLong type index from type list.
609 *
610 * @return uLong type index from type list
611 */
612 public int getULongIndex() {
613 return uLongIndex;
614 }
615
616 /**
617 * Sets uLong type index from type list.
618 *
619 * @param uLongIndex uLong type index from type list.
620 */
621 private void setULongIndex(int uLongIndex) {
622 this.uLongIndex = uLongIndex;
623 }
624
625 /**
626 * Validates conflict for int and uInt.
627 *
628 * @param typeList type list
629 * @return true if conflict is there
630 */
631 private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) {
632 boolean isIntPresent = false;
633 boolean isUIntPresent = false;
634 for (YangType type : typeList) {
635 if (type.getDataType().equals(INT32)) {
636 setIntIndex(typeList.indexOf(type));
637 isIntPresent = true;
638 }
639 if (type.getDataType().equals(UINT16)) {
640 setUIntIndex(typeList.indexOf(type));
641 isUIntPresent = true;
642 }
643 }
644
645 return isIntPresent && isUIntPresent;
646 }
647
648 /**
649 * Validates conflict for long and uLong.
650 *
651 * @param typeList type list
652 * @return true if conflict is there
653 */
654 private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) {
655 boolean isLongPresent = false;
656 boolean isULongPresent = false;
657 for (YangType type : typeList) {
658 if (type.getDataType().equals(INT64)) {
659 setLongIndex(typeList.indexOf(type));
660 isLongPresent = true;
661 }
662 if (type.getDataType().equals(UINT32)) {
663 setULongIndex(typeList.indexOf(type));
664 isULongPresent = true;
665 }
666 }
667
668 return isLongPresent && isULongPresent;
669 }
670
671 /**
672 * Updates attribute info in case of conflicts.
673 *
674 * @param javaAttributeInfo java attribute info
675 */
676 private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) {
677
678 if (javaAttributeInfo.isIntConflict()) {
679 if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) {
680 setUIntAttribute(javaAttributeInfo);
681 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) {
682 setIntAttribute(javaAttributeInfo);
683 }
684
685 }
686 if (javaAttributeInfo.isLongConflict()) {
687 if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) {
688 setULongAttribute(javaAttributeInfo);
689 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) {
690 setLongAttribute(javaAttributeInfo);
691 }
692
693 }
694 }
695
696 /**
697 * Returns attribute for int.
698 *
699 * @return attribute for int
700 */
701 public JavaAttributeInfo getIntAttribute() {
702 return intAttribute;
703 }
704
705 /**
706 * Sets attribute for int.
707 *
708 * @param intAttribute attribute for int
709 */
710 private void setIntAttribute(JavaAttributeInfo intAttribute) {
711 this.intAttribute = intAttribute;
712 }
713
714 /**
715 * Returns attribute for long.
716 *
717 * @return attribute for long
718 */
719 public JavaAttributeInfo getLongAttribute() {
720 return longAttribute;
721 }
722
723 /**
724 * Sets attribute for long.
725 *
726 * @param longAttribute attribute for long
727 */
728 private void setLongAttribute(JavaAttributeInfo longAttribute) {
729 this.longAttribute = longAttribute;
730 }
731
732 /**
733 * Returns attribute for uInt.
734 *
735 * @return attribute for uInt
736 */
737 public JavaAttributeInfo getUIntAttribute() {
738 return uIntAttribute;
739 }
740
741 /**
742 * Sets attribute for uInt.
743 *
744 * @param uIntAttribute attribute for uInt
745 */
746 private void setUIntAttribute(JavaAttributeInfo uIntAttribute) {
747 this.uIntAttribute = uIntAttribute;
748 }
749
750 /**
751 * Returns attribute for uLong.
752 *
753 * @return attribute for uLong
754 */
755 public JavaAttributeInfo getULongAttribute() {
756 return uLongAttribute;
757 }
758
759 /**
760 * Sets attribute for uLong.
761 *
762 * @param uLongAttribute attribute for uLong
763 */
764 private void setULongAttribute(JavaAttributeInfo uLongAttribute) {
765 this.uLongAttribute = uLongAttribute;
766 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530767}