blob: 09471cdff13bc7def4dcc0965f21df67d5428ffd [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/**
46 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053047 * Maintains the temp files required 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 */
92 public TempJavaEnumerationFragmentFiles(JavaFileInfo javaFileInfo)
93 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 */
110 public File getEnumClassJavaFileHandle() {
111 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 *
191 * @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 saraswal33dfa012016-05-17 19:59:16 +0530195 public 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();
202 String prefixForIdentifier = null;
203 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 /**
222 * Returns java attribute for enum class.
223 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530224 * @param pluginConfig plugin configurations
Bharat saraswalc0e04842016-05-12 13:16:57 +0530225 * @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 *
251 * @param curEnumName the attribute name that needs to be added to temporary
252 * files
253 * @throws IOException IO operation fail
254 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530255 void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
256 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
264 * @param curNode current YANG node
265 * @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 *
278 * @param isErrorOccurred when translator fails to generate java files we
279 * need to close all open file handles include temporary files
280 * and java files.
281 * @throws IOException when failed to delete the temporary files
282 */
283 @Override
284 public void freeTemporaryResources(boolean isErrorOccurred) throws IOException {
285 closeFile(getEnumClassJavaFileHandle(), isErrorOccurred);
286 closeFile(getEnumClassTempFileHandle(), true);
287 super.freeTemporaryResources(isErrorOccurred);
288 }
289
290 /**
291 * Adds to enum string list.
292 *
293 * @param curEnumValue current enum value
294 */
295 private void addToEnumStringList(String curEnumValue) {
janani bdd1314f2016-05-19 17:39:50 +0530296 getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530297 }
298
299 /**
300 * Returns enum string list.
301 *
302 * @return the enumStringList
303 */
304 public List<String> getEnumStringList() {
305 return enumStringList;
306 }
307
308 /**
309 * Sets enum string list.
310 *
311 * @param enumStringList the enumStringList to set
312 */
313 public void setEnumStringList(List<String> enumStringList) {
314 this.enumStringList = enumStringList;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530315 }
316}