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