blob: 082fd5d73aea4a2f0c44f2ccc7d5430a6ff4ab71 [file] [log] [blame]
Marek Gradzkid85036f2016-04-26 12:09:05 +02001#!/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# l
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
17import os, util
18from string import Template
19
20def is_manually_generated(f_name):
21 return f_name in {'control_ping_reply'}
22
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020023
24class_reference_template = Template("""jclass ${ref_name}Class;
25""")
26
27find_class_invocation_template = Template("""
28 ${ref_name}Class = (jclass)(*env)->NewGlobalRef(env, (*env)->FindClass(env, "org/openvpp/jvpp/dto/${class_name}"));
29 if ((*env)->ExceptionCheck(env)) {
30 (*env)->ExceptionDescribe(env);
31 return JNI_ERR;
32 }""")
33
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020034find_class_template = Template("""
35 ${ref_name}Class = (jclass)(*env)->NewGlobalRef(env, (*env)->FindClass(env, "${class_name}"));
36 if ((*env)->ExceptionCheck(env)) {
37 (*env)->ExceptionDescribe(env);
38 return JNI_ERR;
39 }""")
40
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020041class_cache_template = Template("""
42$class_references
43static int cache_class_references(JNIEnv* env) {
44 $find_class_invocations
45 return 0;
46}""")
47
48def generate_class_cache(func_list):
49 class_references = []
50 find_class_invocations = []
51 for f in func_list:
52 c_name = f['name']
53 class_name = util.underscore_to_camelcase_upper(c_name)
54 ref_name = util.underscore_to_camelcase(c_name)
55
56 if not util.is_reply(class_name) or util.is_ignored(c_name) or util.is_notification(c_name):
57 # TODO handle notifications
58 continue
59
60 class_references.append(class_reference_template.substitute(
61 ref_name=ref_name))
62
63 find_class_invocations.append(find_class_invocation_template.substitute(
64 ref_name=ref_name,
65 class_name=class_name))
66
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020067 # add exception class to class cache
68 ref_name = 'callbackException'
69 class_name = 'org/openvpp/jvpp/VppCallbackException'
70 class_references.append(class_reference_template.substitute(
71 ref_name=ref_name))
72 find_class_invocations.append(find_class_template.substitute(
73 ref_name=ref_name,
74 class_name=class_name))
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020075 return class_cache_template.substitute(
76 class_references="".join(class_references), find_class_invocations="".join(find_class_invocations))
77
78
79# TODO: cache method and field identifiers to achieve better performance
Marek Gradzkid85036f2016-04-26 12:09:05 +020080# https://jira.fd.io/browse/HONEYCOMB-42
81request_class_template = Template("""
82 jclass requestClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${java_name_upper}");""")
83
84request_field_identifier_template = Template("""
85 jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, requestClass, "${java_name}", "${jni_signature}");
86 ${jni_type} ${java_name} = (*env)->Get${jni_getter}(env, request, ${java_name}FieldId);
87 """)
88
89u8_struct_setter_template = Template("""
90 mp->${c_name} = ${java_name};""")
91
92u16_struct_setter_template = Template("""
93 mp->${c_name} = clib_host_to_net_u16(${java_name});""")
94
95u32_struct_setter_template = Template("""
96 mp->${c_name} = clib_host_to_net_u32(${java_name});""")
97
98i32_struct_setter_template = Template("""
99 mp->${c_name} = clib_host_to_net_i32(${java_name});!""")
100
101u64_struct_setter_template = Template("""
102 mp->${c_name} = clib_host_to_net_u64(${java_name});""")
103
104u8_array_struct_setter_template = Template("""
105 {
106 jsize cnt = (*env)->GetArrayLength (env, ${java_name});
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200107 size_t max_size = ${field_length};
108 if (max_size != 0 && cnt > max_size) cnt = max_size;
Marek Gradzkid85036f2016-04-26 12:09:05 +0200109 (*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name});
110 }
111""")
112
113u32_array_struct_setter_template = Template("""
114 jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL);
115 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200116 size_t _i;
117 jsize cnt = (*env)->GetArrayLength (env, ${java_name});
118 size_t max_size = ${field_length};
119 if (max_size != 0 && cnt > max_size) cnt = max_size;
120 for (_i = 0; _i < cnt; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200121 mp->${c_name}[_i] = clib_host_to_net_u32(${java_name}ArrayElements[_i]);
122 }
123 }
124 (*env)->ReleaseIntArrayElements (env, ${java_name}, ${java_name}ArrayElements, 0);
125 """)
126
127vl_api_ip4_fib_counter_t_array_struct_setter_template = Template("""
128 // vl_api_ip4_fib_counter_t_array_field_setter_template FIXME""")
129
130vl_api_ip6_fib_counter_t_array_struct_setter_template = Template("""
131 // vl_api_ip6_fib_counter_t_array_field_setter_template FIXME""")
132
133struct_setter_templates = {'u8': u8_struct_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200134 'u16': u16_struct_setter_template,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200135 'u32': u32_struct_setter_template,
136 'i32': u32_struct_setter_template,
137 'u64': u64_struct_setter_template,
138 'u8[]': u8_array_struct_setter_template,
139 'u32[]': u32_array_struct_setter_template,
140 'vl_api_ip4_fib_counter_t[]': vl_api_ip4_fib_counter_t_array_struct_setter_template,
141 'vl_api_ip6_fib_counter_t[]': vl_api_ip6_fib_counter_t_array_struct_setter_template
142 }
143
144jni_impl_template = Template("""
145/**
146 * JNI binding for sending ${c_name} vpe.api message.
147 * Generated based on $inputfile preparsed data:
148$api_data
149 */
150JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppImpl_${java_name}0
151(JNIEnv * env, jclass clazz$args) {
152 vppjni_main_t *jm = &vppjni_main;
153 vl_api_${c_name}_t * mp;
154 u32 my_context_id;
155 int rv;
156 rv = vppjni_sanity_check (jm);
157 if (rv) return rv;
158 my_context_id = vppjni_get_context_id (jm);
159 $request_class
160 $field_identifiers
161 M(${c_name_uppercase}, ${c_name});
162 mp->context = clib_host_to_net_u32 (my_context_id);
163 $struct_setters
164 S;
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200165 if ((*env)->ExceptionCheck(env)) {
166 return JNI_ERR;
167 }
Marek Gradzkid85036f2016-04-26 12:09:05 +0200168 return my_context_id;
169}""")
170
171def generate_jni_impl(func_list, inputfile):
172 jni_impl = []
173 for f in func_list:
174 f_name = f['name']
175 camel_case_function_name = util.underscore_to_camelcase(f_name)
176 if is_manually_generated(f_name) or util.is_reply(camel_case_function_name) \
177 or util.is_ignored(f_name) or util.is_notification(f_name):
178 continue
179
180 arguments = ''
181 request_class = ''
182 field_identifiers = ''
183 struct_setters = ''
184 f_name_uppercase = f_name.upper()
185
186 if f['args']:
187 arguments = ', jobject request'
188 camel_case_function_name_upper = util.underscore_to_camelcase_upper(f_name)
189
190 request_class = request_class_template.substitute(java_name_upper=camel_case_function_name_upper)
191
192 # field identifiers
193 for t in zip(f['types'], f['args']):
194 jni_type = t[0]
195 java_field_name = util.underscore_to_camelcase(t[1])
196 jni_signature = util.jni_2_signature_mapping[jni_type]
197 jni_getter = util.jni_field_accessors[jni_type]
198 field_identifiers += request_field_identifier_template.substitute(
199 jni_type=jni_type,
200 java_name=java_field_name,
201 jni_signature=jni_signature,
202 jni_getter=jni_getter)
203
204 # field setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200205 for t in zip(f['c_types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200206 c_type = t[0]
207 c_name = t[1]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200208 field_length = t[2]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200209 java_field_name = util.underscore_to_camelcase(c_name)
210
211 struct_setter_template = struct_setter_templates[c_type]
212
213 struct_setters += struct_setter_template.substitute(
214 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200215 java_name=java_field_name,
216 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200217
218 jni_impl.append(jni_impl_template.substitute(
219 inputfile=inputfile,
220 api_data=util.api_message_to_javadoc(f),
221 java_name=camel_case_function_name,
222 c_name_uppercase=f_name_uppercase,
223 c_name=f_name,
224 request_class=request_class,
225 field_identifiers=field_identifiers,
226 struct_setters=struct_setters,
227 args=arguments))
228
229 return "\n".join(jni_impl)
230
231
232dto_field_id_template = Template("""
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200233 jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}");""")
Marek Gradzkid85036f2016-04-26 12:09:05 +0200234
235default_dto_field_setter_template = Template("""
236 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name});
237""")
238
Marek Gradzki34e77722016-05-30 21:52:37 +0200239u16_dto_field_setter_template = Template("""
240 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u16(mp->${c_name}));
241""")
242
Marek Gradzkid85036f2016-04-26 12:09:05 +0200243u32_dto_field_setter_template = Template("""
244 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name}));
245""")
246
247u64_dto_field_setter_template = Template("""
248 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name}));
249""")
250
251u8_array_dto_field_setter_template = Template("""
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200252 jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length});
253 (*env)->SetByteArrayRegion(env, ${java_name}, 0, ${field_length}, (const jbyte*)mp->${c_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200254 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
255""")
256
Marek Gradzki97d99512016-06-06 12:36:09 +0200257u32_array_dto_field_setter_template = Template("""
258 {
259 jintArray ${java_name} = (*env)->NewIntArray(env, ${field_length});
260 jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL);
261 unsigned int _i;
262 for (_i = 0; _i < ${field_length}; _i++) {
263 ${java_name}ArrayElements[_i] = clib_net_to_host_u32(mp->${c_name}[_i]);
264 }
265 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
266 }
267""")
268
Marek Gradzkid85036f2016-04-26 12:09:05 +0200269# For each u64 array we get its elements. Then we convert values to host byte order.
Marek Gradzki97d99512016-06-06 12:36:09 +0200270# All changes to jlong* buffer are written to jlongArray (isCopy is set to NULL)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200271u64_array_dto_field_setter_template = Template("""
Marek Gradzkid85036f2016-04-26 12:09:05 +0200272 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200273 jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200274 jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL);
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200275 unsigned int _i;
276 for (_i = 0; _i < ${field_length}; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200277 ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]);
278 }
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200279 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200280 }
Marek Gradzkid85036f2016-04-26 12:09:05 +0200281""")
282
283dto_field_setter_templates = {'u8': default_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200284 'u16': u16_dto_field_setter_template,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200285 'u32': u32_dto_field_setter_template,
286 'i32': u32_dto_field_setter_template,
287 'u64': u64_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200288 'f64': default_dto_field_setter_template, #fixme
Marek Gradzki97d99512016-06-06 12:36:09 +0200289 'u8[]': u8_array_dto_field_setter_template,
290 'u32[]': u32_array_dto_field_setter_template,
291 'u64[]': u64_array_dto_field_setter_template
Marek Gradzkid85036f2016-04-26 12:09:05 +0200292 }
293
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200294# code fragment for checking result of the operation before sending request reply
295callback_err_handler_template = Template("""
296 // for negative result don't send callback message but send error callback
297 if (mp->retval<0) {
298 CallOnError("${handler_name}",mp->context,mp->retval);
299 return;
300 }
301 if (mp->retval == VNET_API_ERROR_IN_PROGRESS) {
302 clib_warning("Result in progress");
303 return;
304 }
305""")
306
Marek Gradzkid85036f2016-04-26 12:09:05 +0200307msg_handler_template = Template("""
308/**
309 * Handler for ${handler_name} vpe.api message.
310 * Generated based on $inputfile preparsed data:
311$api_data
312 */
313static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp)
314{
315 vppjni_main_t * jm = &vppjni_main;
316 JNIEnv *env = jm->jenv;
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200317 $err_handler
Marek Gradzkid85036f2016-04-26 12:09:05 +0200318
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200319 jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V");
Marek Gradzkid85036f2016-04-26 12:09:05 +0200320 jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V");
321
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200322 jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor);
Marek Gradzkid85036f2016-04-26 12:09:05 +0200323 $dto_setters
324 (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
325}""")
326
327def generate_msg_handlers(func_list, inputfile):
328 handlers = []
329 for f in func_list:
330 handler_name = f['name']
331 dto_name = util.underscore_to_camelcase_upper(handler_name)
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200332 ref_name = util.underscore_to_camelcase(handler_name)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200333
334 if is_manually_generated(handler_name) or not util.is_reply(dto_name) or util.is_ignored(handler_name) or util.is_notification(handler_name):
335 # TODO handle notifications
336 continue
337
338 dto_setters = ''
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200339 err_handler = ''
Marek Gradzkid85036f2016-04-26 12:09:05 +0200340 # dto setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200341 for t in zip(f['c_types'], f['types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200342 c_type = t[0]
343 jni_type = t[1]
344 c_name = t[2]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200345 field_length = t[3]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200346
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200347 # for retval don't generate setters and generate retval check
348 if util.is_retval_field(c_name):
349 err_handler = callback_err_handler_template.substitute(
350 handler_name=handler_name
351 )
352 continue
353
Marek Gradzkid85036f2016-04-26 12:09:05 +0200354 java_field_name = util.underscore_to_camelcase(c_name)
355 jni_signature = util.jni_2_signature_mapping[jni_type]
356 jni_setter = util.jni_field_accessors[jni_type]
357
358 dto_setters += dto_field_id_template.substitute(
359 java_name=java_field_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200360 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200361 jni_signature=jni_signature)
362
363 dto_setter_template = dto_field_setter_templates[c_type]
364
365 dto_setters += dto_setter_template.substitute(
366 java_name=java_field_name,
367 jni_signature=jni_signature,
368 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200369 jni_setter=jni_setter,
370 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200371
372 handlers.append(msg_handler_template.substitute(
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200373 inputfile=inputfile,
374 api_data=util.api_message_to_javadoc(f),
375 handler_name=handler_name,
376 dto_name=dto_name,
377 class_ref_name=ref_name,
378 dto_setters=dto_setters,
379 err_handler=err_handler))
Marek Gradzkid85036f2016-04-26 12:09:05 +0200380
381 return "\n".join(handlers)
382
383
384handler_registration_template = Template("""_(${upercase_name}, ${name}) \\
385""")
386
387
388def generate_handler_registration(func_list):
389 handler_registration = ["#define foreach_vpe_api_msg \\\n"]
390 for f in func_list:
391 name = f['name']
392 camelcase_name = util.underscore_to_camelcase(f['name'])
393
394 if not util.is_reply(camelcase_name) or util.is_ignored(name) or util.is_notification(name):
395 # TODO handle notifications
396 continue
397
398 handler_registration.append(handler_registration_template.substitute(
399 name=name,
400 upercase_name=name.upper()))
401
402 return "".join(handler_registration)
403
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200404
405jvpp_c_template = Template("""/**
Marek Gradzkid85036f2016-04-26 12:09:05 +0200406 * This file contains JNI bindings for jvpp Java API.
407 * It was generated by jvpp_c_gen.py based on $inputfile
408 * (python representation of vpe.api generated by vppapigen).
409 */
410
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200411void CallOnError(const char* call, int context, int retval);
412
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200413// JAVA class reference cache
414$class_cache
415
Marek Gradzkid85036f2016-04-26 12:09:05 +0200416// JNI bindings
417$jni_implementations
418
419// Message handlers
420$msg_handlers
421
422// Registration of message handlers in vlib
423$handler_registration
424""")
425
426def generate_jvpp(func_list, inputfile):
427 """ Generates jvpp C file """
428 print "Generating jvpp C"
429
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200430 class_cache = generate_class_cache(func_list)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200431 jni_impl = generate_jni_impl(func_list, inputfile)
432 msg_handlers = generate_msg_handlers(func_list, inputfile)
433 handler_registration = generate_handler_registration(func_list)
434
435 jvpp_c_file = open("jvpp_gen.h", 'w')
436 jvpp_c_file.write(jvpp_c_template.substitute(
437 inputfile=inputfile,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200438 class_cache=class_cache,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200439 jni_implementations=jni_impl,
440 msg_handlers=msg_handlers,
441 handler_registration=handler_registration))
442 jvpp_c_file.flush()
443 jvpp_c_file.close()
444