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