blob: 16503950bcf83b2de31351b81cb3351034a981c6 [file] [log] [blame]
Vinod Kumar S79a374b2016-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 saraswal250a7472016-05-12 13:16:57 +053018import org.onosproject.yangutils.datamodel.YangEnum;
19import org.onosproject.yangutils.datamodel.YangEnumeration;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.translator.exception.TranslatorException;
Shankara-Huaweib7564772016-08-02 18:13:13 +053022import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeTranslator;
Bharat saraswal2da23bf2016-08-25 15:28:39 +053023import org.onosproject.yangutils.utils.io.YangPluginConfig;
Bharat saraswal250a7472016-05-12 13:16:57 +053024
Bharat saraswal9fab16b2016-09-23 23:27:24 +053025import java.io.File;
26import java.io.IOException;
27
Bharat saraswal8beac342016-08-04 02:00:03 +053028import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
Bharat saraswal250a7472016-05-12 13:16:57 +053029import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
30import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
Bharat saraswal9fab16b2016-09-23 23:27:24 +053031import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeStringWithSchemaName;
Bharat saraswal250a7472016-05-12 13:16:57 +053032import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053033import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswal250a7472016-05-12 13:16:57 +053034import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal8beac342016-08-04 02:00:03 +053035import static org.onosproject.yangutils.utils.UtilConstants.INT;
janani b3e357f62016-05-19 17:39:50 +053036import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
37import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
Bharat saraswal8beac342016-08-04 02:00:03 +053038import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
39import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getPrefixForIdentifier;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053040
41/**
Bharat saraswal64e7e232016-07-14 23:33:55 +053042 * Represents implementation of java code fragments temporary implementations. Maintains the temp files required
43 * specific for enumeration java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053044 */
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +053045public class TempJavaEnumerationFragmentFiles
46 extends TempJavaFragmentFiles {
Bharat saraswal250a7472016-05-12 13:16:57 +053047
48 /**
49 * File name for temporary enum class.
50 */
51 private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
52
53 /**
Bharat saraswal250a7472016-05-12 13:16:57 +053054 * Temporary file handle for enum class file.
55 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053056 private final File enumClassTempFileHandle;
Bharat saraswal250a7472016-05-12 13:16:57 +053057
58 /**
59 * Java file handle for enum class.
60 */
61 private File enumClassJavaFileHandle;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053062
63 /**
64 * Creates an instance of temporary java code fragment.
65 *
66 * @param javaFileInfo generated java file info
67 * @throws IOException when fails to create new file handle
68 */
Bharat saraswale50edca2016-08-05 01:58:25 +053069 TempJavaEnumerationFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Vinod Kumar S79a374b2016-04-30 21:09:15 +053070 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +053071
Vinod Kumar S79a374b2016-04-30 21:09:15 +053072 super(javaFileInfo);
Bharat saraswal250a7472016-05-12 13:16:57 +053073 /*
74 * Initialize enum when generation file type matches to enum class mask.
75 */
76 addGeneratedTempFile(ENUM_IMPL_MASK);
Bharat saraswal9fab16b2016-09-23 23:27:24 +053077 enumClassTempFileHandle = getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME);
Bharat saraswal250a7472016-05-12 13:16:57 +053078 }
79
80 /**
Bharat saraswal250a7472016-05-12 13:16:57 +053081 * Returns temporary file handle for enum class file.
82 *
83 * @return temporary file handle for enum class file
84 */
85 public File getEnumClassTempFileHandle() {
86 return enumClassTempFileHandle;
87 }
88
89 /**
Bharat saraswal250a7472016-05-12 13:16:57 +053090 * Adds enum class attributes to temporary file.
91 *
Bharat saraswal748fc3c2016-09-06 16:38:20 +053092 * @param yangEnum YANG enum
Bharat saraswal250a7472016-05-12 13:16:57 +053093 * @throws IOException when fails to do IO operations.
94 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +053095 private void addAttributesForEnumClass(YangEnum yangEnum)
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +053096 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +053097 appendToFile(enumClassTempFileHandle,
Bharat saraswal748fc3c2016-09-06 16:38:20 +053098 generateEnumAttributeStringWithSchemaName(yangEnum.getNamedValue(),
Bharat saraswal9fab16b2016-09-23 23:27:24 +053099 yangEnum.getValue()));
Bharat saraswal250a7472016-05-12 13:16:57 +0530100 }
101
102 /**
103 * Adds enum attributes to temporary files.
104 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530105 * @param curNode current YANG node
106 * @param config plugin configurations
Bharat saraswal250a7472016-05-12 13:16:57 +0530107 * @throws IOException when fails to do IO operations
108 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530109 void addEnumAttributeToTempFiles(YangNode curNode, YangPluginConfig config)
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530110 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530111
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530112 addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum(config),
113 config);
Bharat saraswal250a7472016-05-12 13:16:57 +0530114 if (curNode instanceof YangEnumeration) {
115 YangEnumeration enumeration = (YangEnumeration) curNode;
116 for (YangEnum curEnum : enumeration.getEnumSet()) {
janani b3e357f62016-05-19 17:39:50 +0530117 String enumName = curEnum.getNamedValue();
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530118 String prefix;
janani b3e357f62016-05-19 17:39:50 +0530119 if (enumName.matches(REGEX_FOR_FIRST_DIGIT)) {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530120 prefix = getPrefixForIdentifier(
121 config.getConflictResolver());
122 if (prefix != null) {
123 curEnum.setNamedValue(prefix + enumName);
janani b3e357f62016-05-19 17:39:50 +0530124 } else {
125 curEnum.setNamedValue(YANG_AUTO_PREFIX + enumName);
126 }
127 }
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530128 addJavaSnippetInfoToApplicableTempFiles(curEnum);
Bharat saraswal250a7472016-05-12 13:16:57 +0530129 }
130 } else {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530131 throw new TranslatorException(
132 "current node should be of enumeration type. " +
133 curNode.getName() + " in " + curNode.getLineNumber() +
134 " at " + curNode.getCharPosition() + " in " + curNode
135 .getFileName());
Bharat saraswal250a7472016-05-12 13:16:57 +0530136 }
137 }
138
139 /**
Bharat saraswal64e7e232016-07-14 23:33:55 +0530140 * Returns java attribute for enum class.
141 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530142 * @param config plugin configurations
Bharat saraswal64e7e232016-07-14 23:33:55 +0530143 * @return java attribute
144 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530145 private JavaAttributeInfo getJavaAttributeForEnum(YangPluginConfig config) {
Bharat saraswal2da23bf2016-08-25 15:28:39 +0530146 YangJavaTypeTranslator javaType = new YangJavaTypeTranslator();
Bharat saraswal8beac342016-08-04 02:00:03 +0530147 javaType.setDataType(INT32);
148 javaType.setDataTypeName(INT);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530149 javaType.updateJavaQualifiedInfo(config.getConflictResolver());
Bharat saraswal250a7472016-05-12 13:16:57 +0530150 return getAttributeInfoForTheData(
151 javaType.getJavaQualifiedInfo(),
152 javaType.getDataTypeName(), javaType,
153 getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
154 false);
155 }
156
157 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530158 * Adds the new attribute info to the target generated temporary files.
159 *
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530160 * @param yangEnum @throws IOException IO operation fail
Bharat saraswal250a7472016-05-12 13:16:57 +0530161 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530162 private void addJavaSnippetInfoToApplicableTempFiles(YangEnum yangEnum)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530163 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530164 addAttributesForEnumClass(yangEnum);
Bharat saraswal250a7472016-05-12 13:16:57 +0530165 }
166
167 /**
168 * Constructs java code exit.
169 *
170 * @param fileType generated file type
Bharat saraswal64e7e232016-07-14 23:33:55 +0530171 * @param curNode current YANG node
Bharat saraswal250a7472016-05-12 13:16:57 +0530172 * @throws IOException when fails to generate java files
173 */
174 @Override
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530175 public void generateJavaFile(int fileType, YangNode curNode)
176 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530177 createPackage(curNode);
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530178 enumClassJavaFileHandle = getJavaFileHandle(getJavaClassName(EMPTY_STRING));
179 generateEnumClassFile(enumClassJavaFileHandle, curNode);
Bharat saraswal250a7472016-05-12 13:16:57 +0530180 freeTemporaryResources(false);
181 }
182
183 /**
184 * Removes all temporary file handles.
185 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530186 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
Bharat saraswal250a7472016-05-12 13:16:57 +0530187 * @throws IOException when failed to delete the temporary files
188 */
189 @Override
VinodKumarS-Huawei9a91b482016-08-19 23:22:59 +0530190 public void freeTemporaryResources(boolean isErrorOccurred)
191 throws IOException {
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530192 closeFile(enumClassJavaFileHandle, isErrorOccurred);
193 closeFile(enumClassTempFileHandle, true);
Bharat saraswal250a7472016-05-12 13:16:57 +0530194 super.freeTemporaryResources(isErrorOccurred);
195 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530196}