blob: 9cbe1fcc958667a5e669ad79fcf3a750b846d170 [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_LISTENER_INTERFACE;
31import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
32import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_ENUM_MASK;
33import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_METHOD_MASK;
34import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_ATTRIBUTE_MASK;
35import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_GETTER_MASK;
36import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_SUBJECT_SETTER_MASK;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053037import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_IMPL_MASK;
38import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.RPC_INTERFACE_MASK;
Bharat saraswal33dfa012016-05-17 19:59:16 +053039import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
40import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053041import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
Bharat saraswal33dfa012016-05-17 19:59:16 +053042import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventFile;
43import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventListenerFile;
44import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventSubjectFile;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053045import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateManagerClassFile;
46import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateServiceInterfaceFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053047import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getFileObject;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053048import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
49import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
Bharat saraswalcad0e652016-05-26 23:48:38 +053050import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053051import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getSmallCase;
Bharat saraswal33dfa012016-05-17 19:59:16 +053052import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetterForClass;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053053import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcManagerMethod;
54import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getRpcServiceMethod;
Bharat saraswal33dfa012016-05-17 19:59:16 +053055import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getSetterForClass;
Bharat saraswalb1170bd2016-07-14 13:26:18 +053056import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.addAnnotationsImports;
57import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.addListenersImport;
58import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053059import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
Bharat saraswalc0e04842016-05-12 13:16:57 +053060import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053061import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
62import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +053063import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_REG;
64import static org.onosproject.yangutils.utils.UtilConstants.LISTENER_SERVICE;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053065import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswalc0e04842016-05-12 13:16:57 +053066import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
Bharat saraswal33dfa012016-05-17 19:59:16 +053067import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalc0e04842016-05-12 13:16:57 +053068import static org.onosproject.yangutils.utils.UtilConstants.VOID;
Gaurav Agrawal8a5af142016-06-15 13:58:01 +053069import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
Bharat saraswal96dfef02016-06-16 00:29:12 +053070import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
71import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswal33dfa012016-05-17 19:59:16 +053072import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE;
73import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
74import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.MANAGER_SETTER_METHOD;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053075import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053076
77/**
78 * Represents implementation of java service code fragments temporary implementations.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053079 * Maintains the temp files required specific for service and manager java snippet generation.
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +053080 */
81public class TempJavaServiceFragmentFiles
82 extends TempJavaFragmentFiles {
83
84 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053085 * File name for rpc method.
86 */
87 private static final String RPC_INTERFACE_FILE_NAME = "Rpc";
88
89 /**
90 * File name for rpc implementation method.
91 */
92 private static final String RPC_IMPL_FILE_NAME = "RpcImpl";
93
94 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +053095 * File name for event enum temp file.
96 */
97 private static final String EVENT_ENUM_FILE_NAME = "EventEnum";
98
99 /**
100 * File name for event method temp file.
101 */
102 private static final String EVENT_METHOD_FILE_NAME = "EventMethod";
103
104 /**
105 * File name for event subject attribute temp file.
106 */
107 private static final String EVENT_SUBJECT_ATTRIBUTE_FILE_NAME = "EventSubjectAttribute";
108
109 /**
110 * File name for event subject getter temp file.
111 */
112 private static final String EVENT_SUBJECT_GETTER_FILE_NAME = "EventSubjectGetter";
113
114 /**
115 * File name for event subject setter temp file.
116 */
117 private static final String EVENT_SUBJECT_SETTER_FILE_NAME = "EventSubjectSetter";
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530118
Bharat saraswal33dfa012016-05-17 19:59:16 +0530119 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530120 * File name for generated class file for service
121 * suffix.
122 */
123 private static final String SERVICE_FILE_NAME_SUFFIX = "Service";
124
125 /**
126 * File name for generated class file for manager
127 * suffix.
128 */
129 private static final String MANAGER_FILE_NAME_SUFFIX = "Manager";
130
131 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530132 * File name for generated class file for special type like union, typedef
133 * suffix.
134 */
135 private static final String EVENT_FILE_NAME_SUFFIX = "Event";
136
137 /**
138 * File name for generated class file for special type like union, typedef
139 * suffix.
140 */
141 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
142
143 /**
144 * File name for generated class file for special type like union, typedef
145 * suffix.
146 */
147 public static final String EVENT_SUBJECT_NAME_SUFFIX = "EventSubject";
148
149 private static final String JAVA_FILE_EXTENSION = ".java";
150
151 /**
152 * Java file handle for event subject file.
153 */
154 private File eventSubjectJavaFileHandle;
155
156 /**
157 * Java file handle for event listener file.
158 */
159 private File eventListenerJavaFileHandle;
160
161 /**
162 * Java file handle for event file.
163 */
164 private File eventJavaFileHandle;
165
166 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530167 * Temporary file handle for rpc interface.
168 */
169 private File rpcInterfaceTempFileHandle;
170
171 /**
172 * Temporary file handle for rpc manager impl.
173 */
174 private File rpcImplTempFileHandle;
175
176 /**
177 * Java file handle for rpc interface file.
178 */
179 private File serviceInterfaceJavaFileHandle;
180
181 /**
182 * Java file handle for manager impl file.
183 */
184 private File managerJavaFileHandle;
185
186 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530187 * Java file handle for event enum impl file.
188 */
189 private File eventEnumTempFileHandle;
190
191 /**
192 * Java file handle for event method impl file.
193 */
194 private File eventMethodTempFileHandle;
195
196 /**
197 * Java file handle for event subject attribute file.
198 */
199 private File eventSubjectAttributeTempFileHandle;
200
201 /**
202 * Java file handle for event subject getter impl file.
203 */
204 private File eventSubjectGetterTempFileHandle;
205
206 /**
207 * Java file handle for event subject setter impl file.
208 */
209 private File eventSubjectSetterTempFileHandle;
210
211 /**
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530212 * Returns rpc method's java file handle.
213 *
214 * @return java file handle
215 */
216 private File getServiceInterfaceJavaFileHandle() {
217 return serviceInterfaceJavaFileHandle;
218 }
219
220 /**
221 * Sets rpc method's java file handle.
222 *
223 * @param serviceInterfaceJavaFileHandle file handle for to rpc method
224 */
225 private void setServiceInterfaceJavaFileHandle(File serviceInterfaceJavaFileHandle) {
226 this.serviceInterfaceJavaFileHandle = serviceInterfaceJavaFileHandle;
227 }
228
229 /**
230 * Returns managers java file handle.
231 *
232 * @return java file handle
233 */
234 public File getManagerJavaFileHandle() {
235 return managerJavaFileHandle;
236 }
237
238 /**
239 * Sets manager java file handle.
240 *
241 * @param managerJavaFileHandle file handle for to manager
242 */
243 public void setManagerJavaFileHandle(File managerJavaFileHandle) {
244 this.managerJavaFileHandle = managerJavaFileHandle;
245 }
246
247 /**
248 * Returns rpc method's temporary file handle.
249 *
250 * @return temporary file handle
251 */
252 public File getRpcInterfaceTempFileHandle() {
253 return rpcInterfaceTempFileHandle;
254 }
255
256 /**
257 * Sets rpc method's temporary file handle.
258 *
259 * @param rpcInterfaceTempFileHandle file handle for to rpc method
260 */
261 private void setRpcInterfaceTempFileHandle(File rpcInterfaceTempFileHandle) {
262 this.rpcInterfaceTempFileHandle = rpcInterfaceTempFileHandle;
263 }
264
265 /**
266 * Retrieves the manager impl temp file.
267 *
268 * @return the manager impl temp file
269 */
270 public File getRpcImplTempFileHandle() {
271 return rpcImplTempFileHandle;
272 }
273
274 /**
275 * Sets the manager impl temp file.
276 *
277 * @param rpcImplTempFileHandle the manager impl temp file
278 */
279 public void setRpcImplTempFileHandle(File rpcImplTempFileHandle) {
280 this.rpcImplTempFileHandle = rpcImplTempFileHandle;
281 }
282
283 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530284 * Returns event's java file handle.
285 *
286 * @return java file handle
287 */
288 private File getEventJavaFileHandle() {
289 return eventJavaFileHandle;
290 }
291
292 /**
293 * Sets event's java file handle.
294 *
295 * @param eventJavaFileHandle file handle for event
296 */
297 private void setEventJavaFileHandle(File eventJavaFileHandle) {
298 this.eventJavaFileHandle = eventJavaFileHandle;
299 }
300
301 /**
302 * Returns event listeners's java file handle.
303 *
304 * @return java file handle
305 */
306 private File getEventListenerJavaFileHandle() {
307 return eventListenerJavaFileHandle;
308 }
309
310 /**
311 * Sets event's java file handle.
312 *
313 * @param eventListenerJavaFileHandle file handle for event
314 */
315 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
316 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
317 }
318
319 /**
320 * Returns event subject's java file handle.
321 *
322 * @return java file handle
323 */
324 private File getEventSubjectJavaFileHandle() {
325 return eventSubjectJavaFileHandle;
326 }
327
328 /**
329 * Sets event's subject java file handle.
330 *
331 * @param eventSubjectJavaFileHandle file handle for event's subject
332 */
333 private void setEventSubjectJavaFileHandle(File eventSubjectJavaFileHandle) {
334 this.eventSubjectJavaFileHandle = eventSubjectJavaFileHandle;
335 }
336
337 /**
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530338 * Creates an instance of temporary java code fragment.
339 *
340 * @param javaFileInfo generated file information
341 * @throws IOException when fails to create new file handle
342 */
343 public TempJavaServiceFragmentFiles(JavaFileInfo javaFileInfo)
344 throws IOException {
345 super(javaFileInfo);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530346
347 addGeneratedTempFile(RPC_INTERFACE_MASK);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530348 addGeneratedTempFile(RPC_IMPL_MASK);
349
Bharat saraswal33dfa012016-05-17 19:59:16 +0530350 addGeneratedTempFile(EVENT_ENUM_MASK);
351 addGeneratedTempFile(EVENT_METHOD_MASK);
352 addGeneratedTempFile(EVENT_SUBJECT_ATTRIBUTE_MASK);
353 addGeneratedTempFile(EVENT_SUBJECT_GETTER_MASK);
354 addGeneratedTempFile(EVENT_SUBJECT_SETTER_MASK);
355
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530356 setRpcInterfaceTempFileHandle(getTemporaryFileHandle(RPC_INTERFACE_FILE_NAME));
357 setRpcImplTempFileHandle(getTemporaryFileHandle(RPC_IMPL_FILE_NAME));
Bharat saraswal33dfa012016-05-17 19:59:16 +0530358
359 setEventEnumTempFileHandle(getTemporaryFileHandle(EVENT_ENUM_FILE_NAME));
360 setEventMethodTempFileHandle(getTemporaryFileHandle(EVENT_METHOD_FILE_NAME));
361 setEventSubjectAttributeTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_ATTRIBUTE_FILE_NAME));
362 setEventSubjectGetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_GETTER_FILE_NAME));
363 setEventSubjectSetterTempFileHandle(getTemporaryFileHandle(EVENT_SUBJECT_SETTER_FILE_NAME));
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530364 }
365
366 /**
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530367 * Constructs java code exit.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530368 *
369 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530370 * @param curNode current YANG node
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530371 * @throws IOException when fails to generate java files
372 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530373 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530374 public void generateJavaFile(int fileType, YangNode curNode)
375 throws IOException {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530376 List<String> imports = getJavaImportData().getImports();
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530377
378 createPackage(curNode);
379
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530380 boolean isNotification = false;
Bharat saraswal33dfa012016-05-17 19:59:16 +0530381 if (curNode instanceof YangJavaModule) {
382 if (!((YangJavaModule) curNode).getNotificationNodes().isEmpty()) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530383 isNotification = true;
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530384 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530385 } else if (curNode instanceof YangJavaSubModule) {
386 if (!((YangJavaSubModule) curNode).getNotificationNodes().isEmpty()) {
387 isNotification = true;
388 }
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530389 }
390
391 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530392 addListenersImport(curNode, imports, true, LISTENER_SERVICE);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530393 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530394 /**
395 * Creates rpc interface file.
396 */
397 setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
398 generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent());
399
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530400 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530401 addListenersImport(curNode, imports, false, LISTENER_SERVICE);
402 addListenersImport(curNode, imports, true, LISTENER_REG);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530403 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530404 addAnnotationsImports(imports, true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530405 /**
406 * Create builder class file.
407 */
408 setManagerJavaFileHandle(getJavaFileHandle(getJavaClassName(MANAGER_FILE_NAME_SUFFIX)));
409 generateManagerClassFile(getManagerJavaFileHandle(), imports, curNode, isAttributePresent());
410
411 insertDataIntoJavaFile(getManagerJavaFileHandle(), getJavaClassDefClose());
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530412 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530413 addListenersImport(curNode, imports, false, LISTENER_REG);
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530414 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530415 addAnnotationsImports(imports, false);
416
417 if (isNotification) {
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530418 generateEventJavaFile(curNode);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530419 generateEventListenerJavaFile(GENERATE_EVENT_LISTENER_INTERFACE, curNode);
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530420 generateEventSubjectJavaFile(curNode);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530421 }
422
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530423 /**
424 * Close all the file handles.
425 */
426 freeTemporaryResources(false);
427 }
428
429 /**
430 * Adds rpc string information to applicable temp file.
431 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530432 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530433 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530434 * @param rpcName name of the rpc function
435 * @param pluginConfig plugin configurations
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530436 * @throws IOException IO operation fail
437 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530438 private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530439 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530440 String rpcName)
441 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530442 String rpcInput = EMPTY_STRING;
443 String rpcOutput = VOID;
Bharat saraswalcad0e652016-05-26 23:48:38 +0530444 String rpcInputJavaDoc = EMPTY_STRING;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530445 if (javaAttributeInfoOfInput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530446 rpcInput = getCapitalCase(javaAttributeInfoOfInput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530447 }
448 if (javaAttributeInfoOfOutput != null) {
janani b4a6711a2016-05-17 13:12:22 +0530449 rpcOutput = getCapitalCase(javaAttributeInfoOfOutput.getAttributeName());
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530450 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530451 if (!rpcInput.equals(EMPTY_STRING)) {
452 rpcInputJavaDoc = RPC_INPUT_VAR_NAME;
453 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530454 appendToFile(getRpcInterfaceTempFileHandle(),
Bharat saraswalcad0e652016-05-26 23:48:38 +0530455 generateJavaDocForRpc(rpcName, rpcInputJavaDoc, rpcOutput, pluginConfig)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530456 + getRpcServiceMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
457 appendToFile(getRpcImplTempFileHandle(),
458 getRpcManagerMethod(rpcName, rpcInput, rpcOutput, pluginConfig) + NEW_LINE);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530459 }
460
461 /**
462 * Adds the JAVA rpc snippet information.
463 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530464 * @param javaAttributeInfoOfInput rpc's input node attribute info
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530465 * @param javaAttributeInfoOfOutput rpc's output node attribute info
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530466 * @param pluginConfig plugin configurations
467 * @param rpcName name of the rpc function
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530468 * @throws IOException IO operation fail
469 */
470 public void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfoOfInput,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530471 JavaAttributeInfo javaAttributeInfoOfOutput, YangPluginConfig pluginConfig,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530472 String rpcName)
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530473 throws IOException {
Bharat saraswal96dfef02016-06-16 00:29:12 +0530474 addRpcString(javaAttributeInfoOfInput, javaAttributeInfoOfOutput, pluginConfig, rpcName);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530475 }
476
477 /**
478 * Constructs java code exit.
479 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530480 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530481 * @throws IOException when fails to generate java files
482 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530483 public void generateEventJavaFile(YangNode curNode)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530484 throws IOException {
485
486 List<String> imports = new ArrayList<>();
487
488 imports.add(getJavaImportData().getAbstractEventsImport());
Bharat saraswal96dfef02016-06-16 00:29:12 +0530489 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode).getJavaFileInfo().getJavaName());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530490 String nodeName = curNodeInfo + EVENT_STRING;
491
492 addEnumMethod(nodeName, curNodeInfo + EVENT_SUBJECT_NAME_SUFFIX);
493
494 /**
495 * Creates event interface file.
496 */
497 setEventJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo + EVENT_FILE_NAME_SUFFIX));
498 generateEventFile(getEventJavaFileHandle(), curNode, imports);
499
500 /**
501 * Close all the file handles.
502 */
503 freeTemporaryResources(false);
504 }
505
506 /**
507 * Constructs java code exit.
508 *
509 * @param fileType generated file type
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530510 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530511 * @throws IOException when fails to generate java files
512 */
513 public void generateEventListenerJavaFile(int fileType, YangNode curNode)
514 throws IOException {
515
516 List<String> imports = new ArrayList<>();
517
518 imports.add(getJavaImportData().getEventListenerImport());
519 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
520 .getJavaFileInfo().getJavaName());
521 /**
522 * Creates event listener interface file.
523 */
524 setEventListenerJavaFileHandle(
525 getJavaFileHandle(curNode, curNodeInfo + EVENT_LISTENER_FILE_NAME_SUFFIX));
526 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, imports);
527
528 /**
529 * Close all the file handles.
530 */
531 freeTemporaryResources(false);
532 }
533
534 /**
535 * Constructs java code exit.
536 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530537 * @param curNode current YANG node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530538 * @throws IOException when fails to generate java files
539 */
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530540 public void generateEventSubjectJavaFile(YangNode curNode)
Bharat saraswal33dfa012016-05-17 19:59:16 +0530541 throws IOException {
542
543 String curNodeInfo = getCapitalCase(((JavaFileInfoContainer) curNode)
544 .getJavaFileInfo().getJavaName());
545 /**
546 * Creates event interface file.
547 */
548 setEventSubjectJavaFileHandle(getJavaFileHandle(curNode, curNodeInfo +
549 EVENT_SUBJECT_NAME_SUFFIX));
550 generateEventSubjectFile(getEventSubjectJavaFileHandle(), curNode);
551
552 /**
553 * Close all the file handles.
554 */
555 freeTemporaryResources(false);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530556 }
557
558 /**
559 * Removes all temporary file handles.
560 *
561 * @param isErrorOccurred when translator fails to generate java files we
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530562 * need to close all open file handles include temporary files
563 * and java files.
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530564 * @throws IOException when failed to delete the temporary files
565 */
Bharat saraswalc0e04842016-05-12 13:16:57 +0530566 @Override
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530567 public void freeTemporaryResources(boolean isErrorOccurred)
568 throws IOException {
569 boolean isError = isErrorOccurred;
570
571 closeFile(getServiceInterfaceJavaFileHandle(), isError);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530572 closeFile(getManagerJavaFileHandle(), isError);
573
574 if (getEventJavaFileHandle() != null) {
575 closeFile(getEventJavaFileHandle(), isError);
576 }
577 if (getEventListenerJavaFileHandle() != null) {
578 closeFile(getEventListenerJavaFileHandle(), isError);
579 }
580 if (getEventSubjectJavaFileHandle() != null) {
581 closeFile(getEventSubjectJavaFileHandle(), isError);
582 }
583
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530584 closeFile(getRpcInterfaceTempFileHandle(), true);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530585 closeFile(getRpcImplTempFileHandle(), true);
586 closeFile(getGetterInterfaceTempFileHandle(), true);
587 closeFile(getSetterInterfaceTempFileHandle(), true);
588 closeFile(getSetterImplTempFileHandle(), true);
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530589
590 super.freeTemporaryResources(isErrorOccurred);
591
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530592 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530593
594 /**
595 * Returns event enum temp file.
596 *
597 * @return event enum temp file
598 */
599 public File getEventEnumTempFileHandle() {
600 return eventEnumTempFileHandle;
601 }
602
603 /**
604 * Sets event enum temp file.
605 *
606 * @param eventEnumTempFileHandle event enum temp file
607 */
608 public void setEventEnumTempFileHandle(File eventEnumTempFileHandle) {
609 this.eventEnumTempFileHandle = eventEnumTempFileHandle;
610 }
611
612 /**
613 * Returns event method temp file.
614 *
615 * @return event method temp file
616 */
617 public File getEventMethodTempFileHandle() {
618 return eventMethodTempFileHandle;
619 }
620
621 /**
622 * Sets event method temp file.
623 *
624 * @param eventMethodTempFileHandle event method temp file
625 */
626 public void setEventMethodTempFileHandle(File eventMethodTempFileHandle) {
627 this.eventMethodTempFileHandle = eventMethodTempFileHandle;
628 }
629
630 /**
631 * Returns event subject attribute temp file.
632 *
633 * @return event subject attribute temp file
634 */
635 public File getEventSubjectAttributeTempFileHandle() {
636 return eventSubjectAttributeTempFileHandle;
637 }
638
639 /**
640 * Sets event subject attribute temp file.
641 *
642 * @param eventSubjectAttributeTempFileHandle event subject attribute temp file
643 */
644 public void setEventSubjectAttributeTempFileHandle(File eventSubjectAttributeTempFileHandle) {
645 this.eventSubjectAttributeTempFileHandle = eventSubjectAttributeTempFileHandle;
646 }
647
648 /**
649 * Returns event subject getter temp file.
650 *
651 * @return event subject getter temp file
652 */
653 public File getEventSubjectGetterTempFileHandle() {
654 return eventSubjectGetterTempFileHandle;
655 }
656
657 /**
658 * Sets event subject getter temp file.
659 *
660 * @param eventSubjectGetterTempFileHandle event subject getter temp file
661 */
662 public void setEventSubjectGetterTempFileHandle(File eventSubjectGetterTempFileHandle) {
663 this.eventSubjectGetterTempFileHandle = eventSubjectGetterTempFileHandle;
664 }
665
666 /**
667 * Returns event subject setter temp file.
668 *
669 * @return event subject setter temp file
670 */
671 public File getEventSubjectSetterTempFileHandle() {
672 return eventSubjectSetterTempFileHandle;
673 }
674
675 /**
676 * Sets event subject setter temp file.
677 *
678 * @param eventSubjectSetterTempFileHandle event subject setter temp file
679 */
680 public void setEventSubjectSetterTempFileHandle(File eventSubjectSetterTempFileHandle) {
681 this.eventSubjectSetterTempFileHandle = eventSubjectSetterTempFileHandle;
682 }
683
684 /**
685 * Adds java snippet for events to event subject file.
686 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530687 * @param curNode current node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530688 * @param pluginConfig plugin configurations
689 * @throws IOException when fails to do IO operations
690 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530691 public void addJavaSnippetOfEvent(YangNode curNode, YangPluginConfig pluginConfig)
692 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530693
694 String currentInfo = getCapitalCase(getCamelCase(((YangNotification) curNode).getName(),
695 pluginConfig.getConflictResolver()));
Bharat saraswalcad0e652016-05-26 23:48:38 +0530696 String notificationName = ((YangNotification) curNode).getName();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530697
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530698 JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(curNode,
Bharat saraswal33dfa012016-05-17 19:59:16 +0530699 getCapitalCase(currentInfo));
700
Bharat saraswal96dfef02016-06-16 00:29:12 +0530701 JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(qualifiedTypeInfo, getSmallCase(currentInfo),
702 null, false, false);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530703
704 /*Adds java info for event in respective temp files.*/
Bharat saraswalcad0e652016-05-26 23:48:38 +0530705 addEventEnum(notificationName, pluginConfig);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530706 addEventSubjectAttribute(javaAttributeInfo, pluginConfig);
707 addEventSubjectGetter(javaAttributeInfo, pluginConfig);
708 addEventSubjectSetter(javaAttributeInfo, pluginConfig, currentInfo);
709 }
710
711 /*Adds event to enum temp file.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530712 private void addEventEnum(String notificationName, YangPluginConfig pluginConfig)
713 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530714 appendToFile(getEventEnumTempFileHandle(),
715 getJavaDoc(ENUM_ATTRIBUTE, notificationName, false, pluginConfig) + FOUR_SPACE_INDENTATION
Bharat saraswalcad0e652016-05-26 23:48:38 +0530716 + getEnumJavaAttribute(notificationName).toUpperCase() + COMMA + NEW_LINE);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530717 }
718
719 /*Adds event method in event class*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530720 private void addEnumMethod(String eventClassname, String className)
721 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530722 appendToFile(getEventMethodTempFileHandle(), getEventFileContents(eventClassname, className));
723 }
724
725 /*Adds event method contents to event file.*/
726 private static String getEventFileContents(String eventClassname, String classname) {
727 return "\n" +
728 " /**\n" +
729 " * Creates " + classname + " event with type and subject.\n" +
730 " *\n" +
731 " * @param type event type\n" +
732 " * @param subject subject " + classname + "\n" +
733 " */\n" +
734 " public " + eventClassname + "(Type type, " + getCapitalCase(classname) + " subject) {\n" +
735 " super(type, subject);\n" +
736 " }\n" +
737 "\n" +
738 " /**\n" +
739 " * Creates " + classname + " event with type, subject and time.\n" +
740 " *\n" +
741 " * @param type event type\n" +
742 " * @param subject subject " + classname + "\n" +
743 " * @param time time of event\n" +
744 " */\n" +
745 " public " + eventClassname + "(Type type, " + getCapitalCase(classname)
746 + " subject, long time) {\n" +
747 " super(type, subject, time);\n" +
748 " }\n" +
749 "\n";
750 }
751
752 /*Adds events to event subject file.*/
753 private void addEventSubjectAttribute(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
754 throws IOException {
755 appendToFile(getEventSubjectAttributeTempFileHandle(),
756 FOUR_SPACE_INDENTATION + parseAttribute(attr, pluginConfig));
757 }
758
759 /*Adds getter method for event in event subject class.*/
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530760 private void addEventSubjectGetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig)
761 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530762 appendToFile(getEventSubjectGetterTempFileHandle(),
763 getJavaDoc(GETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
764 + getGetterForClass(attr, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
765 }
766
767 /*Adds setter method for event in event subject class.*/
768 private void addEventSubjectSetter(JavaAttributeInfo attr, YangPluginConfig pluginConfig, String className)
769 throws IOException {
770 appendToFile(getEventSubjectSetterTempFileHandle(),
771 getJavaDoc(MANAGER_SETTER_METHOD, getCapitalCase(attr.getAttributeName()), false, pluginConfig)
772 + getSetterForClass(attr, className, GENERATE_EVENT_SUBJECT_CLASS) + NEW_LINE);
773 }
774
775 /**
776 * Returns a temporary file handle for the event's file type.
777 *
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530778 * @param name file name
Bharat saraswal33dfa012016-05-17 19:59:16 +0530779 * @return temporary file handle
780 * @throws IOException when fails to create new file handle
781 */
782 private File getJavaFileHandle(YangNode curNode, String name)
783 throws IOException {
784
785 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530786 JavaFileInfo childInfo = ((JavaFileInfoContainer) curNode.getChild()).getJavaFileInfo();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530787
788 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION,
Bharat saraswalb1170bd2016-07-14 13:26:18 +0530789 childInfo);
Bharat saraswal33dfa012016-05-17 19:59:16 +0530790 }
791
792 /**
793 * Returns the directory path.
794 *
795 * @return directory path
796 */
797 private String getDirPath(JavaFileInfo parentInfo) {
798 return (parentInfo.getPackageFilePath() + SLASH + parentInfo.getJavaName()).toLowerCase();
799 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530800}