blob: 97817d423cfda9723e2f09b1cac9be080985afc9 [file] [log] [blame]
Vinod Kumar S79a374b2016-04-30 21:09:15 +05301/*
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
17package org.onosproject.yangutils.translator.tojava;
18
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053019import java.io.File;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053020import java.io.IOException;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053021import java.util.List;
22
23import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswale50edca2016-08-05 01:58:25 +053024import org.onosproject.yangutils.utils.io.YangPluginConfig;
Shankara-Huaweib7564772016-08-02 18:13:13 +053025import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModuleTranslator;
26import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModuleTranslator;
Bharat saraswalaf413b82016-07-14 15:18:20 +053027import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053028
29import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK;
30import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK;
Bharat saraswalaf413b82016-07-14 15:18:20 +053031import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.addListenersImport;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053032import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
Bharat saraswal8beac342016-08-04 02:00:03 +053033import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.addResolvedAugmentedDataNodeImports;
Bharat saraswalaf413b82016-07-14 15:18:20 +053034import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053035import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
36import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
Bharat saraswal250a7472016-05-12 13:16:57 +053037import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +053038import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053039import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal250a7472016-05-12 13:16:57 +053040import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
41import static org.onosproject.yangutils.utils.UtilConstants.VOID;
Bharat saraswalaf413b82016-07-14 15:18:20 +053042import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Bharat saraswalaf413b82016-07-14 15:18:20 +053043import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
Bharat saraswalaf413b82016-07-14 15:18:20 +053044import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswalaf413b82016-07-14 15:18:20 +053045import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053046
47/**
Bharat saraswalaf413b82016-07-14 15:18:20 +053048 * 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 S79a374b2016-04-30 21:09:15 +053050 */
51public class TempJavaServiceFragmentFiles
52 extends TempJavaFragmentFiles {
53
54 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053055 * 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 saraswal039f59c2016-07-14 21:57:13 +053065 * File name for generated class file for service suffix.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053066 */
67 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
68
69 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053070 * 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 saraswalaf413b82016-07-14 15:18:20 +053085 * 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 saraswale50edca2016-08-05 01:58:25 +053090 TempJavaServiceFragmentFiles(JavaFileInfoTranslator javaFileInfo)
Bharat saraswalaf413b82016-07-14 15:18:20 +053091 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 saraswalaf413b82016-07-14 15:18:20 +0530100 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
101 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530102 }
103
104 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530105 * 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-Huawei6266db32016-05-10 17:58:57 +0530123 * 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 saraswal64e7e232016-07-14 23:33:55 +0530154 private void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530155 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
156 }
157
158 /**
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530159 * Constructs java code exit.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530160 *
161 * @param fileType generated file type
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530162 * @param curNode current YANG node
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530163 * @throws IOException when fails to generate java files
164 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530165 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530166 public void generateJavaFile(int fileType, YangNode curNode)
167 throws IOException {
Bharat saraswal8beac342016-08-04 02:00:03 +0530168
169 addResolvedAugmentedDataNodeImports(curNode);
Bharat saraswalaf413b82016-07-14 15:18:20 +0530170 List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getServiceTempFiles()
171 .getJavaImportData().getImports();
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530172 createPackage(curNode);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530173 boolean isNotification = false;
Shankara-Huaweib7564772016-08-02 18:13:13 +0530174 if (curNode instanceof YangJavaModuleTranslator) {
175 if (!((YangJavaModuleTranslator) curNode).getNotificationNodes().isEmpty()) {
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530176 isNotification = true;
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530177 }
Shankara-Huaweib7564772016-08-02 18:13:13 +0530178 } else if (curNode instanceof YangJavaSubModuleTranslator) {
179 if (!((YangJavaSubModuleTranslator) curNode).getNotificationNodes().isEmpty()) {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530180 isNotification = true;
181 }
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530182 }
183
184 if (isNotification) {
Bharat saraswald14cbe82016-07-14 13:26:18 +0530185 addListenersImport(curNode, imports, true, LISTENER_SERVICE);
Bharat saraswal4aaab4d2016-05-17 14:19:38 +0530186 }
Bharat saraswal64e7e232016-07-14 23:33:55 +0530187
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530188 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
Bharat saraswalaf413b82016-07-14 15:18:20 +0530189 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports);
Bharat saraswal64e7e232016-07-14 23:33:55 +0530190
191 // Close all the file handles.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530192 freeTemporaryResources(false);
193 }
194
195 /**
196 * Adds rpc string information to applicable temp file.
197 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530198 * @param javaAttributeInfoOfInput RPCs input node attribute info
199 * @param javaAttributeInfoOfOutput RPCs output node attribute info
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530200 * @param rpcName name of the rpc function
201 * @param pluginConfig plugin configurations
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530202 * @throws IOException IO operation fail
203 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530204 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswalaf413b82016-07-14 15:18:20 +0530205 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
206 String rpcName)
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530207 throws IOException {
Bharat saraswal250a7472016-05-12 13:16:57 +0530208 String rpcInput = EMPTY_STRING;
209 String rpcOutput = VOID;
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530210 String rpcInputJavaDoc = EMPTY_STRING;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530211 if (javaAttributeInfoOfInput != null) {
janani b703cfe42016-05-17 13:12:22 +0530212 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530213 }
214 if (javaAttributeInfoOfOutput != null) {
janani b703cfe42016-05-17 13:12:22 +0530215 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530216 }
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530217 if (!rpcInput.equals(EMPTY_STRING)) {
218 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
219 }
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530220 appendToFile(getRpcInterfaceTempFileHandle(),
Bharat saraswal5cd9e9c2016-05-26 23:48:38 +0530221 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530222 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
223 appendToFile(getRpcImplTempFileHandle(),
224 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530225 }
226
227 /**
228 * Adds the JAVA rpc snippet information.
229 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530230 * @param javaAttributeInfoOfInput RPCs input node attribute info
231 * @param javaAttributeInfoOfOutput RPCs output node attribute info
VinodKumarS-Huaweid81eccb2016-06-01 14:30:22 +0530232 * @param pluginConfig plugin configurations
233 * @param rpcName name of the rpc function
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530234 * @throws IOException IO operation fail
235 */
236 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswalaf413b82016-07-14 15:18:20 +0530237 JavaAttributeInfo javaAttributeInfoOfOutput,
238 YangPluginConfig pluginConfig, String rpcName)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530239 throws IOException {
Bharat saraswalc2d3be12016-06-16 00:29:12 +0530240 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530241 }
242
243 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530244 * Removes all temporary file handles.
245 *
Bharat saraswal64e7e232016-07-14 23:33:55 +0530246 * @param isErrorOccurred flag to tell translator that error has occurred while file generation
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530247 * @throws IOException when failed to delete the temporary files
248 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530249 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530250 public void freeTemporaryResources(boolean isErrorOccurred)
251 throws IOException {
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530252
Bharat saraswal64e7e232016-07-14 23:33:55 +0530253 closeFile(getServiceInterfaceJavaFileHandle(), isErrorOccurred);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530254
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530255 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswal250a7472016-05-12 13:16:57 +0530256 closeFile(getRpcImplTempFileHandle(), true);
257 closeFile(getGetterInterfaceTempFileHandle(), true);
258 closeFile(getSetterInterfaceTempFileHandle(), true);
259 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530260
261 super.freeTemporaryResources(isErrorOccurred);
262
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530263 }
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530264}