Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 19 | import java.io.File; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 20 | import java.io.IOException; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 24 | import org.onosproject.yangutils.utils.io.YangPluginConfig; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator; |
| 26 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 28 | |
| 29 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK; |
| 30 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.addListenersImport; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.addResolvedAugmentedDataNodeImports; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod; |
| 36 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME; |
| 41 | import static org.onosproject.yangutils.utils.UtilConstants.VOID; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 42 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 44 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 48 | * Represents implementation of java service code fragments temporary implementations. Maintains the temp files required |
| 49 | * specific for service and manager java snippet generation. |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 50 | */ |
| 51 | public class TempJavaServiceFragmentFiles |
| 52 | extends TempJavaFragmentFiles { |
| 53 | |
| 54 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 55 | * File name for rpc method. |
| 56 | */ |
| 57 | private static final String RPC_INTERFACE_FILE_NAME = "Rpc"; |
| 58 | |
| 59 | /** |
| 60 | * File name for rpc implementation method. |
| 61 | */ |
| 62 | private static final String RPC_IMPL_FILE_NAME = "RpcImpl"; |
| 63 | |
| 64 | /** |
Bharat saraswal | 039f59c | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 65 | * File name for generated class file for service suffix. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 66 | */ |
| 67 | private static final String SERVICE_FILE_NAME_SUFFIX = "Service"; |
| 68 | |
| 69 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 70 | * Temporary file handle for rpc interface. |
| 71 | */ |
| 72 | private File rpcInterfaceTempFileHandle; |
| 73 | |
| 74 | /** |
| 75 | * Temporary file handle for rpc manager impl. |
| 76 | */ |
| 77 | private File rpcImplTempFileHandle; |
| 78 | |
| 79 | /** |
| 80 | * Java file handle for rpc interface file. |
| 81 | */ |
| 82 | private File serviceInterfaceJavaFileHandle; |
| 83 | |
| 84 | /** |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 85 | * Creates an instance of temporary java code fragment. |
| 86 | * |
| 87 | * @param javaFileInfo generated file information |
| 88 | * @throws IOException when fails to create new file handle |
| 89 | */ |
Bharat saraswal | e50edca | 2016-08-05 01:58:25 +0530 | [diff] [blame^] | 90 | TempJavaServiceFragmentFiles(JavaFileInfoTranslator javaFileInfo) |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 91 | throws IOException { |
| 92 | setJavaExtendsListHolder(new JavaExtendsListHolder()); |
| 93 | setJavaImportData(new JavaImportData()); |
| 94 | setJavaFileInfo(javaFileInfo); |
| 95 | setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(), |
| 96 | getJavaFileInfo().getPackageFilePath())); |
| 97 | addGeneratedTempFile(RPC_INTERFACE_MASK); |
| 98 | addGeneratedTempFile(RPC_IMPL_MASK); |
| 99 | |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 100 | setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME)); |
| 101 | setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME)); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 105 | * Returns rpc method's java file handle. |
| 106 | * |
| 107 | * @return java file handle |
| 108 | */ |
| 109 | private File getServiceInterfaceJavaFileHandle() { |
| 110 | return serviceInterfaceJavaFileHandle; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets rpc method's java file handle. |
| 115 | * |
| 116 | * @param serviceInterfaceJavaFileHandle file handle for to rpc method |
| 117 | */ |
| 118 | private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) { |
| 119 | this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle; |
| 120 | } |
| 121 | |
| 122 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 123 | * Returns rpc method's temporary file handle. |
| 124 | * |
| 125 | * @return temporary file handle |
| 126 | */ |
| 127 | public File getRpcInterfaceTempFileHandle() { |
| 128 | return rpcInterfaceTempFileHandle; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sets rpc method's temporary file handle. |
| 133 | * |
| 134 | * @param rpcInterfaceTempFileHandle file handle for to rpc method |
| 135 | */ |
| 136 | private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) { |
| 137 | this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Retrieves the manager impl temp file. |
| 142 | * |
| 143 | * @return the manager impl temp file |
| 144 | */ |
| 145 | public File getRpcImplTempFileHandle() { |
| 146 | return rpcImplTempFileHandle; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Sets the manager impl temp file. |
| 151 | * |
| 152 | * @param rpcImplTempFileHandle the manager impl temp file |
| 153 | */ |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 154 | private void setRpcImplTempFileHandle(File rpcImplTempFileHandle) { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 155 | this.rpcImplTempFileHandle = rpcImplTempFileHandle; |
| 156 | } |
| 157 | |
| 158 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 159 | * Constructs java code exit. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 160 | * |
| 161 | * @param fileType generated file type |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 162 | * @param curNode current YANG node |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 163 | * @throws IOException when fails to generate java files |
| 164 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 165 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 166 | public void generateJavaFile(int fileType, YangNode curNode) |
| 167 | throws IOException { |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 168 | |
| 169 | addResolvedAugmentedDataNodeImports(curNode); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 170 | List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getServiceTempFiles() |
| 171 | .getJavaImportData().getImports(); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 172 | createPackage(curNode); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 173 | boolean isNotification = false; |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 174 | if (curNode instanceof YangJavaModuleTranslator) { |
| 175 | if (!((YangJavaModuleTranslator) curNode).getNotificationNodes().isEmpty()) { |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 176 | isNotification = true; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 177 | } |
Shankara-Huawei | b756477 | 2016-08-02 18:13:13 +0530 | [diff] [blame] | 178 | } else if (curNode instanceof YangJavaSubModuleTranslator) { |
| 179 | if (!((YangJavaSubModuleTranslator) curNode).getNotificationNodes().isEmpty()) { |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 180 | isNotification = true; |
| 181 | } |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (isNotification) { |
Bharat saraswal | d14cbe8 | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 185 | addListenersImport(curNode, imports, true, LISTENER_SERVICE); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 186 | } |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 187 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 188 | setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX))); |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 189 | generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports); |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 190 | |
| 191 | // Close all the file handles. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 192 | freeTemporaryResources(false); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Adds rpc string information to applicable temp file. |
| 197 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 198 | * @param javaAttributeInfoOfInput RPCs input node attribute info |
| 199 | * @param javaAttributeInfoOfOutput RPCs output node attribute info |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 200 | * @param rpcName name of the rpc function |
| 201 | * @param pluginConfig plugin configurations |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 202 | * @throws IOException IO operation fail |
| 203 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 204 | private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput, |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 205 | JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig, |
| 206 | String rpcName) |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 207 | throws IOException { |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 208 | String rpcInput = EMPTY_STRING; |
| 209 | String rpcOutput = VOID; |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 210 | String rpcInputJavaDoc = EMPTY_STRING; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 211 | if (javaAttributeInfoOfInput != null) { |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 212 | rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 213 | } |
| 214 | if (javaAttributeInfoOfOutput != null) { |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 215 | rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 216 | } |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 217 | if (!rpcInput.equals(EMPTY_STRING)) { |
| 218 | rpcInputJavaDoc = RPC_INPUT_VAR_NAME; |
| 219 | } |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 220 | appendToFile(getRpcInterfaceTempFileHandle(), |
Bharat saraswal | 5cd9e9c | 2016-05-26 23:48:38 +0530 | [diff] [blame] | 221 | generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig) |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 222 | + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE); |
| 223 | appendToFile(getRpcImplTempFileHandle(), |
| 224 | getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Adds the JAVA rpc snippet information. |
| 229 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 230 | * @param javaAttributeInfoOfInput RPCs input node attribute info |
| 231 | * @param javaAttributeInfoOfOutput RPCs output node attribute info |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 232 | * @param pluginConfig plugin configurations |
| 233 | * @param rpcName name of the rpc function |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 234 | * @throws IOException IO operation fail |
| 235 | */ |
| 236 | public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput, |
Bharat saraswal | af413b8 | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 237 | JavaAttributeInfo javaAttributeInfoOfOutput, |
| 238 | YangPluginConfig pluginConfig, String rpcName) |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 239 | throws IOException { |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 240 | addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 244 | * Removes all temporary file handles. |
| 245 | * |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 246 | * @param isErrorOccurred flag to tell translator that error has occurred while file generation |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 247 | * @throws IOException when failed to delete the temporary files |
| 248 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 249 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 250 | public void freeTemporaryResources(boolean isErrorOccurred) |
| 251 | throws IOException { |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 252 | |
Bharat saraswal | 64e7e23 | 2016-07-14 23:33:55 +0530 | [diff] [blame] | 253 | closeFile(getServiceInterfaceJavaFileHandle(), isErrorOccurred); |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 254 | |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 255 | closeFile(getRpcInterfaceTempFileHandle(), true); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 256 | closeFile(getRpcImplTempFileHandle(), true); |
| 257 | closeFile(getGetterInterfaceTempFileHandle(), true); |
| 258 | closeFile(getSetterInterfaceTempFileHandle(), true); |
| 259 | closeFile(getSetterImplTempFileHandle(), true); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 260 | |
| 261 | super.freeTemporaryResources(isErrorOccurred); |
| 262 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 263 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 264 | } |