blob: da252d108e470c0b03762824bd81688d2a4d587a [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
Bharat saraswalc0e04842016-05-12 13:16:57 +053025import org.onosproject.yangutils.datamodel.YangEnum;
26import org.onosproject.yangutils.datamodel.YangEnumeration;
27import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053028import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
29import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
Bharat saraswalc0e04842016-05-12 13:16:57 +053030import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +053031import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
Bharat saraswalc0e04842016-05-12 13:16:57 +053032
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053033import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
Bharat saraswalc0e04842016-05-12 13:16:57 +053034import 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;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053038import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
janani bdd1314f2016-05-19 17:39:50 +053039import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Bharat saraswalc0e04842016-05-12 13:16:57 +053040import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053041import static org.onosproject.yangutils.utils.UtilConstants.INT;
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;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053044import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
45import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPrefixForIdentifier;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046
47/**
Bharat saraswale707f032016-07-14 23:33:55 +053048 * Represents implementation of java code fragments temporary implementations. Maintains the temp files required
49 * specific for enumeration java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053050 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053051public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
52
53 /**
54 * File name for temporary enum class.
55 */
56 private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
57
58 /**
59 * File name for enum class file name suffix.
60 */
61 private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
62
63 /**
64 * Current enum's value.
65 */
66 private int enumValue;
67
68 /**
69 * Contains data of enumSet.
70 */
71 private Map<String, Integer> enumStringMap = new HashMap<>();
72
73 /**
74 * Contains data of enumSet.
75 */
76 private List<String> enumStringList;
77
78 /**
79 * Temporary file handle for enum class file.
80 */
81 private File enumClassTempFileHandle;
82
83 /**
84 * Java file handle for enum class.
85 */
86 private File enumClassJavaFileHandle;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053087
88 /**
89 * Creates an instance of temporary java code fragment.
90 *
91 * @param javaFileInfo generated java file info
92 * @throws IOException when fails to create new file handle
93 */
Bharat saraswale707f032016-07-14 23:33:55 +053094 TempJavaEnumerationFragmentFiles(JavaFileInfo javaFileInfo)
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053095 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +053096
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053097 super(javaFileInfo);
Bharat saraswalc0e04842016-05-12 13:16:57 +053098 setEnumSetJavaMap(new HashMap<>());
99 setEnumStringList(new ArrayList<>());
100 /*
101 * Initialize enum when generation file type matches to enum class mask.
102 */
103 addGeneratedTempFile(ENUM_IMPL_MASK);
104 setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
105 }
106
107 /**
108 * Returns enum class java file handle.
109 *
110 * @return enum class java file handle
111 */
Bharat saraswale707f032016-07-14 23:33:55 +0530112 private File getEnumClassJavaFileHandle() {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530113 return enumClassJavaFileHandle;
114 }
115
116 /**
117 * Sets enum class java file handle.
118 *
119 * @param enumClassJavaFileHandle enum class java file handle
120 */
121 private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) {
122 this.enumClassJavaFileHandle = enumClassJavaFileHandle;
123 }
124
125 /**
126 * Returns enum's value.
127 *
128 * @return enum's value
129 */
130 private int getEnumValue() {
131 return enumValue;
132 }
133
134 /**
135 * Sets enum's value.
136 *
137 * @param enumValue enum's value
138 */
139 private void setEnumValue(int enumValue) {
140 this.enumValue = enumValue;
141 }
142
143 /**
144 * Returns enum set java map.
145 *
146 * @return the enum set java map
147 */
148 public Map<String, Integer> getEnumSetJavaMap() {
149 return enumStringMap;
150 }
151
152 /**
153 * Sets enum set java map.
154 *
155 * @param map the enum set java map to set
156 */
157 private void setEnumSetJavaMap(Map<String, Integer> map) {
158 this.enumStringMap = map;
159 }
160
161 /**
162 * Returns temporary file handle for enum class file.
163 *
164 * @return temporary file handle for enum class file
165 */
166 public File getEnumClassTempFileHandle() {
167 return enumClassTempFileHandle;
168 }
169
170 /**
171 * Sets temporary file handle for enum class file.
172 *
173 * @param enumClassTempFileHandle temporary file handle for enum class file
174 */
175 private void setEnumClassTempFileHandle(File enumClassTempFileHandle) {
176 this.enumClassTempFileHandle = enumClassTempFileHandle;
177 }
178
179 /**
180 * Adds enum class attributes to temporary file.
181 *
janani bdd1314f2016-05-19 17:39:50 +0530182 * @param curEnumName current YANG enum
Bharat saraswalc0e04842016-05-12 13:16:57 +0530183 * @throws IOException when fails to do IO operations.
184 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530185 private void addAttributesForEnumClass(String curEnumName, YangPluginConfig pluginConfig) throws IOException {
186 appendToFile(getEnumClassTempFileHandle(),
187 generateEnumAttributeString(curEnumName, getEnumValue(), pluginConfig));
Bharat saraswalc0e04842016-05-12 13:16:57 +0530188 }
189
190 /**
191 * Adds enum attributes to temporary files.
192 *
Bharat saraswale707f032016-07-14 23:33:55 +0530193 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530194 * @param pluginConfig plugin configurations
Bharat saraswalc0e04842016-05-12 13:16:57 +0530195 * @throws IOException when fails to do IO operations
196 */
Bharat saraswale707f032016-07-14 23:33:55 +0530197 void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig pluginConfig) throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530198
Bharat saraswal33dfa012016-05-17 19:59:16 +0530199 super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(pluginConfig), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530200 if (curNode instanceof YangEnumeration) {
201 YangEnumeration enumeration = (YangEnumeration) curNode;
202 for (YangEnum curEnum : enumeration.getEnumSet()) {
janani bdd1314f2016-05-19 17:39:50 +0530203 String enumName = curEnum.getNamedValue();
Bharat saraswale707f032016-07-14 23:33:55 +0530204 String prefixForIdentifier;
janani bdd1314f2016-05-19 17:39:50 +0530205 if (enumName.matches(REGEX_FOR_FIRST_DIGIT)) {
206 prefixForIdentifier = getPrefixForIdentifier(pluginConfig.getConflictResolver());
207 if (prefixForIdentifier != null) {
208 curEnum.setNamedValue(prefixForIdentifier + enumName);
209 } else {
210 curEnum.setNamedValue(YANG_AUTO_PREFIX + enumName);
211 }
212 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530213 setEnumValue(curEnum.getValue());
214 addToEnumStringList(curEnum.getNamedValue());
215 addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530216 addJavaSnippetInfoToApplicableTempFiles(curEnum.getNamedValue(), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530217 }
218 } else {
219 throw new TranslatorException("current node should be of enumeration type.");
220 }
221 }
222
223 /**
Bharat saraswale707f032016-07-14 23:33:55 +0530224 * Returns java attribute for enum class.
225 *
226 * @param pluginConfig plugin configurations
227 * @return java attribute
228 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530229 public JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig pluginConfig) {
Shankara-Huaweibdf24bb2016-08-02 18:13:13 +0530230 YangJavaTypeTranslator<?> javaType = new YangJavaTypeTranslator<>();
Bharat saraswal2d90b0c2016-08-04 02:00:03 +0530231 javaType.setDataType(INT32);
232 javaType.setDataTypeName(INT);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530233 javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530234 return getAttributeInfoForTheData(
235 javaType.getJavaQualifiedInfo(),
236 javaType.getDataTypeName(), javaType,
237 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
238 false);
239 }
240
241 /**
242 * Adds current enum name to java list.
243 *
244 * @param curEnumName current enum name
245 */
246 private void addToEnumSetJavaMap(String curEnumName, int value) {
janani bdd1314f2016-05-19 17:39:50 +0530247 getEnumSetJavaMap().put(getEnumJavaAttribute(curEnumName).toUpperCase(), value);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530248 }
249
250 /**
251 * Adds the new attribute info to the target generated temporary files.
252 *
Bharat saraswale707f032016-07-14 23:33:55 +0530253 * @param curEnumName the attribute name that needs to be added to temporary files
Bharat saraswalc0e04842016-05-12 13:16:57 +0530254 * @throws IOException IO operation fail
255 */
Bharat saraswale707f032016-07-14 23:33:55 +0530256 private void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530257 throws IOException {
janani bdd1314f2016-05-19 17:39:50 +0530258 addAttributesForEnumClass(getEnumJavaAttribute(curEnumName), pluginConfig);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530259 }
260
261 /**
262 * Constructs java code exit.
263 *
264 * @param fileType generated file type
Bharat saraswale707f032016-07-14 23:33:55 +0530265 * @param curNode current YANG node
Bharat saraswalc0e04842016-05-12 13:16:57 +0530266 * @throws IOException when fails to generate java files
267 */
268 @Override
269 public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
270 createPackage(curNode);
271 setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
272 setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
273 freeTemporaryResources(false);
274 }
275
276 /**
277 * Removes all temporary file handles.
278 *
Bharat saraswale707f032016-07-14 23:33:55 +0530279 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Bharat saraswalc0e04842016-05-12 13:16:57 +0530280 * @throws IOException when failed to delete the temporary files
281 */
282 @Override
283 public void freeTemporaryResources(boolean isErrorOccurred) throws IOException {
284 closeFile(getEnumClassJavaFileHandle(), isErrorOccurred);
285 closeFile(getEnumClassTempFileHandle(), true);
286 super.freeTemporaryResources(isErrorOccurred);
287 }
288
289 /**
290 * Adds to enum string list.
291 *
292 * @param curEnumValue current enum value
293 */
294 private void addToEnumStringList(String curEnumValue) {
janani bdd1314f2016-05-19 17:39:50 +0530295 getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase());
Bharat saraswalc0e04842016-05-12 13:16:57 +0530296 }
297
298 /**
299 * Returns enum string list.
300 *
301 * @return the enumStringList
302 */
303 public List<String> getEnumStringList() {
304 return enumStringList;
305 }
306
307 /**
308 * Sets enum string list.
309 *
310 * @param enumStringList the enumStringList to set
311 */
Bharat saraswale707f032016-07-14 23:33:55 +0530312 private void setEnumStringList(List<String> enumStringList) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530313 this.enumStringList = enumStringList;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530314 }
315}