blob: d26d07ef2da6b0103364ea97fb09cbf04429d205 [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;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053025import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaNotification;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053026
27import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK;
28import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK;
29import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
30import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateManagerClassFile;
31import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
janani b4a6711a2016-05-17 13:12:22 +053032import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053033import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
34import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053035import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addListnersImport;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053036import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswalc0e04842016-05-12 13:16:57 +053037import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053038import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
39import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053040import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalc0e04842016-05-12 13:16:57 +053041import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
42import static org.onosproject.yangutils.utils.UtilConstants.VOID;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053043import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
44import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
45import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053046
47/**
48 * Represents implementation of java service code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053049 * Maintains the temp files required specific for service and manager java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053050 */
51public class TempJavaServiceFragmentFiles
52 extends TempJavaFragmentFiles {
53
54 /**
VinodKumarS-Huaweicb3a1f52016-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 /**
65 * File name for generated class file for service
66 * suffix.
67 */
68 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
69
70 /**
71 * File name for generated class file for manager
72 * suffix.
73 */
74 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
75
76 /**
77 * Temporary file handle for rpc interface.
78 */
79 private File rpcInterfaceTempFileHandle;
80
81 /**
82 * Temporary file handle for rpc manager impl.
83 */
84 private File rpcImplTempFileHandle;
85
86 /**
87 * Java file handle for rpc interface file.
88 */
89 private File serviceInterfaceJavaFileHandle;
90
91 /**
92 * Java file handle for manager impl file.
93 */
94 private File managerJavaFileHandle;
95
96 /**
97 * Returns rpc method's java file handle.
98 *
99 * @return java file handle
100 */
101 private File getServiceInterfaceJavaFileHandle() {
102 return serviceInterfaceJavaFileHandle;
103 }
104
105 /**
106 * Sets rpc method's java file handle.
107 *
108 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
109 */
110 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
111 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
112 }
113
114 /**
115 * Returns managers java file handle.
116 *
117 * @return java file handle
118 */
119 public File getManagerJavaFileHandle() {
120 return managerJavaFileHandle;
121 }
122
123 /**
124 * Sets manager java file handle.
125 *
126 * @param managerJavaFileHandle file handle for to manager
127 */
128 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
129 this.managerJavaFileHandle = managerJavaFileHandle;
130 }
131
132 /**
133 * Returns rpc method's temporary file handle.
134 *
135 * @return temporary file handle
136 */
137 public File getRpcInterfaceTempFileHandle() {
138 return rpcInterfaceTempFileHandle;
139 }
140
141 /**
142 * Sets rpc method's temporary file handle.
143 *
144 * @param rpcInterfaceTempFileHandle file handle for to rpc method
145 */
146 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
147 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
148 }
149
150 /**
151 * Retrieves the manager impl temp file.
152 *
153 * @return the manager impl temp file
154 */
155 public File getRpcImplTempFileHandle() {
156 return rpcImplTempFileHandle;
157 }
158
159 /**
160 * Sets the manager impl temp file.
161 *
162 * @param rpcImplTempFileHandle the manager impl temp file
163 */
164 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
165 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
166 }
167
168 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530169 * Creates an instance of temporary java code fragment.
170 *
171 * @param javaFileInfo generated file information
172 * @throws IOException when fails to create new file handle
173 */
174 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
175 throws IOException {
176 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530177
178 addGeneratedTempFile(RPC_INTERFACE_MASK);
179
180 addGeneratedTempFile(RPC_IMPL_MASK);
181
182 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
183 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
184 }
185
186 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530187 * Constructs java code exit.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530188 *
189 * @param fileType generated file type
190 * @param curNode current YANG node
191 * @throws IOException when fails to generate java files
192 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530193 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530194 public void generateJavaFile(int fileType, YangNode curNode)
195 throws IOException {
196 List<String> imports = new ArrayList<>();
197 imports = getJavaImportData().getImports();
198
199 createPackage(curNode);
200
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530201 boolean isNotification = false;
202 YangNode tempNode = curNode.getChild();
203 while (tempNode != null) {
204 if (tempNode instanceof YangJavaNotification) {
205 isNotification = true;
206 break;
207 }
208 tempNode = tempNode.getNextSibling();
209 }
210
211 if (isNotification) {
212 addListnersImport(curNode, imports, true, LISTENER_SERVICE);
213 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530214 /**
215 * Creates rpc interface file.
216 */
217 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
218 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent());
219
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530220 if (isNotification) {
221 addListnersImport(curNode, imports, false, LISTENER_SERVICE);
222 addListnersImport(curNode, imports, true, LISTENER_REG);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530223 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530224 /**
225 * Create builder class file.
226 */
227 setManagerJavaFileHandle(getJavaFileHandle(getJavaClassName(MANAGER_FILE_NAME_SUFFIX)));
228 generateManagerClassFile(getManagerJavaFileHandle(), imports, curNode, isAttributePresent());
229
230 insertDataIntoJavaFile(getManagerJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530231 if (isNotification) {
232 addListnersImport(curNode, imports, false, LISTENER_REG);
233 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530234 /**
235 * Close all the file handles.
236 */
237 freeTemporaryResources(false);
238 }
239
240 /**
241 * Adds rpc string information to applicable temp file.
242 *
243 * @param javaAttributeInfoOfInput rpc's input node attribute info
244 * @param javaAttributeInfoOfOutput rpc's output node attribute info
245 * @param rpcName name of the rpc function
246 * @throws IOException IO operation fail
247 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530248 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
249 JavaAttributeInfo javaAttributeInfoOfOutput,
250 String rpcName) throws IOException {
251 String rpcInput = EMPTY_STRING;
252 String rpcOutput = VOID;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530253 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530254 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530255 }
256 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530257 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530258 }
Bharat saraswalc0e04842016-05-12 13:16:57 +0530259 appendToFile(getRpcInterfaceTempFileHandle(), generateJavaDocForRpc(rpcName, RPC_INPUT_VAR_NAME, rpcOutput)
260 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
261 appendToFile(getRpcImplTempFileHandle(), getRpcManagerMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530262 }
263
264 /**
265 * Adds the JAVA rpc snippet information.
266 *
267 * @param javaAttributeInfoOfInput rpc's input node attribute info
268 * @param javaAttributeInfoOfOutput rpc's output node attribute info
269 * @param rpcName name of the rpc function
270 * @throws IOException IO operation fail
271 */
272 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
273 JavaAttributeInfo javaAttributeInfoOfOutput,
274 String rpcName)
275 throws IOException {
276 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, rpcName);
277 }
278
279 /**
280 * Removes all temporary file handles.
281 *
282 * @param isErrorOccurred when translator fails to generate java files we
283 * need to close all open file handles include temporary files
284 * and java files.
285 * @throws IOException when failed to delete the temporary files
286 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530287 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530288 public void freeTemporaryResources(boolean isErrorOccurred)
289 throws IOException {
290 boolean isError = isErrorOccurred;
291
292 closeFile(getServiceInterfaceJavaFileHandle(), isError);
293 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530294 closeFile(getRpcImplTempFileHandle(), true);
295 closeFile(getGetterInterfaceTempFileHandle(), true);
296 closeFile(getSetterInterfaceTempFileHandle(), true);
297 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530298
299 super.freeTemporaryResources(isErrorOccurred);
300
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530301 }
302}