blob: 5a5e27ff8dd584d6e2064b002d885eb185441759 [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 saraswal33dfa012016-05-17 19:59:16 +053025import org.onosproject.yangutils.datamodel.YangNotification;
26import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
27import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
28import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053029
Bharat saraswal33dfa012016-05-17 19:59:16 +053030import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_CLASS;
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_LISTENER_INTERFACE;
32import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
33import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_ENUM_MASK;
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_METHOD_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_ATTRIBUTE_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_GETTER_MASK;
37import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_SETTER_MASK;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053038import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK;
39import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK;
Bharat saraswal33dfa012016-05-17 19:59:16 +053040import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
41import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053042import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswal33dfa012016-05-17 19:59:16 +053043import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventFile;
44import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventListenerFile;
45import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventSubjectFile;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateManagerClassFile;
47import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053048import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
49import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053050import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Bharat saraswal33dfa012016-05-17 19:59:16 +053051import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
52import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053053import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
54import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
Bharat saraswal33dfa012016-05-17 19:59:16 +053055import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
56import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAnnotationsImports;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053057import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addListnersImport;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053058import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053059import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswalc0e04842016-05-12 13:16:57 +053060import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053061import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
62import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053063import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
64import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053065import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalc0e04842016-05-12 13:16:57 +053066import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
Bharat saraswal33dfa012016-05-17 19:59:16 +053067import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalc0e04842016-05-12 13:16:57 +053068import static org.onosproject.yangutils.utils.UtilConstants.VOID;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053069import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
70import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
Bharat saraswal33dfa012016-05-17 19:59:16 +053071import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
72import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE;
73import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
74import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.MANAGER_SETTER_METHOD;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053075import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076
77/**
78 * Represents implementation of java service code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053079 * Maintains the temp files required specific for service and manager java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080 */
81public class TempJavaServiceFragmentFiles
82 extends TempJavaFragmentFiles {
83
84 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053085 * File name for rpc method.
86 */
87 private static final String RPC_INTERFACE_FILE_NAME = "Rpc";
88
89 /**
90 * File name for rpc implementation method.
91 */
92 private static final String RPC_IMPL_FILE_NAME = "RpcImpl";
93
94 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053095 * File name for event enum temp file.
96 */
97 private static final String EVENT_ENUM_FILE_NAME = "EventEnum";
98
99 /**
100 * File name for event method temp file.
101 */
102 private static final String EVENT_METHOD_FILE_NAME = "EventMethod";
103
104 /**
105 * File name for event subject attribute temp file.
106 */
107 private static final String EVENT_SUBJECT_ATTRIBUTE_FILE_NAME = "EventSubjectAttribute";
108
109 /**
110 * File name for event subject getter temp file.
111 */
112 private static final String EVENT_SUBJECT_GETTER_FILE_NAME = "EventSubjectGetter";
113
114 /**
115 * File name for event subject setter temp file.
116 */
117 private static final String EVENT_SUBJECT_SETTER_FILE_NAME = "EventSubjectSetter";
118 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530119 * File name for generated class file for service
120 * suffix.
121 */
122 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
123
124 /**
125 * File name for generated class file for manager
126 * suffix.
127 */
128 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
129
130 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530131 * File name for generated class file for special type like union, typedef
132 * suffix.
133 */
134 private static final String EVENT_FILE_NAME_SUFFIX = "Event";
135
136 /**
137 * File name for generated class file for special type like union, typedef
138 * suffix.
139 */
140 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
141
142 /**
143 * File name for generated class file for special type like union, typedef
144 * suffix.
145 */
146 public static final String EVENT_SUBJECT_NAME_SUFFIX = "EventSubject";
147
148 private static final String JAVA_FILE_EXTENSION = ".java";
149
150 /**
151 * Java file handle for event subject file.
152 */
153 private File eventSubjectJavaFileHandle;
154
155 /**
156 * Java file handle for event listener file.
157 */
158 private File eventListenerJavaFileHandle;
159
160 /**
161 * Java file handle for event file.
162 */
163 private File eventJavaFileHandle;
164
165 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530166 * Temporary file handle for rpc interface.
167 */
168 private File rpcInterfaceTempFileHandle;
169
170 /**
171 * Temporary file handle for rpc manager impl.
172 */
173 private File rpcImplTempFileHandle;
174
175 /**
176 * Java file handle for rpc interface file.
177 */
178 private File serviceInterfaceJavaFileHandle;
179
180 /**
181 * Java file handle for manager impl file.
182 */
183 private File managerJavaFileHandle;
184
185 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530186 * Java file handle for event enum impl file.
187 */
188 private File eventEnumTempFileHandle;
189
190 /**
191 * Java file handle for event method impl file.
192 */
193 private File eventMethodTempFileHandle;
194
195 /**
196 * Java file handle for event subject attribute file.
197 */
198 private File eventSubjectAttributeTempFileHandle;
199
200 /**
201 * Java file handle for event subject getter impl file.
202 */
203 private File eventSubjectGetterTempFileHandle;
204
205 /**
206 * Java file handle for event subject setter impl file.
207 */
208 private File eventSubjectSetterTempFileHandle;
209
210 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530211 * Returns rpc method's java file handle.
212 *
213 * @return java file handle
214 */
215 private File getServiceInterfaceJavaFileHandle() {
216 return serviceInterfaceJavaFileHandle;
217 }
218
219 /**
220 * Sets rpc method's java file handle.
221 *
222 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
223 */
224 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
225 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
226 }
227
228 /**
229 * Returns managers java file handle.
230 *
231 * @return java file handle
232 */
233 public File getManagerJavaFileHandle() {
234 return managerJavaFileHandle;
235 }
236
237 /**
238 * Sets manager java file handle.
239 *
240 * @param managerJavaFileHandle file handle for to manager
241 */
242 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
243 this.managerJavaFileHandle = managerJavaFileHandle;
244 }
245
246 /**
247 * Returns rpc method's temporary file handle.
248 *
249 * @return temporary file handle
250 */
251 public File getRpcInterfaceTempFileHandle() {
252 return rpcInterfaceTempFileHandle;
253 }
254
255 /**
256 * Sets rpc method's temporary file handle.
257 *
258 * @param rpcInterfaceTempFileHandle file handle for to rpc method
259 */
260 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
261 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
262 }
263
264 /**
265 * Retrieves the manager impl temp file.
266 *
267 * @return the manager impl temp file
268 */
269 public File getRpcImplTempFileHandle() {
270 return rpcImplTempFileHandle;
271 }
272
273 /**
274 * Sets the manager impl temp file.
275 *
276 * @param rpcImplTempFileHandle the manager impl temp file
277 */
278 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
279 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
280 }
281
282 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530283 * Returns event's java file handle.
284 *
285 * @return java file handle
286 */
287 private File getEventJavaFileHandle() {
288 return eventJavaFileHandle;
289 }
290
291 /**
292 * Sets event's java file handle.
293 *
294 * @param eventJavaFileHandle file handle for event
295 */
296 private void setEventJavaFileHandle(File eventJavaFileHandle) {
297 this.eventJavaFileHandle = eventJavaFileHandle;
298 }
299
300 /**
301 * Returns event listeners's java file handle.
302 *
303 * @return java file handle
304 */
305 private File getEventListenerJavaFileHandle() {
306 return eventListenerJavaFileHandle;
307 }
308
309 /**
310 * Sets event's java file handle.
311 *
312 * @param eventListenerJavaFileHandle file handle for event
313 */
314 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
315 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
316 }
317
318 /**
319 * Returns event subject's java file handle.
320 *
321 * @return java file handle
322 */
323 private File getEventSubjectJavaFileHandle() {
324 return eventSubjectJavaFileHandle;
325 }
326
327 /**
328 * Sets event's subject java file handle.
329 *
330 * @param eventSubjectJavaFileHandle file handle for event's subject
331 */
332 private void setEventSubjectJavaFileHandle(File eventSubjectJavaFileHandle) {
333 this.eventSubjectJavaFileHandle = eventSubjectJavaFileHandle;
334 }
335
336 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530337 * Creates an instance of temporary java code fragment.
338 *
339 * @param javaFileInfo generated file information
340 * @throws IOException when fails to create new file handle
341 */
342 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
343 throws IOException {
344 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530345
346 addGeneratedTempFile(RPC_INTERFACE_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530347 addGeneratedTempFile(RPC_IMPL_MASK);
348
Bharat saraswal33dfa012016-05-17 19:59:16 +0530349 addGeneratedTempFile(EVENT_ENUM_MASK);
350 addGeneratedTempFile(EVENT_METHOD_MASK);
351 addGeneratedTempFile(EVENT_SUBJECT_ATTRIBUTE_MASK);
352 addGeneratedTempFile(EVENT_SUBJECT_GETTER_MASK);
353 addGeneratedTempFile(EVENT_SUBJECT_SETTER_MASK);
354
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530355 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
356 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
Bharat saraswal33dfa012016-05-17 19:59:16 +0530357
358 setEventEnumTempFileHandle(getTemporaryFileHandle(EVENT_ENUM_FILE_NAME));
359 setEventMethodTempFileHandle(getTemporaryFileHandle(EVENT_METHOD_FILE_NAME));
360 setEventSubjectAttributeTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_ATTRIBUTE_FILE_NAME));
361 setEventSubjectGetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_GETTER_FILE_NAME));
362 setEventSubjectSetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_SETTER_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530363 }
364
365 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530366 * Constructs java code exit.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530367 *
368 * @param fileType generated file type
369 * @param curNode current YANG node
370 * @throws IOException when fails to generate java files
371 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530372 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530373 public void generateJavaFile(int fileType, YangNode curNode)
374 throws IOException {
375 List<String> imports = new ArrayList<>();
376 imports = getJavaImportData().getImports();
377
378 createPackage(curNode);
379
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530380 boolean isNotification = false;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530381 if (curNode instanceof YangJavaModule) {
382 if (!((YangJavaModule) curNode).getNotificationNodes().isEmpty()) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530383 isNotification = true;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530384 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530385 } else if (curNode instanceof YangJavaSubModule) {
386 if (!((YangJavaSubModule) curNode).getNotificationNodes().isEmpty()) {
387 isNotification = true;
388 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530389 }
390
391 if (isNotification) {
392 addListnersImport(curNode, imports, true, LISTENER_SERVICE);
393 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530394 /**
395 * Creates rpc interface file.
396 */
397 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
398 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent());
399
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530400 if (isNotification) {
401 addListnersImport(curNode, imports, false, LISTENER_SERVICE);
402 addListnersImport(curNode, imports, true, LISTENER_REG);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530403 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530404 addAnnotationsImports(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530405 /**
406 * Create builder class file.
407 */
408 setManagerJavaFileHandle(getJavaFileHandle(getJavaClassName(MANAGER_FILE_NAME_SUFFIX)));
409 generateManagerClassFile(getManagerJavaFileHandle(), imports, curNode, isAttributePresent());
410
411 insertDataIntoJavaFile(getManagerJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530412 if (isNotification) {
413 addListnersImport(curNode, imports, false, LISTENER_REG);
414 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530415 addAnnotationsImports(imports, false);
416
417 if (isNotification) {
418 generateEventJavaFile(GENERATE_EVENT_CLASS, curNode);
419 generateEventListenerJavaFile(GENERATE_EVENT_LISTENER_INTERFACE, curNode);
420 generateEventSubjectJavaFile(GENERATE_EVENT_SUBJECT_CLASS, curNode);
421 }
422
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530423 /**
424 * Close all the file handles.
425 */
426 freeTemporaryResources(false);
427 }
428
429 /**
430 * Adds rpc string information to applicable temp file.
431 *
432 * @param javaAttributeInfoOfInput rpc's input node attribute info
433 * @param javaAttributeInfoOfOutput rpc's output node attribute info
434 * @param rpcName name of the rpc function
Bharat saraswal33dfa012016-05-17 19:59:16 +0530435 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530436 * @throws IOException IO operation fail
437 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530438 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530439 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
Bharat saraswalc0e04842016-05-12 13:16:57 +0530440 String rpcName) throws IOException {
441 String rpcInput = EMPTY_STRING;
442 String rpcOutput = VOID;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530443 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530444 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530445 }
446 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530447 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530448 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530449 appendToFile(getRpcInterfaceTempFileHandle(),
450 generateJavaDocForRpc(rpcName, RPC_INPUT_VAR_NAME, rpcOutput, pluginConfig)
451 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
452 appendToFile(getRpcImplTempFileHandle(),
453 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530454 }
455
456 /**
457 * Adds the JAVA rpc snippet information.
458 *
459 * @param javaAttributeInfoOfInput rpc's input node attribute info
460 * @param javaAttributeInfoOfOutput rpc's output node attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530461 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530462 * @param rpcName name of the rpc function
463 * @throws IOException IO operation fail
464 */
465 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530466 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530467 String rpcName)
468 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530469 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName);
470 }
471
472 /**
473 * Constructs java code exit.
474 *
475 * @param fileType generated file type
476 * @param curNode current YANG node
477 * @throws IOException when fails to generate java files
478 */
479 public void generateEventJavaFile(int fileType, YangNode curNode)
480 throws IOException {
481
482 List<String> imports = new ArrayList<>();
483
484 imports.add(getJavaImportData().getAbstractEventsImport());
485 String curNodeInfo =
486 getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName());
487 String nodeName = curNodeInfo + EVENT_STRING;
488
489 addEnumMethod(nodeName, curNodeInfo + EVENT_SUBJECT_NAME_SUFFIX);
490
491 /**
492 * Creates event interface file.
493 */
494 setEventJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo + EVENT_FILE_NAME_SUFFIX));
495 generateEventFile(getEventJavaFileHandle(), curNode, imports);
496
497 /**
498 * Close all the file handles.
499 */
500 freeTemporaryResources(false);
501 }
502
503 /**
504 * Constructs java code exit.
505 *
506 * @param fileType generated file type
507 * @param curNode current YANG node
508 * @throws IOException when fails to generate java files
509 */
510 public void generateEventListenerJavaFile(int fileType, YangNode curNode)
511 throws IOException {
512
513 List<String> imports = new ArrayList<>();
514
515 imports.add(getJavaImportData().getEventListenerImport());
516 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
517 .getJavaFileInfo().getJavaName());
518 /**
519 * Creates event listener interface file.
520 */
521 setEventListenerJavaFileHandle(
522 getJavaFileHandle(curNode, curNodeInfo + EVENT_LISTENER_FILE_NAME_SUFFIX));
523 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, imports);
524
525 /**
526 * Close all the file handles.
527 */
528 freeTemporaryResources(false);
529 }
530
531 /**
532 * Constructs java code exit.
533 *
534 * @param fileType generated file type
535 * @param curNode current YANG node
536 * @throws IOException when fails to generate java files
537 */
538 public void generateEventSubjectJavaFile(int fileType, YangNode curNode)
539 throws IOException {
540
541 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
542 .getJavaFileInfo().getJavaName());
543 /**
544 * Creates event interface file.
545 */
546 setEventSubjectJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo +
547 EVENT_SUBJECT_NAME_SUFFIX));
548 generateEventSubjectFile(getEventSubjectJavaFileHandle(), curNode);
549
550 /**
551 * Close all the file handles.
552 */
553 freeTemporaryResources(false);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530554 }
555
556 /**
557 * Removes all temporary file handles.
558 *
559 * @param isErrorOccurred when translator fails to generate java files we
560 * need to close all open file handles include temporary files
561 * and java files.
562 * @throws IOException when failed to delete the temporary files
563 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530564 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530565 public void freeTemporaryResources(boolean isErrorOccurred)
566 throws IOException {
567 boolean isError = isErrorOccurred;
568
569 closeFile(getServiceInterfaceJavaFileHandle(), isError);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530570 closeFile(getManagerJavaFileHandle(), isError);
571
572 if (getEventJavaFileHandle() != null) {
573 closeFile(getEventJavaFileHandle(), isError);
574 }
575 if (getEventListenerJavaFileHandle() != null) {
576 closeFile(getEventListenerJavaFileHandle(), isError);
577 }
578 if (getEventSubjectJavaFileHandle() != null) {
579 closeFile(getEventSubjectJavaFileHandle(), isError);
580 }
581
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530582 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530583 closeFile(getRpcImplTempFileHandle(), true);
584 closeFile(getGetterInterfaceTempFileHandle(), true);
585 closeFile(getSetterInterfaceTempFileHandle(), true);
586 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530587
588 super.freeTemporaryResources(isErrorOccurred);
589
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530590 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530591
592 /**
593 * Returns event enum temp file.
594 *
595 * @return event enum temp file
596 */
597 public File getEventEnumTempFileHandle() {
598 return eventEnumTempFileHandle;
599 }
600
601 /**
602 * Sets event enum temp file.
603 *
604 * @param eventEnumTempFileHandle event enum temp file
605 */
606 public void setEventEnumTempFileHandle(File eventEnumTempFileHandle) {
607 this.eventEnumTempFileHandle = eventEnumTempFileHandle;
608 }
609
610 /**
611 * Returns event method temp file.
612 *
613 * @return event method temp file
614 */
615 public File getEventMethodTempFileHandle() {
616 return eventMethodTempFileHandle;
617 }
618
619 /**
620 * Sets event method temp file.
621 *
622 * @param eventMethodTempFileHandle event method temp file
623 */
624 public void setEventMethodTempFileHandle(File eventMethodTempFileHandle) {
625 this.eventMethodTempFileHandle = eventMethodTempFileHandle;
626 }
627
628 /**
629 * Returns event subject attribute temp file.
630 *
631 * @return event subject attribute temp file
632 */
633 public File getEventSubjectAttributeTempFileHandle() {
634 return eventSubjectAttributeTempFileHandle;
635 }
636
637 /**
638 * Sets event subject attribute temp file.
639 *
640 * @param eventSubjectAttributeTempFileHandle event subject attribute temp file
641 */
642 public void setEventSubjectAttributeTempFileHandle(File eventSubjectAttributeTempFileHandle) {
643 this.eventSubjectAttributeTempFileHandle = eventSubjectAttributeTempFileHandle;
644 }
645
646 /**
647 * Returns event subject getter temp file.
648 *
649 * @return event subject getter temp file
650 */
651 public File getEventSubjectGetterTempFileHandle() {
652 return eventSubjectGetterTempFileHandle;
653 }
654
655 /**
656 * Sets event subject getter temp file.
657 *
658 * @param eventSubjectGetterTempFileHandle event subject getter temp file
659 */
660 public void setEventSubjectGetterTempFileHandle(File eventSubjectGetterTempFileHandle) {
661 this.eventSubjectGetterTempFileHandle = eventSubjectGetterTempFileHandle;
662 }
663
664 /**
665 * Returns event subject setter temp file.
666 *
667 * @return event subject setter temp file
668 */
669 public File getEventSubjectSetterTempFileHandle() {
670 return eventSubjectSetterTempFileHandle;
671 }
672
673 /**
674 * Sets event subject setter temp file.
675 *
676 * @param eventSubjectSetterTempFileHandle event subject setter temp file
677 */
678 public void setEventSubjectSetterTempFileHandle(File eventSubjectSetterTempFileHandle) {
679 this.eventSubjectSetterTempFileHandle = eventSubjectSetterTempFileHandle;
680 }
681
682 /**
683 * Adds java snippet for events to event subject file.
684 *
685 * @param curNode current node
686 * @param pluginConfig plugin configurations
687 * @throws IOException when fails to do IO operations
688 */
689 public void addJavaSnippetOfEvent(YangNode curNode, YangPluginConfig pluginConfig) throws IOException {
690
691 String currentInfo = getCapitalCase(getCamelCase(((YangNotification) curNode).getName(),
692 pluginConfig.getConflictResolver()));
693
694 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode.getParent(),
695 getCapitalCase(currentInfo));
696
697 JavaAttributeInfo javaAttributeInfo =
698 getAttributeInfoForTheData(qualifiedTypeInfo, getSmallCase(currentInfo),
699 null, false, false);
700
701 /*Adds java info for event in respective temp files.*/
702 addEventEnum(currentInfo, pluginConfig);
703 addEventSubjectAttribute(javaAttributeInfo, pluginConfig);
704 addEventSubjectGetter(javaAttributeInfo, pluginConfig);
705 addEventSubjectSetter(javaAttributeInfo, pluginConfig, currentInfo);
706 }
707
708 /*Adds event to enum temp file.*/
709 private void addEventEnum(String notificationName, YangPluginConfig pluginConfig) throws IOException {
710 appendToFile(getEventEnumTempFileHandle(),
711 getJavaDoc(ENUM_ATTRIBUTE, notificationName, false, pluginConfig) + FOUR_SPACE_INDENTATION
712 + notificationName.toUpperCase() + COMMA + NEW_LINE);
713 }
714
715 /*Adds event method in event class*/
716 private void addEnumMethod(String eventClassname, String className) throws IOException {
717 appendToFile(getEventMethodTempFileHandle(), getEventFileContents(eventClassname, className));
718 }
719
720 /*Adds event method contents to event file.*/
721 private static String getEventFileContents(String eventClassname, String classname) {
722 return "\n" +
723 " /**\n" +
724 " * Creates " + classname + " event with type and subject.\n" +
725 " *\n" +
726 " * @param type event type\n" +
727 " * @param subject subject " + classname + "\n" +
728 " */\n" +
729 " public " + eventClassname + "(Type type, " + getCapitalCase(classname) + " subject) {\n" +
730 " super(type, subject);\n" +
731 " }\n" +
732 "\n" +
733 " /**\n" +
734 " * Creates " + classname + " event with type, subject and time.\n" +
735 " *\n" +
736 " * @param type event type\n" +
737 " * @param subject subject " + classname + "\n" +
738 " * @param time time of event\n" +
739 " */\n" +
740 " public " + eventClassname + "(Type type, " + getCapitalCase(classname)
741 + " subject, long time) {\n" +
742 " super(type, subject, time);\n" +
743 " }\n" +
744 "\n";
745 }
746
747 /*Adds events to event subject file.*/
748 private void addEventSubjectAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
749 throws IOException {
750 appendToFile(getEventSubjectAttributeTempFileHandle(),
751 FOUR_SPACE_INDENTATION + parseAttribute(attr, pluginConfig));
752 }
753
754 /*Adds getter method for event in event subject class.*/
755 private void addEventSubjectGetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig) throws IOException {
756 appendToFile(getEventSubjectGetterTempFileHandle(),
757 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
758 + getGetterForClass(attr, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
759 }
760
761 /*Adds setter method for event in event subject class.*/
762 private void addEventSubjectSetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig, String className)
763 throws IOException {
764 appendToFile(getEventSubjectSetterTempFileHandle(),
765 getJavaDoc(MANAGER_SETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
766 + getSetterForClass(attr, className, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
767 }
768
769 /**
770 * Returns a temporary file handle for the event's file type.
771 *
772 * @param fileName file name
773 * @return temporary file handle
774 * @throws IOException when fails to create new file handle
775 */
776 private File getJavaFileHandle(YangNode curNode, String name)
777 throws IOException {
778
779 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
780
781 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION,
782 parentInfo);
783 }
784
785 /**
786 * Returns the directory path.
787 *
788 * @return directory path
789 */
790 private String getDirPath(JavaFileInfo parentInfo) {
791 return (parentInfo.getPackageFilePath() + SLASH + parentInfo.getJavaName()).toLowerCase();
792 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530793}