blob: 001807abf4e49318439aa5069abd1596eab872d3 [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
25import org.onosproject.yangutils.datamodel.YangDataTypes;
26import 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;
Bharat saraswal33dfa012016-05-17 19:59:16 +053031import org.onosproject.yangutils.translator.tojava.utils.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;
37import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
38import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
39import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053040
41/**
42 * Represents implementation of java code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053043 * Maintains the temp files required specific for enumeration java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053044 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053045public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
46
47 /**
48 * File name for temporary enum class.
49 */
50 private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
51
52 /**
53 * File name for enum class file name suffix.
54 */
55 private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
56
57 /**
58 * Current enum's value.
59 */
60 private int enumValue;
61
62 /**
63 * Contains data of enumSet.
64 */
65 private Map<String, Integer> enumStringMap = new HashMap<>();
66
67 /**
68 * Contains data of enumSet.
69 */
70 private List<String> enumStringList;
71
72 /**
73 * Temporary file handle for enum class file.
74 */
75 private File enumClassTempFileHandle;
76
77 /**
78 * Java file handle for enum class.
79 */
80 private File enumClassJavaFileHandle;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053081
82 /**
83 * Creates an instance of temporary java code fragment.
84 *
85 * @param javaFileInfo generated java file info
86 * @throws IOException when fails to create new file handle
87 */
88 public TempJavaEnumerationFragmentFiles(JavaFileInfo javaFileInfo)
89 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +053090
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053091 super(javaFileInfo);
Bharat saraswalc0e04842016-05-12 13:16:57 +053092 setEnumSetJavaMap(new HashMap<>());
93 setEnumStringList(new ArrayList<>());
94 /*
95 * Initialize enum when generation file type matches to enum class mask.
96 */
97 addGeneratedTempFile(ENUM_IMPL_MASK);
98 setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
99 }
100
101 /**
102 * Returns enum class java file handle.
103 *
104 * @return enum class java file handle
105 */
106 public File getEnumClassJavaFileHandle() {
107 return enumClassJavaFileHandle;
108 }
109
110 /**
111 * Sets enum class java file handle.
112 *
113 * @param enumClassJavaFileHandle enum class java file handle
114 */
115 private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) {
116 this.enumClassJavaFileHandle = enumClassJavaFileHandle;
117 }
118
119 /**
120 * Returns enum's value.
121 *
122 * @return enum's value
123 */
124 private int getEnumValue() {
125 return enumValue;
126 }
127
128 /**
129 * Sets enum's value.
130 *
131 * @param enumValue enum's value
132 */
133 private void setEnumValue(int enumValue) {
134 this.enumValue = enumValue;
135 }
136
137 /**
138 * Returns enum set java map.
139 *
140 * @return the enum set java map
141 */
142 public Map<String, Integer> getEnumSetJavaMap() {
143 return enumStringMap;
144 }
145
146 /**
147 * Sets enum set java map.
148 *
149 * @param map the enum set java map to set
150 */
151 private void setEnumSetJavaMap(Map<String, Integer> map) {
152 this.enumStringMap = map;
153 }
154
155 /**
156 * Returns temporary file handle for enum class file.
157 *
158 * @return temporary file handle for enum class file
159 */
160 public File getEnumClassTempFileHandle() {
161 return enumClassTempFileHandle;
162 }
163
164 /**
165 * Sets temporary file handle for enum class file.
166 *
167 * @param enumClassTempFileHandle temporary file handle for enum class file
168 */
169 private void setEnumClassTempFileHandle(File enumClassTempFileHandle) {
170 this.enumClassTempFileHandle = enumClassTempFileHandle;
171 }
172
173 /**
174 * Adds enum class attributes to temporary file.
175 *
176 * @param curEnumInfo current YANG enum
177 * @throws IOException when fails to do IO operations.
178 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530179 private void addAttributesForEnumClass(String curEnumName, YangPluginConfig pluginConfig) throws IOException {
180 appendToFile(getEnumClassTempFileHandle(),
181 generateEnumAttributeString(curEnumName, getEnumValue(), pluginConfig));
Bharat saraswalc0e04842016-05-12 13:16:57 +0530182 }
183
184 /**
185 * Adds enum attributes to temporary files.
186 *
187 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530188 * @param pluginConfig plugin configurations
Bharat saraswalc0e04842016-05-12 13:16:57 +0530189 * @throws IOException when fails to do IO operations
190 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530191 public void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig pluginConfig) throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530192
Bharat saraswal33dfa012016-05-17 19:59:16 +0530193 super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(pluginConfig), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530194 if (curNode instanceof YangEnumeration) {
195 YangEnumeration enumeration = (YangEnumeration) curNode;
196 for (YangEnum curEnum : enumeration.getEnumSet()) {
197 setEnumValue(curEnum.getValue());
198 addToEnumStringList(curEnum.getNamedValue());
199 addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530200 addJavaSnippetInfoToApplicableTempFiles(curEnum.getNamedValue(), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530201 }
202 } else {
203 throw new TranslatorException("current node should be of enumeration type.");
204 }
205 }
206
207 /**
208 * Returns java attribute for enum class.
209 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530210 * @param pluginConfig plugin configurations
Bharat saraswalc0e04842016-05-12 13:16:57 +0530211 * @return java attribute
212 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530213 public JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530214 YangJavaType<?> javaType = new YangJavaType<>();
215 javaType.setDataType(YangDataTypes.INT32);
216 javaType.setDataTypeName("int");
Bharat saraswal33dfa012016-05-17 19:59:16 +0530217 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530218 return getAttributeInfoForTheData(
219 javaType.getJavaQualifiedInfo(),
220 javaType.getDataTypeName(), javaType,
221 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
222 false);
223 }
224
225 /**
226 * Adds current enum name to java list.
227 *
228 * @param curEnumName current enum name
229 */
230 private void addToEnumSetJavaMap(String curEnumName, int value) {
231 getEnumSetJavaMap().put(curEnumName.toUpperCase(), value);
232 }
233
234 /**
235 * Adds the new attribute info to the target generated temporary files.
236 *
237 * @param curEnumName the attribute name that needs to be added to temporary
238 * files
239 * @throws IOException IO operation fail
240 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530241 void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
242 throws IOException {
243 addAttributesForEnumClass(curEnumName, pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530244 }
245
246 /**
247 * Constructs java code exit.
248 *
249 * @param fileType generated file type
250 * @param curNode current YANG node
251 * @throws IOException when fails to generate java files
252 */
253 @Override
254 public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
255 createPackage(curNode);
256 setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
257 setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
258 freeTemporaryResources(false);
259 }
260
261 /**
262 * Removes all temporary file handles.
263 *
264 * @param isErrorOccurred when translator fails to generate java files we
265 * need to close all open file handles include temporary files
266 * and java files.
267 * @throws IOException when failed to delete the temporary files
268 */
269 @Override
270 public void freeTemporaryResources(boolean isErrorOccurred) throws IOException {
271 closeFile(getEnumClassJavaFileHandle(), isErrorOccurred);
272 closeFile(getEnumClassTempFileHandle(), true);
273 super.freeTemporaryResources(isErrorOccurred);
274 }
275
276 /**
277 * Adds to enum string list.
278 *
279 * @param curEnumValue current enum value
280 */
281 private void addToEnumStringList(String curEnumValue) {
282 getEnumStringList().add(curEnumValue.toUpperCase());
283 }
284
285 /**
286 * Returns enum string list.
287 *
288 * @return the enumStringList
289 */
290 public List<String> getEnumStringList() {
291 return enumStringList;
292 }
293
294 /**
295 * Sets enum string list.
296 *
297 * @param enumStringList the enumStringList to set
298 */
299 public void setEnumStringList(List<String> enumStringList) {
300 this.enumStringList = enumStringList;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530301 }
302}