blob: b0145a8919283f88faae717904c34b817abb15ac [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 org.onosproject.yangutils.datamodel.YangNode;
20import org.onosproject.yangutils.datamodel.YangType;
21import org.onosproject.yangutils.datamodel.YangTypeHolder;
Bharat saraswal64e7e232016-07-14 23:33:55 +053022import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053023import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053024import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
Gaurav Agrawal8a147522016-08-10 13:43:01 +053025import org.onosproject.yangutils.utils.io.YangPluginConfig;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053026
Bharat saraswal9fab16b2016-09-23 23:27:24 +053027import java.io.File;
28import java.io.IOException;
29import java.util.ArrayList;
30import java.util.List;
31
Bharat saraswal94844d62016-10-13 13:28:03 +053032import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY;
Bharat saraswal54e4bab2016-10-05 23:32:14 +053033import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BITS;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053034import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16;
Bharat saraswal64e7e232016-07-14 23:33:55 +053035import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
36import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
37import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16;
38import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053039import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT8;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053040import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
41import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
42import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
43import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
44import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
45import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Shankara-Huaweib7564772016-08-02 18:13:13 +053046import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053047import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
48import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
Bharat saraswal64e7e232016-07-14 23:33:55 +053049import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053050import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
51import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
Bharat saraswal64e7e232016-07-14 23:33:55 +053052import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT;
53import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053054import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.SHORT_TYPE_CONFLICT;
Bharat saraswal94844d62016-10-13 13:28:03 +053055import static org.onosproject.yangutils.utils.UtilConstants.BASE64;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053056import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal94844d62016-10-13 13:28:03 +053057import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_PKG;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053058import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal64e7e232016-07-14 23:33:55 +053059import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
60import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053061
62/**
Bharat saraswal64e7e232016-07-14 23:33:55 +053063 * Represents implementation of java data type code fragments temporary implementations. Maintains the temp files
64 * required specific for user defined data type java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053065 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +053066//TODO: Update with multi type handler framework.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053067public class TempJavaTypeFragmentFiles
68 extends TempJavaFragmentFiles {
69
70 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053071 * File name for of string method.
72 */
73 private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
74
75 /**
76 * File name for construction for special type like union, typedef.
77 */
78 private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053079
80 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +053081 * Integer index in type list.
82 */
83 private int intIndex = 0;
84
85 /**
86 * UInt index in type list.
87 */
88 private int uIntIndex = 0;
89
90 /**
91 * long index in type list.
92 */
93 private int longIndex = 0;
94
95 /**
96 * ULong index in type list.
97 */
98 private int uLongIndex = 0;
99
100 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530101 * short index in type list.
102 */
103 private int shortIndex = 0;
104
105 /**
106 * Uint8 index in type list.
107 */
108 private int uInt8Index = 0;
109
110 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530111 * Temporary file handle for of string method of class.
112 */
113 private File ofStringImplTempFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530114
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530115 /**
116 * Temporary file handle for constructor for type class.
117 */
118 private File constructorForTypeTempFileHandle;
119
120 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530121 * Java file handle for typedef class file.
122 */
123 private File typedefClassJavaFileHandle;
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530124
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530125 /**
126 * Java file handle for type class like union, typedef file.
127 */
128 private File typeClassJavaFileHandle;
129
130 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530131 * Java attribute for int.
132 */
133 private JavaAttributeInfo intAttribute;
134
135 /**
136 * Java attribute for long.
137 */
138 private JavaAttributeInfo longAttribute;
139
140 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530141 * Java attribute for short.
142 */
143 private JavaAttributeInfo shortAttribute;
144
145 /**
146 * Java attribute for uint8.
147 */
148 private JavaAttributeInfo uInt8Attribute;
149
150 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530151 * Java attribute for uInt.
152 */
153 private JavaAttributeInfo uIntAttribute;
154
155 /**
156 * Java attribute for uLong.
157 */
158 private JavaAttributeInfo uLongAttribute;
159
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530160 private List<YangType<?>> local = new ArrayList<>();
161
Bharat saraswal64e7e232016-07-14 23:33:55 +0530162 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530163 * Creates an instance of temporary java code fragment.
164 *
165 * @param javaFileInfo generated java file info
166 * @throws IOException when fails to create new file handle
167 */
Bharat saraswale50edca2016-08-05 01:58:25 +0530168 TempJavaTypeFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530169 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530170
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530171 super(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530172
173 /*
174 * Initialize getterImpl, attributes, hash code, equals and to strings
175 * when generation file type matches to typeDef class mask.
176 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530177 addGeneratedTempFile(OF_STRING_IMPL_MASK | CONSTRUCTOR_FOR_TYPE_MASK |
178 FROM_STRING_IMPL_MASK);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530179
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530180 ofStringImplTempFileHandle = getTemporaryFileHandle(
181 OF_STRING_METHOD_FILE_NAME);
182 constructorForTypeTempFileHandle = getTemporaryFileHandle(
183 CONSTRUCTOR_FOR_TYPE_FILE_NAME);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530184
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530185 }
186
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530187 /**
188 * Returns type class constructor method's temporary file handle.
189 *
190 * @return type class constructor method's temporary file handle
191 */
192
193 public File getConstructorForTypeTempFileHandle() {
194 return constructorForTypeTempFileHandle;
195 }
196
197 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530198 * Returns java file handle for typedef class file.
199 *
200 * @return java file handle for typedef class file
201 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530202 private File getTypedefClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530203 return typedefClassJavaFileHandle;
204 }
205
206 /**
207 * Sets the java file handle for typedef class file.
208 *
209 * @param typedefClassJavaFileHandle java file handle
210 */
211 private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) {
212 this.typedefClassJavaFileHandle = typedefClassJavaFileHandle;
213 }
214
215 /**
216 * Returns java file handle for type class file.
217 *
218 * @return java file handle for type class file
219 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530220 private File getTypeClassJavaFileHandle() {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530221 return typeClassJavaFileHandle;
222 }
223
224 /**
225 * Sets the java file handle for type class file.
226 *
227 * @param typeClassJavaFileHandle type file handle
228 */
229 private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) {
230 this.typeClassJavaFileHandle = typeClassJavaFileHandle;
231 }
232
233 /**
234 * Returns of string method's temporary file handle.
235 *
236 * @return of string method's temporary file handle
237 */
238
239 public File getOfStringImplTempFileHandle() {
240 return ofStringImplTempFileHandle;
241 }
242
243 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530244 * Adds all the type in the current data model node as part of the generated temporary file.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530245 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530246 * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530247 * @param config plugin configurations for naming conventions
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530248 * @throws IOException IO operation fail
249 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530250 void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig config)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530251 throws IOException {
252
253 List<YangType<?>> typeList = yangTypeHolder.getTypeList();
254 if (typeList != null) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530255 List<YangType<?>> types = validateTypes(typeList);
256 for (YangType<?> type : types) {
257 if (!(type instanceof YangJavaTypeTranslator)) {
258 throw new TranslatorException(
259 "Type does not have Java info " + type
260 .getDataTypeName() + " in " + type
261 .getLineNumber() + " at " + type
262 .getCharPosition() + " in " +
263 type.getFileName());
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530264 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530265 JavaAttributeInfo javaAttributeInfo = getAttributeForType(type,
266 config);
267 if (type.getDataType() == BITS) {
268 addBitsHandler(javaAttributeInfo, type, this);
269 }
Bharat saraswal94844d62016-10-13 13:28:03 +0530270 if (type.getDataType() == BINARY) {
271 JavaQualifiedTypeInfoTranslator info = new
272 JavaQualifiedTypeInfoTranslator();
273 info.setClassInfo(BASE64);
274 info.setPkgInfo(JAVA_UTIL_PKG);
275 getJavaImportData().addImportInfo(
276 info, getGeneratedJavaClassName(), getJavaFileInfo()
277 .getPackage());
278 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530279 addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530280 config, types);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530281 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530282 addTypeConstructor();
283 addMethodsInConflictCase(config);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530284 }
285 }
286
287 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530288 * Returns java attribute.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530289 *
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530290 * @param type YANG type
291 * @param config plugin configurations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530292 * @return java attribute
293 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530294 private JavaAttributeInfo getAttributeForType(YangType type,
295 YangPluginConfig config) {
296 YangJavaTypeTranslator javaType = (YangJavaTypeTranslator) type;
297 javaType.updateJavaQualifiedInfo(config.getConflictResolver());
298 String typeName = getCamelCase(javaType.getDataTypeName(), config
299 .getConflictResolver());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530300 return getAttributeInfoForTheData(
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530301 javaType.getJavaQualifiedInfo(), typeName, javaType,
302 getIsQualifiedAccessOrAddToImportList(
303 javaType.getJavaQualifiedInfo()), false);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530304 }
305
306 /**
307 * Adds the new attribute info to the target generated temporary files for union class.
308 *
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530309 * @param attr the attribute info that needs to be added to temporary files
310 * @param config plugin configurations
311 * @param types type list
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530312 * @throws IOException IO operation fail
313 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530314 private void addJavaSnippetInfoToApplicableTempFiles(
315 JavaAttributeInfo attr, YangPluginConfig config, List<YangType<?>> types)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530316 throws IOException {
317
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530318 YangDataTypes attrType = attr.getAttributeType().getDataType();
Bharat saraswal64e7e232016-07-14 23:33:55 +0530319
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530320 if (attrType == INT16 || attrType == UINT8) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530321 boolean isShortConflict = validateForConflictingShortTypes(types);
322 attr.setShortConflict(isShortConflict);
323 updateAttributeCondition(attr);
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530324 if (!isShortConflict) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530325 addMethodsWhenNoConflictingTypes(attr, config, types);
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530326 }
327 } else if (attrType == INT32 || attrType == UINT16) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530328 boolean isIntConflict = validateForConflictingIntTypes(types);
329 attr.setIntConflict(isIntConflict);
330 updateAttributeCondition(attr);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530331 if (!isIntConflict) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530332 addMethodsWhenNoConflictingTypes(attr, config, types);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530333 }
334 } else if (attrType == INT64 || attrType == UINT32) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530335 boolean isLongConflict = validateForConflictingLongTypes(types);
336 attr.setLongConflict(isLongConflict);
337 updateAttributeCondition(attr);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530338 if (!isLongConflict) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530339 addMethodsWhenNoConflictingTypes(attr, config, types);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530340 }
341 } else {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530342 addMethodsWhenNoConflictingTypes(attr, config, types);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530343 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530344 super.addJavaSnippetInfoToApplicableTempFiles(attr, config);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530345
Bharat saraswal64e7e232016-07-14 23:33:55 +0530346 }
347
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530348 private List<YangType<?>> validateTypes(List<YangType<?>> types) {
349 String curType;
350 List<String> preType = new ArrayList<>();
351 for (YangType type : types) {
352 curType = type.getDataTypeName();
353 if (!preType.contains(curType)) {
354 preType.add(curType);
355 local.add(type);
356 }
357 }
358 return local;
359 }
360
Bharat saraswal64e7e232016-07-14 23:33:55 +0530361 /**
Shankara-Huaweib7564772016-08-02 18:13:13 +0530362 * Adds of method and constructor when there is no conflicting types.
Bharat saraswal64e7e232016-07-14 23:33:55 +0530363 *
364 * @param javaAttributeInfo java attribute info
365 * @param pluginConfig plugin configurations
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530366 * @param types YANG type
Shankara-Huaweib7564772016-08-02 18:13:13 +0530367 * @throws IOException when fails to do IO operations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530368 */
369 private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530370 YangPluginConfig pluginConfig, List<YangType<?>> types)
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530371 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530372 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530373 addOfStringMethod(javaAttributeInfo, pluginConfig);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530374 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530375
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530376 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530377 addTypeConstructor(javaAttributeInfo, types.indexOf
378 (javaAttributeInfo.getAttributeType()));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530379 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530380 }
381
382 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530383 * Adds of, getter and from string method in conflict cases.
384 *
385 * @param pluginConfig plugin configurations
386 * @throws IOException when fails to do IO operations
387 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530388 private void addMethodsInConflictCase(YangPluginConfig pluginConfig)
389 throws IOException {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530390 JavaAttributeInfo attr = getIntAttribute();
391 if (attr != null) {
392 attr = getUIntAttribute();
393 }
394 if (attr != null) {
395 if (attr.isIntConflict()) {
396 if (getIntIndex() < getUIntIndex()) {
397 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530398 getGeneratedJavaClassName())
Bharat saraswal64e7e232016-07-14 23:33:55 +0530399 + NEW_LINE);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530400 addGetterImpl(getIntAttribute());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530401 addFromStringMethod(getIntAttribute(), pluginConfig);
402 } else {
403 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530404 getGeneratedJavaClassName())
Bharat saraswal64e7e232016-07-14 23:33:55 +0530405 + NEW_LINE);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530406 addGetterImpl(getUIntAttribute());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530407 addFromStringMethod(getUIntAttribute(), pluginConfig);
408 }
409 }
410 }
411 attr = getLongAttribute();
412 if (attr != null) {
413 attr = getULongAttribute();
414 }
415 if (attr != null) {
416 if (attr.isLongConflict()) {
417 if (getLongIndex() < getULongIndex()) {
418 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530419 getGeneratedJavaClassName())
Bharat saraswal64e7e232016-07-14 23:33:55 +0530420 + NEW_LINE);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530421 addGetterImpl(getLongAttribute());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530422 addFromStringMethod(getLongAttribute(), pluginConfig);
423 } else {
424 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530425 getGeneratedJavaClassName())
Bharat saraswal64e7e232016-07-14 23:33:55 +0530426 + NEW_LINE);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530427 addGetterImpl(getULongAttribute());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530428 addFromStringMethod(getULongAttribute(), pluginConfig);
429 }
430 }
431 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530432
433 attr = getShortAttribute();
434 if (attr != null) {
435 attr = getUInt8Attribute();
436 }
437 if (attr != null) {
438 if (attr.isShortConflict()) {
439 if (getShortIndex() < getUInt8Index()) {
440 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getShortAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530441 getGeneratedJavaClassName())
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530442 + NEW_LINE);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530443 addGetterImpl(getShortAttribute());
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530444 addFromStringMethod(getShortAttribute(), pluginConfig);
445 } else {
446 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUInt8Attribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530447 getGeneratedJavaClassName())
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530448 + NEW_LINE);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530449 addGetterImpl(getUInt8Attribute());
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530450 addFromStringMethod(getUInt8Attribute(), pluginConfig);
451 }
452 }
453 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530454 }
455
456 /**
457 * Adds from string method for conflict case.
458 *
459 * @param newAttrInfo new attribute
460 * @param pluginConfig plugin configurations
461 * @throws IOException when fails to do IO operations
462 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530463 private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig)
464 throws IOException {
Bharat saraswal64e7e232016-07-14 23:33:55 +0530465
Shankara-Huaweib7564772016-08-02 18:13:13 +0530466 JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530467 pluginConfig.getConflictResolver());
Bharat saraswal64e7e232016-07-14 23:33:55 +0530468 /*
469 * Create a new java attribute info with qualified information of
470 * wrapper classes.
471 */
472 JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530473 newAttrInfo.getAttributeName(),
474 newAttrInfo.getAttributeType(),
475 getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530476
Bharat saraswal0663aff2016-10-18 23:16:14 +0530477 addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530478 }
479
480 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530481 * Adds type constructor.
482 *
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530483 * @param attr attribute info
484 * @param count count of types
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530485 * @throws IOException when fails to append to temporary file
486 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530487 private void addTypeConstructor(JavaAttributeInfo attr, int count)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530488 throws IOException {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530489 appendToFile(getConstructorForTypeTempFileHandle(),
490 getTypeConstructorStringAndJavaDoc(
491 attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), count)
492 + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530493 }
494
495 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530496 * Adds type constructor.
497 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530498 * @throws IOException when fails to append to temporary file
499 */
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530500 private void addTypeConstructor()
Bharat saraswal64e7e232016-07-14 23:33:55 +0530501 throws IOException {
502 JavaAttributeInfo attr = getIntAttribute();
503 if (attr != null) {
504 attr = getUIntAttribute();
505 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530506 boolean index = getIntIndex() < getUIntIndex();
507 int count;
508 if (index) {
509 count = getIntIndex();
510 } else {
511 count = getUIntIndex();
512 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530513 if (attr != null) {
514 if (attr.isIntConflict()) {
515 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
516 getIntAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530517 getUIntAttribute(), getGeneratedJavaClassName(), INT_TYPE_CONFLICT,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530518 index, count) + NEW_LINE);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530519 }
520 }
521 attr = getLongAttribute();
522 if (attr != null) {
523 attr = getULongAttribute();
524 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530525 index = getLongIndex() < getULongIndex();
526 if (index) {
527 count = getLongIndex();
528 } else {
529 count = getULongIndex();
530 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530531 if (attr != null) {
532 if (attr.isLongConflict()) {
533 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
534 getLongAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530535 getULongAttribute(), getGeneratedJavaClassName(), LONG_TYPE_CONFLICT,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530536 index, count) + NEW_LINE);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530537 }
538 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530539 attr = getShortAttribute();
540 if (attr != null) {
541 attr = getUInt8Attribute();
542 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530543 index = getShortIndex() < getUInt8Index();
544 if (index) {
545 count = getShortIndex();
546 } else {
547 count = getUInt8Index();
548 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530549 if (attr != null) {
550 if (attr.isShortConflict()) {
551 appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
552 getShortAttribute(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530553 getUInt8Attribute(), getGeneratedJavaClassName(), SHORT_TYPE_CONFLICT,
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530554 index, count) + NEW_LINE);
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530555 }
556 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530557 }
558
559 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530560 * Adds of string for type.
561 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530562 * @param attr attribute info
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530563 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530564 * @throws IOException when fails to append to temporary file
565 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530566 private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530567 throws IOException {
568 appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr,
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530569 getGeneratedJavaClassName())
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530570 + NEW_LINE);
571 }
572
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530573 /**
574 * Removes all temporary file handles.
575 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530576 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530577 * @throws IOException when failed to delete the temporary files
578 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530579 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530580 public void freeTemporaryResources(boolean isErrorOccurred)
581 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530582
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530583 if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0 ||
584 (getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
585 closeFile(typeClassJavaFileHandle, isErrorOccurred);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530586 }
587
588 if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530589 closeFile(constructorForTypeTempFileHandle, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530590 }
591 if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530592 closeFile(ofStringImplTempFileHandle, true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530593 }
594 if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
595 closeFile(getFromStringImplTempFileHandle(), true);
596 }
597
598 super.freeTemporaryResources(isErrorOccurred);
Bharat saraswal250a7472016-05-12 13:16:57 +0530599
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530600 }
601
602 /**
603 * Constructs java code exit.
604 *
605 * @param fileType generated file type
Bharat saraswal64e7e232016-07-14 23:33:55 +0530606 * @param curNode current YANG node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530607 * @throws IOException when fails to generate java files
608 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530609 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530610 public void generateJavaFile(int fileType, YangNode curNode)
611 throws IOException {
612 List<String> imports = new ArrayList<>();
613 if (isAttributePresent()) {
614 imports = getJavaImportData().getImports();
615 }
616
617 createPackage(curNode);
Bharat saraswal94844d62016-10-13 13:28:03 +0530618 imports.add(getJavaImportData().getImportForHashAndEquals());
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530619
620 /*
621 * Creates type def class file.
622 */
623 if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530624 setTypedefClassJavaFileHandle(getJavaFileHandle(
625 getJavaClassName(EMPTY_STRING)));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530626 generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports);
627 }
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530628 String bit = getJavaImportData().getImportForToBitSet();
629 if (!imports.contains(bit)) {
630 imports.add(bit);
631 }
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530632 /*
633 * Creates type class file.
634 */
635 if ((fileType & GENERATE_UNION_CLASS) != 0) {
Bharat saraswal54e4bab2016-10-05 23:32:14 +0530636 setTypeClassJavaFileHandle(getJavaFileHandle(
637 getJavaClassName(EMPTY_STRING)));
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530638 generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports);
639 }
640
641 /*
642 * Close all the file handles.
643 */
644 freeTemporaryResources(false);
645 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530646
647 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530648 * Returns short type index from type list.
649 *
650 * @return short type index from type list
651 */
652 public int getShortIndex() {
653 return shortIndex;
654 }
655
656 /**
657 * Sets short type index from type list.
658 *
659 * @param shortIndex short type index from type list.
660 */
661 private void setShortIndex(int shortIndex) {
662 this.shortIndex = shortIndex;
663 }
664
665 /**
666 * Returns uInt type index from type list.
667 *
668 * @return uInt type index from type list
669 */
670 public int getUInt8Index() {
671 return uInt8Index;
672 }
673
674 /**
675 * Sets uInt8 type index from type list.
676 *
677 * @param uInt8Index uInt8 type index from type list.
678 */
679 private void setUInt8Index(int uInt8Index) {
680 this.uInt8Index = uInt8Index;
681 }
682
683 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530684 * Returns int type index from type list.
685 *
686 * @return int type index from type list
687 */
688 public int getIntIndex() {
689 return intIndex;
690 }
691
692 /**
693 * Sets int type index from type list.
694 *
695 * @param intIndex int type index from type list.
696 */
697 private void setIntIndex(int intIndex) {
698 this.intIndex = intIndex;
699 }
700
701 /**
702 * Returns uInt type index from type list.
703 *
704 * @return uInt type index from type list
705 */
706 public int getUIntIndex() {
707 return uIntIndex;
708 }
709
710 /**
711 * Sets uInt type index from type list.
712 *
713 * @param uIntIndex uInt type index from type list.
714 */
715 private void setUIntIndex(int uIntIndex) {
716 this.uIntIndex = uIntIndex;
717 }
718
719 /**
720 * Returns long type index from type list.
721 *
722 * @return long type index from type list
723 */
724 public int getLongIndex() {
725 return longIndex;
726 }
727
728 /**
729 * Sets long type index from type list.
730 *
731 * @param longIndex long type index from type list.
732 */
733 private void setLongIndex(int longIndex) {
734 this.longIndex = longIndex;
735 }
736
737 /**
738 * Returns uLong type index from type list.
739 *
740 * @return uLong type index from type list
741 */
742 public int getULongIndex() {
743 return uLongIndex;
744 }
745
746 /**
747 * Sets uLong type index from type list.
748 *
749 * @param uLongIndex uLong type index from type list.
750 */
751 private void setULongIndex(int uLongIndex) {
752 this.uLongIndex = uLongIndex;
753 }
754
755 /**
756 * Validates conflict for int and uInt.
757 *
758 * @param typeList type list
759 * @return true if conflict is there
760 */
761 private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) {
762 boolean isIntPresent = false;
763 boolean isUIntPresent = false;
764 for (YangType type : typeList) {
765 if (type.getDataType().equals(INT32)) {
766 setIntIndex(typeList.indexOf(type));
767 isIntPresent = true;
768 }
769 if (type.getDataType().equals(UINT16)) {
770 setUIntIndex(typeList.indexOf(type));
771 isUIntPresent = true;
772 }
773 }
774
775 return isIntPresent && isUIntPresent;
776 }
777
778 /**
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530779 * Validates conflict for int and uInt.
780 *
781 * @param typeList type list
782 * @return true if conflict is there
783 */
784 private boolean validateForConflictingShortTypes(List<YangType<?>> typeList) {
785 boolean isShortPresent = false;
786 boolean isUInt8Present = false;
787 for (YangType type : typeList) {
788 if (type.getDataType().equals(INT16)) {
789 setShortIndex(typeList.indexOf(type));
790 isShortPresent = true;
791 }
792 if (type.getDataType().equals(UINT8)) {
793 setUInt8Index(typeList.indexOf(type));
794 isUInt8Present = true;
795 }
796 }
797
798 return isShortPresent && isUInt8Present;
799 }
800
801 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530802 * Validates conflict for long and uLong.
803 *
804 * @param typeList type list
805 * @return true if conflict is there
806 */
807 private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) {
808 boolean isLongPresent = false;
809 boolean isULongPresent = false;
810 for (YangType type : typeList) {
811 if (type.getDataType().equals(INT64)) {
812 setLongIndex(typeList.indexOf(type));
813 isLongPresent = true;
814 }
815 if (type.getDataType().equals(UINT32)) {
816 setULongIndex(typeList.indexOf(type));
817 isULongPresent = true;
818 }
819 }
820
821 return isLongPresent && isULongPresent;
822 }
823
824 /**
825 * Updates attribute info in case of conflicts.
826 *
827 * @param javaAttributeInfo java attribute info
828 */
829 private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) {
830
831 if (javaAttributeInfo.isIntConflict()) {
832 if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) {
833 setUIntAttribute(javaAttributeInfo);
834 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) {
835 setIntAttribute(javaAttributeInfo);
836 }
837
838 }
839 if (javaAttributeInfo.isLongConflict()) {
840 if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) {
841 setULongAttribute(javaAttributeInfo);
842 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) {
843 setLongAttribute(javaAttributeInfo);
844 }
845
846 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530847 if (javaAttributeInfo.isShortConflict()) {
848 if (javaAttributeInfo.getAttributeType().getDataType() == UINT8) {
849 setUInt8Attribute(javaAttributeInfo);
850 } else if (javaAttributeInfo.getAttributeType().getDataType() == INT16) {
851 setShortAttribute(javaAttributeInfo);
852 }
853
854 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530855 }
856
857 /**
858 * Returns attribute for int.
859 *
860 * @return attribute for int
861 */
862 public JavaAttributeInfo getIntAttribute() {
863 return intAttribute;
864 }
865
866 /**
867 * Sets attribute for int.
868 *
869 * @param intAttribute attribute for int
870 */
871 private void setIntAttribute(JavaAttributeInfo intAttribute) {
872 this.intAttribute = intAttribute;
873 }
874
875 /**
876 * Returns attribute for long.
877 *
878 * @return attribute for long
879 */
880 public JavaAttributeInfo getLongAttribute() {
881 return longAttribute;
882 }
883
884 /**
885 * Sets attribute for long.
886 *
887 * @param longAttribute attribute for long
888 */
889 private void setLongAttribute(JavaAttributeInfo longAttribute) {
890 this.longAttribute = longAttribute;
891 }
892
893 /**
894 * Returns attribute for uInt.
895 *
896 * @return attribute for uInt
897 */
898 public JavaAttributeInfo getUIntAttribute() {
899 return uIntAttribute;
900 }
901
902 /**
903 * Sets attribute for uInt.
904 *
905 * @param uIntAttribute attribute for uInt
906 */
907 private void setUIntAttribute(JavaAttributeInfo uIntAttribute) {
908 this.uIntAttribute = uIntAttribute;
909 }
910
911 /**
912 * Returns attribute for uLong.
913 *
914 * @return attribute for uLong
915 */
916 public JavaAttributeInfo getULongAttribute() {
917 return uLongAttribute;
918 }
919
920 /**
921 * Sets attribute for uLong.
922 *
923 * @param uLongAttribute attribute for uLong
924 */
925 private void setULongAttribute(JavaAttributeInfo uLongAttribute) {
926 this.uLongAttribute = uLongAttribute;
927 }
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530928
929 /**
930 * Returns attribute for uInt8.
931 *
932 * @return attribute for uInt8
933 */
934 public JavaAttributeInfo getUInt8Attribute() {
935 return uInt8Attribute;
936 }
937
938 /**
939 * Sets attribute for uInt8.
940 *
941 * @param uInt8Attribute attribute for uInt8
942 */
943 private void setUInt8Attribute(JavaAttributeInfo uInt8Attribute) {
944 this.uInt8Attribute = uInt8Attribute;
945 }
946
947 /**
948 * Returns attribute for short.
949 *
950 * @return attribute for short
951 */
952 public JavaAttributeInfo getShortAttribute() {
953 return shortAttribute;
954 }
955
956 /**
957 * Sets attribute for short.
958 *
959 * @param shortAttribute attribute for short
960 */
961 private void setShortAttribute(JavaAttributeInfo shortAttribute) {
962 this.shortAttribute = shortAttribute;
963 }
964
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530965}