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 | package org.onosproject.yangutils.translator.tojava; |
| 17 | |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 18 | import java.io.File; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangEnum; |
| 26 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
| 27 | import org.onosproject.yangutils.datamodel.YangNode; |
| 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; |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 31 | |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK; |
| 34 | import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData; |
| 35 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString; |
| 36 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage; |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.INT; |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 41 | import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT; |
| 42 | import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile; |
| 44 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPrefixForIdentifier; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 45 | |
| 46 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 47 | * Represents implementation of java code fragments temporary implementations. Maintains the temp files required |
| 48 | * specific for enumeration java snippet generation. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 49 | */ |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 50 | public class TempJavaEnumerationFragmentFiles |
| 51 | extends TempJavaFragmentFiles { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * File name for temporary enum class. |
| 55 | */ |
| 56 | private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass"; |
| 57 | |
| 58 | /** |
| 59 | * File name for enum class file name suffix. |
| 60 | */ |
| 61 | private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING; |
| 62 | |
| 63 | /** |
| 64 | * Current enum's value. |
| 65 | */ |
| 66 | private int enumValue; |
| 67 | |
| 68 | /** |
| 69 | * Contains data of enumSet. |
| 70 | */ |
| 71 | private Map<String, Integer> enumStringMap = new HashMap<>(); |
| 72 | |
| 73 | /** |
| 74 | * Contains data of enumSet. |
| 75 | */ |
| 76 | private List<String> enumStringList; |
| 77 | |
| 78 | /** |
| 79 | * Temporary file handle for enum class file. |
| 80 | */ |
| 81 | private File enumClassTempFileHandle; |
| 82 | |
| 83 | /** |
| 84 | * Java file handle for enum class. |
| 85 | */ |
| 86 | private File enumClassJavaFileHandle; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * Creates an instance of temporary java code fragment. |
| 90 | * |
| 91 | * @param javaFileInfo generated java file info |
| 92 | * @throws IOException when fails to create new file handle |
| 93 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame] | 94 | TempJavaEnumerationFragmentFiles(JavaFileInfoTranslator javaFileInfo) |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 95 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 96 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 97 | super(javaFileInfo); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 98 | setEnumSetJavaMap(new HashMap<>()); |
| 99 | setEnumStringList(new ArrayList<>()); |
| 100 | /* |
| 101 | * Initialize enum when generation file type matches to enum class mask. |
| 102 | */ |
| 103 | addGeneratedTempFile(ENUM_IMPL_MASK); |
| 104 | setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns enum class java file handle. |
| 109 | * |
| 110 | * @return enum class java file handle |
| 111 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 112 | private File getEnumClassJavaFileHandle() { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 113 | return enumClassJavaFileHandle; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sets enum class java file handle. |
| 118 | * |
| 119 | * @param enumClassJavaFileHandle enum class java file handle |
| 120 | */ |
| 121 | private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) { |
| 122 | this.enumClassJavaFileHandle = enumClassJavaFileHandle; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns enum's value. |
| 127 | * |
| 128 | * @return enum's value |
| 129 | */ |
| 130 | private int getEnumValue() { |
| 131 | return enumValue; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Sets enum's value. |
| 136 | * |
| 137 | * @param enumValue enum's value |
| 138 | */ |
| 139 | private void setEnumValue(int enumValue) { |
| 140 | this.enumValue = enumValue; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns enum set java map. |
| 145 | * |
| 146 | * @return the enum set java map |
| 147 | */ |
| 148 | public Map<String, Integer> getEnumSetJavaMap() { |
| 149 | return enumStringMap; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Sets enum set java map. |
| 154 | * |
| 155 | * @param map the enum set java map to set |
| 156 | */ |
| 157 | private void setEnumSetJavaMap(Map<String, Integer> map) { |
| 158 | this.enumStringMap = map; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns temporary file handle for enum class file. |
| 163 | * |
| 164 | * @return temporary file handle for enum class file |
| 165 | */ |
| 166 | public File getEnumClassTempFileHandle() { |
| 167 | return enumClassTempFileHandle; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Sets temporary file handle for enum class file. |
| 172 | * |
| 173 | * @param enumClassTempFileHandle temporary file handle for enum class file |
| 174 | */ |
| 175 | private void setEnumClassTempFileHandle(File enumClassTempFileHandle) { |
| 176 | this.enumClassTempFileHandle = enumClassTempFileHandle; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Adds enum class attributes to temporary file. |
| 181 | * |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 182 | * @param curEnumName current YANG enum |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 183 | * @throws IOException when fails to do IO operations. |
| 184 | */ |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 185 | private void addAttributesForEnumClass(String curEnumName, YangPluginConfig pluginConfig) |
| 186 | throws IOException { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 187 | appendToFile(getEnumClassTempFileHandle(), |
| 188 | generateEnumAttributeString(curEnumName, getEnumValue(), pluginConfig)); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Adds enum attributes to temporary files. |
| 193 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 194 | * @param curNode current YANG node |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 195 | * @param pluginConfig plugin configurations |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 196 | * @throws IOException when fails to do IO operations |
| 197 | */ |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 198 | void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig pluginConfig) |
| 199 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 200 | |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 201 | super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(pluginConfig), pluginConfig); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 202 | if (curNode instanceof YangEnumeration) { |
| 203 | YangEnumeration enumeration = (YangEnumeration) curNode; |
| 204 | for (YangEnum curEnum : enumeration.getEnumSet()) { |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 205 | String enumName = curEnum.getNamedValue(); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 206 | String prefixForIdentifier; |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 207 | if (enumName.matches(REGEX_FOR_FIRST_DIGIT)) { |
| 208 | prefixForIdentifier = getPrefixForIdentifier(pluginConfig.getConflictResolver()); |
| 209 | if (prefixForIdentifier != null) { |
| 210 | curEnum.setNamedValue(prefixForIdentifier + enumName); |
| 211 | } else { |
| 212 | curEnum.setNamedValue(YANG_AUTO_PREFIX + enumName); |
| 213 | } |
| 214 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 215 | setEnumValue(curEnum.getValue()); |
| 216 | addToEnumStringList(curEnum.getNamedValue()); |
| 217 | addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue()); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 218 | addJavaSnippetInfoToApplicableTempFiles(curEnum.getNamedValue(), pluginConfig); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 219 | } |
| 220 | } else { |
| 221 | throw new TranslatorException("current node should be of enumeration type."); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 226 | * Returns java attribute for enum class. |
| 227 | * |
| 228 | * @param pluginConfig plugin configurations |
| 229 | * @return java attribute |
| 230 | */ |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 231 | public JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) { |
Bharat saraswal | 2da23bf | 2016-08-25 15:28:39 +0530 | [diff] [blame] | 232 | YangJavaTypeTranslator javaType = new YangJavaTypeTranslator(); |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 233 | javaType.setDataType(INT32); |
| 234 | javaType.setDataTypeName(INT); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 235 | javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver()); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 236 | return getAttributeInfoForTheData( |
| 237 | javaType.getJavaQualifiedInfo(), |
| 238 | javaType.getDataTypeName(), javaType, |
| 239 | getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()), |
| 240 | false); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Adds current enum name to java list. |
| 245 | * |
| 246 | * @param curEnumName current enum name |
| 247 | */ |
| 248 | private void addToEnumSetJavaMap(String curEnumName, int value) { |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 249 | getEnumSetJavaMap().put(getEnumJavaAttribute(curEnumName).toUpperCase(), value); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Adds the new attribute info to the target generated temporary files. |
| 254 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 255 | * @param curEnumName the attribute name that needs to be added to temporary files |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 256 | * @throws IOException IO operation fail |
| 257 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 258 | private void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig) |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 259 | throws IOException { |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 260 | addAttributesForEnumClass(getEnumJavaAttribute(curEnumName), pluginConfig); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Constructs java code exit. |
| 265 | * |
| 266 | * @param fileType generated file type |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 267 | * @param curNode current YANG node |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 268 | * @throws IOException when fails to generate java files |
| 269 | */ |
| 270 | @Override |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 271 | public void generateJavaFile(int fileType, YangNode curNode) |
| 272 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 273 | createPackage(curNode); |
| 274 | setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX))); |
| 275 | setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode)); |
| 276 | freeTemporaryResources(false); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Removes all temporary file handles. |
| 281 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 282 | * @param isErrorOccurred flag to tell translator that error has occurred while file generation |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 283 | * @throws IOException when failed to delete the temporary files |
| 284 | */ |
| 285 | @Override |
VinodKumarS-Huawei | 9a91b48 | 2016-08-19 23:22:59 +0530 | [diff] [blame] | 286 | public void freeTemporaryResources(boolean isErrorOccurred) |
| 287 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 288 | closeFile(getEnumClassJavaFileHandle(), isErrorOccurred); |
| 289 | closeFile(getEnumClassTempFileHandle(), true); |
| 290 | super.freeTemporaryResources(isErrorOccurred); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Adds to enum string list. |
| 295 | * |
| 296 | * @param curEnumValue current enum value |
| 297 | */ |
| 298 | private void addToEnumStringList(String curEnumValue) { |
janani b | 3e357f6 | 2016-05-19 17:39:50 +0530 | [diff] [blame] | 299 | getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase()); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Returns enum string list. |
| 304 | * |
| 305 | * @return the enumStringList |
| 306 | */ |
| 307 | public List<String> getEnumStringList() { |
| 308 | return enumStringList; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Sets enum string list. |
| 313 | * |
| 314 | * @param enumStringList the enumStringList to set |
| 315 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 316 | private void setEnumStringList(List<String> enumStringList) { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 317 | this.enumStringList = enumStringList; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 318 | } |
| 319 | } |