blob: 087788d71f2469c504438a7d8ea39008aff11922 [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
Gaurav Agrawal72cd1b72016-06-30 13:28:14 +053025import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
Bharat saraswalc0e04842016-05-12 13:16:57 +053026import org.onosproject.yangutils.datamodel.YangEnum;
27import org.onosproject.yangutils.datamodel.YangEnumeration;
28import org.onosproject.yangutils.datamodel.YangNode;
29import org.onosproject.yangutils.translator.exception.TranslatorException;
30import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053031import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
Bharat saraswalc0e04842016-05-12 13:16:57 +053032
33import 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;
janani bdd1314f2016-05-19 17:39:50 +053037import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053038import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPrefixForIdentifier;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053039import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053040import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
janani bdd1314f2016-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;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053043import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044
45/**
Bharat saraswale707f032016-07-14 23:33:55 +053046 * Represents implementation of java code fragments temporary implementations. Maintains the temp files required
47 * specific for enumeration java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053048 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053049public 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 Se4b9b0c2016-04-30 21:09:15 +053085
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 */
Bharat saraswale707f032016-07-14 23:33:55 +053092 TempJavaEnumerationFragmentFiles(JavaFileInfo javaFileInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053093 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +053094
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053095 super(javaFileInfo);
Bharat saraswalc0e04842016-05-12 13:16:57 +053096 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 */
Bharat saraswale707f032016-07-14 23:33:55 +0530110 private File getEnumClassJavaFileHandle() {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530111 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 bdd1314f2016-05-19 17:39:50 +0530180 * @param curEnumName current YANG enum
Bharat saraswalc0e04842016-05-12 13:16:57 +0530181 * @throws IOException when fails to do IO operations.
182 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530183 private void addAttributesForEnumClass(String curEnumName, YangPluginConfig pluginConfig) throws IOException {
184 appendToFile(getEnumClassTempFileHandle(),
185 generateEnumAttributeString(curEnumName, getEnumValue(), pluginConfig));
Bharat saraswalc0e04842016-05-12 13:16:57 +0530186 }
187
188 /**
189 * Adds enum attributes to temporary files.
190 *
Bharat saraswale707f032016-07-14 23:33:55 +0530191 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530192 * @param pluginConfig plugin configurations
Bharat saraswalc0e04842016-05-12 13:16:57 +0530193 * @throws IOException when fails to do IO operations
194 */
Bharat saraswale707f032016-07-14 23:33:55 +0530195 void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig pluginConfig) throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530196
Bharat saraswal33dfa012016-05-17 19:59:16 +0530197 super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(pluginConfig), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530198 if (curNode instanceof YangEnumeration) {
199 YangEnumeration enumeration = (YangEnumeration) curNode;
200 for (YangEnum curEnum : enumeration.getEnumSet()) {
janani bdd1314f2016-05-19 17:39:50 +0530201 String enumName = curEnum.getNamedValue();
Bharat saraswale707f032016-07-14 23:33:55 +0530202 String prefixForIdentifier;
janani bdd1314f2016-05-19 17:39:50 +0530203 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 saraswalc0e04842016-05-12 13:16:57 +0530211 setEnumValue(curEnum.getValue());
212 addToEnumStringList(curEnum.getNamedValue());
213 addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530214 addJavaSnippetInfoToApplicableTempFiles(curEnum.getNamedValue(), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530215 }
216 } else {
217 throw new TranslatorException("current node should be of enumeration type.");
218 }
219 }
220
221 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530222 * Returns java attribute for enum class.
223 *
224 * @param pluginConfig plugin configurations
225 * @return java attribute
226 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530227 public JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530228 YangJavaType<?> javaType = new YangJavaType<>();
229 javaType.setDataType(YangDataTypes.INT32);
230 javaType.setDataTypeName("int");
Bharat saraswal33dfa012016-05-17 19:59:16 +0530231 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530232 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 bdd1314f2016-05-19 17:39:50 +0530245 getEnumSetJavaMap().put(getEnumJavaAttribute(curEnumName).toUpperCase(), value);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530246 }
247
248 /**
249 * Adds the new attribute info to the target generated temporary files.
250 *
Bharat saraswale707f032016-07-14 23:33:55 +0530251 * @param curEnumName the attribute name that needs to be added to temporary files
Bharat saraswalc0e04842016-05-12 13:16:57 +0530252 * @throws IOException IO operation fail
253 */
Bharat saraswale707f032016-07-14 23:33:55 +0530254 private void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530255 throws IOException {
janani bdd1314f2016-05-19 17:39:50 +0530256 addAttributesForEnumClass(getEnumJavaAttribute(curEnumName), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530257 }
258
259 /**
260 * Constructs java code exit.
261 *
262 * @param fileType generated file type
Bharat saraswale707f032016-07-14 23:33:55 +0530263 * @param curNode current YANG node
Bharat saraswalc0e04842016-05-12 13:16:57 +0530264 * @throws IOException when fails to generate java files
265 */
266 @Override
267 public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
268 createPackage(curNode);
269 setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
270 setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
271 freeTemporaryResources(false);
272 }
273
274 /**
275 * Removes all temporary file handles.
276 *
Bharat saraswale707f032016-07-14 23:33:55 +0530277 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Bharat saraswalc0e04842016-05-12 13:16:57 +0530278 * @throws IOException when failed to delete the temporary files
279 */
280 @Override
281 public void freeTemporaryResources(boolean isErrorOccurred) throws IOException {
282 closeFile(getEnumClassJavaFileHandle(), isErrorOccurred);
283 closeFile(getEnumClassTempFileHandle(), true);
284 super.freeTemporaryResources(isErrorOccurred);
285 }
286
287 /**
288 * Adds to enum string list.
289 *
290 * @param curEnumValue current enum value
291 */
292 private void addToEnumStringList(String curEnumValue) {
janani bdd1314f2016-05-19 17:39:50 +0530293 getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530294 }
295
296 /**
297 * Returns enum string list.
298 *
299 * @return the enumStringList
300 */
301 public List<String> getEnumStringList() {
302 return enumStringList;
303 }
304
305 /**
306 * Sets enum string list.
307 *
308 * @param enumStringList the enumStringList to set
309 */
Bharat saraswale707f032016-07-14 23:33:55 +0530310 private void setEnumStringList(List<String> enumStringList) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530311 this.enumStringList = enumStringList;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530312 }
313}