blob: c41fdb083db76aa717e5a816801d39457288469b [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;
Bharat saraswalcad0e652016-05-26 23:48:38 +053042import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaDataType;
43import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053044import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswalcad0e652016-05-26 23:48:38 +053045import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getListAttribute;
Bharat saraswal33dfa012016-05-17 19:59:16 +053046import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventFile;
47import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventListenerFile;
48import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventSubjectFile;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053049import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateManagerClassFile;
50import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053051import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
52import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
janani b4a6711a2016-05-17 13:12:22 +053053import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCapitalCase;
Bharat saraswalcad0e652016-05-26 23:48:38 +053054import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Bharat saraswal33dfa012016-05-17 19:59:16 +053055import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
56import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053057import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
58import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
Bharat saraswal33dfa012016-05-17 19:59:16 +053059import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
60import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAnnotationsImports;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053061import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addListnersImport;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053062import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053063import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswalc0e04842016-05-12 13:16:57 +053064import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053065import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
66import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053067import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
68import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053069import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalc0e04842016-05-12 13:16:57 +053070import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
Bharat saraswal33dfa012016-05-17 19:59:16 +053071import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalc0e04842016-05-12 13:16:57 +053072import static org.onosproject.yangutils.utils.UtilConstants.VOID;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053073import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
74import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
Bharat saraswal33dfa012016-05-17 19:59:16 +053075import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
76import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE;
77import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
78import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.MANAGER_SETTER_METHOD;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053079import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080
81/**
82 * Represents implementation of java service code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053083 * Maintains the temp files required specific for service and manager java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053084 */
85public class TempJavaServiceFragmentFiles
86 extends TempJavaFragmentFiles {
87
88 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053089 * File name for rpc method.
90 */
91 private static final String RPC_INTERFACE_FILE_NAME = "Rpc";
92
93 /**
94 * File name for rpc implementation method.
95 */
96 private static final String RPC_IMPL_FILE_NAME = "RpcImpl";
97
98 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053099 * File name for event enum temp file.
100 */
101 private static final String EVENT_ENUM_FILE_NAME = "EventEnum";
102
103 /**
104 * File name for event method temp file.
105 */
106 private static final String EVENT_METHOD_FILE_NAME = "EventMethod";
107
108 /**
109 * File name for event subject attribute temp file.
110 */
111 private static final String EVENT_SUBJECT_ATTRIBUTE_FILE_NAME = "EventSubjectAttribute";
112
113 /**
114 * File name for event subject getter temp file.
115 */
116 private static final String EVENT_SUBJECT_GETTER_FILE_NAME = "EventSubjectGetter";
117
118 /**
119 * File name for event subject setter temp file.
120 */
121 private static final String EVENT_SUBJECT_SETTER_FILE_NAME = "EventSubjectSetter";
122 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530123 * File name for generated class file for service
124 * suffix.
125 */
126 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
127
128 /**
129 * File name for generated class file for manager
130 * suffix.
131 */
132 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
133
134 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530135 * File name for generated class file for special type like union, typedef
136 * suffix.
137 */
138 private static final String EVENT_FILE_NAME_SUFFIX = "Event";
139
140 /**
141 * File name for generated class file for special type like union, typedef
142 * suffix.
143 */
144 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
145
146 /**
147 * File name for generated class file for special type like union, typedef
148 * suffix.
149 */
150 public static final String EVENT_SUBJECT_NAME_SUFFIX = "EventSubject";
151
152 private static final String JAVA_FILE_EXTENSION = ".java";
153
154 /**
155 * Java file handle for event subject file.
156 */
157 private File eventSubjectJavaFileHandle;
158
159 /**
160 * Java file handle for event listener file.
161 */
162 private File eventListenerJavaFileHandle;
163
164 /**
165 * Java file handle for event file.
166 */
167 private File eventJavaFileHandle;
168
169 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530170 * Temporary file handle for rpc interface.
171 */
172 private File rpcInterfaceTempFileHandle;
173
174 /**
175 * Temporary file handle for rpc manager impl.
176 */
177 private File rpcImplTempFileHandle;
178
179 /**
180 * Java file handle for rpc interface file.
181 */
182 private File serviceInterfaceJavaFileHandle;
183
184 /**
185 * Java file handle for manager impl file.
186 */
187 private File managerJavaFileHandle;
188
189 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530190 * Java file handle for event enum impl file.
191 */
192 private File eventEnumTempFileHandle;
193
194 /**
195 * Java file handle for event method impl file.
196 */
197 private File eventMethodTempFileHandle;
198
199 /**
200 * Java file handle for event subject attribute file.
201 */
202 private File eventSubjectAttributeTempFileHandle;
203
204 /**
205 * Java file handle for event subject getter impl file.
206 */
207 private File eventSubjectGetterTempFileHandle;
208
209 /**
210 * Java file handle for event subject setter impl file.
211 */
212 private File eventSubjectSetterTempFileHandle;
213
214 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530215 * Returns rpc method's java file handle.
216 *
217 * @return java file handle
218 */
219 private File getServiceInterfaceJavaFileHandle() {
220 return serviceInterfaceJavaFileHandle;
221 }
222
223 /**
224 * Sets rpc method's java file handle.
225 *
226 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
227 */
228 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
229 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
230 }
231
232 /**
233 * Returns managers java file handle.
234 *
235 * @return java file handle
236 */
237 public File getManagerJavaFileHandle() {
238 return managerJavaFileHandle;
239 }
240
241 /**
242 * Sets manager java file handle.
243 *
244 * @param managerJavaFileHandle file handle for to manager
245 */
246 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
247 this.managerJavaFileHandle = managerJavaFileHandle;
248 }
249
250 /**
251 * Returns rpc method's temporary file handle.
252 *
253 * @return temporary file handle
254 */
255 public File getRpcInterfaceTempFileHandle() {
256 return rpcInterfaceTempFileHandle;
257 }
258
259 /**
260 * Sets rpc method's temporary file handle.
261 *
262 * @param rpcInterfaceTempFileHandle file handle for to rpc method
263 */
264 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
265 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
266 }
267
268 /**
269 * Retrieves the manager impl temp file.
270 *
271 * @return the manager impl temp file
272 */
273 public File getRpcImplTempFileHandle() {
274 return rpcImplTempFileHandle;
275 }
276
277 /**
278 * Sets the manager impl temp file.
279 *
280 * @param rpcImplTempFileHandle the manager impl temp file
281 */
282 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
283 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
284 }
285
286 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530287 * Returns event's java file handle.
288 *
289 * @return java file handle
290 */
291 private File getEventJavaFileHandle() {
292 return eventJavaFileHandle;
293 }
294
295 /**
296 * Sets event's java file handle.
297 *
298 * @param eventJavaFileHandle file handle for event
299 */
300 private void setEventJavaFileHandle(File eventJavaFileHandle) {
301 this.eventJavaFileHandle = eventJavaFileHandle;
302 }
303
304 /**
305 * Returns event listeners's java file handle.
306 *
307 * @return java file handle
308 */
309 private File getEventListenerJavaFileHandle() {
310 return eventListenerJavaFileHandle;
311 }
312
313 /**
314 * Sets event's java file handle.
315 *
316 * @param eventListenerJavaFileHandle file handle for event
317 */
318 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
319 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
320 }
321
322 /**
323 * Returns event subject's java file handle.
324 *
325 * @return java file handle
326 */
327 private File getEventSubjectJavaFileHandle() {
328 return eventSubjectJavaFileHandle;
329 }
330
331 /**
332 * Sets event's subject java file handle.
333 *
334 * @param eventSubjectJavaFileHandle file handle for event's subject
335 */
336 private void setEventSubjectJavaFileHandle(File eventSubjectJavaFileHandle) {
337 this.eventSubjectJavaFileHandle = eventSubjectJavaFileHandle;
338 }
339
340 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530341 * Creates an instance of temporary java code fragment.
342 *
343 * @param javaFileInfo generated file information
344 * @throws IOException when fails to create new file handle
345 */
346 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
347 throws IOException {
348 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530349
350 addGeneratedTempFile(RPC_INTERFACE_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530351 addGeneratedTempFile(RPC_IMPL_MASK);
352
Bharat saraswal33dfa012016-05-17 19:59:16 +0530353 addGeneratedTempFile(EVENT_ENUM_MASK);
354 addGeneratedTempFile(EVENT_METHOD_MASK);
355 addGeneratedTempFile(EVENT_SUBJECT_ATTRIBUTE_MASK);
356 addGeneratedTempFile(EVENT_SUBJECT_GETTER_MASK);
357 addGeneratedTempFile(EVENT_SUBJECT_SETTER_MASK);
358
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530359 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
360 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
Bharat saraswal33dfa012016-05-17 19:59:16 +0530361
362 setEventEnumTempFileHandle(getTemporaryFileHandle(EVENT_ENUM_FILE_NAME));
363 setEventMethodTempFileHandle(getTemporaryFileHandle(EVENT_METHOD_FILE_NAME));
364 setEventSubjectAttributeTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_ATTRIBUTE_FILE_NAME));
365 setEventSubjectGetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_GETTER_FILE_NAME));
366 setEventSubjectSetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_SETTER_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530367 }
368
369 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530370 * Constructs java code exit.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530371 *
372 * @param fileType generated file type
373 * @param curNode current YANG node
374 * @throws IOException when fails to generate java files
375 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530376 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530377 public void generateJavaFile(int fileType, YangNode curNode)
378 throws IOException {
379 List<String> imports = new ArrayList<>();
380 imports = getJavaImportData().getImports();
381
382 createPackage(curNode);
383
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530384 boolean isNotification = false;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530385 if (curNode instanceof YangJavaModule) {
386 if (!((YangJavaModule) curNode).getNotificationNodes().isEmpty()) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530387 isNotification = true;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530388 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530389 } else if (curNode instanceof YangJavaSubModule) {
390 if (!((YangJavaSubModule) curNode).getNotificationNodes().isEmpty()) {
391 isNotification = true;
392 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530393 }
394
395 if (isNotification) {
396 addListnersImport(curNode, imports, true, LISTENER_SERVICE);
397 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530398 /**
399 * Creates rpc interface file.
400 */
401 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
402 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent());
403
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530404 if (isNotification) {
405 addListnersImport(curNode, imports, false, LISTENER_SERVICE);
406 addListnersImport(curNode, imports, true, LISTENER_REG);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530407 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530408 addAnnotationsImports(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530409 /**
410 * Create builder class file.
411 */
412 setManagerJavaFileHandle(getJavaFileHandle(getJavaClassName(MANAGER_FILE_NAME_SUFFIX)));
413 generateManagerClassFile(getManagerJavaFileHandle(), imports, curNode, isAttributePresent());
414
415 insertDataIntoJavaFile(getManagerJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530416 if (isNotification) {
417 addListnersImport(curNode, imports, false, LISTENER_REG);
418 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530419 addAnnotationsImports(imports, false);
420
421 if (isNotification) {
422 generateEventJavaFile(GENERATE_EVENT_CLASS, curNode);
423 generateEventListenerJavaFile(GENERATE_EVENT_LISTENER_INTERFACE, curNode);
424 generateEventSubjectJavaFile(GENERATE_EVENT_SUBJECT_CLASS, curNode);
425 }
426
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530427 /**
428 * Close all the file handles.
429 */
430 freeTemporaryResources(false);
431 }
432
433 /**
434 * Adds rpc string information to applicable temp file.
435 *
436 * @param javaAttributeInfoOfInput rpc's input node attribute info
437 * @param javaAttributeInfoOfOutput rpc's output node attribute info
438 * @param rpcName name of the rpc function
Bharat saraswal33dfa012016-05-17 19:59:16 +0530439 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530440 * @throws IOException IO operation fail
441 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530442 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530443 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
Bharat saraswalc0e04842016-05-12 13:16:57 +0530444 String rpcName) throws IOException {
445 String rpcInput = EMPTY_STRING;
446 String rpcOutput = VOID;
Bharat saraswalcad0e652016-05-26 23:48:38 +0530447 String rpcInputJavaDoc = EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530448 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530449 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530450 }
451 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530452 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530453 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530454 if (!rpcInput.equals(EMPTY_STRING)) {
455 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
456 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530457 appendToFile(getRpcInterfaceTempFileHandle(),
Bharat saraswalcad0e652016-05-26 23:48:38 +0530458 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530459 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
460 appendToFile(getRpcImplTempFileHandle(),
461 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530462 }
463
464 /**
Bharat saraswalcad0e652016-05-26 23:48:38 +0530465 * Adds rpc string information to applicable temp file.
466 *
467 * @param javaAttributeInfoOfInput rpc's input node attribute info
468 * @param javaAttributeInfoOfOutput rpc's output node attribute info
469 * @param rpcName name of the rpc function
470 * @param pluginConfig plugin configurations
471 * @param isInputLeafHolder if input node is leaf holder
472 * @param isOutputLeafHolder if output node is leaf holder
473 * @param isInputSingleChildHolder if input node is single child holder
474 * @param isOutputSingleChildHolder if input node is single child holder
475 * @throws IOException IO operation fail
476 */
477 public void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
478 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
479 String rpcName, boolean isInputLeafHolder, boolean isOutputLeafHolder,
480 boolean isInputSingleChildHolder, boolean isOutputSingleChildHolder) throws IOException {
481 String rpcInput = EMPTY_STRING;
482 String rpcOutput = VOID;
483 String rpcInputJavaDoc = EMPTY_STRING;
484 if (javaAttributeInfoOfInput != null) {
485 rpcInput = getInputOutputNames(javaAttributeInfoOfInput, isInputLeafHolder, isInputSingleChildHolder,
486 pluginConfig);
487 }
488 if (javaAttributeInfoOfOutput != null) {
489 rpcOutput =
490 getInputOutputNames(javaAttributeInfoOfOutput, isOutputLeafHolder, isOutputSingleChildHolder,
491 pluginConfig);
492 }
493 if (!rpcInput.equals(EMPTY_STRING)) {
494 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
495 }
496
497 appendToFile(getRpcInterfaceTempFileHandle(),
498 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
499 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
500 appendToFile(getRpcImplTempFileHandle(),
501 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
502 }
503
504 /**
505 * Returns names for input and output.
506 *
507 * @param attr attribute info
508 * @param isLeafHolder if leaf holder
509 * @param isSinglechildHolder if single child holder
510 * @param pluginConfig plugin configurations
511 * @return names for input and output
512 */
513 private String getInputOutputNames(JavaAttributeInfo attr, boolean isLeafHolder, boolean isSinglechildHolder,
514 YangPluginConfig pluginConfig) {
515 if (!attr.isListAttr()) {
516 if (!isLeafHolder || isSinglechildHolder) {
517 String attrName = "";
518 if (!attr.isQualifiedName()) {
519 attrName = getCapitalCase(attr.getAttributeName());
520 } else {
521 attrName = attr.getImportInfo().getPkgInfo() + "." + getCapitalCase(attr.getAttributeName());
522 }
523 return attrName;
524 } else {
525 String rpcInput = getJavaImportClass(attr.getAttributeType(), false,
526 pluginConfig.getConflictResolver());
527 if (rpcInput == null) {
528 rpcInput = getJavaDataType(attr.getAttributeType());
529 }
530 return rpcInput;
531 }
532 } else {
533 if (!isLeafHolder || isSinglechildHolder) {
534 String attrName = "";
535 if (!attr.isQualifiedName()) {
536 attrName = getCapitalCase(attr.getAttributeName());
537 } else {
538 attrName = attr.getImportInfo().getPkgInfo() + "." + getCapitalCase(attr.getAttributeName());
539 }
540 return getListAttribute(attrName);
541
542 } else {
543 return getListAttribute(getJavaImportClass(attr.getAttributeType(), true,
544 pluginConfig.getConflictResolver()));
545
546 }
547 }
548 }
549
550 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530551 * Adds the JAVA rpc snippet information.
552 *
553 * @param javaAttributeInfoOfInput rpc's input node attribute info
554 * @param javaAttributeInfoOfOutput rpc's output node attribute info
Bharat saraswal33dfa012016-05-17 19:59:16 +0530555 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530556 * @param rpcName name of the rpc function
Bharat saraswalcad0e652016-05-26 23:48:38 +0530557 * @param isInputLeafHolder if input node is leaf holder
558 * @param isOutputLeafHolder if output node is leaf holder
559 * @param isInputSingleChildHolder if input node is single child holder
560 * @param isOutputSingleChildHolder if input node is single child holder
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530561 * @throws IOException IO operation fail
562 */
563 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530564 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
Bharat saraswalcad0e652016-05-26 23:48:38 +0530565 String rpcName, boolean isInputLeafHolder, boolean isOutputLeafHolder,
566 boolean isInputSingleChildHolder, boolean isOutputSingleChildHolder)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530567 throws IOException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530568 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName, isInputLeafHolder,
569 isOutputLeafHolder, isInputSingleChildHolder, isOutputSingleChildHolder);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530570 }
571
572 /**
573 * Constructs java code exit.
574 *
575 * @param fileType generated file type
576 * @param curNode current YANG node
577 * @throws IOException when fails to generate java files
578 */
579 public void generateEventJavaFile(int fileType, YangNode curNode)
580 throws IOException {
581
582 List<String> imports = new ArrayList<>();
583
584 imports.add(getJavaImportData().getAbstractEventsImport());
585 String curNodeInfo =
586 getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName());
587 String nodeName = curNodeInfo + EVENT_STRING;
588
589 addEnumMethod(nodeName, curNodeInfo + EVENT_SUBJECT_NAME_SUFFIX);
590
591 /**
592 * Creates event interface file.
593 */
594 setEventJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo + EVENT_FILE_NAME_SUFFIX));
595 generateEventFile(getEventJavaFileHandle(), curNode, imports);
596
597 /**
598 * Close all the file handles.
599 */
600 freeTemporaryResources(false);
601 }
602
603 /**
604 * Constructs java code exit.
605 *
606 * @param fileType generated file type
607 * @param curNode current YANG node
608 * @throws IOException when fails to generate java files
609 */
610 public void generateEventListenerJavaFile(int fileType, YangNode curNode)
611 throws IOException {
612
613 List<String> imports = new ArrayList<>();
614
615 imports.add(getJavaImportData().getEventListenerImport());
616 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
617 .getJavaFileInfo().getJavaName());
618 /**
619 * Creates event listener interface file.
620 */
621 setEventListenerJavaFileHandle(
622 getJavaFileHandle(curNode, curNodeInfo + EVENT_LISTENER_FILE_NAME_SUFFIX));
623 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, imports);
624
625 /**
626 * Close all the file handles.
627 */
628 freeTemporaryResources(false);
629 }
630
631 /**
632 * Constructs java code exit.
633 *
634 * @param fileType generated file type
635 * @param curNode current YANG node
636 * @throws IOException when fails to generate java files
637 */
638 public void generateEventSubjectJavaFile(int fileType, YangNode curNode)
639 throws IOException {
640
641 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
642 .getJavaFileInfo().getJavaName());
643 /**
644 * Creates event interface file.
645 */
646 setEventSubjectJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo +
647 EVENT_SUBJECT_NAME_SUFFIX));
648 generateEventSubjectFile(getEventSubjectJavaFileHandle(), curNode);
649
650 /**
651 * Close all the file handles.
652 */
653 freeTemporaryResources(false);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530654 }
655
656 /**
657 * Removes all temporary file handles.
658 *
659 * @param isErrorOccurred when translator fails to generate java files we
660 * need to close all open file handles include temporary files
661 * and java files.
662 * @throws IOException when failed to delete the temporary files
663 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530664 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530665 public void freeTemporaryResources(boolean isErrorOccurred)
666 throws IOException {
667 boolean isError = isErrorOccurred;
668
669 closeFile(getServiceInterfaceJavaFileHandle(), isError);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530670 closeFile(getManagerJavaFileHandle(), isError);
671
672 if (getEventJavaFileHandle() != null) {
673 closeFile(getEventJavaFileHandle(), isError);
674 }
675 if (getEventListenerJavaFileHandle() != null) {
676 closeFile(getEventListenerJavaFileHandle(), isError);
677 }
678 if (getEventSubjectJavaFileHandle() != null) {
679 closeFile(getEventSubjectJavaFileHandle(), isError);
680 }
681
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530682 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530683 closeFile(getRpcImplTempFileHandle(), true);
684 closeFile(getGetterInterfaceTempFileHandle(), true);
685 closeFile(getSetterInterfaceTempFileHandle(), true);
686 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530687
688 super.freeTemporaryResources(isErrorOccurred);
689
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530690 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530691
692 /**
693 * Returns event enum temp file.
694 *
695 * @return event enum temp file
696 */
697 public File getEventEnumTempFileHandle() {
698 return eventEnumTempFileHandle;
699 }
700
701 /**
702 * Sets event enum temp file.
703 *
704 * @param eventEnumTempFileHandle event enum temp file
705 */
706 public void setEventEnumTempFileHandle(File eventEnumTempFileHandle) {
707 this.eventEnumTempFileHandle = eventEnumTempFileHandle;
708 }
709
710 /**
711 * Returns event method temp file.
712 *
713 * @return event method temp file
714 */
715 public File getEventMethodTempFileHandle() {
716 return eventMethodTempFileHandle;
717 }
718
719 /**
720 * Sets event method temp file.
721 *
722 * @param eventMethodTempFileHandle event method temp file
723 */
724 public void setEventMethodTempFileHandle(File eventMethodTempFileHandle) {
725 this.eventMethodTempFileHandle = eventMethodTempFileHandle;
726 }
727
728 /**
729 * Returns event subject attribute temp file.
730 *
731 * @return event subject attribute temp file
732 */
733 public File getEventSubjectAttributeTempFileHandle() {
734 return eventSubjectAttributeTempFileHandle;
735 }
736
737 /**
738 * Sets event subject attribute temp file.
739 *
740 * @param eventSubjectAttributeTempFileHandle event subject attribute temp file
741 */
742 public void setEventSubjectAttributeTempFileHandle(File eventSubjectAttributeTempFileHandle) {
743 this.eventSubjectAttributeTempFileHandle = eventSubjectAttributeTempFileHandle;
744 }
745
746 /**
747 * Returns event subject getter temp file.
748 *
749 * @return event subject getter temp file
750 */
751 public File getEventSubjectGetterTempFileHandle() {
752 return eventSubjectGetterTempFileHandle;
753 }
754
755 /**
756 * Sets event subject getter temp file.
757 *
758 * @param eventSubjectGetterTempFileHandle event subject getter temp file
759 */
760 public void setEventSubjectGetterTempFileHandle(File eventSubjectGetterTempFileHandle) {
761 this.eventSubjectGetterTempFileHandle = eventSubjectGetterTempFileHandle;
762 }
763
764 /**
765 * Returns event subject setter temp file.
766 *
767 * @return event subject setter temp file
768 */
769 public File getEventSubjectSetterTempFileHandle() {
770 return eventSubjectSetterTempFileHandle;
771 }
772
773 /**
774 * Sets event subject setter temp file.
775 *
776 * @param eventSubjectSetterTempFileHandle event subject setter temp file
777 */
778 public void setEventSubjectSetterTempFileHandle(File eventSubjectSetterTempFileHandle) {
779 this.eventSubjectSetterTempFileHandle = eventSubjectSetterTempFileHandle;
780 }
781
782 /**
783 * Adds java snippet for events to event subject file.
784 *
785 * @param curNode current node
786 * @param pluginConfig plugin configurations
787 * @throws IOException when fails to do IO operations
788 */
789 public void addJavaSnippetOfEvent(YangNode curNode, YangPluginConfig pluginConfig) throws IOException {
790
791 String currentInfo = getCapitalCase(getCamelCase(((YangNotification) curNode).getName(),
792 pluginConfig.getConflictResolver()));
Bharat saraswalcad0e652016-05-26 23:48:38 +0530793 String notificationName = ((YangNotification) curNode).getName();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530794
795 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode.getParent(),
796 getCapitalCase(currentInfo));
797
798 JavaAttributeInfo javaAttributeInfo =
799 getAttributeInfoForTheData(qualifiedTypeInfo, getSmallCase(currentInfo),
800 null, false, false);
801
802 /*Adds java info for event in respective temp files.*/
Bharat saraswalcad0e652016-05-26 23:48:38 +0530803 addEventEnum(notificationName, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530804 addEventSubjectAttribute(javaAttributeInfo, pluginConfig);
805 addEventSubjectGetter(javaAttributeInfo, pluginConfig);
806 addEventSubjectSetter(javaAttributeInfo, pluginConfig, currentInfo);
807 }
808
809 /*Adds event to enum temp file.*/
810 private void addEventEnum(String notificationName, YangPluginConfig pluginConfig) throws IOException {
811 appendToFile(getEventEnumTempFileHandle(),
812 getJavaDoc(ENUM_ATTRIBUTE, notificationName, false, pluginConfig) + FOUR_SPACE_INDENTATION
Bharat saraswalcad0e652016-05-26 23:48:38 +0530813 + getEnumJavaAttribute(notificationName).toUpperCase() + COMMA + NEW_LINE);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530814 }
815
816 /*Adds event method in event class*/
817 private void addEnumMethod(String eventClassname, String className) throws IOException {
818 appendToFile(getEventMethodTempFileHandle(), getEventFileContents(eventClassname, className));
819 }
820
821 /*Adds event method contents to event file.*/
822 private static String getEventFileContents(String eventClassname, String classname) {
823 return "\n" +
824 " /**\n" +
825 " * Creates " + classname + " event with type and subject.\n" +
826 " *\n" +
827 " * @param type event type\n" +
828 " * @param subject subject " + classname + "\n" +
829 " */\n" +
830 " public " + eventClassname + "(Type type, " + getCapitalCase(classname) + " subject) {\n" +
831 " super(type, subject);\n" +
832 " }\n" +
833 "\n" +
834 " /**\n" +
835 " * Creates " + classname + " event with type, subject and time.\n" +
836 " *\n" +
837 " * @param type event type\n" +
838 " * @param subject subject " + classname + "\n" +
839 " * @param time time of event\n" +
840 " */\n" +
841 " public " + eventClassname + "(Type type, " + getCapitalCase(classname)
842 + " subject, long time) {\n" +
843 " super(type, subject, time);\n" +
844 " }\n" +
845 "\n";
846 }
847
848 /*Adds events to event subject file.*/
849 private void addEventSubjectAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
850 throws IOException {
851 appendToFile(getEventSubjectAttributeTempFileHandle(),
852 FOUR_SPACE_INDENTATION + parseAttribute(attr, pluginConfig));
853 }
854
855 /*Adds getter method for event in event subject class.*/
856 private void addEventSubjectGetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig) throws IOException {
857 appendToFile(getEventSubjectGetterTempFileHandle(),
858 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
859 + getGetterForClass(attr, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
860 }
861
862 /*Adds setter method for event in event subject class.*/
863 private void addEventSubjectSetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig, String className)
864 throws IOException {
865 appendToFile(getEventSubjectSetterTempFileHandle(),
866 getJavaDoc(MANAGER_SETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
867 + getSetterForClass(attr, className, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
868 }
869
870 /**
871 * Returns a temporary file handle for the event's file type.
872 *
873 * @param fileName file name
874 * @return temporary file handle
875 * @throws IOException when fails to create new file handle
876 */
877 private File getJavaFileHandle(YangNode curNode, String name)
878 throws IOException {
879
880 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
881
882 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION,
883 parentInfo);
884 }
885
886 /**
887 * Returns the directory path.
888 *
889 * @return directory path
890 */
891 private String getDirPath(JavaFileInfo parentInfo) {
892 return (parentInfo.getPackageFilePath() + SLASH + parentInfo.getJavaName()).toLowerCase();
893 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530894}