blob: e5a73085542f94f10f921f05d7f77ffa04f0fbff [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;
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_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;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053049import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
50import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswalcad0e652016-05-26 23:48:38 +053051import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053052import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getSmallCase;
Bharat saraswal33dfa012016-05-17 19:59:16 +053053import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053054import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
55import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
Bharat saraswal33dfa012016-05-17 19:59:16 +053056import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
57import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAnnotationsImports;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053058import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addListnersImport;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053059import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053060import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswalc0e04842016-05-12 13:16:57 +053061import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053062import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
63import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053064import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
65import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053066import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalc0e04842016-05-12 13:16:57 +053067import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
Bharat saraswal33dfa012016-05-17 19:59:16 +053068import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalc0e04842016-05-12 13:16:57 +053069import static org.onosproject.yangutils.utils.UtilConstants.VOID;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053070import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswal96dfef02016-06-16 00:29:12 +053071import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
72import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswal33dfa012016-05-17 19:59:16 +053073import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE;
74import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
75import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.MANAGER_SETTER_METHOD;
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/**
79 * Represents implementation of java service code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053080 * Maintains the temp files required 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 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053086 * File name for rpc method.
87 */
88 private static final String RPC_INTERFACE_FILE_NAME = "Rpc";
89
90 /**
91 * File name for rpc implementation method.
92 */
93 private static final String RPC_IMPL_FILE_NAME = "RpcImpl";
94
95 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053096 * File name for event enum temp file.
97 */
98 private static final String EVENT_ENUM_FILE_NAME = "EventEnum";
99
100 /**
101 * File name for event method temp file.
102 */
103 private static final String EVENT_METHOD_FILE_NAME = "EventMethod";
104
105 /**
106 * File name for event subject attribute temp file.
107 */
108 private static final String EVENT_SUBJECT_ATTRIBUTE_FILE_NAME = "EventSubjectAttribute";
109
110 /**
111 * File name for event subject getter temp file.
112 */
113 private static final String EVENT_SUBJECT_GETTER_FILE_NAME = "EventSubjectGetter";
114
115 /**
116 * File name for event subject setter temp file.
117 */
118 private static final String EVENT_SUBJECT_SETTER_FILE_NAME = "EventSubjectSetter";
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530119
Bharat saraswal33dfa012016-05-17 19:59:16 +0530120 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530121 * File name for generated class file for service
122 * suffix.
123 */
124 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
125
126 /**
127 * File name for generated class file for manager
128 * suffix.
129 */
130 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
131
132 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530133 * File name for generated class file for special type like union, typedef
134 * suffix.
135 */
136 private static final String EVENT_FILE_NAME_SUFFIX = "Event";
137
138 /**
139 * File name for generated class file for special type like union, typedef
140 * suffix.
141 */
142 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
143
144 /**
145 * File name for generated class file for special type like union, typedef
146 * suffix.
147 */
148 public static final String EVENT_SUBJECT_NAME_SUFFIX = "EventSubject";
149
150 private static final String JAVA_FILE_EXTENSION = ".java";
151
152 /**
153 * Java file handle for event subject file.
154 */
155 private File eventSubjectJavaFileHandle;
156
157 /**
158 * Java file handle for event listener file.
159 */
160 private File eventListenerJavaFileHandle;
161
162 /**
163 * Java file handle for event file.
164 */
165 private File eventJavaFileHandle;
166
167 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530168 * Temporary file handle for rpc interface.
169 */
170 private File rpcInterfaceTempFileHandle;
171
172 /**
173 * Temporary file handle for rpc manager impl.
174 */
175 private File rpcImplTempFileHandle;
176
177 /**
178 * Java file handle for rpc interface file.
179 */
180 private File serviceInterfaceJavaFileHandle;
181
182 /**
183 * Java file handle for manager impl file.
184 */
185 private File managerJavaFileHandle;
186
187 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530188 * Java file handle for event enum impl file.
189 */
190 private File eventEnumTempFileHandle;
191
192 /**
193 * Java file handle for event method impl file.
194 */
195 private File eventMethodTempFileHandle;
196
197 /**
198 * Java file handle for event subject attribute file.
199 */
200 private File eventSubjectAttributeTempFileHandle;
201
202 /**
203 * Java file handle for event subject getter impl file.
204 */
205 private File eventSubjectGetterTempFileHandle;
206
207 /**
208 * Java file handle for event subject setter impl file.
209 */
210 private File eventSubjectSetterTempFileHandle;
211
212 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530213 * Returns rpc method's java file handle.
214 *
215 * @return java file handle
216 */
217 private File getServiceInterfaceJavaFileHandle() {
218 return serviceInterfaceJavaFileHandle;
219 }
220
221 /**
222 * Sets rpc method's java file handle.
223 *
224 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
225 */
226 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
227 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
228 }
229
230 /**
231 * Returns managers java file handle.
232 *
233 * @return java file handle
234 */
235 public File getManagerJavaFileHandle() {
236 return managerJavaFileHandle;
237 }
238
239 /**
240 * Sets manager java file handle.
241 *
242 * @param managerJavaFileHandle file handle for to manager
243 */
244 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
245 this.managerJavaFileHandle = managerJavaFileHandle;
246 }
247
248 /**
249 * Returns rpc method's temporary file handle.
250 *
251 * @return temporary file handle
252 */
253 public File getRpcInterfaceTempFileHandle() {
254 return rpcInterfaceTempFileHandle;
255 }
256
257 /**
258 * Sets rpc method's temporary file handle.
259 *
260 * @param rpcInterfaceTempFileHandle file handle for to rpc method
261 */
262 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
263 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
264 }
265
266 /**
267 * Retrieves the manager impl temp file.
268 *
269 * @return the manager impl temp file
270 */
271 public File getRpcImplTempFileHandle() {
272 return rpcImplTempFileHandle;
273 }
274
275 /**
276 * Sets the manager impl temp file.
277 *
278 * @param rpcImplTempFileHandle the manager impl temp file
279 */
280 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
281 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
282 }
283
284 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530285 * Returns event's java file handle.
286 *
287 * @return java file handle
288 */
289 private File getEventJavaFileHandle() {
290 return eventJavaFileHandle;
291 }
292
293 /**
294 * Sets event's java file handle.
295 *
296 * @param eventJavaFileHandle file handle for event
297 */
298 private void setEventJavaFileHandle(File eventJavaFileHandle) {
299 this.eventJavaFileHandle = eventJavaFileHandle;
300 }
301
302 /**
303 * Returns event listeners's java file handle.
304 *
305 * @return java file handle
306 */
307 private File getEventListenerJavaFileHandle() {
308 return eventListenerJavaFileHandle;
309 }
310
311 /**
312 * Sets event's java file handle.
313 *
314 * @param eventListenerJavaFileHandle file handle for event
315 */
316 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
317 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
318 }
319
320 /**
321 * Returns event subject's java file handle.
322 *
323 * @return java file handle
324 */
325 private File getEventSubjectJavaFileHandle() {
326 return eventSubjectJavaFileHandle;
327 }
328
329 /**
330 * Sets event's subject java file handle.
331 *
332 * @param eventSubjectJavaFileHandle file handle for event's subject
333 */
334 private void setEventSubjectJavaFileHandle(File eventSubjectJavaFileHandle) {
335 this.eventSubjectJavaFileHandle = eventSubjectJavaFileHandle;
336 }
337
338 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530339 * Creates an instance of temporary java code fragment.
340 *
341 * @param javaFileInfo generated file information
342 * @throws IOException when fails to create new file handle
343 */
344 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
345 throws IOException {
346 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530347
348 addGeneratedTempFile(RPC_INTERFACE_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530349 addGeneratedTempFile(RPC_IMPL_MASK);
350
Bharat saraswal33dfa012016-05-17 19:59:16 +0530351 addGeneratedTempFile(EVENT_ENUM_MASK);
352 addGeneratedTempFile(EVENT_METHOD_MASK);
353 addGeneratedTempFile(EVENT_SUBJECT_ATTRIBUTE_MASK);
354 addGeneratedTempFile(EVENT_SUBJECT_GETTER_MASK);
355 addGeneratedTempFile(EVENT_SUBJECT_SETTER_MASK);
356
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530357 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
358 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
Bharat saraswal33dfa012016-05-17 19:59:16 +0530359
360 setEventEnumTempFileHandle(getTemporaryFileHandle(EVENT_ENUM_FILE_NAME));
361 setEventMethodTempFileHandle(getTemporaryFileHandle(EVENT_METHOD_FILE_NAME));
362 setEventSubjectAttributeTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_ATTRIBUTE_FILE_NAME));
363 setEventSubjectGetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_GETTER_FILE_NAME));
364 setEventSubjectSetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_SETTER_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530365 }
366
367 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530368 * Constructs java code exit.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530369 *
370 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530371 * @param curNode current YANG node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530372 * @throws IOException when fails to generate java files
373 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530374 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530375 public void generateJavaFile(int fileType, YangNode curNode)
376 throws IOException {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530377 List<String> imports = getJavaImportData().getImports();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530378
379 createPackage(curNode);
380
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530381 boolean isNotification = false;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530382 if (curNode instanceof YangJavaModule) {
383 if (!((YangJavaModule) curNode).getNotificationNodes().isEmpty()) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530384 isNotification = true;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530385 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530386 } else if (curNode instanceof YangJavaSubModule) {
387 if (!((YangJavaSubModule) curNode).getNotificationNodes().isEmpty()) {
388 isNotification = true;
389 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530390 }
391
392 if (isNotification) {
393 addListnersImport(curNode, imports, true, LISTENER_SERVICE);
394 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530395 /**
396 * Creates rpc interface file.
397 */
398 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
399 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent());
400
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530401 if (isNotification) {
402 addListnersImport(curNode, imports, false, LISTENER_SERVICE);
403 addListnersImport(curNode, imports, true, LISTENER_REG);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530404 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530405 addAnnotationsImports(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530406 /**
407 * Create builder class file.
408 */
409 setManagerJavaFileHandle(getJavaFileHandle(getJavaClassName(MANAGER_FILE_NAME_SUFFIX)));
410 generateManagerClassFile(getManagerJavaFileHandle(), imports, curNode, isAttributePresent());
411
412 insertDataIntoJavaFile(getManagerJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530413 if (isNotification) {
414 addListnersImport(curNode, imports, false, LISTENER_REG);
415 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530416 addAnnotationsImports(imports, false);
417
418 if (isNotification) {
419 generateEventJavaFile(GENERATE_EVENT_CLASS, curNode);
420 generateEventListenerJavaFile(GENERATE_EVENT_LISTENER_INTERFACE, curNode);
421 generateEventSubjectJavaFile(GENERATE_EVENT_SUBJECT_CLASS, curNode);
422 }
423
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530424 /**
425 * Close all the file handles.
426 */
427 freeTemporaryResources(false);
428 }
429
430 /**
431 * Adds rpc string information to applicable temp file.
432 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530433 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530434 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530435 * @param rpcName name of the rpc function
436 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530437 * @throws IOException IO operation fail
438 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530439 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530440 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530441 String rpcName)
442 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530443 String rpcInput = EMPTY_STRING;
444 String rpcOutput = VOID;
Bharat saraswalcad0e652016-05-26 23:48:38 +0530445 String rpcInputJavaDoc = EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530446 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530447 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530448 }
449 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530450 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530451 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530452 if (!rpcInput.equals(EMPTY_STRING)) {
453 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
454 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530455 appendToFile(getRpcInterfaceTempFileHandle(),
Bharat saraswalcad0e652016-05-26 23:48:38 +0530456 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530457 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
458 appendToFile(getRpcImplTempFileHandle(),
459 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530460 }
461
462 /**
463 * Adds the JAVA rpc snippet information.
464 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530465 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530466 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530467 * @param pluginConfig plugin configurations
468 * @param rpcName name of the rpc function
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530469 * @throws IOException IO operation fail
470 */
471 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530472 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530473 String rpcName)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530474 throws IOException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530475 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530476 }
477
478 /**
479 * Constructs java code exit.
480 *
481 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530482 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530483 * @throws IOException when fails to generate java files
484 */
485 public void generateEventJavaFile(int fileType, YangNode curNode)
486 throws IOException {
487
488 List<String> imports = new ArrayList<>();
489
490 imports.add(getJavaImportData().getAbstractEventsImport());
Bharat saraswal96dfef02016-06-16 00:29:12 +0530491 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530492 String nodeName = curNodeInfo + EVENT_STRING;
493
494 addEnumMethod(nodeName, curNodeInfo + EVENT_SUBJECT_NAME_SUFFIX);
495
496 /**
497 * Creates event interface file.
498 */
499 setEventJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo + EVENT_FILE_NAME_SUFFIX));
500 generateEventFile(getEventJavaFileHandle(), curNode, imports);
501
502 /**
503 * Close all the file handles.
504 */
505 freeTemporaryResources(false);
506 }
507
508 /**
509 * Constructs java code exit.
510 *
511 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530512 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530513 * @throws IOException when fails to generate java files
514 */
515 public void generateEventListenerJavaFile(int fileType, YangNode curNode)
516 throws IOException {
517
518 List<String> imports = new ArrayList<>();
519
520 imports.add(getJavaImportData().getEventListenerImport());
521 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
522 .getJavaFileInfo().getJavaName());
523 /**
524 * Creates event listener interface file.
525 */
526 setEventListenerJavaFileHandle(
527 getJavaFileHandle(curNode, curNodeInfo + EVENT_LISTENER_FILE_NAME_SUFFIX));
528 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, imports);
529
530 /**
531 * Close all the file handles.
532 */
533 freeTemporaryResources(false);
534 }
535
536 /**
537 * Constructs java code exit.
538 *
539 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530540 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530541 * @throws IOException when fails to generate java files
542 */
543 public void generateEventSubjectJavaFile(int fileType, YangNode curNode)
544 throws IOException {
545
546 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
547 .getJavaFileInfo().getJavaName());
548 /**
549 * Creates event interface file.
550 */
551 setEventSubjectJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo +
552 EVENT_SUBJECT_NAME_SUFFIX));
553 generateEventSubjectFile(getEventSubjectJavaFileHandle(), curNode);
554
555 /**
556 * Close all the file handles.
557 */
558 freeTemporaryResources(false);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530559 }
560
561 /**
562 * Removes all temporary file handles.
563 *
564 * @param isErrorOccurred when translator fails to generate java files we
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530565 * need to close all open file handles include temporary files
566 * and java files.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530567 * @throws IOException when failed to delete the temporary files
568 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530569 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530570 public void freeTemporaryResources(boolean isErrorOccurred)
571 throws IOException {
572 boolean isError = isErrorOccurred;
573
574 closeFile(getServiceInterfaceJavaFileHandle(), isError);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530575 closeFile(getManagerJavaFileHandle(), isError);
576
577 if (getEventJavaFileHandle() != null) {
578 closeFile(getEventJavaFileHandle(), isError);
579 }
580 if (getEventListenerJavaFileHandle() != null) {
581 closeFile(getEventListenerJavaFileHandle(), isError);
582 }
583 if (getEventSubjectJavaFileHandle() != null) {
584 closeFile(getEventSubjectJavaFileHandle(), isError);
585 }
586
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530587 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530588 closeFile(getRpcImplTempFileHandle(), true);
589 closeFile(getGetterInterfaceTempFileHandle(), true);
590 closeFile(getSetterInterfaceTempFileHandle(), true);
591 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530592
593 super.freeTemporaryResources(isErrorOccurred);
594
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530595 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530596
597 /**
598 * Returns event enum temp file.
599 *
600 * @return event enum temp file
601 */
602 public File getEventEnumTempFileHandle() {
603 return eventEnumTempFileHandle;
604 }
605
606 /**
607 * Sets event enum temp file.
608 *
609 * @param eventEnumTempFileHandle event enum temp file
610 */
611 public void setEventEnumTempFileHandle(File eventEnumTempFileHandle) {
612 this.eventEnumTempFileHandle = eventEnumTempFileHandle;
613 }
614
615 /**
616 * Returns event method temp file.
617 *
618 * @return event method temp file
619 */
620 public File getEventMethodTempFileHandle() {
621 return eventMethodTempFileHandle;
622 }
623
624 /**
625 * Sets event method temp file.
626 *
627 * @param eventMethodTempFileHandle event method temp file
628 */
629 public void setEventMethodTempFileHandle(File eventMethodTempFileHandle) {
630 this.eventMethodTempFileHandle = eventMethodTempFileHandle;
631 }
632
633 /**
634 * Returns event subject attribute temp file.
635 *
636 * @return event subject attribute temp file
637 */
638 public File getEventSubjectAttributeTempFileHandle() {
639 return eventSubjectAttributeTempFileHandle;
640 }
641
642 /**
643 * Sets event subject attribute temp file.
644 *
645 * @param eventSubjectAttributeTempFileHandle event subject attribute temp file
646 */
647 public void setEventSubjectAttributeTempFileHandle(File eventSubjectAttributeTempFileHandle) {
648 this.eventSubjectAttributeTempFileHandle = eventSubjectAttributeTempFileHandle;
649 }
650
651 /**
652 * Returns event subject getter temp file.
653 *
654 * @return event subject getter temp file
655 */
656 public File getEventSubjectGetterTempFileHandle() {
657 return eventSubjectGetterTempFileHandle;
658 }
659
660 /**
661 * Sets event subject getter temp file.
662 *
663 * @param eventSubjectGetterTempFileHandle event subject getter temp file
664 */
665 public void setEventSubjectGetterTempFileHandle(File eventSubjectGetterTempFileHandle) {
666 this.eventSubjectGetterTempFileHandle = eventSubjectGetterTempFileHandle;
667 }
668
669 /**
670 * Returns event subject setter temp file.
671 *
672 * @return event subject setter temp file
673 */
674 public File getEventSubjectSetterTempFileHandle() {
675 return eventSubjectSetterTempFileHandle;
676 }
677
678 /**
679 * Sets event subject setter temp file.
680 *
681 * @param eventSubjectSetterTempFileHandle event subject setter temp file
682 */
683 public void setEventSubjectSetterTempFileHandle(File eventSubjectSetterTempFileHandle) {
684 this.eventSubjectSetterTempFileHandle = eventSubjectSetterTempFileHandle;
685 }
686
687 /**
688 * Adds java snippet for events to event subject file.
689 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530690 * @param curNode current node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530691 * @param pluginConfig plugin configurations
692 * @throws IOException when fails to do IO operations
693 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530694 public void addJavaSnippetOfEvent(YangNode curNode, YangPluginConfig pluginConfig)
695 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530696
697 String currentInfo = getCapitalCase(getCamelCase(((YangNotification) curNode).getName(),
698 pluginConfig.getConflictResolver()));
Bharat saraswalcad0e652016-05-26 23:48:38 +0530699 String notificationName = ((YangNotification) curNode).getName();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530700
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530701 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530702 getCapitalCase(currentInfo));
703
Bharat saraswal96dfef02016-06-16 00:29:12 +0530704 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(qualifiedTypeInfo, getSmallCase(currentInfo),
705 null, false, false);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530706
707 /*Adds java info for event in respective temp files.*/
Bharat saraswalcad0e652016-05-26 23:48:38 +0530708 addEventEnum(notificationName, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530709 addEventSubjectAttribute(javaAttributeInfo, pluginConfig);
710 addEventSubjectGetter(javaAttributeInfo, pluginConfig);
711 addEventSubjectSetter(javaAttributeInfo, pluginConfig, currentInfo);
712 }
713
714 /*Adds event to enum temp file.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530715 private void addEventEnum(String notificationName, YangPluginConfig pluginConfig)
716 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530717 appendToFile(getEventEnumTempFileHandle(),
718 getJavaDoc(ENUM_ATTRIBUTE, notificationName, false, pluginConfig) + FOUR_SPACE_INDENTATION
Bharat saraswalcad0e652016-05-26 23:48:38 +0530719 + getEnumJavaAttribute(notificationName).toUpperCase() + COMMA + NEW_LINE);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530720 }
721
722 /*Adds event method in event class*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530723 private void addEnumMethod(String eventClassname, String className)
724 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530725 appendToFile(getEventMethodTempFileHandle(), getEventFileContents(eventClassname, className));
726 }
727
728 /*Adds event method contents to event file.*/
729 private static String getEventFileContents(String eventClassname, String classname) {
730 return "\n" +
731 " /**\n" +
732 " * Creates " + classname + " event with type and subject.\n" +
733 " *\n" +
734 " * @param type event type\n" +
735 " * @param subject subject " + classname + "\n" +
736 " */\n" +
737 " public " + eventClassname + "(Type type, " + getCapitalCase(classname) + " subject) {\n" +
738 " super(type, subject);\n" +
739 " }\n" +
740 "\n" +
741 " /**\n" +
742 " * Creates " + classname + " event with type, subject and time.\n" +
743 " *\n" +
744 " * @param type event type\n" +
745 " * @param subject subject " + classname + "\n" +
746 " * @param time time of event\n" +
747 " */\n" +
748 " public " + eventClassname + "(Type type, " + getCapitalCase(classname)
749 + " subject, long time) {\n" +
750 " super(type, subject, time);\n" +
751 " }\n" +
752 "\n";
753 }
754
755 /*Adds events to event subject file.*/
756 private void addEventSubjectAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
757 throws IOException {
758 appendToFile(getEventSubjectAttributeTempFileHandle(),
759 FOUR_SPACE_INDENTATION + parseAttribute(attr, pluginConfig));
760 }
761
762 /*Adds getter method for event in event subject class.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530763 private void addEventSubjectGetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
764 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530765 appendToFile(getEventSubjectGetterTempFileHandle(),
766 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
767 + getGetterForClass(attr, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
768 }
769
770 /*Adds setter method for event in event subject class.*/
771 private void addEventSubjectSetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig, String className)
772 throws IOException {
773 appendToFile(getEventSubjectSetterTempFileHandle(),
774 getJavaDoc(MANAGER_SETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
775 + getSetterForClass(attr, className, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
776 }
777
778 /**
779 * Returns a temporary file handle for the event's file type.
780 *
781 * @param fileName file name
782 * @return temporary file handle
783 * @throws IOException when fails to create new file handle
784 */
785 private File getJavaFileHandle(YangNode curNode, String name)
786 throws IOException {
787
788 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
789
790 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION,
791 parentInfo);
792 }
793
794 /**
795 * Returns the directory path.
796 *
797 * @return directory path
798 */
799 private String getDirPath(JavaFileInfo parentInfo) {
800 return (parentInfo.getPackageFilePath() + SLASH + parentInfo.getJavaName()).toLowerCase();
801 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530802}