VPP-205: jvpp plugin support.
[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             $plugin_package.$dto_package.$callback_dto_reply_dump replyDump = completableFuture.getReplyDump();
130             if(replyDump == null) {
131                 replyDump = new $plugin_package.$dto_package.$callback_dto_reply_dump();
132                 completableFuture.setReplyDump(replyDump);
133             }
134
135             replyDump.$callback_dto_field.add(reply);
136         }
137     }
138 """)
139
140
141 def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, future_facade_package, inputfile):
142     """ Generates JVpp interface and JNI implementation """
143     print "Generating JVpp future facade"
144
145     if not os.path.exists(future_facade_package):
146         raise Exception("%s folder is missing" % future_facade_package)
147
148     methods = []
149     methods_impl = []
150     callbacks = []
151     for func in func_list:
152         camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
153
154         if util.is_ignored(func['name']) or util.is_control_ping(camel_case_name_with_suffix):
155             continue
156
157         if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']):
158             continue
159
160         camel_case_method_name = util.underscore_to_camelcase(func['name'])
161
162         if not util.is_notification(func["name"]):
163             camel_case_request_method_name = util.remove_reply_suffix(util.underscore_to_camelcase(func['name']))
164             if util.is_details(camel_case_name_with_suffix):
165                 camel_case_reply_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']),
166                                                                      func['name'])
167                 callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package,
168                                                                                          plugin_package=plugin_package,
169                                                                                          dto_package=dto_package,
170                                                                                          callback_dto=camel_case_name_with_suffix,
171                                                                                          callback_dto_field=camel_case_method_name,
172                                                                                          callback_dto_reply_dump=camel_case_reply_name + dto_gen.dump_dto_suffix,
173                                                                                          future_package=future_facade_package))
174
175                 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
176                                                                       dto_package=dto_package,
177                                                                       method_name=camel_case_request_method_name +
178                                                                                   util.underscore_to_camelcase_upper(util.dump_suffix),
179                                                                       reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
180                                                                       request_name=util.remove_reply_suffix(camel_case_reply_name) +
181                                                                                    util.underscore_to_camelcase_upper(util.dump_suffix)))
182                 methods_impl.append(future_jvpp_method_impl_template.substitute(plugin_package=plugin_package,
183                                                                                 dto_package=dto_package,
184                                                                                 method_name=camel_case_request_method_name +
185                                                                                             util.underscore_to_camelcase_upper(util.dump_suffix),
186                                                                                 reply_name=camel_case_reply_name + dto_gen.dump_dto_suffix,
187                                                                                 request_name=util.remove_reply_suffix(camel_case_reply_name) +
188                                                                                              util.underscore_to_camelcase_upper(util.dump_suffix)))
189             else:
190                 request_name = util.underscore_to_camelcase_upper(util.unconventional_naming_rep_req[func['name']]) \
191                     if func['name'] in util.unconventional_naming_rep_req else util.remove_reply_suffix(camel_case_name_with_suffix)
192
193                 methods.append(future_jvpp_method_template.substitute(plugin_package=plugin_package,
194                                                                       dto_package=dto_package,
195                                                                       method_name=camel_case_request_method_name,
196                                                                       reply_name=camel_case_name_with_suffix,
197                                                                       request_name=request_name))
198                 methods_impl.append(future_jvpp_method_impl_template.substitute(plugin_package=plugin_package,
199                                                                                 dto_package=dto_package,
200                                                                                 method_name=camel_case_request_method_name,
201                                                                                 reply_name=camel_case_name_with_suffix,
202                                                                                 request_name=request_name))
203
204                 callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package,
205                                                                                  plugin_package=plugin_package,
206                                                                                  dto_package=dto_package,
207                                                                                  callback_dto=camel_case_name_with_suffix))
208
209         if util.is_notification(func["name"]):
210             callbacks.append(jvpp_facade_callback_notification_method_template.substitute(plugin_package=plugin_package,
211                                                                                           dto_package=dto_package,
212                                                                                           callback_dto=util.add_notification_suffix(camel_case_name_with_suffix)))
213
214     jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacadeCallback.java" % plugin_name), 'w')
215     jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
216                                                              base_package=base_package,
217                                                              plugin_package=plugin_package,
218                                                              plugin_name=plugin_name,
219                                                              dto_package=dto_package,
220                                                              notification_package=notification_package,
221                                                              callback_package=callback_package,
222                                                              methods="".join(callbacks),
223                                                              future_package=future_facade_package))
224     jvpp_file.flush()
225     jvpp_file.close()
226
227     jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%s.java" % plugin_name), 'w')
228     jvpp_file.write(future_jvpp_template.substitute(inputfile=inputfile,
229                                                     base_package=base_package,
230                                                     plugin_package=plugin_package,
231                                                     plugin_name=plugin_name,
232                                                     notification_package=notification_package,
233                                                     methods="".join(methods),
234                                                     future_package=future_facade_package))
235     jvpp_file.flush()
236     jvpp_file.close()
237
238     jvpp_file = open(os.path.join(future_facade_package, "FutureJVpp%sFacade.java" % plugin_name), 'w')
239     jvpp_file.write(future_jvpp_facade_template.substitute(inputfile=inputfile,
240                                                            base_package=base_package,
241                                                            plugin_package=plugin_package,
242                                                            plugin_name=plugin_name,
243                                                            dto_package=dto_package,
244                                                            notification_package=notification_package,
245                                                            methods="".join(methods_impl),
246                                                            future_package=future_facade_package))
247     jvpp_file.flush()
248     jvpp_file.close()
249
250
251 future_jvpp_template = Template('''
252 package $plugin_package.$future_package;
253
254 /**
255  * <p>Async facade extension adding specific methods for each request invocation
256  * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
257  * <br>(python representation of api file generated by vppapigen).
258  */
259 public interface FutureJVpp${plugin_name} extends $base_package.$future_package.FutureJVppInvoker {
260 $methods
261
262     @Override
263     public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry();
264
265 }
266 ''')
267
268 future_jvpp_method_template = Template('''
269     java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request);
270 ''')
271
272
273 future_jvpp_facade_template = Template('''
274 package $plugin_package.$future_package;
275
276 /**
277  * <p>Implementation of FutureJVpp based on AbstractFutureJVppInvoker
278  * <br>It was generated by jvpp_future_facade_gen.py based on $inputfile
279  * <br>(python representation of api file generated by vppapigen).
280  */
281 public class FutureJVpp${plugin_name}Facade extends $base_package.$future_package.AbstractFutureJVppInvoker implements FutureJVpp${plugin_name} {
282
283     private final $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl notificationRegistry = new $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl();
284
285     /**
286      * <p>Create FutureJVpp${plugin_name}Facade object for provided JVpp instance.
287      * Constructor internally creates FutureJVppFacadeCallback class for processing callbacks
288      * and then connects to provided JVpp instance
289      *
290      * @param jvpp provided $base_package.JVpp instance
291      *
292      * @throws java.io.IOException in case instance cannot connect to JVPP
293      */
294     public FutureJVpp${plugin_name}Facade(final $base_package.JVppRegistry registry, final $base_package.JVpp jvpp) throws java.io.IOException {
295         super(jvpp, registry, new java.util.HashMap<>());
296         java.util.Objects.requireNonNull(registry, "JVppRegistry should not be null");
297         registry.register(jvpp, new FutureJVpp${plugin_name}FacadeCallback(getRequests(), notificationRegistry));
298     }
299
300     @Override
301     public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry() {
302         return notificationRegistry;
303     }
304
305 $methods
306 }
307 ''')
308
309 future_jvpp_method_impl_template = Template('''
310     @Override
311     public java.util.concurrent.CompletionStage<$plugin_package.$dto_package.$reply_name> $method_name($plugin_package.$dto_package.$request_name request) {
312         return send(request);
313     }
314 ''')
315
316 # Returns request name or special one from unconventional_naming_rep_req map
317 def get_standard_dump_reply_name(camel_case_dto_name, func_name):
318     # FIXME this is a hotfix for sub-details callbacks
319     # FIXME also for L2FibTableEntry
320     # It's all because unclear mapping between
321     #  request -> reply,
322     #  dump -> reply, details,
323     #  notification_start -> reply, notifications
324
325     # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code
326     suffix = func_name.split("_")[-1]
327     return util.underscore_to_camelcase_upper(
328         util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \
329         else camel_case_dto_name