blob: d17c081198427e90da7527910a6780258449d7b6 [file] [log] [blame]
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301/*
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 */
16package org.onosproject.yangutils.translator.tojava;
17
Bharat saraswal250a7472016-05-12 13:16:57 +053018import java.io.File;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053019import java.io.IOException;
Bharat saraswal250a7472016-05-12 13:16:57 +053020import java.util.ArrayList;
21import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
24
Bharat saraswal250a7472016-05-12 13:16:57 +053025import org.onosproject.yangutils.datamodel.YangEnum;
26import org.onosproject.yangutils.datamodel.YangEnumeration;
27import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswale50edca2016-08-05 01:58:25 +053028import org.onosproject.yangutils.utils.io.YangPluginConfig;
Bharat saraswal250a7472016-05-12 13:16:57 +053029import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053030import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
Bharat saraswal250a7472016-05-12 13:16:57 +053031
Bharat saraswal8beac342016-08-04 02:00:03 +053032import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
Bharat saraswal250a7472016-05-12 13:16:57 +053033import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
34import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
35import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
36import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053037import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
janani b3e357f62016-05-19 17:39:50 +053038import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Bharat saraswal250a7472016-05-12 13:16:57 +053039import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal8beac342016-08-04 02:00:03 +053040import static org.onosproject.yangutils.utils.UtilConstants.INT;
janani b3e357f62016-05-19 17:39:50 +053041import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
42import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
Bharat saraswal8beac342016-08-04 02:00:03 +053043import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
44import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPrefixForIdentifier;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053045
46/**
Bharat saraswal64e7e232016-07-14 23:33:55 +053047 * Represents implementation of java code fragments temporary implementations. Maintains the temp files required
48 * specific for enumeration java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053049 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +053050public class TempJavaEnumerationFragmentFiles
51 extends TempJavaFragmentFiles {
Bharat saraswal250a7472016-05-12 13:16:57 +053052
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 S79a374b2016-04-30 21:09:15 +053087
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 saraswale50edca2016-08-05 01:58:25 +053094 TempJavaEnumerationFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +053095 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +053096
Vinod Kumar S79a374b2016-04-30 21:09:15 +053097 super(javaFileInfo);
Bharat saraswal250a7472016-05-12 13:16:57 +053098 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 saraswal64e7e232016-07-14 23:33:55 +0530112 private File getEnumClassJavaFileHandle() {
Bharat saraswal250a7472016-05-12 13:16:57 +0530113 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 b3e357f62016-05-19 17:39:50 +0530182 * @param curEnumName current YANG enum
Bharat saraswal250a7472016-05-12 13:16:57 +0530183 * @throws IOException when fails to do IO operations.
184 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530185 private void addAttributesForEnumClass(String curEnumName, YangPluginConfig pluginConfig)
186 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530187 appendToFile(getEnumClassTempFileHandle(),
188 generateEnumAttributeString(curEnumName, getEnumValue(), pluginConfig));
Bharat saraswal250a7472016-05-12 13:16:57 +0530189 }
190
191 /**
192 * Adds enum attributes to temporary files.
193 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530194 * @param curNode current YANG node
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530195 * @param pluginConfig plugin configurations
Bharat saraswal250a7472016-05-12 13:16:57 +0530196 * @throws IOException when fails to do IO operations
197 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530198 void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig pluginConfig)
199 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530200
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530201 super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(pluginConfig), pluginConfig);
Bharat saraswal250a7472016-05-12 13:16:57 +0530202 if (curNode instanceof YangEnumeration) {
203 YangEnumeration enumeration = (YangEnumeration) curNode;
204 for (YangEnum curEnum : enumeration.getEnumSet()) {
janani b3e357f62016-05-19 17:39:50 +0530205 String enumName = curEnum.getNamedValue();
Bharat saraswal64e7e232016-07-14 23:33:55 +0530206 String prefixForIdentifier;
janani b3e357f62016-05-19 17:39:50 +0530207 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 saraswal250a7472016-05-12 13:16:57 +0530215 setEnumValue(curEnum.getValue());
216 addToEnumStringList(curEnum.getNamedValue());
217 addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue());
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530218 addJavaSnippetInfoToApplicableTempFiles(curEnum.getNamedValue(), pluginConfig);
Bharat saraswal250a7472016-05-12 13:16:57 +0530219 }
220 } else {
221 throw new TranslatorException("current node should be of enumeration type.");
222 }
223 }
224
225 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530226 * Returns java attribute for enum class.
227 *
228 * @param pluginConfig plugin configurations
229 * @return java attribute
230 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530231 public JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) {
Shankara-Huaweib7564772016-08-02 18:13:13 +0530232 YangJavaTypeTranslator<?> javaType = new YangJavaTypeTranslator<>();
Bharat saraswal8beac342016-08-04 02:00:03 +0530233 javaType.setDataType(INT32);
234 javaType.setDataTypeName(INT);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530235 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswal250a7472016-05-12 13:16:57 +0530236 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 b3e357f62016-05-19 17:39:50 +0530249 getEnumSetJavaMap().put(getEnumJavaAttribute(curEnumName).toUpperCase(), value);
Bharat saraswal250a7472016-05-12 13:16:57 +0530250 }
251
252 /**
253 * Adds the new attribute info to the target generated temporary files.
254 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530255 * @param curEnumName the attribute name that needs to be added to temporary files
Bharat saraswal250a7472016-05-12 13:16:57 +0530256 * @throws IOException IO operation fail
257 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530258 private void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530259 throws IOException {
janani b3e357f62016-05-19 17:39:50 +0530260 addAttributesForEnumClass(getEnumJavaAttribute(curEnumName), pluginConfig);
Bharat saraswal250a7472016-05-12 13:16:57 +0530261 }
262
263 /**
264 * Constructs java code exit.
265 *
266 * @param fileType generated file type
Bharat saraswal64e7e232016-07-14 23:33:55 +0530267 * @param curNode current YANG node
Bharat saraswal250a7472016-05-12 13:16:57 +0530268 * @throws IOException when fails to generate java files
269 */
270 @Override
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530271 public void generateJavaFile(int fileType, YangNode curNode)
272 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530273 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 saraswal64e7e232016-07-14 23:33:55 +0530282 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Bharat saraswal250a7472016-05-12 13:16:57 +0530283 * @throws IOException when failed to delete the temporary files
284 */
285 @Override
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530286 public void freeTemporaryResources(boolean isErrorOccurred)
287 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530288 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 b3e357f62016-05-19 17:39:50 +0530299 getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase());
Bharat saraswal250a7472016-05-12 13:16:57 +0530300 }
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 saraswal64e7e232016-07-14 23:33:55 +0530316 private void setEnumStringList(List<String> enumStringList) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530317 this.enumStringList = enumStringList;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530318 }
319}