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