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 java.io.File; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 20 | import java.io.IOException; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 23 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangType; |
| 26 | import org.onosproject.yangutils.datamodel.YangTypeHolder; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator; |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 31 | |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT16; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32; |
| 34 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64; |
| 35 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16; |
| 36 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT8; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
| 39 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS; |
| 40 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK; |
| 41 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK; |
| 42 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK; |
| 43 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoTranslator.getQualifiedInfoOfFromString; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile; |
| 46 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc; |
| 49 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT; |
| 51 | import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.SHORT_TYPE_CONFLICT; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
| 54 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 55 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile; |
| 56 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 57 | |
| 58 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 59 | * Represents implementation of java data type code fragments temporary implementations. Maintains the temp files |
| 60 | * required specific for user defined data type java snippet generation. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 61 | */ |
| 62 | public class TempJavaTypeFragmentFiles |
| 63 | extends TempJavaFragmentFiles { |
| 64 | |
| 65 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 66 | * File name for of string method. |
| 67 | */ |
| 68 | private static final String OF_STRING_METHOD_FILE_NAME = "OfString"; |
| 69 | |
| 70 | /** |
| 71 | * File name for construction for special type like union, typedef. |
| 72 | */ |
| 73 | private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType"; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * File name for typedef class file name suffix. |
| 77 | */ |
| 78 | private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING; |
| 79 | |
| 80 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 81 | * File name for generated class file for special type like union, typedef suffix. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 82 | */ |
| 83 | private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING; |
| 84 | |
| 85 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 86 | * Integer index in type list. |
| 87 | */ |
| 88 | private int intIndex = 0; |
| 89 | |
| 90 | /** |
| 91 | * UInt index in type list. |
| 92 | */ |
| 93 | private int uIntIndex = 0; |
| 94 | |
| 95 | /** |
| 96 | * long index in type list. |
| 97 | */ |
| 98 | private int longIndex = 0; |
| 99 | |
| 100 | /** |
| 101 | * ULong index in type list. |
| 102 | */ |
| 103 | private int uLongIndex = 0; |
| 104 | |
| 105 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 106 | * short index in type list. |
| 107 | */ |
| 108 | private int shortIndex = 0; |
| 109 | |
| 110 | /** |
| 111 | * Uint8 index in type list. |
| 112 | */ |
| 113 | private int uInt8Index = 0; |
| 114 | |
| 115 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 116 | * Temporary file handle for of string method of class. |
| 117 | */ |
| 118 | private File ofStringImplTempFileHandle; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 119 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 120 | /** |
| 121 | * Temporary file handle for constructor for type class. |
| 122 | */ |
| 123 | private File constructorForTypeTempFileHandle; |
| 124 | |
| 125 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 126 | * Java file handle for typedef class file. |
| 127 | */ |
| 128 | private File typedefClassJavaFileHandle; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 129 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 130 | /** |
| 131 | * Java file handle for type class like union, typedef file. |
| 132 | */ |
| 133 | private File typeClassJavaFileHandle; |
| 134 | |
| 135 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 136 | * Java attribute for int. |
| 137 | */ |
| 138 | private JavaAttributeInfo intAttribute; |
| 139 | |
| 140 | /** |
| 141 | * Java attribute for long. |
| 142 | */ |
| 143 | private JavaAttributeInfo longAttribute; |
| 144 | |
| 145 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 146 | * Java attribute for short. |
| 147 | */ |
| 148 | private JavaAttributeInfo shortAttribute; |
| 149 | |
| 150 | /** |
| 151 | * Java attribute for uint8. |
| 152 | */ |
| 153 | private JavaAttributeInfo uInt8Attribute; |
| 154 | |
| 155 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 156 | * Java attribute for uInt. |
| 157 | */ |
| 158 | private JavaAttributeInfo uIntAttribute; |
| 159 | |
| 160 | /** |
| 161 | * Java attribute for uLong. |
| 162 | */ |
| 163 | private JavaAttributeInfo uLongAttribute; |
| 164 | |
| 165 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 166 | * Creates an instance of temporary java code fragment. |
| 167 | * |
| 168 | * @param javaFileInfo generated java file info |
| 169 | * @throws IOException when fails to create new file handle |
| 170 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 171 | TempJavaTypeFragmentFiles(JavaFileInfoTranslator javaFileInfo) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 172 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 173 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 174 | super(javaFileInfo); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * Initialize getterImpl, attributes, hash code, equals and to strings |
| 178 | * when generation file type matches to typeDef class mask. |
| 179 | */ |
| 180 | addGeneratedTempFile(OF_STRING_IMPL_MASK); |
| 181 | addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK); |
| 182 | addGeneratedTempFile(FROM_STRING_IMPL_MASK); |
| 183 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 184 | setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME)); |
| 185 | setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME)); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 186 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 187 | } |
| 188 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 189 | /** |
| 190 | * Returns type class constructor method's temporary file handle. |
| 191 | * |
| 192 | * @return type class constructor method's temporary file handle |
| 193 | */ |
| 194 | |
| 195 | public File getConstructorForTypeTempFileHandle() { |
| 196 | return constructorForTypeTempFileHandle; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Sets type class constructor method's temporary file handle. |
| 201 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 202 | * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 203 | */ |
| 204 | private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) { |
| 205 | this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle; |
| 206 | } |
| 207 | |
| 208 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 209 | * Returns java file handle for typedef class file. |
| 210 | * |
| 211 | * @return java file handle for typedef class file |
| 212 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 213 | private File getTypedefClassJavaFileHandle() { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 214 | return typedefClassJavaFileHandle; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Sets the java file handle for typedef class file. |
| 219 | * |
| 220 | * @param typedefClassJavaFileHandle java file handle |
| 221 | */ |
| 222 | private void setTypedefClassJavaFileHandle(File typedefClassJavaFileHandle) { |
| 223 | this.typedefClassJavaFileHandle = typedefClassJavaFileHandle; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns java file handle for type class file. |
| 228 | * |
| 229 | * @return java file handle for type class file |
| 230 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 231 | private File getTypeClassJavaFileHandle() { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 232 | return typeClassJavaFileHandle; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Sets the java file handle for type class file. |
| 237 | * |
| 238 | * @param typeClassJavaFileHandle type file handle |
| 239 | */ |
| 240 | private void setTypeClassJavaFileHandle(File typeClassJavaFileHandle) { |
| 241 | this.typeClassJavaFileHandle = typeClassJavaFileHandle; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Returns of string method's temporary file handle. |
| 246 | * |
| 247 | * @return of string method's temporary file handle |
| 248 | */ |
| 249 | |
| 250 | public File getOfStringImplTempFileHandle() { |
| 251 | return ofStringImplTempFileHandle; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Set of string method's temporary file handle. |
| 256 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 257 | * @param ofStringImplTempFileHandle of string method's temporary file handle |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 258 | */ |
| 259 | private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) { |
| 260 | this.ofStringImplTempFileHandle = ofStringImplTempFileHandle; |
| 261 | } |
| 262 | |
| 263 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 264 | * 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] | 265 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 266 | * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef |
| 267 | * @param pluginConfig plugin configurations for naming conventions |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 268 | * @throws IOException IO operation fail |
| 269 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 270 | void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 271 | throws IOException { |
| 272 | |
| 273 | List<YangType<?>> typeList = yangTypeHolder.getTypeList(); |
| 274 | if (typeList != null) { |
| 275 | for (YangType<?> yangType : typeList) { |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 276 | if (!(yangType instanceof YangJavaTypeTranslator)) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 277 | throw new TranslatorException("Type does not have Java info"); |
| 278 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 279 | JavaAttributeInfo javaAttributeInfo = getAttributeForType(yangType, pluginConfig); |
| 280 | addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, |
| 281 | pluginConfig, typeList); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 282 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 283 | addTypeConstructor(pluginConfig); |
| 284 | addMethodsInConflictCase(pluginConfig); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 289 | * Returns java attribute. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 290 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 291 | * @param yangType YANG type |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 292 | * @param pluginConfig plugin configurations |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 293 | * @return java attribute |
| 294 | */ |
| 295 | private JavaAttributeInfo getAttributeForType(YangType yangType, YangPluginConfig pluginConfig) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 296 | YangJavaTypeTranslator javaType = (YangJavaTypeTranslator) yangType; |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 297 | javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver()); |
| 298 | String typeName = javaType.getDataTypeName(); |
| 299 | typeName = getCamelCase(typeName, pluginConfig.getConflictResolver()); |
| 300 | return getAttributeInfoForTheData( |
| 301 | javaType.getJavaQualifiedInfo(), |
| 302 | typeName, javaType, |
| 303 | getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()), |
| 304 | false); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Adds the new attribute info to the target generated temporary files for union class. |
| 309 | * |
| 310 | * @param javaAttributeInfo the attribute info that needs to be added to temporary files |
| 311 | * @param pluginConfig plugin configurations |
| 312 | * @param typeList type list |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 313 | * @throws IOException IO operation fail |
| 314 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 315 | private void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfo, |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 316 | YangPluginConfig pluginConfig, List<YangType<?>> typeList) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 317 | throws IOException { |
| 318 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 319 | YangDataTypes attrType = javaAttributeInfo.getAttributeType().getDataType(); |
| 320 | |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 321 | if (attrType == INT16 || attrType == UINT8) { |
| 322 | boolean isShortConflict = validateForConflictingShortTypes(typeList); |
| 323 | javaAttributeInfo.setShortConflict(isShortConflict); |
| 324 | updateAttributeCondition(javaAttributeInfo); |
| 325 | if (!isShortConflict) { |
| 326 | addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig); |
| 327 | } |
| 328 | } else if (attrType == INT32 || attrType == UINT16) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 329 | boolean isIntConflict = validateForConflictingIntTypes(typeList); |
| 330 | javaAttributeInfo.setIntConflict(isIntConflict); |
| 331 | updateAttributeCondition(javaAttributeInfo); |
| 332 | if (!isIntConflict) { |
| 333 | addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig); |
| 334 | } |
| 335 | } else if (attrType == INT64 || attrType == UINT32) { |
| 336 | boolean isLongConflict = validateForConflictingLongTypes(typeList); |
| 337 | javaAttributeInfo.setLongConflict(isLongConflict); |
| 338 | updateAttributeCondition(javaAttributeInfo); |
| 339 | if (!isLongConflict) { |
| 340 | addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig); |
| 341 | } |
| 342 | } else { |
| 343 | addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig); |
| 344 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 345 | super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 346 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /** |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 350 | * Adds of method and constructor when there is no conflicting types. |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 351 | * |
| 352 | * @param javaAttributeInfo java attribute info |
| 353 | * @param pluginConfig plugin configurations |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 354 | * @throws IOException when fails to do IO operations |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 355 | */ |
| 356 | private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo, |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 357 | YangPluginConfig pluginConfig) |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 358 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 359 | if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 360 | addOfStringMethod(javaAttributeInfo, pluginConfig); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 361 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 362 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 363 | if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 364 | addTypeConstructor(javaAttributeInfo, pluginConfig); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 365 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 369 | * Adds of, getter and from string method in conflict cases. |
| 370 | * |
| 371 | * @param pluginConfig plugin configurations |
| 372 | * @throws IOException when fails to do IO operations |
| 373 | */ |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 374 | private void addMethodsInConflictCase(YangPluginConfig pluginConfig) |
| 375 | throws IOException { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 376 | JavaAttributeInfo attr = getIntAttribute(); |
| 377 | if (attr != null) { |
| 378 | attr = getUIntAttribute(); |
| 379 | } |
| 380 | if (attr != null) { |
| 381 | if (attr.isIntConflict()) { |
| 382 | if (getIntIndex() < getUIntIndex()) { |
| 383 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(), |
| 384 | getGeneratedJavaClassName(), pluginConfig) |
| 385 | + NEW_LINE); |
| 386 | addGetterImpl(getIntAttribute(), pluginConfig); |
| 387 | addFromStringMethod(getIntAttribute(), pluginConfig); |
| 388 | } else { |
| 389 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(), |
| 390 | getGeneratedJavaClassName(), pluginConfig) |
| 391 | + NEW_LINE); |
| 392 | addGetterImpl(getUIntAttribute(), pluginConfig); |
| 393 | addFromStringMethod(getUIntAttribute(), pluginConfig); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | attr = getLongAttribute(); |
| 398 | if (attr != null) { |
| 399 | attr = getULongAttribute(); |
| 400 | } |
| 401 | if (attr != null) { |
| 402 | if (attr.isLongConflict()) { |
| 403 | if (getLongIndex() < getULongIndex()) { |
| 404 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(), |
| 405 | getGeneratedJavaClassName(), pluginConfig) |
| 406 | + NEW_LINE); |
| 407 | addGetterImpl(getLongAttribute(), pluginConfig); |
| 408 | addFromStringMethod(getLongAttribute(), pluginConfig); |
| 409 | } else { |
| 410 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(), |
| 411 | getGeneratedJavaClassName(), pluginConfig) |
| 412 | + NEW_LINE); |
| 413 | addGetterImpl(getULongAttribute(), pluginConfig); |
| 414 | addFromStringMethod(getULongAttribute(), pluginConfig); |
| 415 | } |
| 416 | } |
| 417 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 418 | |
| 419 | attr = getShortAttribute(); |
| 420 | if (attr != null) { |
| 421 | attr = getUInt8Attribute(); |
| 422 | } |
| 423 | if (attr != null) { |
| 424 | if (attr.isShortConflict()) { |
| 425 | if (getShortIndex() < getUInt8Index()) { |
| 426 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getShortAttribute(), |
| 427 | getGeneratedJavaClassName(), pluginConfig) |
| 428 | + NEW_LINE); |
| 429 | addGetterImpl(getShortAttribute(), pluginConfig); |
| 430 | addFromStringMethod(getShortAttribute(), pluginConfig); |
| 431 | } else { |
| 432 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUInt8Attribute(), |
| 433 | getGeneratedJavaClassName(), pluginConfig) |
| 434 | + NEW_LINE); |
| 435 | addGetterImpl(getUInt8Attribute(), pluginConfig); |
| 436 | addFromStringMethod(getUInt8Attribute(), pluginConfig); |
| 437 | } |
| 438 | } |
| 439 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Adds from string method for conflict case. |
| 444 | * |
| 445 | * @param newAttrInfo new attribute |
| 446 | * @param pluginConfig plugin configurations |
| 447 | * @throws IOException when fails to do IO operations |
| 448 | */ |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 449 | private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig) |
| 450 | throws IOException { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 451 | |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 452 | JavaQualifiedTypeInfoTranslator qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo, |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 453 | pluginConfig.getConflictResolver()); |
| 454 | /* |
| 455 | * Create a new java attribute info with qualified information of |
| 456 | * wrapper classes. |
| 457 | */ |
| 458 | JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString, |
| 459 | newAttrInfo.getAttributeName(), |
| 460 | newAttrInfo.getAttributeType(), |
| 461 | getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false); |
| 462 | |
| 463 | addFromStringMethod(newAttrInfo, fromStringAttributeInfo); |
| 464 | } |
| 465 | |
| 466 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 467 | * Adds type constructor. |
| 468 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 469 | * @param attr attribute info |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 470 | * @param pluginConfig plugin configurations |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 471 | * @throws IOException when fails to append to temporary file |
| 472 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 473 | private void addTypeConstructor(JavaAttributeInfo attr, YangPluginConfig pluginConfig) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 474 | throws IOException { |
| 475 | appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(attr, |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 476 | getGeneratedJavaClassName(), pluginConfig) + NEW_LINE); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 480 | * Adds type constructor. |
| 481 | * |
| 482 | * @param pluginConfig plugin configurations |
| 483 | * @throws IOException when fails to append to temporary file |
| 484 | */ |
| 485 | private void addTypeConstructor(YangPluginConfig pluginConfig) |
| 486 | throws IOException { |
| 487 | JavaAttributeInfo attr = getIntAttribute(); |
| 488 | if (attr != null) { |
| 489 | attr = getUIntAttribute(); |
| 490 | } |
| 491 | if (attr != null) { |
| 492 | if (attr.isIntConflict()) { |
| 493 | appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc( |
| 494 | getIntAttribute(), |
| 495 | getUIntAttribute(), getGeneratedJavaClassName(), pluginConfig, INT_TYPE_CONFLICT, |
| 496 | getIntIndex() |
| 497 | < getUIntIndex()) + NEW_LINE); |
| 498 | } |
| 499 | } |
| 500 | attr = getLongAttribute(); |
| 501 | if (attr != null) { |
| 502 | attr = getULongAttribute(); |
| 503 | } |
| 504 | if (attr != null) { |
| 505 | if (attr.isLongConflict()) { |
| 506 | appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc( |
| 507 | getLongAttribute(), |
| 508 | getULongAttribute(), getGeneratedJavaClassName(), pluginConfig, LONG_TYPE_CONFLICT, |
| 509 | getLongIndex() |
| 510 | < getULongIndex()) + NEW_LINE); |
| 511 | } |
| 512 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 513 | attr = getShortAttribute(); |
| 514 | if (attr != null) { |
| 515 | attr = getUInt8Attribute(); |
| 516 | } |
| 517 | if (attr != null) { |
| 518 | if (attr.isShortConflict()) { |
| 519 | appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc( |
| 520 | getShortAttribute(), |
| 521 | getUInt8Attribute(), getGeneratedJavaClassName(), pluginConfig, SHORT_TYPE_CONFLICT, |
| 522 | getShortIndex() |
| 523 | < getUInt8Index()) + NEW_LINE); |
| 524 | } |
| 525 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 529 | * Adds of string for type. |
| 530 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 531 | * @param attr attribute info |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 532 | * @param pluginConfig plugin configurations |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 533 | * @throws IOException when fails to append to temporary file |
| 534 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 535 | private void addOfStringMethod(JavaAttributeInfo attr, YangPluginConfig pluginConfig) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 536 | throws IOException { |
| 537 | appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(attr, |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 538 | getGeneratedJavaClassName(), pluginConfig) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 539 | + NEW_LINE); |
| 540 | } |
| 541 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 542 | /** |
| 543 | * Removes all temporary file handles. |
| 544 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 545 | * @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] | 546 | * @throws IOException when failed to delete the temporary files |
| 547 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 548 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 549 | public void freeTemporaryResources(boolean isErrorOccurred) |
| 550 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 551 | |
| 552 | if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 553 | closeFile(getTypedefClassJavaFileHandle(), isErrorOccurred); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) { |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 557 | closeFile(getTypeClassJavaFileHandle(), isErrorOccurred); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) { |
| 561 | closeFile(getConstructorForTypeTempFileHandle(), true); |
| 562 | } |
| 563 | if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) { |
| 564 | closeFile(getOfStringImplTempFileHandle(), true); |
| 565 | } |
| 566 | if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) { |
| 567 | closeFile(getFromStringImplTempFileHandle(), true); |
| 568 | } |
| 569 | |
| 570 | super.freeTemporaryResources(isErrorOccurred); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 571 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Constructs java code exit. |
| 576 | * |
| 577 | * @param fileType generated file type |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 578 | * @param curNode current YANG node |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 579 | * @throws IOException when fails to generate java files |
| 580 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 581 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 582 | public void generateJavaFile(int fileType, YangNode curNode) |
| 583 | throws IOException { |
| 584 | List<String> imports = new ArrayList<>(); |
| 585 | if (isAttributePresent()) { |
| 586 | imports = getJavaImportData().getImports(); |
| 587 | } |
| 588 | |
| 589 | createPackage(curNode); |
| 590 | |
| 591 | /* |
| 592 | * Creates type def class file. |
| 593 | */ |
| 594 | if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) { |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 595 | addImportsToStringAndHasCodeMethods(imports, true); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 596 | setTypedefClassJavaFileHandle(getJavaFileHandle(getJavaClassName(TYPEDEF_CLASS_FILE_NAME_SUFFIX))); |
Gaurav Agrawal | 8a14752 | 2016-08-10 13:43:01 +0530 | [diff] [blame] | 597 | |
| 598 | // In case if data type is binary, MoreObjects is not required and should be removed. |
| 599 | if (curNode instanceof YangTypeHolder) { |
| 600 | YangType yangType = ((YangTypeHolder) curNode).getTypeList().get(0); |
| 601 | if (yangType.getDataType() == YangDataTypes.BINARY) { |
| 602 | imports.remove(getJavaImportData().getImportForToString()); |
| 603 | } |
| 604 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 605 | generateTypeDefClassFile(getTypedefClassJavaFileHandle(), curNode, imports); |
| 606 | } |
| 607 | /* |
| 608 | * Creates type class file. |
| 609 | */ |
| 610 | if ((fileType & GENERATE_UNION_CLASS) != 0) { |
Shankara-Huawei | a1039e5 | 2016-07-14 16:53:09 +0530 | [diff] [blame] | 611 | addImportsToStringAndHasCodeMethods(imports, true); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 612 | setTypeClassJavaFileHandle(getJavaFileHandle(getJavaClassName(UNION_TYPE_CLASS_FILE_NAME_SUFFIX))); |
| 613 | generateUnionClassFile(getTypeClassJavaFileHandle(), curNode, imports); |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Close all the file handles. |
| 618 | */ |
| 619 | freeTemporaryResources(false); |
| 620 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 621 | |
| 622 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 623 | * Returns short type index from type list. |
| 624 | * |
| 625 | * @return short type index from type list |
| 626 | */ |
| 627 | public int getShortIndex() { |
| 628 | return shortIndex; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Sets short type index from type list. |
| 633 | * |
| 634 | * @param shortIndex short type index from type list. |
| 635 | */ |
| 636 | private void setShortIndex(int shortIndex) { |
| 637 | this.shortIndex = shortIndex; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Returns uInt type index from type list. |
| 642 | * |
| 643 | * @return uInt type index from type list |
| 644 | */ |
| 645 | public int getUInt8Index() { |
| 646 | return uInt8Index; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Sets uInt8 type index from type list. |
| 651 | * |
| 652 | * @param uInt8Index uInt8 type index from type list. |
| 653 | */ |
| 654 | private void setUInt8Index(int uInt8Index) { |
| 655 | this.uInt8Index = uInt8Index; |
| 656 | } |
| 657 | |
| 658 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 659 | * Returns int type index from type list. |
| 660 | * |
| 661 | * @return int type index from type list |
| 662 | */ |
| 663 | public int getIntIndex() { |
| 664 | return intIndex; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Sets int type index from type list. |
| 669 | * |
| 670 | * @param intIndex int type index from type list. |
| 671 | */ |
| 672 | private void setIntIndex(int intIndex) { |
| 673 | this.intIndex = intIndex; |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Returns uInt type index from type list. |
| 678 | * |
| 679 | * @return uInt type index from type list |
| 680 | */ |
| 681 | public int getUIntIndex() { |
| 682 | return uIntIndex; |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Sets uInt type index from type list. |
| 687 | * |
| 688 | * @param uIntIndex uInt type index from type list. |
| 689 | */ |
| 690 | private void setUIntIndex(int uIntIndex) { |
| 691 | this.uIntIndex = uIntIndex; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Returns long type index from type list. |
| 696 | * |
| 697 | * @return long type index from type list |
| 698 | */ |
| 699 | public int getLongIndex() { |
| 700 | return longIndex; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Sets long type index from type list. |
| 705 | * |
| 706 | * @param longIndex long type index from type list. |
| 707 | */ |
| 708 | private void setLongIndex(int longIndex) { |
| 709 | this.longIndex = longIndex; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Returns uLong type index from type list. |
| 714 | * |
| 715 | * @return uLong type index from type list |
| 716 | */ |
| 717 | public int getULongIndex() { |
| 718 | return uLongIndex; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Sets uLong type index from type list. |
| 723 | * |
| 724 | * @param uLongIndex uLong type index from type list. |
| 725 | */ |
| 726 | private void setULongIndex(int uLongIndex) { |
| 727 | this.uLongIndex = uLongIndex; |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Validates conflict for int and uInt. |
| 732 | * |
| 733 | * @param typeList type list |
| 734 | * @return true if conflict is there |
| 735 | */ |
| 736 | private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) { |
| 737 | boolean isIntPresent = false; |
| 738 | boolean isUIntPresent = false; |
| 739 | for (YangType type : typeList) { |
| 740 | if (type.getDataType().equals(INT32)) { |
| 741 | setIntIndex(typeList.indexOf(type)); |
| 742 | isIntPresent = true; |
| 743 | } |
| 744 | if (type.getDataType().equals(UINT16)) { |
| 745 | setUIntIndex(typeList.indexOf(type)); |
| 746 | isUIntPresent = true; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | return isIntPresent && isUIntPresent; |
| 751 | } |
| 752 | |
| 753 | /** |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 754 | * Validates conflict for int and uInt. |
| 755 | * |
| 756 | * @param typeList type list |
| 757 | * @return true if conflict is there |
| 758 | */ |
| 759 | private boolean validateForConflictingShortTypes(List<YangType<?>> typeList) { |
| 760 | boolean isShortPresent = false; |
| 761 | boolean isUInt8Present = false; |
| 762 | for (YangType type : typeList) { |
| 763 | if (type.getDataType().equals(INT16)) { |
| 764 | setShortIndex(typeList.indexOf(type)); |
| 765 | isShortPresent = true; |
| 766 | } |
| 767 | if (type.getDataType().equals(UINT8)) { |
| 768 | setUInt8Index(typeList.indexOf(type)); |
| 769 | isUInt8Present = true; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return isShortPresent && isUInt8Present; |
| 774 | } |
| 775 | |
| 776 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 777 | * Validates conflict for long and uLong. |
| 778 | * |
| 779 | * @param typeList type list |
| 780 | * @return true if conflict is there |
| 781 | */ |
| 782 | private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) { |
| 783 | boolean isLongPresent = false; |
| 784 | boolean isULongPresent = false; |
| 785 | for (YangType type : typeList) { |
| 786 | if (type.getDataType().equals(INT64)) { |
| 787 | setLongIndex(typeList.indexOf(type)); |
| 788 | isLongPresent = true; |
| 789 | } |
| 790 | if (type.getDataType().equals(UINT32)) { |
| 791 | setULongIndex(typeList.indexOf(type)); |
| 792 | isULongPresent = true; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | return isLongPresent && isULongPresent; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * Updates attribute info in case of conflicts. |
| 801 | * |
| 802 | * @param javaAttributeInfo java attribute info |
| 803 | */ |
| 804 | private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) { |
| 805 | |
| 806 | if (javaAttributeInfo.isIntConflict()) { |
| 807 | if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) { |
| 808 | setUIntAttribute(javaAttributeInfo); |
| 809 | } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) { |
| 810 | setIntAttribute(javaAttributeInfo); |
| 811 | } |
| 812 | |
| 813 | } |
| 814 | if (javaAttributeInfo.isLongConflict()) { |
| 815 | if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) { |
| 816 | setULongAttribute(javaAttributeInfo); |
| 817 | } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) { |
| 818 | setLongAttribute(javaAttributeInfo); |
| 819 | } |
| 820 | |
| 821 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 822 | if (javaAttributeInfo.isShortConflict()) { |
| 823 | if (javaAttributeInfo.getAttributeType().getDataType() == UINT8) { |
| 824 | setUInt8Attribute(javaAttributeInfo); |
| 825 | } else if (javaAttributeInfo.getAttributeType().getDataType() == INT16) { |
| 826 | setShortAttribute(javaAttributeInfo); |
| 827 | } |
| 828 | |
| 829 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Returns attribute for int. |
| 834 | * |
| 835 | * @return attribute for int |
| 836 | */ |
| 837 | public JavaAttributeInfo getIntAttribute() { |
| 838 | return intAttribute; |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Sets attribute for int. |
| 843 | * |
| 844 | * @param intAttribute attribute for int |
| 845 | */ |
| 846 | private void setIntAttribute(JavaAttributeInfo intAttribute) { |
| 847 | this.intAttribute = intAttribute; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Returns attribute for long. |
| 852 | * |
| 853 | * @return attribute for long |
| 854 | */ |
| 855 | public JavaAttributeInfo getLongAttribute() { |
| 856 | return longAttribute; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Sets attribute for long. |
| 861 | * |
| 862 | * @param longAttribute attribute for long |
| 863 | */ |
| 864 | private void setLongAttribute(JavaAttributeInfo longAttribute) { |
| 865 | this.longAttribute = longAttribute; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * Returns attribute for uInt. |
| 870 | * |
| 871 | * @return attribute for uInt |
| 872 | */ |
| 873 | public JavaAttributeInfo getUIntAttribute() { |
| 874 | return uIntAttribute; |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * Sets attribute for uInt. |
| 879 | * |
| 880 | * @param uIntAttribute attribute for uInt |
| 881 | */ |
| 882 | private void setUIntAttribute(JavaAttributeInfo uIntAttribute) { |
| 883 | this.uIntAttribute = uIntAttribute; |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Returns attribute for uLong. |
| 888 | * |
| 889 | * @return attribute for uLong |
| 890 | */ |
| 891 | public JavaAttributeInfo getULongAttribute() { |
| 892 | return uLongAttribute; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Sets attribute for uLong. |
| 897 | * |
| 898 | * @param uLongAttribute attribute for uLong |
| 899 | */ |
| 900 | private void setULongAttribute(JavaAttributeInfo uLongAttribute) { |
| 901 | this.uLongAttribute = uLongAttribute; |
| 902 | } |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 903 | |
| 904 | /** |
| 905 | * Returns attribute for uInt8. |
| 906 | * |
| 907 | * @return attribute for uInt8 |
| 908 | */ |
| 909 | public JavaAttributeInfo getUInt8Attribute() { |
| 910 | return uInt8Attribute; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Sets attribute for uInt8. |
| 915 | * |
| 916 | * @param uInt8Attribute attribute for uInt8 |
| 917 | */ |
| 918 | private void setUInt8Attribute(JavaAttributeInfo uInt8Attribute) { |
| 919 | this.uInt8Attribute = uInt8Attribute; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Returns attribute for short. |
| 924 | * |
| 925 | * @return attribute for short |
| 926 | */ |
| 927 | public JavaAttributeInfo getShortAttribute() { |
| 928 | return shortAttribute; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Sets attribute for short. |
| 933 | * |
| 934 | * @param shortAttribute attribute for short |
| 935 | */ |
| 936 | private void setShortAttribute(JavaAttributeInfo shortAttribute) { |
| 937 | this.shortAttribute = shortAttribute; |
| 938 | } |
| 939 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 940 | } |