Repair Doxygen build infrastructure
[vpp.git] / vpp-api / java / jvpp / gen / jvppgen / jvpp_callback_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, util
17 from string import Template
18
19 import callback_gen
20 import dto_gen
21
22 jvpp_ifc_template = Template("""
23 package $plugin_package.$callback_facade_package;
24
25 /**
26  * <p>Callback Java API representation of $plugin_package plugin.
27  * <br>It was generated by jvpp_callback_facade_gen.py based on $inputfile
28  * <br>(python representation of api file generated by vppapigen).
29  */
30 public interface CallbackJVpp${plugin_name} extends $base_package.$notification_package.NotificationRegistryProvider, java.lang.AutoCloseable {
31
32     // TODO add send
33
34 $methods
35 }
36 """)
37
38 jvpp_impl_template = Template("""
39 package $plugin_package.$callback_facade_package;
40
41 /**
42  * <p>Default implementation of Callback${plugin_name}JVpp interface.
43  * <br>It was generated by jvpp_callback_facade_gen.py based on $inputfile
44  * <br>(python representation of api file generated by vppapigen).
45  */
46 public final class CallbackJVpp${plugin_name}Facade implements CallbackJVpp${plugin_name} {
47
48     private final $plugin_package.JVpp${plugin_name} jvpp;
49     private final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> callbacks;
50     private final $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl notificationRegistry = new $plugin_package.$notification_package.${plugin_name}NotificationRegistryImpl();
51     /**
52      * <p>Create CallbackJVpp${plugin_name}Facade object for provided JVpp instance.
53      * Constructor internally creates CallbackJVppFacadeCallback class for processing callbacks
54      * and then connects to provided JVpp instance
55      *
56      * @param jvpp provided $base_package.JVpp instance
57      *
58      * @throws java.io.IOException in case instance cannot connect to JVPP
59      */
60     public CallbackJVpp${plugin_name}Facade(final $base_package.JVppRegistry registry, final $plugin_package.JVpp${plugin_name} jvpp) throws java.io.IOException {
61         this.jvpp = java.util.Objects.requireNonNull(jvpp,"jvpp is null");
62         this.callbacks = new java.util.HashMap<>();
63         java.util.Objects.requireNonNull(registry, "JVppRegistry should not be null");
64         registry.register(jvpp, new CallbackJVpp${plugin_name}FacadeCallback(this.callbacks, notificationRegistry));
65     }
66
67     @Override
68     public $plugin_package.$notification_package.${plugin_name}NotificationRegistry getNotificationRegistry() {
69         return notificationRegistry;
70     }
71
72     @Override
73     public void close() throws Exception {
74         jvpp.close();
75     }
76
77     // TODO add send()
78
79 $methods
80 }
81 """)
82
83 method_template = Template(
84     """    void $name($plugin_package.$dto_package.$request request, $plugin_package.$callback_package.$callback callback) throws $base_package.VppInvocationException;""")
85
86 method_impl_template = Template("""    public final void $name($plugin_package.$dto_package.$request request, $plugin_package.$callback_package.$callback callback) throws $base_package.VppInvocationException {
87         synchronized (callbacks) {
88             callbacks.put(jvpp.$name(request), callback);
89         }
90     }
91 """)
92
93 no_arg_method_template = Template("""    void $name($plugin_package.$callback_package.$callback callback) throws $base_package.VppInvocationException;""")
94 no_arg_method_impl_template = Template("""    public final void $name($plugin_package.$callback_package.$callback callback) throws $base_package.VppInvocationException {
95         synchronized (callbacks) {
96             callbacks.put(jvpp.$name(), callback);
97         }
98     }
99 """)
100
101
102 def generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, callback_facade_package, inputfile):
103     """ Generates callback facade """
104     print "Generating JVpp callback facade"
105
106     if os.path.exists(callback_facade_package):
107         util.remove_folder(callback_facade_package)
108
109     os.mkdir(callback_facade_package)
110
111     methods = []
112     methods_impl = []
113     for func in func_list:
114
115         if util.is_notification(func['name']) or util.is_ignored(func['name']):
116             continue
117
118         camel_case_name = util.underscore_to_camelcase(func['name'])
119         camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
120         if util.is_reply(camel_case_name) or util.is_control_ping(camel_case_name):
121             continue
122
123         # Strip suffix for dump calls
124         callback_type = get_request_name(camel_case_name_upper, func['name']) + callback_gen.callback_suffix
125
126         if len(func['args']) == 0:
127             methods.append(no_arg_method_template.substitute(name=camel_case_name,
128                                                              base_package=base_package,
129                                                              plugin_package=plugin_package,
130                                                              dto_package=dto_package,
131                                                              callback_package=callback_package,
132                                                              callback=callback_type))
133             methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
134                                                                        base_package=base_package,
135                                                                        plugin_package=plugin_package,
136                                                                        dto_package=dto_package,
137                                                                        callback_package=callback_package,
138                                                                        callback=callback_type))
139         else:
140             methods.append(method_template.substitute(name=camel_case_name,
141                                                       request=camel_case_name_upper,
142                                                       base_package=base_package,
143                                                       plugin_package=plugin_package,
144                                                       dto_package=dto_package,
145                                                       callback_package=callback_package,
146                                                       callback=callback_type))
147             methods_impl.append(method_impl_template.substitute(name=camel_case_name,
148                                                                 request=camel_case_name_upper,
149                                                                 base_package=base_package,
150                                                                 plugin_package=plugin_package,
151                                                                 dto_package=dto_package,
152                                                                 callback_package=callback_package,
153                                                                 callback=callback_type))
154
155     join = os.path.join(callback_facade_package, "CallbackJVpp%s.java" % plugin_name)
156     jvpp_file = open(join, 'w')
157     jvpp_file.write(
158         jvpp_ifc_template.substitute(inputfile=inputfile,
159                                      methods="\n".join(methods),
160                                      base_package=base_package,
161                                      plugin_package=plugin_package,
162                                      plugin_name=plugin_name,
163                                      dto_package=dto_package,
164                                      notification_package=notification_package,
165                                      callback_facade_package=callback_facade_package))
166     jvpp_file.flush()
167     jvpp_file.close()
168
169     jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVpp%sFacade.java" % plugin_name), 'w')
170     jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
171                                                   methods="\n".join(methods_impl),
172                                                   base_package=base_package,
173                                                   plugin_package=plugin_package,
174                                                   plugin_name=plugin_name,
175                                                   dto_package=dto_package,
176                                                   notification_package=notification_package,
177                                                   callback_package=callback_package,
178                                                   callback_facade_package=callback_facade_package))
179     jvpp_file.flush()
180     jvpp_file.close()
181
182     generate_callback(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, callback_facade_package, inputfile)
183
184
185 jvpp_facade_callback_template = Template("""
186 package $plugin_package.$callback_facade_package;
187
188 /**
189  * <p>Implementation of JVppGlobalCallback interface for Java Callback API.
190  * <br>It was generated by jvpp_callback_facade_gen.py based on $inputfile
191  * <br>(python representation of api file generated by vppapigen).
192  */
193 public final class CallbackJVpp${plugin_name}FacadeCallback implements $plugin_package.$callback_package.JVpp${plugin_name}GlobalCallback {
194
195     private final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requests;
196     private final $plugin_package.$notification_package.Global${plugin_name}NotificationCallback notificationCallback;
197     private static final java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(CallbackJVpp${plugin_name}FacadeCallback.class.getName());
198
199     public CallbackJVpp${plugin_name}FacadeCallback(final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requestMap,
200                                       final $plugin_package.$notification_package.Global${plugin_name}NotificationCallback notificationCallback) {
201         this.requests = requestMap;
202         this.notificationCallback = notificationCallback;
203     }
204
205     @Override
206     public void onError($base_package.VppCallbackException reply) {
207
208         $base_package.$callback_package.JVppCallback failedCall;
209         synchronized(requests) {
210             failedCall = requests.remove(reply.getCtxId());
211         }
212
213         if(failedCall != null) {
214             try {
215                 failedCall.onError(reply);
216             } catch(RuntimeException ex) {
217                 ex.addSuppressed(reply);
218                 LOG.log(java.util.logging.Level.WARNING, String.format("Callback: %s failed while handling exception: %s", failedCall, reply), ex);
219             }
220         }
221     }
222
223     @Override
224     @SuppressWarnings("unchecked")
225     public void onControlPingReply($base_package.$dto_package.ControlPingReply reply) {
226
227         $base_package.$callback_package.ControlPingCallback callback;
228         synchronized(requests) {
229             callback = ($base_package.$callback_package.ControlPingCallback) requests.remove(reply.context);
230         }
231
232         if(callback != null) {
233             callback.onControlPingReply(reply);
234         }
235     }
236
237 $methods
238 }
239 """)
240
241 jvpp_facade_callback_method_template = Template("""
242     @Override
243     @SuppressWarnings("unchecked")
244     public void on$callback_dto($plugin_package.$dto_package.$callback_dto reply) {
245
246         $plugin_package.$callback_package.$callback callback;
247         synchronized(requests) {
248             callback = ($plugin_package.$callback_package.$callback) requests.remove(reply.context);
249         }
250
251         if(callback != null) {
252             callback.on$callback_dto(reply);
253         }
254     }
255 """)
256
257 jvpp_facade_callback_notification_method_template = Template("""
258     @Override
259     @SuppressWarnings("unchecked")
260     public void on$callback_dto($plugin_package.$dto_package.$callback_dto notification) {
261         notificationCallback.on$callback_dto(notification);
262     }
263 """)
264
265
266 def generate_callback(func_list, base_package, plugin_package, plugin_name, dto_package, callback_package, notification_package, callback_facade_package, inputfile):
267     callbacks = []
268     for func in func_list:
269
270         camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
271
272         if util.is_ignored(func['name']) or util.is_control_ping(camel_case_name_with_suffix):
273             continue
274
275         if util.is_reply(camel_case_name_with_suffix):
276             callbacks.append(jvpp_facade_callback_method_template.substitute(plugin_package=plugin_package,
277                                                                              dto_package=dto_package,
278                                                                              callback_package=callback_package,
279                                                                              callback=util.remove_reply_suffix(camel_case_name_with_suffix) + callback_gen.callback_suffix,
280                                                                              callback_dto=camel_case_name_with_suffix))
281
282         if util.is_notification(func["name"]):
283             with_notification_suffix = util.add_notification_suffix(camel_case_name_with_suffix)
284             callbacks.append(jvpp_facade_callback_notification_method_template.substitute(plugin_package=plugin_package,
285                                                                              dto_package=dto_package,
286                                                                              callback_package=callback_package,
287                                                                              callback=with_notification_suffix + callback_gen.callback_suffix,
288                                                                              callback_dto=with_notification_suffix))
289
290     jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVpp%sFacadeCallback.java" % plugin_name), 'w')
291     jvpp_file.write(jvpp_facade_callback_template.substitute(inputfile=inputfile,
292                                                              base_package=base_package,
293                                                              plugin_package=plugin_package,
294                                                              plugin_name=plugin_name,
295                                                              dto_package=dto_package,
296                                                              notification_package=notification_package,
297                                                              callback_package=callback_package,
298                                                              methods="".join(callbacks),
299                                                              callback_facade_package=callback_facade_package))
300     jvpp_file.flush()
301     jvpp_file.close()
302
303
304 # Returns request name or special one from unconventional_naming_rep_req map
305 def get_request_name(camel_case_dto_name, func_name):
306     if func_name in reverse_dict(util.unconventional_naming_rep_req):
307         request_name = util.underscore_to_camelcase_upper(reverse_dict(util.unconventional_naming_rep_req)[func_name])
308     else:
309         request_name = camel_case_dto_name
310     return remove_suffix(request_name)
311
312
313 def reverse_dict(map):
314     return dict((v, k) for k, v in map.iteritems())
315
316
317 def remove_suffix(name):
318     if util.is_reply(name):
319         return util.remove_reply_suffix(name)
320     else:
321         if util.is_dump(name):
322             return util.remove_suffix(name, util.dump_suffix)
323         else:
324             return name