blob: 4fe9cd6c41be034580d4cb99077745c1878b655a [file] [log] [blame]
Vinod Kumar Se4b9b0c2016-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-Huaweicb3a1f52016-05-10 17:58:57 +053019import java.io.File;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053020import java.io.IOException;
VinodKumarS-Huaweicb3a1f52016-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;
janani b4a6711a2016-05-17 13:12:22 +053031import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
33import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
34import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
35import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
36import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053037import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
38import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053039import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalc0e04842016-05-12 13:16:57 +053040import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
41import static org.onosproject.yangutils.utils.UtilConstants.VOID;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053042import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
43import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
44import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053045
46/**
47 * Represents implementation of java service code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053048 * Maintains the temp files required specific for service and manager java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053049 */
50public class TempJavaServiceFragmentFiles
51 extends TempJavaFragmentFiles {
52
53 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053054 * 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 Se4b9b0c2016-04-30 21:09:15 +0530168 * 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-Huaweicb3a1f52016-05-10 17:58:57 +0530176
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 saraswalc0e04842016-05-12 13:16:57 +0530186 * Generate java code.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530187 *
188 * @param fileType generated file type
189 * @param curNode current YANG node
190 * @throws IOException when fails to generate java files
191 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530192 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530193 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-Huaweicb3a1f52016-05-10 17:58:57 +0530206 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 saraswalc0e04842016-05-12 13:16:57 +0530233 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
234 JavaAttributeInfo javaAttributeInfoOfOutput,
235 String rpcName) throws IOException {
236 String rpcInput = EMPTY_STRING;
237 String rpcOutput = VOID;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530238 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530239 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530240 }
241 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530242 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530243 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530244 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-Huaweicb3a1f52016-05-10 17:58:57 +0530247 }
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 saraswalc0e04842016-05-12 13:16:57 +0530272 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530273 public void freeTemporaryResources(boolean isErrorOccurred)
274 throws IOException {
275 boolean isError = isErrorOccurred;
276
277 closeFile(getServiceInterfaceJavaFileHandle(), isError);
278 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530279 closeFile(getRpcImplTempFileHandle(), true);
280 closeFile(getGetterInterfaceTempFileHandle(), true);
281 closeFile(getSetterInterfaceTempFileHandle(), true);
282 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530283
284 super.freeTemporaryResources(isErrorOccurred);
285
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530286 }
287}