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