blob: 7bf3f2ac67ecdb89674916f9a0d0a8a46ec4f003 [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;
28import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053029import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053030import org.onosproject.yangutils.utils.io.YangPluginConfig;
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 {
Bharat saraswale3175d32016-08-31 17:50:11 +0530221 throw new TranslatorException("current node should be of enumeration type. " +
222 curNode.getName() + " in " + curNode.getLineNumber() + " at " + curNode.getCharPosition()
223 + " in " + curNode.getFileName());
Bharat saraswal250a7472016-05-12 13:16:57 +0530224 }
225 }
226
227 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530228 * Returns java attribute for enum class.
229 *
230 * @param pluginConfig plugin configurations
231 * @return java attribute
232 */
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530233 public JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530234 YangJavaTypeTranslator javaType = new YangJavaTypeTranslator();
Bharat saraswal8beac342016-08-04 02:00:03 +0530235 javaType.setDataType(INT32);
236 javaType.setDataTypeName(INT);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530237 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswal250a7472016-05-12 13:16:57 +0530238 return getAttributeInfoForTheData(
239 javaType.getJavaQualifiedInfo(),
240 javaType.getDataTypeName(), javaType,
241 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
242 false);
243 }
244
245 /**
246 * Adds current enum name to java list.
247 *
248 * @param curEnumName current enum name
249 */
250 private void addToEnumSetJavaMap(String curEnumName, int value) {
janani b3e357f62016-05-19 17:39:50 +0530251 getEnumSetJavaMap().put(getEnumJavaAttribute(curEnumName).toUpperCase(), value);
Bharat saraswal250a7472016-05-12 13:16:57 +0530252 }
253
254 /**
255 * Adds the new attribute info to the target generated temporary files.
256 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530257 * @param curEnumName the attribute name that needs to be added to temporary files
Bharat saraswal250a7472016-05-12 13:16:57 +0530258 * @throws IOException IO operation fail
259 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530260 private void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530261 throws IOException {
janani b3e357f62016-05-19 17:39:50 +0530262 addAttributesForEnumClass(getEnumJavaAttribute(curEnumName), pluginConfig);
Bharat saraswal250a7472016-05-12 13:16:57 +0530263 }
264
265 /**
266 * Constructs java code exit.
267 *
268 * @param fileType generated file type
Bharat saraswal64e7e232016-07-14 23:33:55 +0530269 * @param curNode current YANG node
Bharat saraswal250a7472016-05-12 13:16:57 +0530270 * @throws IOException when fails to generate java files
271 */
272 @Override
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530273 public void generateJavaFile(int fileType, YangNode curNode)
274 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530275 createPackage(curNode);
276 setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
277 setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
278 freeTemporaryResources(false);
279 }
280
281 /**
282 * Removes all temporary file handles.
283 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530284 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Bharat saraswal250a7472016-05-12 13:16:57 +0530285 * @throws IOException when failed to delete the temporary files
286 */
287 @Override
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530288 public void freeTemporaryResources(boolean isErrorOccurred)
289 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530290 closeFile(getEnumClassJavaFileHandle(), isErrorOccurred);
291 closeFile(getEnumClassTempFileHandle(), true);
292 super.freeTemporaryResources(isErrorOccurred);
293 }
294
295 /**
296 * Adds to enum string list.
297 *
298 * @param curEnumValue current enum value
299 */
300 private void addToEnumStringList(String curEnumValue) {
janani b3e357f62016-05-19 17:39:50 +0530301 getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase());
Bharat saraswal250a7472016-05-12 13:16:57 +0530302 }
303
304 /**
305 * Returns enum string list.
306 *
307 * @return the enumStringList
308 */
309 public List<String> getEnumStringList() {
310 return enumStringList;
311 }
312
313 /**
314 * Sets enum string list.
315 *
316 * @param enumStringList the enumStringList to set
317 */
Bharat saraswal64e7e232016-07-14 23:33:55 +0530318 private void setEnumStringList(List<String> enumStringList) {
Bharat saraswal250a7472016-05-12 13:16:57 +0530319 this.enumStringList = enumStringList;
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530320 }
321}