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