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