blob: fe1578ad335c4bdabba5a99dd746b0271af4af58 [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-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 saraswalc0e04842016-05-12 13:16:57 +053018import java.io.File;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053019import java.io.IOException;
Bharat saraswalc0e04842016-05-12 13:16:57 +053020import java.util.ArrayList;
21import java.util.HashMap;
22import java.util.List;
23import java.util.Map;
24
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053025import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053026import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Bharat saraswalc0e04842016-05-12 13:16:57 +053027import org.onosproject.yangutils.datamodel.YangEnum;
28import org.onosproject.yangutils.datamodel.YangEnumeration;
29import org.onosproject.yangutils.datamodel.YangNode;
30import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053031import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
32import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswalc0e04842016-05-12 13:16:57 +053033
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
35import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
36import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
37import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
janani bdd1314f2016-05-19 17:39:50 +053038import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053039import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPrefixForIdentifier;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053040import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053041import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
janani bdd1314f2016-05-19 17:39:50 +053042import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
43import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053045
46/**
Bharat saraswale707f032016-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 Se4b9b0c2016-04-30 21:09:15 +053049 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053050public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
51
52 /**
53 * File name for temporary enum class.
54 */
55 private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
56
57 /**
58 * File name for enum class file name suffix.
59 */
60 private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
61
62 /**
63 * Current enum's value.
64 */
65 private int enumValue;
66
67 /**
68 * Contains data of enumSet.
69 */
70 private Map<String, Integer> enumStringMap = new HashMap<>();
71
72 /**
73 * Contains data of enumSet.
74 */
75 private List<String> enumStringList;
76
77 /**
78 * Temporary file handle for enum class file.
79 */
80 private File enumClassTempFileHandle;
81
82 /**
83 * Java file handle for enum class.
84 */
85 private File enumClassJavaFileHandle;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053086
87 /**
88 * Creates an instance of temporary java code fragment.
89 *
90 * @param javaFileInfo generated java file info
91 * @throws IOException when fails to create new file handle
92 */
Bharat saraswale707f032016-07-14 23:33:55 +053093 TempJavaEnumerationFragmentFiles(JavaFileInfo javaFileInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053094 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +053095
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053096 super(javaFileInfo);
Bharat saraswalc0e04842016-05-12 13:16:57 +053097 setEnumSetJavaMap(new HashMap<>());
98 setEnumStringList(new ArrayList<>());
99 /*
100 * Initialize enum when generation file type matches to enum class mask.
101 */
102 addGeneratedTempFile(ENUM_IMPL_MASK);
103 setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
104 }
105
106 /**
107 * Returns enum class java file handle.
108 *
109 * @return enum class java file handle
110 */
Bharat saraswale707f032016-07-14 23:33:55 +0530111 private File getEnumClassJavaFileHandle() {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530112 return enumClassJavaFileHandle;
113 }
114
115 /**
116 * Sets enum class java file handle.
117 *
118 * @param enumClassJavaFileHandle enum class java file handle
119 */
120 private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) {
121 this.enumClassJavaFileHandle = enumClassJavaFileHandle;
122 }
123
124 /**
125 * Returns enum's value.
126 *
127 * @return enum's value
128 */
129 private int getEnumValue() {
130 return enumValue;
131 }
132
133 /**
134 * Sets enum's value.
135 *
136 * @param enumValue enum's value
137 */
138 private void setEnumValue(int enumValue) {
139 this.enumValue = enumValue;
140 }
141
142 /**
143 * Returns enum set java map.
144 *
145 * @return the enum set java map
146 */
147 public Map<String, Integer> getEnumSetJavaMap() {
148 return enumStringMap;
149 }
150
151 /**
152 * Sets enum set java map.
153 *
154 * @param map the enum set java map to set
155 */
156 private void setEnumSetJavaMap(Map<String, Integer> map) {
157 this.enumStringMap = map;
158 }
159
160 /**
161 * Returns temporary file handle for enum class file.
162 *
163 * @return temporary file handle for enum class file
164 */
165 public File getEnumClassTempFileHandle() {
166 return enumClassTempFileHandle;
167 }
168
169 /**
170 * Sets temporary file handle for enum class file.
171 *
172 * @param enumClassTempFileHandle temporary file handle for enum class file
173 */
174 private void setEnumClassTempFileHandle(File enumClassTempFileHandle) {
175 this.enumClassTempFileHandle = enumClassTempFileHandle;
176 }
177
178 /**
179 * Adds enum class attributes to temporary file.
180 *
janani bdd1314f2016-05-19 17:39:50 +0530181 * @param curEnumName current YANG enum
Bharat saraswalc0e04842016-05-12 13:16:57 +0530182 * @throws IOException when fails to do IO operations.
183 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530184 private void addAttributesForEnumClass(String curEnumName, YangPluginConfig pluginConfig) throws IOException {
185 appendToFile(getEnumClassTempFileHandle(),
186 generateEnumAttributeString(curEnumName, getEnumValue(), pluginConfig));
Bharat saraswalc0e04842016-05-12 13:16:57 +0530187 }
188
189 /**
190 * Adds enum attributes to temporary files.
191 *
Bharat saraswale707f032016-07-14 23:33:55 +0530192 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530193 * @param pluginConfig plugin configurations
Bharat saraswalc0e04842016-05-12 13:16:57 +0530194 * @throws IOException when fails to do IO operations
195 */
Bharat saraswale707f032016-07-14 23:33:55 +0530196 void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig pluginConfig) throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530197
Bharat saraswal33dfa012016-05-17 19:59:16 +0530198 super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(pluginConfig), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530199 if (curNode instanceof YangEnumeration) {
200 YangEnumeration enumeration = (YangEnumeration) curNode;
201 for (YangEnum curEnum : enumeration.getEnumSet()) {
janani bdd1314f2016-05-19 17:39:50 +0530202 String enumName = curEnum.getNamedValue();
Bharat saraswale707f032016-07-14 23:33:55 +0530203 String prefixForIdentifier;
janani bdd1314f2016-05-19 17:39:50 +0530204 if (enumName.matches(REGEX_FOR_FIRST_DIGIT)) {
205 prefixForIdentifier = getPrefixForIdentifier(pluginConfig.getConflictResolver());
206 if (prefixForIdentifier != null) {
207 curEnum.setNamedValue(prefixForIdentifier + enumName);
208 } else {
209 curEnum.setNamedValue(YANG_AUTO_PREFIX + enumName);
210 }
211 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530212 setEnumValue(curEnum.getValue());
213 addToEnumStringList(curEnum.getNamedValue());
214 addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530215 addJavaSnippetInfoToApplicableTempFiles(curEnum.getNamedValue(), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530216 }
217 } else {
218 throw new TranslatorException("current node should be of enumeration type.");
219 }
220 }
221
222 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530223 * Returns java attribute for enum class.
224 *
225 * @param pluginConfig plugin configurations
226 * @return java attribute
227 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530228 public JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530229 YangJavaTypeTranslator<?> javaType = new YangJavaTypeTranslator<>();
Bharat saraswalc0e04842016-05-12 13:16:57 +0530230 javaType.setDataType(YangDataTypes.INT32);
231 javaType.setDataTypeName("int");
Bharat saraswal33dfa012016-05-17 19:59:16 +0530232 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530233 return getAttributeInfoForTheData(
234 javaType.getJavaQualifiedInfo(),
235 javaType.getDataTypeName(), javaType,
236 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
237 false);
238 }
239
240 /**
241 * Adds current enum name to java list.
242 *
243 * @param curEnumName current enum name
244 */
245 private void addToEnumSetJavaMap(String curEnumName, int value) {
janani bdd1314f2016-05-19 17:39:50 +0530246 getEnumSetJavaMap().put(getEnumJavaAttribute(curEnumName).toUpperCase(), value);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530247 }
248
249 /**
250 * Adds the new attribute info to the target generated temporary files.
251 *
Bharat saraswale707f032016-07-14 23:33:55 +0530252 * @param curEnumName the attribute name that needs to be added to temporary files
Bharat saraswalc0e04842016-05-12 13:16:57 +0530253 * @throws IOException IO operation fail
254 */
Bharat saraswale707f032016-07-14 23:33:55 +0530255 private void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530256 throws IOException {
janani bdd1314f2016-05-19 17:39:50 +0530257 addAttributesForEnumClass(getEnumJavaAttribute(curEnumName), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530258 }
259
260 /**
261 * Constructs java code exit.
262 *
263 * @param fileType generated file type
Bharat saraswale707f032016-07-14 23:33:55 +0530264 * @param curNode current YANG node
Bharat saraswalc0e04842016-05-12 13:16:57 +0530265 * @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 *
Bharat saraswale707f032016-07-14 23:33:55 +0530278 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Bharat saraswalc0e04842016-05-12 13:16:57 +0530279 * @throws IOException when failed to delete the temporary files
280 */
281 @Override
282 public void freeTemporaryResources(boolean isErrorOccurred) throws IOException {
283 closeFile(getEnumClassJavaFileHandle(), isErrorOccurred);
284 closeFile(getEnumClassTempFileHandle(), true);
285 super.freeTemporaryResources(isErrorOccurred);
286 }
287
288 /**
289 * Adds to enum string list.
290 *
291 * @param curEnumValue current enum value
292 */
293 private void addToEnumStringList(String curEnumValue) {
janani bdd1314f2016-05-19 17:39:50 +0530294 getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530295 }
296
297 /**
298 * Returns enum string list.
299 *
300 * @return the enumStringList
301 */
302 public List<String> getEnumStringList() {
303 return enumStringList;
304 }
305
306 /**
307 * Sets enum string list.
308 *
309 * @param enumStringList the enumStringList to set
310 */
Bharat saraswale707f032016-07-14 23:33:55 +0530311 private void setEnumStringList(List<String> enumStringList) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530312 this.enumStringList = enumStringList;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530313 }
314}