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