blob: 288f6409357cf8b8d408e7608ec61965cf24b476 [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.ArrayList;
22import java.util.List;
23
24import org.onosproject.yangutils.datamodel.YangNode;
25
26import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK;
27import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK;
28import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
29import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateManagerClassFile;
30import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
31import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
32import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
33import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
34import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
35import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswal250a7472016-05-12 13:16:57 +053036import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
37import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053038import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal250a7472016-05-12 13:16:57 +053039import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
40import static org.onosproject.yangutils.utils.UtilConstants.VOID;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053041import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
42import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
43import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Vinod Kumar S79a374b2016-04-30 21:09:15 +053044
45/**
46 * Represents implementation of java service code fragments temporary implementations.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053047 * Maintains the temp files required specific for service and manager java snippet generation.
Vinod Kumar S79a374b2016-04-30 21:09:15 +053048 */
49public class TempJavaServiceFragmentFiles
50 extends TempJavaFragmentFiles {
51
52 /**
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053053 * File name for rpc method.
54 */
55 private static final String RPC_INTERFACE_FILE_NAME = "Rpc";
56
57 /**
58 * File name for rpc implementation method.
59 */
60 private static final String RPC_IMPL_FILE_NAME = "RpcImpl";
61
62 /**
63 * File name for generated class file for service
64 * suffix.
65 */
66 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
67
68 /**
69 * File name for generated class file for manager
70 * suffix.
71 */
72 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
73
74 /**
75 * Temporary file handle for rpc interface.
76 */
77 private File rpcInterfaceTempFileHandle;
78
79 /**
80 * Temporary file handle for rpc manager impl.
81 */
82 private File rpcImplTempFileHandle;
83
84 /**
85 * Java file handle for rpc interface file.
86 */
87 private File serviceInterfaceJavaFileHandle;
88
89 /**
90 * Java file handle for manager impl file.
91 */
92 private File managerJavaFileHandle;
93
94 /**
95 * Returns rpc method's java file handle.
96 *
97 * @return java file handle
98 */
99 private File getServiceInterfaceJavaFileHandle() {
100 return serviceInterfaceJavaFileHandle;
101 }
102
103 /**
104 * Sets rpc method's java file handle.
105 *
106 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
107 */
108 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
109 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
110 }
111
112 /**
113 * Returns managers java file handle.
114 *
115 * @return java file handle
116 */
117 public File getManagerJavaFileHandle() {
118 return managerJavaFileHandle;
119 }
120
121 /**
122 * Sets manager java file handle.
123 *
124 * @param managerJavaFileHandle file handle for to manager
125 */
126 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
127 this.managerJavaFileHandle = managerJavaFileHandle;
128 }
129
130 /**
131 * Returns rpc method's temporary file handle.
132 *
133 * @return temporary file handle
134 */
135 public File getRpcInterfaceTempFileHandle() {
136 return rpcInterfaceTempFileHandle;
137 }
138
139 /**
140 * Sets rpc method's temporary file handle.
141 *
142 * @param rpcInterfaceTempFileHandle file handle for to rpc method
143 */
144 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
145 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
146 }
147
148 /**
149 * Retrieves the manager impl temp file.
150 *
151 * @return the manager impl temp file
152 */
153 public File getRpcImplTempFileHandle() {
154 return rpcImplTempFileHandle;
155 }
156
157 /**
158 * Sets the manager impl temp file.
159 *
160 * @param rpcImplTempFileHandle the manager impl temp file
161 */
162 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
163 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
164 }
165
166 /**
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530167 * Creates an instance of temporary java code fragment.
168 *
169 * @param javaFileInfo generated file information
170 * @throws IOException when fails to create new file handle
171 */
172 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
173 throws IOException {
174 super(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530175
176 addGeneratedTempFile(RPC_INTERFACE_MASK);
177
178 addGeneratedTempFile(RPC_IMPL_MASK);
179
180 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
181 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
182 }
183
184 /**
Bharat saraswal250a7472016-05-12 13:16:57 +0530185 * Generate java code.
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530186 *
187 * @param fileType generated file type
188 * @param curNode current YANG node
189 * @throws IOException when fails to generate java files
190 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530191 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530192 public void generateJavaFile(int fileType, YangNode curNode)
193 throws IOException {
194 List<String> imports = new ArrayList<>();
195 imports = getJavaImportData().getImports();
196
197 createPackage(curNode);
198
199 /**
200 * Creates rpc interface file.
201 */
202 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
203 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent());
204
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530205 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 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530232 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
233 JavaAttributeInfo javaAttributeInfoOfOutput,
234 String rpcName) throws IOException {
235 String rpcInput = EMPTY_STRING;
236 String rpcOutput = VOID;
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530237 if (javaAttributeInfoOfInput != null) {
238 rpcInput = javaAttributeInfoOfInput.getAttributeName();
239 }
240 if (javaAttributeInfoOfOutput != null) {
241 rpcOutput = javaAttributeInfoOfOutput.getAttributeName();
242 }
Bharat saraswal250a7472016-05-12 13:16:57 +0530243 appendToFile(getRpcInterfaceTempFileHandle(), generateJavaDocForRpc(rpcName, RPC_INPUT_VAR_NAME, rpcOutput)
244 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
245 appendToFile(getRpcImplTempFileHandle(), getRpcManagerMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530246 }
247
248 /**
249 * Adds the JAVA rpc snippet information.
250 *
251 * @param javaAttributeInfoOfInput rpc's input node attribute info
252 * @param javaAttributeInfoOfOutput rpc's output node attribute info
253 * @param rpcName name of the rpc function
254 * @throws IOException IO operation fail
255 */
256 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
257 JavaAttributeInfo javaAttributeInfoOfOutput,
258 String rpcName)
259 throws IOException {
260 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, rpcName);
261 }
262
263 /**
264 * Removes all temporary file handles.
265 *
266 * @param isErrorOccurred when translator fails to generate java files we
267 * need to close all open file handles include temporary files
268 * and java files.
269 * @throws IOException when failed to delete the temporary files
270 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530271 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530272 public void freeTemporaryResources(boolean isErrorOccurred)
273 throws IOException {
274 boolean isError = isErrorOccurred;
275
276 closeFile(getServiceInterfaceJavaFileHandle(), isError);
277 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswal250a7472016-05-12 13:16:57 +0530278 closeFile(getRpcImplTempFileHandle(), true);
279 closeFile(getGetterInterfaceTempFileHandle(), true);
280 closeFile(getSetterInterfaceTempFileHandle(), true);
281 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530282
283 super.freeTemporaryResources(isErrorOccurred);
284
Vinod Kumar S79a374b2016-04-30 21:09:15 +0530285 }
286}