blob: af2ed42ed43e8eac263b4821f9cf002c56e2a762 [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.translator.tojava.javamodel.YangJavaModule;
26import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
Bharat saraswalb551aae2016-07-14 15:18:20 +053027import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053028import org.onosproject.yangutils.utils.io.impl.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_LISTENER_INTERFACE;
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
32import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_ENUM_MASK;
33import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_METHOD_MASK;
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_ATTRIBUTE_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_GETTER_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_SETTER_MASK;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK;
38import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK;
Bharat saraswal33dfa012016-05-17 19:59:16 +053039import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
40import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
Bharat saraswalb551aae2016-07-14 15:18:20 +053041import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.addAnnotationsImports;
42import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.addListenersImport;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053043import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswal33dfa012016-05-17 19:59:16 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventFile;
45import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventListenerFile;
46import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventSubjectFile;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053047import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateManagerClassFile;
48import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053049import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Bharat saraswalb551aae2016-07-14 15:18:20 +053050import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswalcad0e652016-05-26 23:48:38 +053051import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Bharat saraswal33dfa012016-05-17 19:59:16 +053052import 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;
Bharat saraswal33dfa012016-05-17 19:59:16 +053056import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswalc0e04842016-05-12 13:16:57 +053057import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053058import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
59import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053060import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
61import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053062import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalc0e04842016-05-12 13:16:57 +053063import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
Bharat saraswal33dfa012016-05-17 19:59:16 +053064import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalc0e04842016-05-12 13:16:57 +053065import static org.onosproject.yangutils.utils.UtilConstants.VOID;
Bharat saraswalb551aae2016-07-14 15:18:20 +053066import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053067import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE;
68import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
69import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.MANAGER_SETTER_METHOD;
Bharat saraswalb551aae2016-07-14 15:18:20 +053070import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
71import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
72import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
73import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
74import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
75import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getSmallCase;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053076import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053077
78/**
Bharat saraswalb551aae2016-07-14 15:18:20 +053079 * Represents implementation of java service code fragments temporary implementations. Maintains the temp files required
80 * specific for service and manager java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053081 */
82public class TempJavaServiceFragmentFiles
83 extends TempJavaFragmentFiles {
84
85 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +053086 * File name for generated class file for special type like union, typedef suffix.
87 */
88 public static final String EVENT_SUBJECT_NAME_SUFFIX = "EventSubject";
89 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053090 * File name for rpc method.
91 */
92 private static final String RPC_INTERFACE_FILE_NAME = "Rpc";
93
94 /**
95 * File name for rpc implementation method.
96 */
97 private static final String RPC_IMPL_FILE_NAME = "RpcImpl";
98
99 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530100 * File name for event enum temp file.
101 */
102 private static final String EVENT_ENUM_FILE_NAME = "EventEnum";
103
104 /**
105 * File name for event method temp file.
106 */
107 private static final String EVENT_METHOD_FILE_NAME = "EventMethod";
108
109 /**
110 * File name for event subject attribute temp file.
111 */
112 private static final String EVENT_SUBJECT_ATTRIBUTE_FILE_NAME = "EventSubjectAttribute";
113
114 /**
115 * File name for event subject getter temp file.
116 */
117 private static final String EVENT_SUBJECT_GETTER_FILE_NAME = "EventSubjectGetter";
118
119 /**
120 * File name for event subject setter temp file.
121 */
122 private static final String EVENT_SUBJECT_SETTER_FILE_NAME = "EventSubjectSetter";
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530123
Bharat saraswal33dfa012016-05-17 19:59:16 +0530124 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530125 * File name for generated class file for service
126 * suffix.
127 */
128 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
129
130 /**
131 * File name for generated class file for manager
132 * suffix.
133 */
134 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
135
136 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530137 * File name for generated class file for special type like union, typedef
138 * suffix.
139 */
140 private static final String EVENT_FILE_NAME_SUFFIX = "Event";
141
142 /**
143 * File name for generated class file for special type like union, typedef
144 * suffix.
145 */
146 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
Bharat saraswal33dfa012016-05-17 19:59:16 +0530147 private static final String JAVA_FILE_EXTENSION = ".java";
148
149 /**
150 * Java file handle for event subject file.
151 */
152 private File eventSubjectJavaFileHandle;
153
154 /**
155 * Java file handle for event listener file.
156 */
157 private File eventListenerJavaFileHandle;
158
159 /**
160 * Java file handle for event file.
161 */
162 private File eventJavaFileHandle;
163
164 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530165 * Temporary file handle for rpc interface.
166 */
167 private File rpcInterfaceTempFileHandle;
168
169 /**
170 * Temporary file handle for rpc manager impl.
171 */
172 private File rpcImplTempFileHandle;
173
174 /**
175 * Java file handle for rpc interface file.
176 */
177 private File serviceInterfaceJavaFileHandle;
178
179 /**
180 * Java file handle for manager impl file.
181 */
182 private File managerJavaFileHandle;
183
184 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530185 * Java file handle for event enum impl file.
186 */
187 private File eventEnumTempFileHandle;
188
189 /**
190 * Java file handle for event method impl file.
191 */
192 private File eventMethodTempFileHandle;
193
194 /**
195 * Java file handle for event subject attribute file.
196 */
197 private File eventSubjectAttributeTempFileHandle;
198
199 /**
200 * Java file handle for event subject getter impl file.
201 */
202 private File eventSubjectGetterTempFileHandle;
203
204 /**
205 * Java file handle for event subject setter impl file.
206 */
207 private File eventSubjectSetterTempFileHandle;
208
209 /**
Bharat saraswalb551aae2016-07-14 15:18:20 +0530210 * Creates an instance of temporary java code fragment.
211 *
212 * @param javaFileInfo generated file information
213 * @throws IOException when fails to create new file handle
214 */
215 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
216 throws IOException {
217 setJavaExtendsListHolder(new JavaExtendsListHolder());
218 setJavaImportData(new JavaImportData());
219 setJavaFileInfo(javaFileInfo);
220 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
221 getJavaFileInfo().getPackageFilePath()));
222 addGeneratedTempFile(RPC_INTERFACE_MASK);
223 addGeneratedTempFile(RPC_IMPL_MASK);
224
225 addGeneratedTempFile(EVENT_ENUM_MASK);
226 addGeneratedTempFile(EVENT_METHOD_MASK);
227 addGeneratedTempFile(EVENT_SUBJECT_ATTRIBUTE_MASK);
228 addGeneratedTempFile(EVENT_SUBJECT_GETTER_MASK);
229 addGeneratedTempFile(EVENT_SUBJECT_SETTER_MASK);
230
231 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
232 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
233
234 setEventEnumTempFileHandle(getTemporaryFileHandle(EVENT_ENUM_FILE_NAME));
235 setEventMethodTempFileHandle(getTemporaryFileHandle(EVENT_METHOD_FILE_NAME));
236 setEventSubjectAttributeTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_ATTRIBUTE_FILE_NAME));
237 setEventSubjectGetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_GETTER_FILE_NAME));
238 setEventSubjectSetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_SETTER_FILE_NAME));
239 }
240
241 /*Adds event method contents to event file.*/
242 private static String getEventFileContents(String eventClassname, String classname) {
243 return "\n" +
244 " /**\n" +
245 " * Creates " + classname + " event with type and subject.\n" +
246 " *\n" +
247 " * @param type event type\n" +
248 " * @param subject subject " + classname + "\n" +
249 " */\n" +
250 " public " + eventClassname + "(Type type, " + getCapitalCase(classname) + " subject) {\n" +
251 " super(type, subject);\n" +
252 " }\n" +
253 "\n" +
254 " /**\n" +
255 " * Creates " + classname + " event with type, subject and time.\n" +
256 " *\n" +
257 " * @param type event type\n" +
258 " * @param subject subject " + classname + "\n" +
259 " * @param time time of event\n" +
260 " */\n" +
261 " public " + eventClassname + "(Type type, " + getCapitalCase(classname)
262 + " subject, long time) {\n" +
263 " super(type, subject, time);\n" +
264 " }\n" +
265 "\n";
266 }
267
268 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530269 * Returns rpc method's java file handle.
270 *
271 * @return java file handle
272 */
273 private File getServiceInterfaceJavaFileHandle() {
274 return serviceInterfaceJavaFileHandle;
275 }
276
277 /**
278 * Sets rpc method's java file handle.
279 *
280 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
281 */
282 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
283 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
284 }
285
286 /**
287 * Returns managers java file handle.
288 *
289 * @return java file handle
290 */
291 public File getManagerJavaFileHandle() {
292 return managerJavaFileHandle;
293 }
294
295 /**
296 * Sets manager java file handle.
297 *
298 * @param managerJavaFileHandle file handle for to manager
299 */
300 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
301 this.managerJavaFileHandle = managerJavaFileHandle;
302 }
303
304 /**
305 * Returns rpc method's temporary file handle.
306 *
307 * @return temporary file handle
308 */
309 public File getRpcInterfaceTempFileHandle() {
310 return rpcInterfaceTempFileHandle;
311 }
312
313 /**
314 * Sets rpc method's temporary file handle.
315 *
316 * @param rpcInterfaceTempFileHandle file handle for to rpc method
317 */
318 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
319 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
320 }
321
322 /**
323 * Retrieves the manager impl temp file.
324 *
325 * @return the manager impl temp file
326 */
327 public File getRpcImplTempFileHandle() {
328 return rpcImplTempFileHandle;
329 }
330
331 /**
332 * Sets the manager impl temp file.
333 *
334 * @param rpcImplTempFileHandle the manager impl temp file
335 */
336 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
337 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
338 }
339
340 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530341 * Returns event's java file handle.
342 *
343 * @return java file handle
344 */
345 private File getEventJavaFileHandle() {
346 return eventJavaFileHandle;
347 }
348
349 /**
350 * Sets event's java file handle.
351 *
352 * @param eventJavaFileHandle file handle for event
353 */
354 private void setEventJavaFileHandle(File eventJavaFileHandle) {
355 this.eventJavaFileHandle = eventJavaFileHandle;
356 }
357
358 /**
359 * Returns event listeners's java file handle.
360 *
361 * @return java file handle
362 */
363 private File getEventListenerJavaFileHandle() {
364 return eventListenerJavaFileHandle;
365 }
366
367 /**
368 * Sets event's java file handle.
369 *
370 * @param eventListenerJavaFileHandle file handle for event
371 */
372 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
373 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
374 }
375
376 /**
377 * Returns event subject's java file handle.
378 *
379 * @return java file handle
380 */
381 private File getEventSubjectJavaFileHandle() {
382 return eventSubjectJavaFileHandle;
383 }
384
385 /**
386 * Sets event's subject java file handle.
387 *
388 * @param eventSubjectJavaFileHandle file handle for event's subject
389 */
390 private void setEventSubjectJavaFileHandle(File eventSubjectJavaFileHandle) {
391 this.eventSubjectJavaFileHandle = eventSubjectJavaFileHandle;
392 }
393
394 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530395 * Constructs java code exit.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530396 *
397 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530398 * @param curNode current YANG node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530399 * @throws IOException when fails to generate java files
400 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530401 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530402 public void generateJavaFile(int fileType, YangNode curNode)
403 throws IOException {
Bharat saraswalb551aae2016-07-14 15:18:20 +0530404 List<String> imports = ((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getServiceTempFiles()
405 .getJavaImportData().getImports();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530406
407 createPackage(curNode);
408
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530409 boolean isNotification = false;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530410 if (curNode instanceof YangJavaModule) {
411 if (!((YangJavaModule) curNode).getNotificationNodes().isEmpty()) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530412 isNotification = true;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530413 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530414 } else if (curNode instanceof YangJavaSubModule) {
415 if (!((YangJavaSubModule) curNode).getNotificationNodes().isEmpty()) {
416 isNotification = true;
417 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530418 }
419
420 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530421 addListenersImport(curNode, imports, true, LISTENER_SERVICE);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530422 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530423 /**
424 * Creates rpc interface file.
425 */
426 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
Bharat saraswalb551aae2016-07-14 15:18:20 +0530427 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530428
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530429 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530430 addListenersImport(curNode, imports, false, LISTENER_SERVICE);
431 addListenersImport(curNode, imports, true, LISTENER_REG);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530432 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530433 addAnnotationsImports(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530434 /**
435 * Create builder class file.
436 */
437 setManagerJavaFileHandle(getJavaFileHandle(getJavaClassName(MANAGER_FILE_NAME_SUFFIX)));
Bharat saraswalb551aae2016-07-14 15:18:20 +0530438 generateManagerClassFile(getManagerJavaFileHandle(), imports, curNode);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530439
440 insertDataIntoJavaFile(getManagerJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530441 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530442 addListenersImport(curNode, imports, false, LISTENER_REG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530443 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530444 addAnnotationsImports(imports, false);
445
446 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530447 generateEventJavaFile(curNode);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530448 generateEventListenerJavaFile(GENERATE_EVENT_LISTENER_INTERFACE, curNode);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530449 generateEventSubjectJavaFile(curNode);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530450 }
451
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530452 /**
453 * Close all the file handles.
454 */
455 freeTemporaryResources(false);
456 }
457
458 /**
459 * Adds rpc string information to applicable temp file.
460 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530461 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530462 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530463 * @param rpcName name of the rpc function
464 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530465 * @throws IOException IO operation fail
466 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530467 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswalb551aae2016-07-14 15:18:20 +0530468 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
469 String rpcName)
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530470 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530471 String rpcInput = EMPTY_STRING;
472 String rpcOutput = VOID;
Bharat saraswalcad0e652016-05-26 23:48:38 +0530473 String rpcInputJavaDoc = EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530474 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530475 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530476 }
477 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530478 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530479 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530480 if (!rpcInput.equals(EMPTY_STRING)) {
481 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
482 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530483 appendToFile(getRpcInterfaceTempFileHandle(),
Bharat saraswalcad0e652016-05-26 23:48:38 +0530484 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530485 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
486 appendToFile(getRpcImplTempFileHandle(),
487 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530488 }
489
490 /**
491 * Adds the JAVA rpc snippet information.
492 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530493 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530494 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530495 * @param pluginConfig plugin configurations
496 * @param rpcName name of the rpc function
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530497 * @throws IOException IO operation fail
498 */
499 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswalb551aae2016-07-14 15:18:20 +0530500 JavaAttributeInfo javaAttributeInfoOfOutput,
501 YangPluginConfig pluginConfig, String rpcName)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530502 throws IOException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530503 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530504 }
505
506 /**
507 * Constructs java code exit.
508 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530509 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530510 * @throws IOException when fails to generate java files
511 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530512 public void generateEventJavaFile(YangNode curNode)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530513 throws IOException {
514
515 List<String> imports = new ArrayList<>();
516
517 imports.add(getJavaImportData().getAbstractEventsImport());
Bharat saraswal96dfef02016-06-16 00:29:12 +0530518 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530519 String nodeName = curNodeInfo + EVENT_STRING;
520
521 addEnumMethod(nodeName, curNodeInfo + EVENT_SUBJECT_NAME_SUFFIX);
522
523 /**
524 * Creates event interface file.
525 */
526 setEventJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo + EVENT_FILE_NAME_SUFFIX));
527 generateEventFile(getEventJavaFileHandle(), curNode, imports);
528
529 /**
530 * Close all the file handles.
531 */
532 freeTemporaryResources(false);
533 }
534
535 /**
536 * Constructs java code exit.
537 *
538 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530539 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530540 * @throws IOException when fails to generate java files
541 */
542 public void generateEventListenerJavaFile(int fileType, YangNode curNode)
543 throws IOException {
544
545 List<String> imports = new ArrayList<>();
546
547 imports.add(getJavaImportData().getEventListenerImport());
548 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
549 .getJavaFileInfo().getJavaName());
550 /**
551 * Creates event listener interface file.
552 */
553 setEventListenerJavaFileHandle(
554 getJavaFileHandle(curNode, curNodeInfo + EVENT_LISTENER_FILE_NAME_SUFFIX));
555 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, imports);
556
557 /**
558 * Close all the file handles.
559 */
560 freeTemporaryResources(false);
561 }
562
563 /**
564 * Constructs java code exit.
565 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530566 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530567 * @throws IOException when fails to generate java files
568 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530569 public void generateEventSubjectJavaFile(YangNode curNode)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530570 throws IOException {
571
572 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
573 .getJavaFileInfo().getJavaName());
574 /**
575 * Creates event interface file.
576 */
577 setEventSubjectJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo +
578 EVENT_SUBJECT_NAME_SUFFIX));
579 generateEventSubjectFile(getEventSubjectJavaFileHandle(), curNode);
580
581 /**
582 * Close all the file handles.
583 */
584 freeTemporaryResources(false);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530585 }
586
587 /**
588 * Removes all temporary file handles.
589 *
Bharat saraswalb551aae2016-07-14 15:18:20 +0530590 * @param isErrorOccurred when translator fails to generate java files we need to close all open file handles
591 * include temporary files and java files.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530592 * @throws IOException when failed to delete the temporary files
593 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530594 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530595 public void freeTemporaryResources(boolean isErrorOccurred)
596 throws IOException {
597 boolean isError = isErrorOccurred;
598
599 closeFile(getServiceInterfaceJavaFileHandle(), isError);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530600 closeFile(getManagerJavaFileHandle(), isError);
601
602 if (getEventJavaFileHandle() != null) {
603 closeFile(getEventJavaFileHandle(), isError);
604 }
605 if (getEventListenerJavaFileHandle() != null) {
606 closeFile(getEventListenerJavaFileHandle(), isError);
607 }
608 if (getEventSubjectJavaFileHandle() != null) {
609 closeFile(getEventSubjectJavaFileHandle(), isError);
610 }
611
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530612 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530613 closeFile(getRpcImplTempFileHandle(), true);
614 closeFile(getGetterInterfaceTempFileHandle(), true);
615 closeFile(getSetterInterfaceTempFileHandle(), true);
616 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530617
618 super.freeTemporaryResources(isErrorOccurred);
619
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530620 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530621
622 /**
623 * Returns event enum temp file.
624 *
625 * @return event enum temp file
626 */
627 public File getEventEnumTempFileHandle() {
628 return eventEnumTempFileHandle;
629 }
630
631 /**
632 * Sets event enum temp file.
633 *
634 * @param eventEnumTempFileHandle event enum temp file
635 */
636 public void setEventEnumTempFileHandle(File eventEnumTempFileHandle) {
637 this.eventEnumTempFileHandle = eventEnumTempFileHandle;
638 }
639
640 /**
641 * Returns event method temp file.
642 *
643 * @return event method temp file
644 */
645 public File getEventMethodTempFileHandle() {
646 return eventMethodTempFileHandle;
647 }
648
649 /**
650 * Sets event method temp file.
651 *
652 * @param eventMethodTempFileHandle event method temp file
653 */
654 public void setEventMethodTempFileHandle(File eventMethodTempFileHandle) {
655 this.eventMethodTempFileHandle = eventMethodTempFileHandle;
656 }
657
658 /**
659 * Returns event subject attribute temp file.
660 *
661 * @return event subject attribute temp file
662 */
663 public File getEventSubjectAttributeTempFileHandle() {
664 return eventSubjectAttributeTempFileHandle;
665 }
666
667 /**
668 * Sets event subject attribute temp file.
669 *
670 * @param eventSubjectAttributeTempFileHandle event subject attribute temp file
671 */
672 public void setEventSubjectAttributeTempFileHandle(File eventSubjectAttributeTempFileHandle) {
673 this.eventSubjectAttributeTempFileHandle = eventSubjectAttributeTempFileHandle;
674 }
675
676 /**
677 * Returns event subject getter temp file.
678 *
679 * @return event subject getter temp file
680 */
681 public File getEventSubjectGetterTempFileHandle() {
682 return eventSubjectGetterTempFileHandle;
683 }
684
685 /**
686 * Sets event subject getter temp file.
687 *
688 * @param eventSubjectGetterTempFileHandle event subject getter temp file
689 */
690 public void setEventSubjectGetterTempFileHandle(File eventSubjectGetterTempFileHandle) {
691 this.eventSubjectGetterTempFileHandle = eventSubjectGetterTempFileHandle;
692 }
693
694 /**
695 * Returns event subject setter temp file.
696 *
697 * @return event subject setter temp file
698 */
699 public File getEventSubjectSetterTempFileHandle() {
700 return eventSubjectSetterTempFileHandle;
701 }
702
703 /**
704 * Sets event subject setter temp file.
705 *
706 * @param eventSubjectSetterTempFileHandle event subject setter temp file
707 */
708 public void setEventSubjectSetterTempFileHandle(File eventSubjectSetterTempFileHandle) {
709 this.eventSubjectSetterTempFileHandle = eventSubjectSetterTempFileHandle;
710 }
711
712 /**
713 * Adds java snippet for events to event subject file.
714 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530715 * @param curNode current node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530716 * @param pluginConfig plugin configurations
717 * @throws IOException when fails to do IO operations
718 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530719 public void addJavaSnippetOfEvent(YangNode curNode, YangPluginConfig pluginConfig)
720 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530721
Bharat saraswalb551aae2016-07-14 15:18:20 +0530722 String currentInfo = getCapitalCase(getCamelCase(curNode.getName(),
Bharat saraswal33dfa012016-05-17 19:59:16 +0530723 pluginConfig.getConflictResolver()));
Bharat saraswalb551aae2016-07-14 15:18:20 +0530724 String notificationName = curNode.getName();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530725
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530726 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530727 getCapitalCase(currentInfo));
728
Bharat saraswal96dfef02016-06-16 00:29:12 +0530729 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(qualifiedTypeInfo, getSmallCase(currentInfo),
730 null, false, false);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530731
732 /*Adds java info for event in respective temp files.*/
Bharat saraswalcad0e652016-05-26 23:48:38 +0530733 addEventEnum(notificationName, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530734 addEventSubjectAttribute(javaAttributeInfo, pluginConfig);
735 addEventSubjectGetter(javaAttributeInfo, pluginConfig);
736 addEventSubjectSetter(javaAttributeInfo, pluginConfig, currentInfo);
737 }
738
739 /*Adds event to enum temp file.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530740 private void addEventEnum(String notificationName, YangPluginConfig pluginConfig)
741 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530742 appendToFile(getEventEnumTempFileHandle(),
743 getJavaDoc(ENUM_ATTRIBUTE, notificationName, false, pluginConfig) + FOUR_SPACE_INDENTATION
Bharat saraswalcad0e652016-05-26 23:48:38 +0530744 + getEnumJavaAttribute(notificationName).toUpperCase() + COMMA + NEW_LINE);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530745 }
746
747 /*Adds event method in event class*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530748 private void addEnumMethod(String eventClassname, String className)
749 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530750 appendToFile(getEventMethodTempFileHandle(), getEventFileContents(eventClassname, className));
751 }
752
Bharat saraswal33dfa012016-05-17 19:59:16 +0530753 /*Adds events to event subject file.*/
754 private void addEventSubjectAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
755 throws IOException {
756 appendToFile(getEventSubjectAttributeTempFileHandle(),
757 FOUR_SPACE_INDENTATION + parseAttribute(attr, pluginConfig));
758 }
759
760 /*Adds getter method for event in event subject class.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530761 private void addEventSubjectGetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
762 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530763 appendToFile(getEventSubjectGetterTempFileHandle(),
764 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
765 + getGetterForClass(attr, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
766 }
767
768 /*Adds setter method for event in event subject class.*/
769 private void addEventSubjectSetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig, String className)
770 throws IOException {
771 appendToFile(getEventSubjectSetterTempFileHandle(),
772 getJavaDoc(MANAGER_SETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
773 + getSetterForClass(attr, className, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
774 }
775
776 /**
777 * Returns a temporary file handle for the event's file type.
778 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530779 * @param name file name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530780 * @return temporary file handle
781 * @throws IOException when fails to create new file handle
782 */
783 private File getJavaFileHandle(YangNode curNode, String name)
784 throws IOException {
785
786 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530787 JavaFileInfo childInfo = ((JavaFileInfoContainer) curNode.getChild()).getJavaFileInfo();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530788
789 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530790 childInfo);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530791 }
792
793 /**
794 * Returns the directory path.
795 *
796 * @return directory path
797 */
798 private String getDirPath(JavaFileInfo parentInfo) {
799 return (parentInfo.getPackageFilePath() + SLASH + parentInfo.getJavaName()).toLowerCase();
800 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530801}