Repair Doxygen build infrastructure
[vpp.git] / vpp-api / java / jvpp / gen / jvppgen / jvpp_future_facade_gen.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2016 Cisco and/or its affiliates.
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 import os
17 from string import Template
18
19 import dto_gen
20 import util
21
22 jvpp_facade_callback_template = Template("""
23 package $plugin_package.$future_package;
24
25 /**
26  * <p>Async facade callback setting values to future objects
27  * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
28  * <br>(python representation of api file generated by vppapigen).
29  */
30 public final class FutureJVpp${plugin_name}FacadeCallback implements $plugin_package.$callback_package.JVpp${plugin_name}GlobalCallback {
31
32     private final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requests;
33     private final $plugin_package.$notification_package.Global${plugin_name}NotificationCallback notificationCallback;
34
35     public FutureJVpp${plugin_name}FacadeCallback(
36         final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requestMap,
37         final $plugin_package.$notification_package.Global${plugin_name}NotificationCallback notificationCallback) {
38         this.requests = requestMap;
39         this.notificationCallback = notificationCallback;
40     }
41
42     @Override
43     @SuppressWarnings("unchecked")
44     public void onError($base_package.VppCallbackException reply) {
45         final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
46
47         synchronized(requests) {
48             completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.getCtxId());
49         }
50
51         if(completableFuture != null) {
52             completableFuture.completeExceptionally(reply);
53
54             synchronized(requests) {
55                 requests.remove(reply.getCtxId());
56             }
57         }
58     }
59
60     @Override
61     @SuppressWarnings("unchecked")
62     public void onControlPingReply($base_package.$dto_package.ControlPingReply reply) {
63         final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
64
65         synchronized(requests) {
66             completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.context);
67         }
68
69         if(completableFuture != null) {
70             // Finish dump call
71             if (completableFuture instanceof $base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) {
72                 completableFuture.complete((($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getReplyDump());
73                 // Remove future mapped to dump call context id
74                 synchronized(requests) {
75                     requests.remove((($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture) completableFuture).getContextId());
76                 }
77             } else {
78                 completableFuture.complete(reply);
79             }
80
81             synchronized(requests) {
82                 requests.remove(reply.context);
83             }
84         }
85     }
86
87 $methods
88 }
89 """)
90
91 jvpp_facade_callback_method_template = Template("""
92     @Override
93     @SuppressWarnings("unchecked")
94     public void on$callback_dto($plugin_package.$dto_package.$callback_dto reply) {
95         final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture;
96
97         synchronized(requests) {
98             completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.context);
99         }
100
101         if(completableFuture != null) {
102             completableFuture.complete(reply);
103
104             synchronized(requests) {
105                 requests.remove(reply.context);
106             }
107         }
108     }
109 """)
110
111 jvpp_facade_callback_notification_method_template = Template("""
112     @Override
113     public void on$callback_dto($plugin_package.$dto_package.$callback_dto notification) {
114         notificationCallback.on$callback_dto(notification);
115     }
116 """)
117
118 jvpp_facade_details_callback_method_template = Template("""
119     @Override
120     @SuppressWarnings("unchecked")
121     public void on$callback_dto($plugin_package.$dto_package.$callback_dto reply) {
122         final $base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.$dto_package.$callback_dto_reply_dump> completableFuture;
123
124         synchronized(requests) {
125             completableFuture = ($base_package.$future_package.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.$dto_package.$callback_dto_reply_dump>) requests.get(reply.context);
126         }
127
128         if(completableFuture != null) {
129             completableFuture.getReplyDump().$callback_dto_field.add(reply);
130         }
131     }
132 """)
133
134
135 def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, future_facade_package, inputfile):
136     """ Generates JVpp interface and JNI implementation """
137     print "Generating JVpp future facade"
138
139     if not os.path.exists(future_facade_package):
140         raise Exception("%s folder is missing" % future_facade_package)
141
142     methods = []
143     methods_impl = []
144     callbacks = []
145     for func in func_list:
146         camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
147
148         if util.is_ignored(func['name']) or util.is_control_ping(camel_case_name_with_suffix):
149             continue
150
151         if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']):
152             continue
153
154         camel_case_method_name = util.underscore_to_camelcase(func['name'])
155
156         if not util.is_notification(func["name"]):
157             camel_case_request_method_name = util.remove_reply_suffix(util.underscore_to_camelcase(func['name']))
158             if util.is_details(camel_case_name_with_suffix):
159                 camel_case_reply_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']),
160                                                                      func['name'])
161                 callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package,
162                                                                                          plugin_package=plugin_package,
163                                                                                          dto_package=dto_package,
164                                                                                          callback_dto=camel_case_name_with_suffix,
165                                                                                          callback_dto_field=camel_case_method_name,
166                                                                                          callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix,
167                                                                                          future_package=future_facade_package))
168
169                 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
170                                                                       dto_package=dto_package,
171                                                                       method_name=camel_case_request_method_name +
172                                                                                   util.underscore_to_camelcase_upper(util.dump_suffix),
173                                                                       reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
174                                                                       request_name=util.remove_reply_suffix(camel_case_reply_name) +
175                                                                                    util.underscore_to_camelcase_upper(util.dump_suffix)))
176                 methods_impl.append(future_jvpp_dump_method_impl_template.substitute(plugin_package=plugin_package,
177                                                                                      dto_package=dto_package,
178                                                                                      method_name=camel_case_request_method_name +
179                                                                                                  util.underscore_to_camelcase_upper(util.dump_suffix),
180                                                                                      reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
181                                                                                      request_name=util.remove_reply_suffix(camel_case_reply_name) +
182                                                                                                   util.underscore_to_camelcase_upper(util.dump_suffix)))
183             else:
184                 request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \
185                     if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix)
186
187                 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
188                                                                       dto_package=dto_package,
189                                                                       method_name=camel_case_request_method_name,
190                                                                       reply_name=camel_case_name_with_suffix,
191                                                                       request_name=request_name))
192                 methods_impl.append(future_jvpp_method_impl_template.substitute(plugin_package=plugin_package,
193                                                                                 dto_package=dto_package,
194                                                                                 method_name=camel_case_request_method_name,
195                                                                                 reply_name=camel_case_name_with_suffix,
196                                                                                 request_name=request_name))
197
198                 callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package,
199                                                                                  plugin_package=plugin_package,
200                                                                                  dto_package=dto_package,
201                                                                                  callback_dto=camel_case_name_with_suffix))
202
203         if util.is_notification(func["name"]):
204             callbacks.append(jvpp_facade_callback_notification_method_template.substitute(plugin_package=plugin_package,
205                                                                                           dto_package=dto_package,
206                                                                                           callback_dto=util.add_notification_suffix(camel_case_name_with_suffix)))
207
208     jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacadeCallback.java" % plugin_name), 'w')
209     jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
210                                                              base_package=base_package,
211                                                              plugin_package=plugin_package,
212                                                              plugin_name=plugin_name,
213                                                              dto_package=dto_package,
214                                                              notification_package=notification_package,
215                                                              callback_package=callback_package,
216                                                              methods="".join(callbacks),
217                                                              future_package=future_facade_package))
218     jvpp_file.flush()
219     jvpp_file.close()
220
221     jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%s.java" % plugin_name), 'w')
222     jvpp_file.write(future_jvpp_template.substitute(inputfile=inputfile,
223                                                     base_package=base_package,
224                                                     plugin_package=plugin_package,
225                                                     plugin_name=plugin_name,
226                                                     notification_package=notification_package,
227                                                     methods="".join(methods),
228                                                     future_package=future_facade_package))
229     jvpp_file.flush()
230     jvpp_file.close()
231
232     jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacade.java" % plugin_name), 'w')
233     jvpp_file.write(future_jvpp_facade_template.substitute(inputfile=inputfile,
234                                                            base_package=base_package,
235                                                            plugin_package=plugin_package,
236                                                            plugin_name=plugin_name,
237                                                            dto_package=dto_package,
238                                                            notification_package=notification_package,
239                                                            methods="".join(methods_impl),
240                                                            future_package=future_facade_package))
241     jvpp_file.flush()
242     jvpp_file.close()
243
244
245 future_jvpp_template = Template('''
246 package $plugin_package.$future_package;
247
248 /**
249  * <p>Async facade extension adding specific methods for each request invocation
250  * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
251  * <br>(python representation of api file generated by vppapigen).
252  */
253 public interface FutureJVpp${plugin_name} extends $base_package.$future_package.FutureJVppInvoker {
254 $methods
255
256     @Override
257     public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry();
258
259 }
260 ''')
261
262 future_jvpp_method_template = Template('''
263     java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request);
264 ''')
265
266
267 future_jvpp_facade_template = Template('''
268 package $plugin_package.$future_package;
269
270 /**
271  * <p>Implementation of FutureJVpp based on AbstractFutureJVppInvoker
272  * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
273  * <br>(python representation of api file generated by vppapigen).
274  */
275 public class FutureJVpp${plugin_name}Facade extends $base_package.$future_package.AbstractFutureJVppInvoker implements FutureJVpp${plugin_name} {
276
277     private final $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl notificationRegistry = new $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl();
278
279     /**
280      * <p>Create FutureJVpp${plugin_name}Facade object for provided JVpp instance.
281      * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks
282      * and then connects to provided JVpp instance
283      *
284      * @param jvpp provided $base_package.JVpp instance
285      *
286      * @throws java.io.IOException in case instance cannot connect to JVPP
287      */
288     public FutureJVpp${plugin_name}Facade(final $base_package.JVppRegistry registry, final $base_package.JVpp jvpp) throws java.io.IOException {
289         super(jvpp, registry, new java.util.HashMap<>());
290         java.util.Objects.requireNonNull(registry, "JVppRegistry should not be null");
291         registry.register(jvpp, new FutureJVpp${plugin_name}FacadeCallback(getRequests(), notificationRegistry));
292     }
293
294     @Override
295     public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry() {
296         return notificationRegistry;
297     }
298
299 $methods
300 }
301 ''')
302
303 future_jvpp_method_impl_template = Template('''
304     @Override
305     public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
306         return send(request);
307     }
308 ''')
309
310 future_jvpp_dump_method_impl_template = Template('''
311     @Override
312     public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
313         return send(request, new $plugin_package.$dto_package.$reply_name());
314     }
315 ''')
316
317
318 # Returns request name or special one from unconventional_naming_rep_req map
319 def get_standard_dump_reply_name(camel_case_dto_name, func_name):
320     # FIXME this is a hotfix for sub-details callbacks
321     # FIXME also for L2FibTableEntry
322     # It's all because unclear mapping between
323     #  request -> reply,
324     #  dump -> reply, details,
325     #  notification_start -> reply, notifications
326
327     # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code
328     suffix = func_name.split("_")[-1]
329     return util.underscore_to_camelcase_upper(
330         util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \
331         else camel_case_dto_name