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.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaNotification; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 26 | |
| 27 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK; |
| 28 | import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK; |
| 29 | import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose; |
| 30 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateManagerClassFile; |
| 31 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile; |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod; |
| 34 | import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod; |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addListnersImport; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile; |
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_REG; |
| 39 | import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
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; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 43 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
| 44 | import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc; |
| 45 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile; |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Represents implementation of java service code fragments temporary implementations. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 49 | * Maintains the temp files required 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 | /** |
| 65 | * File name for generated class file for service |
| 66 | * suffix. |
| 67 | */ |
| 68 | private static final String SERVICE_FILE_NAME_SUFFIX = "Service"; |
| 69 | |
| 70 | /** |
| 71 | * File name for generated class file for manager |
| 72 | * suffix. |
| 73 | */ |
| 74 | private static final String MANAGER_FILE_NAME_SUFFIX = "Manager"; |
| 75 | |
| 76 | /** |
| 77 | * Temporary file handle for rpc interface. |
| 78 | */ |
| 79 | private File rpcInterfaceTempFileHandle; |
| 80 | |
| 81 | /** |
| 82 | * Temporary file handle for rpc manager impl. |
| 83 | */ |
| 84 | private File rpcImplTempFileHandle; |
| 85 | |
| 86 | /** |
| 87 | * Java file handle for rpc interface file. |
| 88 | */ |
| 89 | private File serviceInterfaceJavaFileHandle; |
| 90 | |
| 91 | /** |
| 92 | * Java file handle for manager impl file. |
| 93 | */ |
| 94 | private File managerJavaFileHandle; |
| 95 | |
| 96 | /** |
| 97 | * Returns rpc method's java file handle. |
| 98 | * |
| 99 | * @return java file handle |
| 100 | */ |
| 101 | private File getServiceInterfaceJavaFileHandle() { |
| 102 | return serviceInterfaceJavaFileHandle; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sets rpc method's java file handle. |
| 107 | * |
| 108 | * @param serviceInterfaceJavaFileHandle file handle for to rpc method |
| 109 | */ |
| 110 | private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) { |
| 111 | this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns managers java file handle. |
| 116 | * |
| 117 | * @return java file handle |
| 118 | */ |
| 119 | public File getManagerJavaFileHandle() { |
| 120 | return managerJavaFileHandle; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets manager java file handle. |
| 125 | * |
| 126 | * @param managerJavaFileHandle file handle for to manager |
| 127 | */ |
| 128 | public void setManagerJavaFileHandle(File managerJavaFileHandle) { |
| 129 | this.managerJavaFileHandle = managerJavaFileHandle; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns rpc method's temporary file handle. |
| 134 | * |
| 135 | * @return temporary file handle |
| 136 | */ |
| 137 | public File getRpcInterfaceTempFileHandle() { |
| 138 | return rpcInterfaceTempFileHandle; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets rpc method's temporary file handle. |
| 143 | * |
| 144 | * @param rpcInterfaceTempFileHandle file handle for to rpc method |
| 145 | */ |
| 146 | private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) { |
| 147 | this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Retrieves the manager impl temp file. |
| 152 | * |
| 153 | * @return the manager impl temp file |
| 154 | */ |
| 155 | public File getRpcImplTempFileHandle() { |
| 156 | return rpcImplTempFileHandle; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets the manager impl temp file. |
| 161 | * |
| 162 | * @param rpcImplTempFileHandle the manager impl temp file |
| 163 | */ |
| 164 | public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) { |
| 165 | this.rpcImplTempFileHandle = rpcImplTempFileHandle; |
| 166 | } |
| 167 | |
| 168 | /** |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 169 | * Creates an instance of temporary java code fragment. |
| 170 | * |
| 171 | * @param javaFileInfo generated file information |
| 172 | * @throws IOException when fails to create new file handle |
| 173 | */ |
| 174 | public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo) |
| 175 | throws IOException { |
| 176 | super(javaFileInfo); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 177 | |
| 178 | addGeneratedTempFile(RPC_INTERFACE_MASK); |
| 179 | |
| 180 | addGeneratedTempFile(RPC_IMPL_MASK); |
| 181 | |
| 182 | setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME)); |
| 183 | setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME)); |
| 184 | } |
| 185 | |
| 186 | /** |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 187 | * Constructs java code exit. |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 188 | * |
| 189 | * @param fileType generated file type |
| 190 | * @param curNode current YANG node |
| 191 | * @throws IOException when fails to generate java files |
| 192 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 193 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 194 | public void generateJavaFile(int fileType, YangNode curNode) |
| 195 | throws IOException { |
| 196 | List<String> imports = new ArrayList<>(); |
| 197 | imports = getJavaImportData().getImports(); |
| 198 | |
| 199 | createPackage(curNode); |
| 200 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 201 | boolean isNotification = false; |
| 202 | YangNode tempNode = curNode.getChild(); |
| 203 | while (tempNode != null) { |
| 204 | if (tempNode instanceof YangJavaNotification) { |
| 205 | isNotification = true; |
| 206 | break; |
| 207 | } |
| 208 | tempNode = tempNode.getNextSibling(); |
| 209 | } |
| 210 | |
| 211 | if (isNotification) { |
| 212 | addListnersImport(curNode, imports, true, LISTENER_SERVICE); |
| 213 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 214 | /** |
| 215 | * Creates rpc interface file. |
| 216 | */ |
| 217 | setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX))); |
| 218 | generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent()); |
| 219 | |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 220 | if (isNotification) { |
| 221 | addListnersImport(curNode, imports, false, LISTENER_SERVICE); |
| 222 | addListnersImport(curNode, imports, true, LISTENER_REG); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 223 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 224 | /** |
| 225 | * Create builder class file. |
| 226 | */ |
| 227 | setManagerJavaFileHandle(getJavaFileHandle(getJavaClassName(MANAGER_FILE_NAME_SUFFIX))); |
| 228 | generateManagerClassFile(getManagerJavaFileHandle(), imports, curNode, isAttributePresent()); |
| 229 | |
| 230 | insertDataIntoJavaFile(getManagerJavaFileHandle(), getJavaClassDefClose()); |
Bharat saraswal | 4aaab4d | 2016-05-17 14:19:38 +0530 | [diff] [blame] | 231 | if (isNotification) { |
| 232 | addListnersImport(curNode, imports, false, LISTENER_REG); |
| 233 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 234 | /** |
| 235 | * Close all the file handles. |
| 236 | */ |
| 237 | freeTemporaryResources(false); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Adds rpc string information to applicable temp file. |
| 242 | * |
| 243 | * @param javaAttributeInfoOfInput rpc's input node attribute info |
| 244 | * @param javaAttributeInfoOfOutput rpc's output node attribute info |
| 245 | * @param rpcName name of the rpc function |
| 246 | * @throws IOException IO operation fail |
| 247 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 248 | private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput, |
| 249 | JavaAttributeInfo javaAttributeInfoOfOutput, |
| 250 | String rpcName) throws IOException { |
| 251 | String rpcInput = EMPTY_STRING; |
| 252 | String rpcOutput = VOID; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 253 | if (javaAttributeInfoOfInput != null) { |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 254 | rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 255 | } |
| 256 | if (javaAttributeInfoOfOutput != null) { |
janani b | 703cfe4 | 2016-05-17 13:12:22 +0530 | [diff] [blame] | 257 | rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName()); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 258 | } |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 259 | appendToFile(getRpcInterfaceTempFileHandle(), generateJavaDocForRpc(rpcName, RPC_INPUT_VAR_NAME, rpcOutput) |
| 260 | + getRpcServiceMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE); |
| 261 | appendToFile(getRpcImplTempFileHandle(), getRpcManagerMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Adds the JAVA rpc snippet information. |
| 266 | * |
| 267 | * @param javaAttributeInfoOfInput rpc's input node attribute info |
| 268 | * @param javaAttributeInfoOfOutput rpc's output node attribute info |
| 269 | * @param rpcName name of the rpc function |
| 270 | * @throws IOException IO operation fail |
| 271 | */ |
| 272 | public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput, |
| 273 | JavaAttributeInfo javaAttributeInfoOfOutput, |
| 274 | String rpcName) |
| 275 | throws IOException { |
| 276 | addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, rpcName); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Removes all temporary file handles. |
| 281 | * |
| 282 | * @param isErrorOccurred when translator fails to generate java files we |
| 283 | * need to close all open file handles include temporary files |
| 284 | * and java files. |
| 285 | * @throws IOException when failed to delete the temporary files |
| 286 | */ |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 287 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 288 | public void freeTemporaryResources(boolean isErrorOccurred) |
| 289 | throws IOException { |
| 290 | boolean isError = isErrorOccurred; |
| 291 | |
| 292 | closeFile(getServiceInterfaceJavaFileHandle(), isError); |
| 293 | closeFile(getRpcInterfaceTempFileHandle(), true); |
Bharat saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 294 | closeFile(getRpcImplTempFileHandle(), true); |
| 295 | closeFile(getGetterInterfaceTempFileHandle(), true); |
| 296 | closeFile(getSetterInterfaceTempFileHandle(), true); |
| 297 | closeFile(getSetterImplTempFileHandle(), true); |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 298 | |
| 299 | super.freeTemporaryResources(isErrorOccurred); |
| 300 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 301 | } |
| 302 | } |