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