blob: f13e6a5e75ae0de50ecd3ff53e71b5932e5a7f07 [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;
Bharat saraswal33dfa012016-05-17 19:59:16 +053074import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE;
75import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
76import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.MANAGER_SETTER_METHOD;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053077import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
78import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
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";
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530122
Bharat saraswal33dfa012016-05-17 19:59:16 +0530123 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530124 * File name for generated class file for service
125 * suffix.
126 */
127 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
128
129 /**
130 * File name for generated class file for manager
131 * suffix.
132 */
133 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
134
135 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530136 * File name for generated class file for special type like union, typedef
137 * suffix.
138 */
139 private static final String EVENT_FILE_NAME_SUFFIX = "Event";
140
141 /**
142 * File name for generated class file for special type like union, typedef
143 * suffix.
144 */
145 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
146
147 /**
148 * File name for generated class file for special type like union, typedef
149 * suffix.
150 */
151 public static final String EVENT_SUBJECT_NAME_SUFFIX = "EventSubject";
152
153 private static final String JAVA_FILE_EXTENSION = ".java";
154
155 /**
156 * Java file handle for event subject file.
157 */
158 private File eventSubjectJavaFileHandle;
159
160 /**
161 * Java file handle for event listener file.
162 */
163 private File eventListenerJavaFileHandle;
164
165 /**
166 * Java file handle for event file.
167 */
168 private File eventJavaFileHandle;
169
170 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530171 * Temporary file handle for rpc interface.
172 */
173 private File rpcInterfaceTempFileHandle;
174
175 /**
176 * Temporary file handle for rpc manager impl.
177 */
178 private File rpcImplTempFileHandle;
179
180 /**
181 * Java file handle for rpc interface file.
182 */
183 private File serviceInterfaceJavaFileHandle;
184
185 /**
186 * Java file handle for manager impl file.
187 */
188 private File managerJavaFileHandle;
189
190 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530191 * Java file handle for event enum impl file.
192 */
193 private File eventEnumTempFileHandle;
194
195 /**
196 * Java file handle for event method impl file.
197 */
198 private File eventMethodTempFileHandle;
199
200 /**
201 * Java file handle for event subject attribute file.
202 */
203 private File eventSubjectAttributeTempFileHandle;
204
205 /**
206 * Java file handle for event subject getter impl file.
207 */
208 private File eventSubjectGetterTempFileHandle;
209
210 /**
211 * Java file handle for event subject setter impl file.
212 */
213 private File eventSubjectSetterTempFileHandle;
214
215 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530216 * Returns rpc method's java file handle.
217 *
218 * @return java file handle
219 */
220 private File getServiceInterfaceJavaFileHandle() {
221 return serviceInterfaceJavaFileHandle;
222 }
223
224 /**
225 * Sets rpc method's java file handle.
226 *
227 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
228 */
229 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
230 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
231 }
232
233 /**
234 * Returns managers java file handle.
235 *
236 * @return java file handle
237 */
238 public File getManagerJavaFileHandle() {
239 return managerJavaFileHandle;
240 }
241
242 /**
243 * Sets manager java file handle.
244 *
245 * @param managerJavaFileHandle file handle for to manager
246 */
247 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
248 this.managerJavaFileHandle = managerJavaFileHandle;
249 }
250
251 /**
252 * Returns rpc method's temporary file handle.
253 *
254 * @return temporary file handle
255 */
256 public File getRpcInterfaceTempFileHandle() {
257 return rpcInterfaceTempFileHandle;
258 }
259
260 /**
261 * Sets rpc method's temporary file handle.
262 *
263 * @param rpcInterfaceTempFileHandle file handle for to rpc method
264 */
265 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
266 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
267 }
268
269 /**
270 * Retrieves the manager impl temp file.
271 *
272 * @return the manager impl temp file
273 */
274 public File getRpcImplTempFileHandle() {
275 return rpcImplTempFileHandle;
276 }
277
278 /**
279 * Sets the manager impl temp file.
280 *
281 * @param rpcImplTempFileHandle the manager impl temp file
282 */
283 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
284 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
285 }
286
287 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530288 * Returns event's java file handle.
289 *
290 * @return java file handle
291 */
292 private File getEventJavaFileHandle() {
293 return eventJavaFileHandle;
294 }
295
296 /**
297 * Sets event's java file handle.
298 *
299 * @param eventJavaFileHandle file handle for event
300 */
301 private void setEventJavaFileHandle(File eventJavaFileHandle) {
302 this.eventJavaFileHandle = eventJavaFileHandle;
303 }
304
305 /**
306 * Returns event listeners's java file handle.
307 *
308 * @return java file handle
309 */
310 private File getEventListenerJavaFileHandle() {
311 return eventListenerJavaFileHandle;
312 }
313
314 /**
315 * Sets event's java file handle.
316 *
317 * @param eventListenerJavaFileHandle file handle for event
318 */
319 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
320 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
321 }
322
323 /**
324 * Returns event subject's java file handle.
325 *
326 * @return java file handle
327 */
328 private File getEventSubjectJavaFileHandle() {
329 return eventSubjectJavaFileHandle;
330 }
331
332 /**
333 * Sets event's subject java file handle.
334 *
335 * @param eventSubjectJavaFileHandle file handle for event's subject
336 */
337 private void setEventSubjectJavaFileHandle(File eventSubjectJavaFileHandle) {
338 this.eventSubjectJavaFileHandle = eventSubjectJavaFileHandle;
339 }
340
341 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530342 * Creates an instance of temporary java code fragment.
343 *
344 * @param javaFileInfo generated file information
345 * @throws IOException when fails to create new file handle
346 */
347 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
348 throws IOException {
349 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530350
351 addGeneratedTempFile(RPC_INTERFACE_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530352 addGeneratedTempFile(RPC_IMPL_MASK);
353
Bharat saraswal33dfa012016-05-17 19:59:16 +0530354 addGeneratedTempFile(EVENT_ENUM_MASK);
355 addGeneratedTempFile(EVENT_METHOD_MASK);
356 addGeneratedTempFile(EVENT_SUBJECT_ATTRIBUTE_MASK);
357 addGeneratedTempFile(EVENT_SUBJECT_GETTER_MASK);
358 addGeneratedTempFile(EVENT_SUBJECT_SETTER_MASK);
359
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530360 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
361 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
Bharat saraswal33dfa012016-05-17 19:59:16 +0530362
363 setEventEnumTempFileHandle(getTemporaryFileHandle(EVENT_ENUM_FILE_NAME));
364 setEventMethodTempFileHandle(getTemporaryFileHandle(EVENT_METHOD_FILE_NAME));
365 setEventSubjectAttributeTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_ATTRIBUTE_FILE_NAME));
366 setEventSubjectGetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_GETTER_FILE_NAME));
367 setEventSubjectSetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_SETTER_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530368 }
369
370 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530371 * Constructs java code exit.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530372 *
373 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530374 * @param curNode current YANG node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530375 * @throws IOException when fails to generate java files
376 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530377 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530378 public void generateJavaFile(int fileType, YangNode curNode)
379 throws IOException {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530380 List<String> imports = getJavaImportData().getImports();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530381
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 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530436 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530437 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530438 * @param rpcName name of the rpc function
439 * @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,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530444 String rpcName)
445 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530446 String rpcInput = EMPTY_STRING;
447 String rpcOutput = VOID;
Bharat saraswalcad0e652016-05-26 23:48:38 +0530448 String rpcInputJavaDoc = EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530449 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530450 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530451 }
452 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530453 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530454 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530455 if (!rpcInput.equals(EMPTY_STRING)) {
456 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
457 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530458 appendToFile(getRpcInterfaceTempFileHandle(),
Bharat saraswalcad0e652016-05-26 23:48:38 +0530459 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530460 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
461 appendToFile(getRpcImplTempFileHandle(),
462 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530463 }
464
465 /**
Bharat saraswalcad0e652016-05-26 23:48:38 +0530466 * Adds rpc string information to applicable temp file.
467 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530468 * @param javaAttributeInfoOfInput rpc's input node attribute info
Bharat saraswalcad0e652016-05-26 23:48:38 +0530469 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530470 * @param rpcName name of the rpc function
471 * @param pluginConfig plugin configurations
472 * @param isInputLeafHolder if input node is leaf holder
473 * @param isOutputLeafHolder if output node is leaf holder
474 * @param isInputSingleChildHolder if input node is single child holder
Bharat saraswalcad0e652016-05-26 23:48:38 +0530475 * @param isOutputSingleChildHolder if input node is single child holder
476 * @throws IOException IO operation fail
477 */
478 public void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
479 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
480 String rpcName, boolean isInputLeafHolder, boolean isOutputLeafHolder,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530481 boolean isInputSingleChildHolder, boolean isOutputSingleChildHolder)
482 throws IOException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530483 String rpcInput = EMPTY_STRING;
484 String rpcOutput = VOID;
485 String rpcInputJavaDoc = EMPTY_STRING;
486 if (javaAttributeInfoOfInput != null) {
487 rpcInput = getInputOutputNames(javaAttributeInfoOfInput, isInputLeafHolder, isInputSingleChildHolder,
488 pluginConfig);
489 }
490 if (javaAttributeInfoOfOutput != null) {
491 rpcOutput =
492 getInputOutputNames(javaAttributeInfoOfOutput, isOutputLeafHolder, isOutputSingleChildHolder,
493 pluginConfig);
494 }
495 if (!rpcInput.equals(EMPTY_STRING)) {
496 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
497 }
498
499 appendToFile(getRpcInterfaceTempFileHandle(),
500 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
501 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
502 appendToFile(getRpcImplTempFileHandle(),
503 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
504 }
505
506 /**
507 * Returns names for input and output.
508 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530509 * @param attr attribute info
510 * @param isLeafHolder if leaf holder
Bharat saraswalcad0e652016-05-26 23:48:38 +0530511 * @param isSinglechildHolder if single child holder
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530512 * @param pluginConfig plugin configurations
Bharat saraswalcad0e652016-05-26 23:48:38 +0530513 * @return names for input and output
514 */
515 private String getInputOutputNames(JavaAttributeInfo attr, boolean isLeafHolder, boolean isSinglechildHolder,
516 YangPluginConfig pluginConfig) {
517 if (!attr.isListAttr()) {
518 if (!isLeafHolder || isSinglechildHolder) {
519 String attrName = "";
520 if (!attr.isQualifiedName()) {
521 attrName = getCapitalCase(attr.getAttributeName());
522 } else {
523 attrName = attr.getImportInfo().getPkgInfo() + "." + getCapitalCase(attr.getAttributeName());
524 }
525 return attrName;
526 } else {
527 String rpcInput = getJavaImportClass(attr.getAttributeType(), false,
528 pluginConfig.getConflictResolver());
529 if (rpcInput == null) {
530 rpcInput = getJavaDataType(attr.getAttributeType());
531 }
532 return rpcInput;
533 }
534 } else {
535 if (!isLeafHolder || isSinglechildHolder) {
536 String attrName = "";
537 if (!attr.isQualifiedName()) {
538 attrName = getCapitalCase(attr.getAttributeName());
539 } else {
540 attrName = attr.getImportInfo().getPkgInfo() + "." + getCapitalCase(attr.getAttributeName());
541 }
542 return getListAttribute(attrName);
543
544 } else {
545 return getListAttribute(getJavaImportClass(attr.getAttributeType(), true,
546 pluginConfig.getConflictResolver()));
547
548 }
549 }
550 }
551
552 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530553 * Adds the JAVA rpc snippet information.
554 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530555 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530556 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530557 * @param pluginConfig plugin configurations
558 * @param rpcName name of the rpc function
559 * @param isInputLeafHolder if input node is leaf holder
560 * @param isOutputLeafHolder if output node is leaf holder
561 * @param isInputSingleChildHolder if input node is single child holder
Bharat saraswalcad0e652016-05-26 23:48:38 +0530562 * @param isOutputSingleChildHolder if input node is single child holder
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530563 * @throws IOException IO operation fail
564 */
565 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530566 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
Bharat saraswalcad0e652016-05-26 23:48:38 +0530567 String rpcName, boolean isInputLeafHolder, boolean isOutputLeafHolder,
568 boolean isInputSingleChildHolder, boolean isOutputSingleChildHolder)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530569 throws IOException {
Bharat saraswalcad0e652016-05-26 23:48:38 +0530570 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName, isInputLeafHolder,
571 isOutputLeafHolder, isInputSingleChildHolder, isOutputSingleChildHolder);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530572 }
573
574 /**
575 * Constructs java code exit.
576 *
577 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530578 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530579 * @throws IOException when fails to generate java files
580 */
581 public void generateEventJavaFile(int fileType, YangNode curNode)
582 throws IOException {
583
584 List<String> imports = new ArrayList<>();
585
586 imports.add(getJavaImportData().getAbstractEventsImport());
587 String curNodeInfo =
588 getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName());
589 String nodeName = curNodeInfo + EVENT_STRING;
590
591 addEnumMethod(nodeName, curNodeInfo + EVENT_SUBJECT_NAME_SUFFIX);
592
593 /**
594 * Creates event interface file.
595 */
596 setEventJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo + EVENT_FILE_NAME_SUFFIX));
597 generateEventFile(getEventJavaFileHandle(), curNode, imports);
598
599 /**
600 * Close all the file handles.
601 */
602 freeTemporaryResources(false);
603 }
604
605 /**
606 * Constructs java code exit.
607 *
608 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530609 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530610 * @throws IOException when fails to generate java files
611 */
612 public void generateEventListenerJavaFile(int fileType, YangNode curNode)
613 throws IOException {
614
615 List<String> imports = new ArrayList<>();
616
617 imports.add(getJavaImportData().getEventListenerImport());
618 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
619 .getJavaFileInfo().getJavaName());
620 /**
621 * Creates event listener interface file.
622 */
623 setEventListenerJavaFileHandle(
624 getJavaFileHandle(curNode, curNodeInfo + EVENT_LISTENER_FILE_NAME_SUFFIX));
625 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, imports);
626
627 /**
628 * Close all the file handles.
629 */
630 freeTemporaryResources(false);
631 }
632
633 /**
634 * Constructs java code exit.
635 *
636 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530637 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530638 * @throws IOException when fails to generate java files
639 */
640 public void generateEventSubjectJavaFile(int fileType, YangNode curNode)
641 throws IOException {
642
643 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
644 .getJavaFileInfo().getJavaName());
645 /**
646 * Creates event interface file.
647 */
648 setEventSubjectJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo +
649 EVENT_SUBJECT_NAME_SUFFIX));
650 generateEventSubjectFile(getEventSubjectJavaFileHandle(), curNode);
651
652 /**
653 * Close all the file handles.
654 */
655 freeTemporaryResources(false);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530656 }
657
658 /**
659 * Removes all temporary file handles.
660 *
661 * @param isErrorOccurred when translator fails to generate java files we
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530662 * need to close all open file handles include temporary files
663 * and java files.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530664 * @throws IOException when failed to delete the temporary files
665 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530666 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530667 public void freeTemporaryResources(boolean isErrorOccurred)
668 throws IOException {
669 boolean isError = isErrorOccurred;
670
671 closeFile(getServiceInterfaceJavaFileHandle(), isError);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530672 closeFile(getManagerJavaFileHandle(), isError);
673
674 if (getEventJavaFileHandle() != null) {
675 closeFile(getEventJavaFileHandle(), isError);
676 }
677 if (getEventListenerJavaFileHandle() != null) {
678 closeFile(getEventListenerJavaFileHandle(), isError);
679 }
680 if (getEventSubjectJavaFileHandle() != null) {
681 closeFile(getEventSubjectJavaFileHandle(), isError);
682 }
683
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530684 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530685 closeFile(getRpcImplTempFileHandle(), true);
686 closeFile(getGetterInterfaceTempFileHandle(), true);
687 closeFile(getSetterInterfaceTempFileHandle(), true);
688 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530689
690 super.freeTemporaryResources(isErrorOccurred);
691
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530692 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530693
694 /**
695 * Returns event enum temp file.
696 *
697 * @return event enum temp file
698 */
699 public File getEventEnumTempFileHandle() {
700 return eventEnumTempFileHandle;
701 }
702
703 /**
704 * Sets event enum temp file.
705 *
706 * @param eventEnumTempFileHandle event enum temp file
707 */
708 public void setEventEnumTempFileHandle(File eventEnumTempFileHandle) {
709 this.eventEnumTempFileHandle = eventEnumTempFileHandle;
710 }
711
712 /**
713 * Returns event method temp file.
714 *
715 * @return event method temp file
716 */
717 public File getEventMethodTempFileHandle() {
718 return eventMethodTempFileHandle;
719 }
720
721 /**
722 * Sets event method temp file.
723 *
724 * @param eventMethodTempFileHandle event method temp file
725 */
726 public void setEventMethodTempFileHandle(File eventMethodTempFileHandle) {
727 this.eventMethodTempFileHandle = eventMethodTempFileHandle;
728 }
729
730 /**
731 * Returns event subject attribute temp file.
732 *
733 * @return event subject attribute temp file
734 */
735 public File getEventSubjectAttributeTempFileHandle() {
736 return eventSubjectAttributeTempFileHandle;
737 }
738
739 /**
740 * Sets event subject attribute temp file.
741 *
742 * @param eventSubjectAttributeTempFileHandle event subject attribute temp file
743 */
744 public void setEventSubjectAttributeTempFileHandle(File eventSubjectAttributeTempFileHandle) {
745 this.eventSubjectAttributeTempFileHandle = eventSubjectAttributeTempFileHandle;
746 }
747
748 /**
749 * Returns event subject getter temp file.
750 *
751 * @return event subject getter temp file
752 */
753 public File getEventSubjectGetterTempFileHandle() {
754 return eventSubjectGetterTempFileHandle;
755 }
756
757 /**
758 * Sets event subject getter temp file.
759 *
760 * @param eventSubjectGetterTempFileHandle event subject getter temp file
761 */
762 public void setEventSubjectGetterTempFileHandle(File eventSubjectGetterTempFileHandle) {
763 this.eventSubjectGetterTempFileHandle = eventSubjectGetterTempFileHandle;
764 }
765
766 /**
767 * Returns event subject setter temp file.
768 *
769 * @return event subject setter temp file
770 */
771 public File getEventSubjectSetterTempFileHandle() {
772 return eventSubjectSetterTempFileHandle;
773 }
774
775 /**
776 * Sets event subject setter temp file.
777 *
778 * @param eventSubjectSetterTempFileHandle event subject setter temp file
779 */
780 public void setEventSubjectSetterTempFileHandle(File eventSubjectSetterTempFileHandle) {
781 this.eventSubjectSetterTempFileHandle = eventSubjectSetterTempFileHandle;
782 }
783
784 /**
785 * Adds java snippet for events to event subject file.
786 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530787 * @param curNode current node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530788 * @param pluginConfig plugin configurations
789 * @throws IOException when fails to do IO operations
790 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530791 public void addJavaSnippetOfEvent(YangNode curNode, YangPluginConfig pluginConfig)
792 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530793
794 String currentInfo = getCapitalCase(getCamelCase(((YangNotification) curNode).getName(),
795 pluginConfig.getConflictResolver()));
Bharat saraswalcad0e652016-05-26 23:48:38 +0530796 String notificationName = ((YangNotification) curNode).getName();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530797
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530798 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530799 getCapitalCase(currentInfo));
800
801 JavaAttributeInfo javaAttributeInfo =
802 getAttributeInfoForTheData(qualifiedTypeInfo, getSmallCase(currentInfo),
803 null, false, false);
804
805 /*Adds java info for event in respective temp files.*/
Bharat saraswalcad0e652016-05-26 23:48:38 +0530806 addEventEnum(notificationName, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530807 addEventSubjectAttribute(javaAttributeInfo, pluginConfig);
808 addEventSubjectGetter(javaAttributeInfo, pluginConfig);
809 addEventSubjectSetter(javaAttributeInfo, pluginConfig, currentInfo);
810 }
811
812 /*Adds event to enum temp file.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530813 private void addEventEnum(String notificationName, YangPluginConfig pluginConfig)
814 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530815 appendToFile(getEventEnumTempFileHandle(),
816 getJavaDoc(ENUM_ATTRIBUTE, notificationName, false, pluginConfig) + FOUR_SPACE_INDENTATION
Bharat saraswalcad0e652016-05-26 23:48:38 +0530817 + getEnumJavaAttribute(notificationName).toUpperCase() + COMMA + NEW_LINE);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530818 }
819
820 /*Adds event method in event class*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530821 private void addEnumMethod(String eventClassname, String className)
822 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530823 appendToFile(getEventMethodTempFileHandle(), getEventFileContents(eventClassname, className));
824 }
825
826 /*Adds event method contents to event file.*/
827 private static String getEventFileContents(String eventClassname, String classname) {
828 return "\n" +
829 " /**\n" +
830 " * Creates " + classname + " event with type and subject.\n" +
831 " *\n" +
832 " * @param type event type\n" +
833 " * @param subject subject " + classname + "\n" +
834 " */\n" +
835 " public " + eventClassname + "(Type type, " + getCapitalCase(classname) + " subject) {\n" +
836 " super(type, subject);\n" +
837 " }\n" +
838 "\n" +
839 " /**\n" +
840 " * Creates " + classname + " event with type, subject and time.\n" +
841 " *\n" +
842 " * @param type event type\n" +
843 " * @param subject subject " + classname + "\n" +
844 " * @param time time of event\n" +
845 " */\n" +
846 " public " + eventClassname + "(Type type, " + getCapitalCase(classname)
847 + " subject, long time) {\n" +
848 " super(type, subject, time);\n" +
849 " }\n" +
850 "\n";
851 }
852
853 /*Adds events to event subject file.*/
854 private void addEventSubjectAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
855 throws IOException {
856 appendToFile(getEventSubjectAttributeTempFileHandle(),
857 FOUR_SPACE_INDENTATION + parseAttribute(attr, pluginConfig));
858 }
859
860 /*Adds getter method for event in event subject class.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530861 private void addEventSubjectGetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
862 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530863 appendToFile(getEventSubjectGetterTempFileHandle(),
864 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
865 + getGetterForClass(attr, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
866 }
867
868 /*Adds setter method for event in event subject class.*/
869 private void addEventSubjectSetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig, String className)
870 throws IOException {
871 appendToFile(getEventSubjectSetterTempFileHandle(),
872 getJavaDoc(MANAGER_SETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
873 + getSetterForClass(attr, className, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
874 }
875
876 /**
877 * Returns a temporary file handle for the event's file type.
878 *
879 * @param fileName file name
880 * @return temporary file handle
881 * @throws IOException when fails to create new file handle
882 */
883 private File getJavaFileHandle(YangNode curNode, String name)
884 throws IOException {
885
886 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
887
888 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION,
889 parentInfo);
890 }
891
892 /**
893 * Returns the directory path.
894 *
895 * @return directory path
896 */
897 private String getDirPath(JavaFileInfo parentInfo) {
898 return (parentInfo.getPackageFilePath() + SLASH + parentInfo.getJavaName()).toLowerCase();
899 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530900}