blob: 13da55c2e8e643f7b72752bdf1fcad2c2c597754 [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;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053027import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
28import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswale707f032016-07-14 23:33:55 +053029import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053031import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032
Bharat saraswale707f032016-07-14 23:33:55 +053033import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
34import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
35import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16;
36import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
38import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
39import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
40import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
41import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
42import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053043import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
45import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswale707f032016-07-14 23:33:55 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053047import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
48import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswale707f032016-07-14 23:33:55 +053049import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT;
50import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053051import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
52import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswale707f032016-07-14 23:33:55 +053053import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
54import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053055
56/**
Bharat saraswale707f032016-07-14 23:33:55 +053057 * Represents implementation of java data type code fragments temporary implementations. Maintains the temp files
58 * required specific for user defined data type java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053059 */
60public class TempJavaTypeFragmentFiles
61 extends TempJavaFragmentFiles {
62
63 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053064 * File name for of string method.
65 */
66 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
67
68 /**
69 * File name for construction for special type like union, typedef.
70 */
71 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053072
73 /**
74 * File name for typedef class file name suffix.
75 */
76 private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
77
78 /**
Bharat saraswale707f032016-07-14 23:33:55 +053079 * File name for generated class file for special type like union, typedef suffix.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053080 */
81 private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
82
83 /**
Bharat saraswale707f032016-07-14 23:33:55 +053084 * Integer index in type list.
85 */
86 private int intIndex = 0;
87
88 /**
89 * UInt index in type list.
90 */
91 private int uIntIndex = 0;
92
93 /**
94 * long index in type list.
95 */
96 private int longIndex = 0;
97
98 /**
99 * ULong index in type list.
100 */
101 private int uLongIndex = 0;
102
103 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530104 * Temporary file handle for of string method of class.
105 */
106 private File ofStringImplTempFileHandle;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530107
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530108 /**
109 * Temporary file handle for constructor for type class.
110 */
111 private File constructorForTypeTempFileHandle;
112
113 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530114 * Java file handle for typedef class file.
115 */
116 private File typedefClassJavaFileHandle;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530117
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530118 /**
119 * Java file handle for type class like union, typedef file.
120 */
121 private File typeClassJavaFileHandle;
122
123 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530124 * Java attribute for int.
125 */
126 private JavaAttributeInfo intAttribute;
127
128 /**
129 * Java attribute for long.
130 */
131 private JavaAttributeInfo longAttribute;
132
133 /**
134 * Java attribute for uInt.
135 */
136 private JavaAttributeInfo uIntAttribute;
137
138 /**
139 * Java attribute for uLong.
140 */
141 private JavaAttributeInfo uLongAttribute;
142
143 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530144 * Creates an instance of temporary java code fragment.
145 *
146 * @param javaFileInfo generated java file info
147 * @throws IOException when fails to create new file handle
148 */
Bharat saraswale707f032016-07-14 23:33:55 +0530149 TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530150 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530151
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530152 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530153
154 /*
155 * Initialize getterImpl, attributes, hash code, equals and to strings
156 * when generation file type matches to typeDef class mask.
157 */
158 addGeneratedTempFile(OF_STRING_IMPL_MASK);
159 addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
160 addGeneratedTempFile(FROM_STRING_IMPL_MASK);
161
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530162 setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
163 setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530164
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530165 }
166
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530167 /**
168 * Returns type class constructor method's temporary file handle.
169 *
170 * @return type class constructor method's temporary file handle
171 */
172
173 public File getConstructorForTypeTempFileHandle() {
174 return constructorForTypeTempFileHandle;
175 }
176
177 /**
178 * Sets type class constructor method's temporary file handle.
179 *
Bharat saraswale707f032016-07-14 23:33:55 +0530180 * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530181 */
182 private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
183 this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
184 }
185
186 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530187 * Returns java file handle for typedef class file.
188 *
189 * @return java file handle for typedef class file
190 */
Bharat saraswale707f032016-07-14 23:33:55 +0530191 private File getTypedefClassJavaFileHandle() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530192 return typedefClassJavaFileHandle;
193 }
194
195 /**
196 * Sets the java file handle for typedef class file.
197 *
198 * @param typedefClassJavaFileHandle java file handle
199 */
200 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
201 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
202 }
203
204 /**
205 * Returns java file handle for type class file.
206 *
207 * @return java file handle for type class file
208 */
Bharat saraswale707f032016-07-14 23:33:55 +0530209 private File getTypeClassJavaFileHandle() {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530210 return typeClassJavaFileHandle;
211 }
212
213 /**
214 * Sets the java file handle for type class file.
215 *
216 * @param typeClassJavaFileHandle type file handle
217 */
218 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
219 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
220 }
221
222 /**
223 * Returns of string method's temporary file handle.
224 *
225 * @return of string method's temporary file handle
226 */
227
228 public File getOfStringImplTempFileHandle() {
229 return ofStringImplTempFileHandle;
230 }
231
232 /**
233 * Set of string method's temporary file handle.
234 *
Bharat saraswale707f032016-07-14 23:33:55 +0530235 * @param ofStringImplTempFileHandle of string method's temporary file handle
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530236 */
237 private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
238 this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
239 }
240
241 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530242 * Adds all the type in the current data model node as part of the generated temporary file.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530243 *
Bharat saraswale707f032016-07-14 23:33:55 +0530244 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
245 * @param pluginConfig plugin configurations for naming conventions
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530246 * @throws IOException IO operation fail
247 */
Bharat saraswale707f032016-07-14 23:33:55 +0530248 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530249 throws IOException {
250
251 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
252 if (typeList != null) {
253 for (YangType<?> yangType : typeList) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530254 if (!(yangType instanceof YangJavaTypeTranslator)) {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530255 throw new TranslatorException("Type does not have Java info");
256 }
Bharat saraswale707f032016-07-14 23:33:55 +0530257 JavaAttributeInfo javaAttributeInfo = getAttributeForType(yangType, pluginConfig);
258 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo,
259 pluginConfig, typeList);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530260 }
Bharat saraswale707f032016-07-14 23:33:55 +0530261 addTypeConstructor(pluginConfig);
262 addMethodsInConflictCase(pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530263 }
264 }
265
266 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530267 * Returns java attribute.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530268 *
Bharat saraswale707f032016-07-14 23:33:55 +0530269 * @param yangType YANG type
Bharat saraswal33dfa012016-05-17 19:59:16 +0530270 * @param pluginConfig plugin configurations
Bharat saraswale707f032016-07-14 23:33:55 +0530271 * @return java attribute
272 */
273 private JavaAttributeInfo getAttributeForType(YangType yangType, YangPluginConfig pluginConfig) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530274 YangJavaTypeTranslator<?> javaType = (YangJavaTypeTranslator<?>) yangType;
Bharat saraswale707f032016-07-14 23:33:55 +0530275 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
276 String typeName = javaType.getDataTypeName();
277 typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
278 return getAttributeInfoForTheData(
279 javaType.getJavaQualifiedInfo(),
280 typeName, javaType,
281 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
282 false);
283 }
284
285 /**
286 * Adds the new attribute info to the target generated temporary files for union class.
287 *
288 * @param javaAttributeInfo the attribute info that needs to be added to temporary files
289 * @param pluginConfig plugin configurations
290 * @param typeList type list
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530291 * @throws IOException IO operation fail
292 */
Bharat saraswale707f032016-07-14 23:33:55 +0530293 private void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfo,
294 YangPluginConfig pluginConfig, List<YangType<?>> typeList)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530295 throws IOException {
296
Bharat saraswale707f032016-07-14 23:33:55 +0530297 YangDataTypes attrType = javaAttributeInfo.getAttributeType().getDataType();
298
299 if (attrType == INT32 || attrType == UINT16) {
300 boolean isIntConflict = validateForConflictingIntTypes(typeList);
301 javaAttributeInfo.setIntConflict(isIntConflict);
302 updateAttributeCondition(javaAttributeInfo);
303 if (!isIntConflict) {
304 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
305 }
306 } else if (attrType == INT64 || attrType == UINT32) {
307 boolean isLongConflict = validateForConflictingLongTypes(typeList);
308 javaAttributeInfo.setLongConflict(isLongConflict);
309 updateAttributeCondition(javaAttributeInfo);
310 if (!isLongConflict) {
311 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
312 }
313 } else {
314 addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
315 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530316 super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530317
Bharat saraswale707f032016-07-14 23:33:55 +0530318 }
319
320 /**
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530321 * Adds of method and constructor when there is no conflicting types.
Bharat saraswale707f032016-07-14 23:33:55 +0530322 *
323 * @param javaAttributeInfo java attribute info
324 * @param pluginConfig plugin configurations
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530325 * @throws IOException when fails to do IO operations
Bharat saraswale707f032016-07-14 23:33:55 +0530326 */
327 private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo,
328 YangPluginConfig pluginConfig) throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530329 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530330 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530331 }
Bharat saraswale707f032016-07-14 23:33:55 +0530332
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530333 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530334 addTypeConstructor(javaAttributeInfo, pluginConfig);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530335 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530336 }
337
338 /**
Bharat saraswale707f032016-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 */
344 private void addMethodsInConflictCase(YangPluginConfig pluginConfig) throws IOException {
345 JavaAttributeInfo attr = getIntAttribute();
346 if (attr != null) {
347 attr = getUIntAttribute();
348 }
349 if (attr != null) {
350 if (attr.isIntConflict()) {
351 if (getIntIndex() < getUIntIndex()) {
352 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(),
353 getGeneratedJavaClassName(), pluginConfig)
354 + NEW_LINE);
355 addGetterImpl(getIntAttribute(), pluginConfig);
356 addFromStringMethod(getIntAttribute(), pluginConfig);
357 } else {
358 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(),
359 getGeneratedJavaClassName(), pluginConfig)
360 + NEW_LINE);
361 addGetterImpl(getUIntAttribute(), pluginConfig);
362 addFromStringMethod(getUIntAttribute(), pluginConfig);
363 }
364 }
365 }
366 attr = getLongAttribute();
367 if (attr != null) {
368 attr = getULongAttribute();
369 }
370 if (attr != null) {
371 if (attr.isLongConflict()) {
372 if (getLongIndex() < getULongIndex()) {
373 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(),
374 getGeneratedJavaClassName(), pluginConfig)
375 + NEW_LINE);
376 addGetterImpl(getLongAttribute(), pluginConfig);
377 addFromStringMethod(getLongAttribute(), pluginConfig);
378 } else {
379 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(),
380 getGeneratedJavaClassName(), pluginConfig)
381 + NEW_LINE);
382 addGetterImpl(getULongAttribute(), pluginConfig);
383 addFromStringMethod(getULongAttribute(), pluginConfig);
384 }
385 }
386 }
387 }
388
389 /**
390 * Adds from string method for conflict case.
391 *
392 * @param newAttrInfo new attribute
393 * @param pluginConfig plugin configurations
394 * @throws IOException when fails to do IO operations
395 */
396 private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig) throws IOException {
397
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530398 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswale707f032016-07-14 23:33:55 +0530399 pluginConfig.getConflictResolver());
400 /*
401 * Create a new java attribute info with qualified information of
402 * wrapper classes.
403 */
404 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
405 newAttrInfo.getAttributeName(),
406 newAttrInfo.getAttributeType(),
407 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
408
409 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
410 }
411
412 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530413 * Adds type constructor.
414 *
Bharat saraswale707f032016-07-14 23:33:55 +0530415 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530416 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530417 * @throws IOException when fails to append to temporary file
418 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530419 private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530420 throws IOException {
421 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530422 getGeneratedJavaClassName(), pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530423 }
424
425 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530426 * Adds type constructor.
427 *
428 * @param pluginConfig plugin configurations
429 * @throws IOException when fails to append to temporary file
430 */
431 private void addTypeConstructor(YangPluginConfig pluginConfig)
432 throws IOException {
433 JavaAttributeInfo attr = getIntAttribute();
434 if (attr != null) {
435 attr = getUIntAttribute();
436 }
437 if (attr != null) {
438 if (attr.isIntConflict()) {
439 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
440 getIntAttribute(),
441 getUIntAttribute(), getGeneratedJavaClassName(), pluginConfig, INT_TYPE_CONFLICT,
442 getIntIndex()
443 < getUIntIndex()) + NEW_LINE);
444 }
445 }
446 attr = getLongAttribute();
447 if (attr != null) {
448 attr = getULongAttribute();
449 }
450 if (attr != null) {
451 if (attr.isLongConflict()) {
452 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
453 getLongAttribute(),
454 getULongAttribute(), getGeneratedJavaClassName(), pluginConfig, LONG_TYPE_CONFLICT,
455 getLongIndex()
456 < getULongIndex()) + NEW_LINE);
457 }
458 }
459 }
460
461 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530462 * Adds of string for type.
463 *
Bharat saraswale707f032016-07-14 23:33:55 +0530464 * @param attr attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530465 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530466 * @throws IOException when fails to append to temporary file
467 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530468 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530469 throws IOException {
470 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530471 getGeneratedJavaClassName(), pluginConfig)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530472 + NEW_LINE);
473 }
474
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530475 /**
476 * Removes all temporary file handles.
477 *
Bharat saraswale707f032016-07-14 23:33:55 +0530478 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530479 * @throws IOException when failed to delete the temporary files
480 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530481 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530482 public void freeTemporaryResources(boolean isErrorOccurred)
483 throws IOException {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530484
485 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswale707f032016-07-14 23:33:55 +0530486 closeFile(getTypedefClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530487 }
488
489 if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
Bharat saraswale707f032016-07-14 23:33:55 +0530490 closeFile(getTypeClassJavaFileHandle(), isErrorOccurred);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530491 }
492
493 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
494 closeFile(getConstructorForTypeTempFileHandle(), true);
495 }
496 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
497 closeFile(getOfStringImplTempFileHandle(), true);
498 }
499 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
500 closeFile(getFromStringImplTempFileHandle(), true);
501 }
502
503 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530504
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530505 }
506
507 /**
508 * Constructs java code exit.
509 *
510 * @param fileType generated file type
Bharat saraswale707f032016-07-14 23:33:55 +0530511 * @param curNode current YANG node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530512 * @throws IOException when fails to generate java files
513 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530514 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530515 public void generateJavaFile(int fileType, YangNode curNode)
516 throws IOException {
517 List<String> imports = new ArrayList<>();
518 if (isAttributePresent()) {
519 imports = getJavaImportData().getImports();
520 }
521
522 createPackage(curNode);
523
524 /*
525 * Creates type def class file.
526 */
527 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530528 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530529 setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX)));
530 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
531 }
532 /*
533 * Creates type class file.
534 */
535 if ((fileType & GENERATE_UNION_CLASS) != 0) {
Shankara-Huaweib9999eb2016-07-14 16:53:09 +0530536 addImportsToStringAndHasCodeMethods(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530537 setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX)));
538 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
539 }
540
541 /*
542 * Close all the file handles.
543 */
544 freeTemporaryResources(false);
545 }
Bharat saraswale707f032016-07-14 23:33:55 +0530546
547 /**
548 * Returns int type index from type list.
549 *
550 * @return int type index from type list
551 */
552 public int getIntIndex() {
553 return intIndex;
554 }
555
556 /**
557 * Sets int type index from type list.
558 *
559 * @param intIndex int type index from type list.
560 */
561 private void setIntIndex(int intIndex) {
562 this.intIndex = intIndex;
563 }
564
565 /**
566 * Returns uInt type index from type list.
567 *
568 * @return uInt type index from type list
569 */
570 public int getUIntIndex() {
571 return uIntIndex;
572 }
573
574 /**
575 * Sets uInt type index from type list.
576 *
577 * @param uIntIndex uInt type index from type list.
578 */
579 private void setUIntIndex(int uIntIndex) {
580 this.uIntIndex = uIntIndex;
581 }
582
583 /**
584 * Returns long type index from type list.
585 *
586 * @return long type index from type list
587 */
588 public int getLongIndex() {
589 return longIndex;
590 }
591
592 /**
593 * Sets long type index from type list.
594 *
595 * @param longIndex long type index from type list.
596 */
597 private void setLongIndex(int longIndex) {
598 this.longIndex = longIndex;
599 }
600
601 /**
602 * Returns uLong type index from type list.
603 *
604 * @return uLong type index from type list
605 */
606 public int getULongIndex() {
607 return uLongIndex;
608 }
609
610 /**
611 * Sets uLong type index from type list.
612 *
613 * @param uLongIndex uLong type index from type list.
614 */
615 private void setULongIndex(int uLongIndex) {
616 this.uLongIndex = uLongIndex;
617 }
618
619 /**
620 * Validates conflict for int and uInt.
621 *
622 * @param typeList type list
623 * @return true if conflict is there
624 */
625 private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) {
626 boolean isIntPresent = false;
627 boolean isUIntPresent = false;
628 for (YangType type : typeList) {
629 if (type.getDataType().equals(INT32)) {
630 setIntIndex(typeList.indexOf(type));
631 isIntPresent = true;
632 }
633 if (type.getDataType().equals(UINT16)) {
634 setUIntIndex(typeList.indexOf(type));
635 isUIntPresent = true;
636 }
637 }
638
639 return isIntPresent && isUIntPresent;
640 }
641
642 /**
643 * Validates conflict for long and uLong.
644 *
645 * @param typeList type list
646 * @return true if conflict is there
647 */
648 private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) {
649 boolean isLongPresent = false;
650 boolean isULongPresent = false;
651 for (YangType type : typeList) {
652 if (type.getDataType().equals(INT64)) {
653 setLongIndex(typeList.indexOf(type));
654 isLongPresent = true;
655 }
656 if (type.getDataType().equals(UINT32)) {
657 setULongIndex(typeList.indexOf(type));
658 isULongPresent = true;
659 }
660 }
661
662 return isLongPresent && isULongPresent;
663 }
664
665 /**
666 * Updates attribute info in case of conflicts.
667 *
668 * @param javaAttributeInfo java attribute info
669 */
670 private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) {
671
672 if (javaAttributeInfo.isIntConflict()) {
673 if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) {
674 setUIntAttribute(javaAttributeInfo);
675 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) {
676 setIntAttribute(javaAttributeInfo);
677 }
678
679 }
680 if (javaAttributeInfo.isLongConflict()) {
681 if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) {
682 setULongAttribute(javaAttributeInfo);
683 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) {
684 setLongAttribute(javaAttributeInfo);
685 }
686
687 }
688 }
689
690 /**
691 * Returns attribute for int.
692 *
693 * @return attribute for int
694 */
695 public JavaAttributeInfo getIntAttribute() {
696 return intAttribute;
697 }
698
699 /**
700 * Sets attribute for int.
701 *
702 * @param intAttribute attribute for int
703 */
704 private void setIntAttribute(JavaAttributeInfo intAttribute) {
705 this.intAttribute = intAttribute;
706 }
707
708 /**
709 * Returns attribute for long.
710 *
711 * @return attribute for long
712 */
713 public JavaAttributeInfo getLongAttribute() {
714 return longAttribute;
715 }
716
717 /**
718 * Sets attribute for long.
719 *
720 * @param longAttribute attribute for long
721 */
722 private void setLongAttribute(JavaAttributeInfo longAttribute) {
723 this.longAttribute = longAttribute;
724 }
725
726 /**
727 * Returns attribute for uInt.
728 *
729 * @return attribute for uInt
730 */
731 public JavaAttributeInfo getUIntAttribute() {
732 return uIntAttribute;
733 }
734
735 /**
736 * Sets attribute for uInt.
737 *
738 * @param uIntAttribute attribute for uInt
739 */
740 private void setUIntAttribute(JavaAttributeInfo uIntAttribute) {
741 this.uIntAttribute = uIntAttribute;
742 }
743
744 /**
745 * Returns attribute for uLong.
746 *
747 * @return attribute for uLong
748 */
749 public JavaAttributeInfo getULongAttribute() {
750 return uLongAttribute;
751 }
752
753 /**
754 * Sets attribute for uLong.
755 *
756 * @param uLongAttribute attribute for uLong
757 */
758 private void setULongAttribute(JavaAttributeInfo uLongAttribute) {
759 this.uLongAttribute = uLongAttribute;
760 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530761}