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