blob: 8e05be5f4dc661c6cd45dc9c0d4bdcac893d37ea [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-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-Huaweicb3a1f52016-05-10 17:58:57 +053019import java.io.File;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020import java.io.IOException;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053021import java.util.ArrayList;
22import java.util.List;
23
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.datamodel.YangType;
26import org.onosproject.yangutils.datamodel.YangTypeHolder;
Bharat saraswale707f032016-07-14 23:33:55 +053027import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import org.onosproject.yangutils.translator.exception.TranslatorException;
29import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053030import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031
Bharat saraswale707f032016-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-Huaweicb3a1f52016-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;
Bharat saraswale707f032016-07-14 23:33:55 +053042import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
VinodKumarS-Huaweicb3a1f52016-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 saraswale707f032016-07-14 23:33:55 +053045import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huaweicb3a1f52016-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 saraswale707f032016-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-Huaweicb3a1f52016-05-10 17:58:57 +053050import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
51import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswale707f032016-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 Se4b9b0c2016-04-30 21:09:15 +053054
55/**
Bharat saraswale707f032016-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 Se4b9b0c2016-04-30 21:09:15 +053058 */
59public class TempJavaTypeFragmentFiles
60 extends TempJavaFragmentFiles {
61
62 /**
VinodKumarS-Huaweicb3a1f52016-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-Huaweicb3a1f52016-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 saraswale707f032016-07-14 23:33:55 +053078 * File name for generated class file for special type like union, typedef suffix.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053079 */
80 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
81
82 /**
Bharat saraswale707f032016-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-Huaweicb3a1f52016-05-10 17:58:57 +0530103 * Temporary file handle for of string method of class.
104 */
105 private File ofStringImplTempFileHandle;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530106
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530107 /**
108 * Temporary file handle for constructor for type class.
109 */
110 private File constructorForTypeTempFileHandle;
111
112 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530113 * Java file handle for typedef class file.
114 */
115 private File typedefClassJavaFileHandle;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530116
VinodKumarS-Huaweicb3a1f52016-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 saraswale707f032016-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 Se4b9b0c2016-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 saraswale707f032016-07-14 23:33:55 +0530148 TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530149 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530150
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530151 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-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-Huaweicb3a1f52016-05-10 17:58:57 +0530161 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
162 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530163
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530164 }
165
VinodKumarS-Huaweicb3a1f52016-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 saraswale707f032016-07-14 23:33:55 +0530179 * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530180 */
181 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
182 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
183 }
184
185 /**
VinodKumarS-Huaweicb3a1f52016-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 saraswale707f032016-07-14 23:33:55 +0530190 private File getTypedefClassJavaFileHandle() {
VinodKumarS-Huaweicb3a1f52016-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 saraswale707f032016-07-14 23:33:55 +0530208 private File getTypeClassJavaFileHandle() {
VinodKumarS-Huaweicb3a1f52016-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 saraswale707f032016-07-14 23:33:55 +0530234 * @param ofStringImplTempFileHandle of string method's temporary file handle
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530235 */
236 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
237 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
238 }
239
240 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530241 * Adds all the type in the current data model node as part of the generated temporary file.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530242 *
Bharat saraswale707f032016-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-Huaweicb3a1f52016-05-10 17:58:57 +0530245 * @throws IOException IO operation fail
246 */
Bharat saraswale707f032016-07-14 23:33:55 +0530247 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530248 throws IOException {
249
250 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
251 if (typeList != null) {
252 for (YangType<?> yangType : typeList) {
253 if (!(yangType instanceof YangJavaType)) {
254 throw new TranslatorException("Type does not have Java info");
255 }
Bharat saraswale707f032016-07-14 23:33:55 +0530256 JavaAttributeInfo javaAttributeInfo = getAttributeForType(yangType, pluginConfig);
257 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo,
258 pluginConfig, typeList);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530259 }
Bharat saraswale707f032016-07-14 23:33:55 +0530260 addTypeConstructor(pluginConfig);
261 addMethodsInConflictCase(pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530262 }
263 }
264
265 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530266 * Returns java attribute.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530267 *
Bharat saraswale707f032016-07-14 23:33:55 +0530268 * @param yangType YANG type
Bharat saraswal33dfa012016-05-17 19:59:16 +0530269 * @param pluginConfig plugin configurations
Bharat saraswale707f032016-07-14 23:33:55 +0530270 * @return java attribute
271 */
272 private JavaAttributeInfo getAttributeForType(YangType yangType, YangPluginConfig pluginConfig) {
273 YangJavaType<?> javaType = (YangJavaType<?>) yangType;
274 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-Huaweicb3a1f52016-05-10 17:58:57 +0530290 * @throws IOException IO operation fail
291 */
Bharat saraswale707f032016-07-14 23:33:55 +0530292 private void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfo,
293 YangPluginConfig pluginConfig, List<YangType<?>> typeList)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530294 throws IOException {
295
Bharat saraswale707f032016-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 saraswal33dfa012016-05-17 19:59:16 +0530315 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530316
Bharat saraswale707f032016-07-14 23:33:55 +0530317 }
318
319 /**
320 * Adds of method and constructor when there is no conflictiing types.
321 *
322 * @param javaAttributeInfo java attribute info
323 * @param pluginConfig plugin configurations
324 * @throws IOException when fails to do IO opearions
325 */
326 private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo,
327 YangPluginConfig pluginConfig) throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530328 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530329 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530330 }
Bharat saraswale707f032016-07-14 23:33:55 +0530331
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530332 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530333 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530334 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530335 }
336
337 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530338 * Adds of, getter and from string method in conflict cases.
339 *
340 * @param pluginConfig plugin configurations
341 * @throws IOException when fails to do IO operations
342 */
343 private void addMethodsInConflictCase(YangPluginConfig pluginConfig) throws IOException {
344 JavaAttributeInfo attr = getIntAttribute();
345 if (attr != null) {
346 attr = getUIntAttribute();
347 }
348 if (attr != null) {
349 if (attr.isIntConflict()) {
350 if (getIntIndex() < getUIntIndex()) {
351 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(),
352 getGeneratedJavaClassName(), pluginConfig)
353 + NEW_LINE);
354 addGetterImpl(getIntAttribute(), pluginConfig);
355 addFromStringMethod(getIntAttribute(), pluginConfig);
356 } else {
357 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(),
358 getGeneratedJavaClassName(), pluginConfig)
359 + NEW_LINE);
360 addGetterImpl(getUIntAttribute(), pluginConfig);
361 addFromStringMethod(getUIntAttribute(), pluginConfig);
362 }
363 }
364 }
365 attr = getLongAttribute();
366 if (attr != null) {
367 attr = getULongAttribute();
368 }
369 if (attr != null) {
370 if (attr.isLongConflict()) {
371 if (getLongIndex() < getULongIndex()) {
372 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(),
373 getGeneratedJavaClassName(), pluginConfig)
374 + NEW_LINE);
375 addGetterImpl(getLongAttribute(), pluginConfig);
376 addFromStringMethod(getLongAttribute(), pluginConfig);
377 } else {
378 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(),
379 getGeneratedJavaClassName(), pluginConfig)
380 + NEW_LINE);
381 addGetterImpl(getULongAttribute(), pluginConfig);
382 addFromStringMethod(getULongAttribute(), pluginConfig);
383 }
384 }
385 }
386 }
387
388 /**
389 * Adds from string method for conflict case.
390 *
391 * @param newAttrInfo new attribute
392 * @param pluginConfig plugin configurations
393 * @throws IOException when fails to do IO operations
394 */
395 private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig) throws IOException {
396
397 JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
398 pluginConfig.getConflictResolver());
399 /*
400 * Create a new java attribute info with qualified information of
401 * wrapper classes.
402 */
403 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
404 newAttrInfo.getAttributeName(),
405 newAttrInfo.getAttributeType(),
406 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
407
408 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
409 }
410
411 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530412 * Adds type constructor.
413 *
Bharat saraswale707f032016-07-14 23:33:55 +0530414 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530415 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530416 * @throws IOException when fails to append to temporary file
417 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530418 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530419 throws IOException {
420 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530421 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530422 }
423
424 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530425 * Adds type constructor.
426 *
427 * @param pluginConfig plugin configurations
428 * @throws IOException when fails to append to temporary file
429 */
430 private void addTypeConstructor(YangPluginConfig pluginConfig)
431 throws IOException {
432 JavaAttributeInfo attr = getIntAttribute();
433 if (attr != null) {
434 attr = getUIntAttribute();
435 }
436 if (attr != null) {
437 if (attr.isIntConflict()) {
438 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
439 getIntAttribute(),
440 getUIntAttribute(), getGeneratedJavaClassName(), pluginConfig, INT_TYPE_CONFLICT,
441 getIntIndex()
442 < getUIntIndex()) + NEW_LINE);
443 }
444 }
445 attr = getLongAttribute();
446 if (attr != null) {
447 attr = getULongAttribute();
448 }
449 if (attr != null) {
450 if (attr.isLongConflict()) {
451 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
452 getLongAttribute(),
453 getULongAttribute(), getGeneratedJavaClassName(), pluginConfig, LONG_TYPE_CONFLICT,
454 getLongIndex()
455 < getULongIndex()) + NEW_LINE);
456 }
457 }
458 }
459
460 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530461 * Adds of string for type.
462 *
Bharat saraswale707f032016-07-14 23:33:55 +0530463 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530464 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530465 * @throws IOException when fails to append to temporary file
466 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530467 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530468 throws IOException {
469 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530470 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530471 + NEW_LINE);
472 }
473
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530474 /**
475 * Removes all temporary file handles.
476 *
Bharat saraswale707f032016-07-14 23:33:55 +0530477 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530478 * @throws IOException when failed to delete the temporary files
479 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530480 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530481 public void freeTemporaryResources(boolean isErrorOccurred)
482 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530483
484 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswale707f032016-07-14 23:33:55 +0530485 closeFile(getTypedefClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530486 }
487
488 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
Bharat saraswale707f032016-07-14 23:33:55 +0530489 closeFile(getTypeClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530490 }
491
492 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
493 closeFile(getConstructorForTypeTempFileHandle(), true);
494 }
495 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
496 closeFile(getOfStringImplTempFileHandle(), true);
497 }
498 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
499 closeFile(getFromStringImplTempFileHandle(), true);
500 }
501
502 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530503
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530504 }
505
506 /**
507 * Constructs java code exit.
508 *
509 * @param fileType generated file type
Bharat saraswale707f032016-07-14 23:33:55 +0530510 * @param curNode current YANG node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530511 * @throws IOException when fails to generate java files
512 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530513 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530514 public void generateJavaFile(int fileType, YangNode curNode)
515 throws IOException {
516 List<String> imports = new ArrayList<>();
517 if (isAttributePresent()) {
518 imports = getJavaImportData().getImports();
519 }
520
521 createPackage(curNode);
522
523 /*
524 * Creates type def class file.
525 */
526 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530527 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530528 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
529 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
530 }
531 /*
532 * Creates type class file.
533 */
534 if ((fileType & GENERATE_UNION_CLASS) != 0) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530535 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530536 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
537 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
538 }
539
540 /*
541 * Close all the file handles.
542 */
543 freeTemporaryResources(false);
544 }
Bharat saraswale707f032016-07-14 23:33:55 +0530545
546 /**
547 * Returns int type index from type list.
548 *
549 * @return int type index from type list
550 */
551 public int getIntIndex() {
552 return intIndex;
553 }
554
555 /**
556 * Sets int type index from type list.
557 *
558 * @param intIndex int type index from type list.
559 */
560 private void setIntIndex(int intIndex) {
561 this.intIndex = intIndex;
562 }
563
564 /**
565 * Returns uInt type index from type list.
566 *
567 * @return uInt type index from type list
568 */
569 public int getUIntIndex() {
570 return uIntIndex;
571 }
572
573 /**
574 * Sets uInt type index from type list.
575 *
576 * @param uIntIndex uInt type index from type list.
577 */
578 private void setUIntIndex(int uIntIndex) {
579 this.uIntIndex = uIntIndex;
580 }
581
582 /**
583 * Returns long type index from type list.
584 *
585 * @return long type index from type list
586 */
587 public int getLongIndex() {
588 return longIndex;
589 }
590
591 /**
592 * Sets long type index from type list.
593 *
594 * @param longIndex long type index from type list.
595 */
596 private void setLongIndex(int longIndex) {
597 this.longIndex = longIndex;
598 }
599
600 /**
601 * Returns uLong type index from type list.
602 *
603 * @return uLong type index from type list
604 */
605 public int getULongIndex() {
606 return uLongIndex;
607 }
608
609 /**
610 * Sets uLong type index from type list.
611 *
612 * @param uLongIndex uLong type index from type list.
613 */
614 private void setULongIndex(int uLongIndex) {
615 this.uLongIndex = uLongIndex;
616 }
617
618 /**
619 * Validates conflict for int and uInt.
620 *
621 * @param typeList type list
622 * @return true if conflict is there
623 */
624 private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) {
625 boolean isIntPresent = false;
626 boolean isUIntPresent = false;
627 for (YangType type : typeList) {
628 if (type.getDataType().equals(INT32)) {
629 setIntIndex(typeList.indexOf(type));
630 isIntPresent = true;
631 }
632 if (type.getDataType().equals(UINT16)) {
633 setUIntIndex(typeList.indexOf(type));
634 isUIntPresent = true;
635 }
636 }
637
638 return isIntPresent && isUIntPresent;
639 }
640
641 /**
642 * Validates conflict for long and uLong.
643 *
644 * @param typeList type list
645 * @return true if conflict is there
646 */
647 private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) {
648 boolean isLongPresent = false;
649 boolean isULongPresent = false;
650 for (YangType type : typeList) {
651 if (type.getDataType().equals(INT64)) {
652 setLongIndex(typeList.indexOf(type));
653 isLongPresent = true;
654 }
655 if (type.getDataType().equals(UINT32)) {
656 setULongIndex(typeList.indexOf(type));
657 isULongPresent = true;
658 }
659 }
660
661 return isLongPresent && isULongPresent;
662 }
663
664 /**
665 * Updates attribute info in case of conflicts.
666 *
667 * @param javaAttributeInfo java attribute info
668 */
669 private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) {
670
671 if (javaAttributeInfo.isIntConflict()) {
672 if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) {
673 setUIntAttribute(javaAttributeInfo);
674 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) {
675 setIntAttribute(javaAttributeInfo);
676 }
677
678 }
679 if (javaAttributeInfo.isLongConflict()) {
680 if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) {
681 setULongAttribute(javaAttributeInfo);
682 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) {
683 setLongAttribute(javaAttributeInfo);
684 }
685
686 }
687 }
688
689 /**
690 * Returns attribute for int.
691 *
692 * @return attribute for int
693 */
694 public JavaAttributeInfo getIntAttribute() {
695 return intAttribute;
696 }
697
698 /**
699 * Sets attribute for int.
700 *
701 * @param intAttribute attribute for int
702 */
703 private void setIntAttribute(JavaAttributeInfo intAttribute) {
704 this.intAttribute = intAttribute;
705 }
706
707 /**
708 * Returns attribute for long.
709 *
710 * @return attribute for long
711 */
712 public JavaAttributeInfo getLongAttribute() {
713 return longAttribute;
714 }
715
716 /**
717 * Sets attribute for long.
718 *
719 * @param longAttribute attribute for long
720 */
721 private void setLongAttribute(JavaAttributeInfo longAttribute) {
722 this.longAttribute = longAttribute;
723 }
724
725 /**
726 * Returns attribute for uInt.
727 *
728 * @return attribute for uInt
729 */
730 public JavaAttributeInfo getUIntAttribute() {
731 return uIntAttribute;
732 }
733
734 /**
735 * Sets attribute for uInt.
736 *
737 * @param uIntAttribute attribute for uInt
738 */
739 private void setUIntAttribute(JavaAttributeInfo uIntAttribute) {
740 this.uIntAttribute = uIntAttribute;
741 }
742
743 /**
744 * Returns attribute for uLong.
745 *
746 * @return attribute for uLong
747 */
748 public JavaAttributeInfo getULongAttribute() {
749 return uLongAttribute;
750 }
751
752 /**
753 * Sets attribute for uLong.
754 *
755 * @param uLongAttribute attribute for uLong
756 */
757 private void setULongAttribute(JavaAttributeInfo uLongAttribute) {
758 this.uLongAttribute = uLongAttribute;
759 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530760}