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