Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangNode; |
| 20 | import org.onosproject.yangutils.datamodel.YangType; |
| 21 | import org.onosproject.yangutils.datamodel.YangTypeHolder; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 26 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 27 | import java.io.File; |
| 28 | import java.io.IOException; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BINARY; |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.BITS; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32; |
| 36 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64; |
| 37 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16; |
| 38 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT8; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
| 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
| 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK; |
| 43 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK; |
| 44 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK; |
| 45 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile; |
| 48 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc; |
| 51 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT; |
| 53 | import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.SHORT_TYPE_CONFLICT; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.utils.UtilConstants.BASE64; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 57 | import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_PKG; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 58 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 59 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile; |
| 60 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 63 | * 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 S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 65 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 66 | //TODO: Update with multi type handler framework. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 67 | public class TempJavaTypeFragmentFiles |
| 68 | extends TempJavaFragmentFiles { |
| 69 | |
| 70 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 71 | * 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-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 79 | |
| 80 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 81 | * 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 saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 101 | * 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-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 111 | * Temporary file handle for of string method of class. |
| 112 | */ |
| 113 | private File ofStringImplTempFileHandle; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 114 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 115 | /** |
| 116 | * Temporary file handle for constructor for type class. |
| 117 | */ |
| 118 | private File constructorForTypeTempFileHandle; |
| 119 | |
| 120 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 121 | * Java file handle for typedef class file. |
| 122 | */ |
| 123 | private File typedefClassJavaFileHandle; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 124 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 125 | /** |
| 126 | * Java file handle for type class like union, typedef file. |
| 127 | */ |
| 128 | private File typeClassJavaFileHandle; |
| 129 | |
| 130 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 131 | * Java attribute for int. |
| 132 | */ |
| 133 | private JavaAttributeInfo intAttribute; |
| 134 | |
| 135 | /** |
| 136 | * Java attribute for long. |
| 137 | */ |
| 138 | private JavaAttributeInfo longAttribute; |
| 139 | |
| 140 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 141 | * Java attribute for short. |
| 142 | */ |
| 143 | private JavaAttributeInfo shortAttribute; |
| 144 | |
| 145 | /** |
| 146 | * Java attribute for uint8. |
| 147 | */ |
| 148 | private JavaAttributeInfo uInt8Attribute; |
| 149 | |
| 150 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 151 | * Java attribute for uInt. |
| 152 | */ |
| 153 | private JavaAttributeInfo uIntAttribute; |
| 154 | |
| 155 | /** |
| 156 | * Java attribute for uLong. |
| 157 | */ |
| 158 | private JavaAttributeInfo uLongAttribute; |
| 159 | |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 160 | private List<YangType<?>> local = new ArrayList<>(); |
| 161 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 162 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 163 | * 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 saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 168 | TempJavaTypeFragmentFiles(JavaFileInfoTranslator javaFileInfo) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 169 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 170 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 171 | super(javaFileInfo); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Initialize getterImpl, attributes, hash code, equals and to strings |
| 175 | * when generation file type matches to typeDef class mask. |
| 176 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 177 | addGeneratedTempFile(OF_STRING_IMPL_MASK | CONSTRUCTOR_FOR_TYPE_MASK | |
| 178 | FROM_STRING_IMPL_MASK); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 179 | |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 180 | ofStringImplTempFileHandle = getTemporaryFileHandle( |
| 181 | OF_STRING_METHOD_FILE_NAME); |
| 182 | constructorForTypeTempFileHandle = getTemporaryFileHandle( |
| 183 | CONSTRUCTOR_FOR_TYPE_FILE_NAME); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 184 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 185 | } |
| 186 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 187 | /** |
| 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-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 198 | * Returns java file handle for typedef class file. |
| 199 | * |
| 200 | * @return java file handle for typedef class file |
| 201 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 202 | private File getTypedefClassJavaFileHandle() { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 203 | 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 220 | private File getTypeClassJavaFileHandle() { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 221 | 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 244 | * Adds all the type in the current data model node as part of the generated temporary file. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 245 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 246 | * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 247 | * @param config plugin configurations for naming conventions |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 248 | * @throws IOException IO operation fail |
| 249 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 250 | void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig config) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 251 | throws IOException { |
| 252 | |
| 253 | List<YangType<?>> typeList = yangTypeHolder.getTypeList(); |
| 254 | if (typeList != null) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 255 | 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-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 264 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 265 | JavaAttributeInfo javaAttributeInfo = getAttributeForType(type, |
| 266 | config); |
| 267 | if (type.getDataType() == BITS) { |
| 268 | addBitsHandler(javaAttributeInfo, type, this); |
| 269 | } |
Bharat saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 270 | 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 279 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 280 | config, types); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 281 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 282 | addTypeConstructor(); |
| 283 | addMethodsInConflictCase(config); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 288 | * Returns java attribute. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 289 | * |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 290 | * @param type YANG type |
| 291 | * @param config plugin configurations |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 292 | * @return java attribute |
| 293 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 294 | 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 300 | return getAttributeInfoForTheData( |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 301 | javaType.getJavaQualifiedInfo(), typeName, javaType, |
| 302 | getIsQualifiedAccessOrAddToImportList( |
| 303 | javaType.getJavaQualifiedInfo()), false); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Adds the new attribute info to the target generated temporary files for union class. |
| 308 | * |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 309 | * @param attr the attribute info that needs to be added to temporary files |
| 310 | * @param config plugin configurations |
| 311 | * @param types type list |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 312 | * @throws IOException IO operation fail |
| 313 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 314 | private void addJavaSnippetInfoToApplicableTempFiles( |
| 315 | JavaAttributeInfo attr, YangPluginConfig config, List<YangType<?>> types) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 316 | throws IOException { |
| 317 | |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 318 | YangDataTypes attrType = attr.getAttributeType().getDataType(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 319 | |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 320 | if (attrType == INT16 || attrType == UINT8) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 321 | boolean isShortConflict = validateForConflictingShortTypes(types); |
| 322 | attr.setShortConflict(isShortConflict); |
| 323 | updateAttributeCondition(attr); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 324 | if (!isShortConflict) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 325 | addMethodsWhenNoConflictingTypes(attr, config, types); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 326 | } |
| 327 | } else if (attrType == INT32 || attrType == UINT16) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 328 | boolean isIntConflict = validateForConflictingIntTypes(types); |
| 329 | attr.setIntConflict(isIntConflict); |
| 330 | updateAttributeCondition(attr); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 331 | if (!isIntConflict) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 332 | addMethodsWhenNoConflictingTypes(attr, config, types); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 333 | } |
| 334 | } else if (attrType == INT64 || attrType == UINT32) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 335 | boolean isLongConflict = validateForConflictingLongTypes(types); |
| 336 | attr.setLongConflict(isLongConflict); |
| 337 | updateAttributeCondition(attr); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 338 | if (!isLongConflict) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 339 | addMethodsWhenNoConflictingTypes(attr, config, types); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 340 | } |
| 341 | } else { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 342 | addMethodsWhenNoConflictingTypes(attr, config, types); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 343 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 344 | super.addJavaSnippetInfoToApplicableTempFiles(attr, config); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 345 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 346 | } |
| 347 | |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 348 | 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 361 | /** |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 362 | * Adds of method and constructor when there is no conflicting types. |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 363 | * |
| 364 | * @param javaAttributeInfo java attribute info |
| 365 | * @param pluginConfig plugin configurations |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 366 | * @param types YANG type |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 367 | * @throws IOException when fails to do IO operations |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 368 | */ |
| 369 | private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 370 | YangPluginConfig pluginConfig, List<YangType<?>> types) |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 371 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 372 | if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 373 | addOfStringMethod(javaAttributeInfo, pluginConfig); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 374 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 375 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 376 | if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 377 | addTypeConstructor(javaAttributeInfo, types.indexOf |
| 378 | (javaAttributeInfo.getAttributeType())); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 379 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 383 | * 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-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 388 | private void addMethodsInConflictCase(YangPluginConfig pluginConfig) |
| 389 | throws IOException { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 390 | 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 saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 398 | getGeneratedJavaClassName()) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 399 | + NEW_LINE); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 400 | addGetterImpl(getIntAttribute()); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 401 | addFromStringMethod(getIntAttribute(), pluginConfig); |
| 402 | } else { |
| 403 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 404 | getGeneratedJavaClassName()) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 405 | + NEW_LINE); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 406 | addGetterImpl(getUIntAttribute()); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 407 | 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 saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 419 | getGeneratedJavaClassName()) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 420 | + NEW_LINE); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 421 | addGetterImpl(getLongAttribute()); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 422 | addFromStringMethod(getLongAttribute(), pluginConfig); |
| 423 | } else { |
| 424 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 425 | getGeneratedJavaClassName()) |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 426 | + NEW_LINE); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 427 | addGetterImpl(getULongAttribute()); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 428 | addFromStringMethod(getULongAttribute(), pluginConfig); |
| 429 | } |
| 430 | } |
| 431 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 432 | |
| 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 saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 441 | getGeneratedJavaClassName()) |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 442 | + NEW_LINE); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 443 | addGetterImpl(getShortAttribute()); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 444 | addFromStringMethod(getShortAttribute(), pluginConfig); |
| 445 | } else { |
| 446 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUInt8Attribute(), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 447 | getGeneratedJavaClassName()) |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 448 | + NEW_LINE); |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 449 | addGetterImpl(getUInt8Attribute()); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 450 | addFromStringMethod(getUInt8Attribute(), pluginConfig); |
| 451 | } |
| 452 | } |
| 453 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 454 | } |
| 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-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 463 | private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig) |
| 464 | throws IOException { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 465 | |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 466 | JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 467 | pluginConfig.getConflictResolver()); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 468 | /* |
| 469 | * Create a new java attribute info with qualified information of |
| 470 | * wrapper classes. |
| 471 | */ |
| 472 | JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 473 | newAttrInfo.getAttributeName(), |
| 474 | newAttrInfo.getAttributeType(), |
| 475 | getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 476 | |
Bharat saraswal | 0663aff | 2016-10-18 23:16:14 +0530 | [diff] [blame] | 477 | addFromStringMethod(newAttrInfo, fromStringAttributeInfo); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 481 | * Adds type constructor. |
| 482 | * |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 483 | * @param attr attribute info |
| 484 | * @param count count of types |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 485 | * @throws IOException when fails to append to temporary file |
| 486 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 487 | private void addTypeConstructor(JavaAttributeInfo attr, int count) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 488 | throws IOException { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 489 | appendToFile(getConstructorForTypeTempFileHandle(), |
| 490 | getTypeConstructorStringAndJavaDoc( |
| 491 | attr, getGeneratedJavaClassName(), getGeneratedJavaFiles(), count) |
| 492 | + NEW_LINE); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 496 | * Adds type constructor. |
| 497 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 498 | * @throws IOException when fails to append to temporary file |
| 499 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 500 | private void addTypeConstructor() |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 501 | throws IOException { |
| 502 | JavaAttributeInfo attr = getIntAttribute(); |
| 503 | if (attr != null) { |
| 504 | attr = getUIntAttribute(); |
| 505 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 506 | boolean index = getIntIndex() < getUIntIndex(); |
| 507 | int count; |
| 508 | if (index) { |
| 509 | count = getIntIndex(); |
| 510 | } else { |
| 511 | count = getUIntIndex(); |
| 512 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 513 | if (attr != null) { |
| 514 | if (attr.isIntConflict()) { |
| 515 | appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc( |
| 516 | getIntAttribute(), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 517 | getUIntAttribute(), getGeneratedJavaClassName(), INT_TYPE_CONFLICT, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 518 | index, count) + NEW_LINE); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | attr = getLongAttribute(); |
| 522 | if (attr != null) { |
| 523 | attr = getULongAttribute(); |
| 524 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 525 | index = getLongIndex() < getULongIndex(); |
| 526 | if (index) { |
| 527 | count = getLongIndex(); |
| 528 | } else { |
| 529 | count = getULongIndex(); |
| 530 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 531 | if (attr != null) { |
| 532 | if (attr.isLongConflict()) { |
| 533 | appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc( |
| 534 | getLongAttribute(), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 535 | getULongAttribute(), getGeneratedJavaClassName(), LONG_TYPE_CONFLICT, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 536 | index, count) + NEW_LINE); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 537 | } |
| 538 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 539 | attr = getShortAttribute(); |
| 540 | if (attr != null) { |
| 541 | attr = getUInt8Attribute(); |
| 542 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 543 | index = getShortIndex() < getUInt8Index(); |
| 544 | if (index) { |
| 545 | count = getShortIndex(); |
| 546 | } else { |
| 547 | count = getUInt8Index(); |
| 548 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 549 | if (attr != null) { |
| 550 | if (attr.isShortConflict()) { |
| 551 | appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc( |
| 552 | getShortAttribute(), |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 553 | getUInt8Attribute(), getGeneratedJavaClassName(), SHORT_TYPE_CONFLICT, |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 554 | index, count) + NEW_LINE); |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 555 | } |
| 556 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 560 | * Adds of string for type. |
| 561 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 562 | * @param attr attribute info |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 563 | * @param pluginConfig plugin configurations |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 564 | * @throws IOException when fails to append to temporary file |
| 565 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 566 | private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 567 | throws IOException { |
| 568 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr, |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 569 | getGeneratedJavaClassName()) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 570 | + NEW_LINE); |
| 571 | } |
| 572 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 573 | /** |
| 574 | * Removes all temporary file handles. |
| 575 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 576 | * @param isErrorOccurred flag to tell translator that error has occurred while file generation |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 577 | * @throws IOException when failed to delete the temporary files |
| 578 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 579 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 580 | public void freeTemporaryResources(boolean isErrorOccurred) |
| 581 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 582 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 583 | if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0 || |
| 584 | (getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) { |
| 585 | closeFile(typeClassJavaFileHandle, isErrorOccurred); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 589 | closeFile(constructorForTypeTempFileHandle, true); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 590 | } |
| 591 | if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) { |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 592 | closeFile(ofStringImplTempFileHandle, true); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 593 | } |
| 594 | if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) { |
| 595 | closeFile(getFromStringImplTempFileHandle(), true); |
| 596 | } |
| 597 | |
| 598 | super.freeTemporaryResources(isErrorOccurred); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 599 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Constructs java code exit. |
| 604 | * |
| 605 | * @param fileType generated file type |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 606 | * @param curNode current YANG node |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 607 | * @throws IOException when fails to generate java files |
| 608 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 609 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 610 | 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 saraswal | 94844d6 | 2016-10-13 13:28:03 +0530 | [diff] [blame] | 618 | imports.add(getJavaImportData().getImportForHashAndEquals()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 619 | |
| 620 | /* |
| 621 | * Creates type def class file. |
| 622 | */ |
| 623 | if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 624 | setTypedefClassJavaFileHandle(getJavaFileHandle( |
| 625 | getJavaClassName(EMPTY_STRING))); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 626 | generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports); |
| 627 | } |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 628 | String bit = getJavaImportData().getImportForToBitSet(); |
| 629 | if (!imports.contains(bit)) { |
| 630 | imports.add(bit); |
| 631 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 632 | /* |
| 633 | * Creates type class file. |
| 634 | */ |
| 635 | if ((fileType & GENERATE_UNION_CLASS) != 0) { |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 636 | setTypeClassJavaFileHandle(getJavaFileHandle( |
| 637 | getJavaClassName(EMPTY_STRING))); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 638 | generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * Close all the file handles. |
| 643 | */ |
| 644 | freeTemporaryResources(false); |
| 645 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 646 | |
| 647 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 648 | * 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 684 | * 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 saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 779 | * 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 802 | * 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 saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 847 | 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 saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 855 | } |
| 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 saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 928 | |
| 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 S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 965 | } |