blob: c006c34103ba9d9b9a0622b5976d4a0ab1f91092 [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
Maros Marsalek7becd082016-05-31 17:45:16 +020056 if util.is_ignored(c_name):
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020057 continue
58
Maros Marsalek7becd082016-05-31 17:45:16 +020059 if util.is_reply(class_name):
60 class_references.append(class_reference_template.substitute(
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020061 ref_name=ref_name))
Maros Marsalek7becd082016-05-31 17:45:16 +020062 find_class_invocations.append(find_class_invocation_template.substitute(
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020063 ref_name=ref_name,
64 class_name=class_name))
Maros Marsalek7becd082016-05-31 17:45:16 +020065 elif util.is_notification(c_name):
66 class_references.append(class_reference_template.substitute(
67 ref_name=util.add_notification_suffix(ref_name)))
68 find_class_invocations.append(find_class_invocation_template.substitute(
69 ref_name=util.add_notification_suffix(ref_name),
70 class_name=util.add_notification_suffix(class_name)))
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020071
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020072 # add exception class to class cache
73 ref_name = 'callbackException'
74 class_name = 'org/openvpp/jvpp/VppCallbackException'
75 class_references.append(class_reference_template.substitute(
76 ref_name=ref_name))
77 find_class_invocations.append(find_class_template.substitute(
78 ref_name=ref_name,
79 class_name=class_name))
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020080 return class_cache_template.substitute(
Maros Marsalek7becd082016-05-31 17:45:16 +020081 class_references="".join(class_references), find_class_invocations="".join(find_class_invocations))
Marek Gradzki0aaf92f2016-05-03 17:05:27 +020082
83
84# TODO: cache method and field identifiers to achieve better performance
Marek Gradzkid85036f2016-04-26 12:09:05 +020085# https://jira.fd.io/browse/HONEYCOMB-42
86request_class_template = Template("""
87 jclass requestClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${java_name_upper}");""")
88
89request_field_identifier_template = Template("""
90 jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, requestClass, "${java_name}", "${jni_signature}");
91 ${jni_type} ${java_name} = (*env)->Get${jni_getter}(env, request, ${java_name}FieldId);
92 """)
93
94u8_struct_setter_template = Template("""
95 mp->${c_name} = ${java_name};""")
96
97u16_struct_setter_template = Template("""
98 mp->${c_name} = clib_host_to_net_u16(${java_name});""")
99
100u32_struct_setter_template = Template("""
101 mp->${c_name} = clib_host_to_net_u32(${java_name});""")
102
103i32_struct_setter_template = Template("""
104 mp->${c_name} = clib_host_to_net_i32(${java_name});!""")
105
106u64_struct_setter_template = Template("""
107 mp->${c_name} = clib_host_to_net_u64(${java_name});""")
108
109u8_array_struct_setter_template = Template("""
110 {
111 jsize cnt = (*env)->GetArrayLength (env, ${java_name});
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200112 size_t max_size = ${field_length};
113 if (max_size != 0 && cnt > max_size) cnt = max_size;
Marek Gradzkid85036f2016-04-26 12:09:05 +0200114 (*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name});
115 }
116""")
117
118u32_array_struct_setter_template = Template("""
119 jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL);
120 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200121 size_t _i;
122 jsize cnt = (*env)->GetArrayLength (env, ${java_name});
123 size_t max_size = ${field_length};
124 if (max_size != 0 && cnt > max_size) cnt = max_size;
125 for (_i = 0; _i < cnt; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200126 mp->${c_name}[_i] = clib_host_to_net_u32(${java_name}ArrayElements[_i]);
127 }
128 }
129 (*env)->ReleaseIntArrayElements (env, ${java_name}, ${java_name}ArrayElements, 0);
130 """)
131
132vl_api_ip4_fib_counter_t_array_struct_setter_template = Template("""
133 // vl_api_ip4_fib_counter_t_array_field_setter_template FIXME""")
134
135vl_api_ip6_fib_counter_t_array_struct_setter_template = Template("""
136 // vl_api_ip6_fib_counter_t_array_field_setter_template FIXME""")
137
138struct_setter_templates = {'u8': u8_struct_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200139 'u16': u16_struct_setter_template,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200140 'u32': u32_struct_setter_template,
141 'i32': u32_struct_setter_template,
142 'u64': u64_struct_setter_template,
143 'u8[]': u8_array_struct_setter_template,
144 'u32[]': u32_array_struct_setter_template,
145 'vl_api_ip4_fib_counter_t[]': vl_api_ip4_fib_counter_t_array_struct_setter_template,
146 'vl_api_ip6_fib_counter_t[]': vl_api_ip6_fib_counter_t_array_struct_setter_template
147 }
148
149jni_impl_template = Template("""
150/**
151 * JNI binding for sending ${c_name} vpe.api message.
152 * Generated based on $inputfile preparsed data:
153$api_data
154 */
155JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppImpl_${java_name}0
156(JNIEnv * env, jclass clazz$args) {
157 vppjni_main_t *jm = &vppjni_main;
158 vl_api_${c_name}_t * mp;
159 u32 my_context_id;
160 int rv;
161 rv = vppjni_sanity_check (jm);
162 if (rv) return rv;
163 my_context_id = vppjni_get_context_id (jm);
164 $request_class
165 $field_identifiers
166 M(${c_name_uppercase}, ${c_name});
167 mp->context = clib_host_to_net_u32 (my_context_id);
168 $struct_setters
169 S;
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200170 if ((*env)->ExceptionCheck(env)) {
171 return JNI_ERR;
172 }
Marek Gradzkid85036f2016-04-26 12:09:05 +0200173 return my_context_id;
174}""")
175
176def generate_jni_impl(func_list, inputfile):
177 jni_impl = []
178 for f in func_list:
179 f_name = f['name']
180 camel_case_function_name = util.underscore_to_camelcase(f_name)
181 if is_manually_generated(f_name) or util.is_reply(camel_case_function_name) \
Maros Marsalek7becd082016-05-31 17:45:16 +0200182 or util.is_ignored(f_name) or util.is_just_notification(f_name):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200183 continue
184
185 arguments = ''
186 request_class = ''
187 field_identifiers = ''
188 struct_setters = ''
189 f_name_uppercase = f_name.upper()
190
191 if f['args']:
192 arguments = ', jobject request'
193 camel_case_function_name_upper = util.underscore_to_camelcase_upper(f_name)
194
195 request_class = request_class_template.substitute(java_name_upper=camel_case_function_name_upper)
196
197 # field identifiers
198 for t in zip(f['types'], f['args']):
199 jni_type = t[0]
200 java_field_name = util.underscore_to_camelcase(t[1])
201 jni_signature = util.jni_2_signature_mapping[jni_type]
202 jni_getter = util.jni_field_accessors[jni_type]
203 field_identifiers += request_field_identifier_template.substitute(
204 jni_type=jni_type,
205 java_name=java_field_name,
206 jni_signature=jni_signature,
207 jni_getter=jni_getter)
208
209 # field setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200210 for t in zip(f['c_types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200211 c_type = t[0]
212 c_name = t[1]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200213 field_length = t[2]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200214 java_field_name = util.underscore_to_camelcase(c_name)
215
216 struct_setter_template = struct_setter_templates[c_type]
217
218 struct_setters += struct_setter_template.substitute(
219 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200220 java_name=java_field_name,
221 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200222
223 jni_impl.append(jni_impl_template.substitute(
224 inputfile=inputfile,
225 api_data=util.api_message_to_javadoc(f),
226 java_name=camel_case_function_name,
227 c_name_uppercase=f_name_uppercase,
228 c_name=f_name,
229 request_class=request_class,
230 field_identifiers=field_identifiers,
231 struct_setters=struct_setters,
232 args=arguments))
233
234 return "\n".join(jni_impl)
235
236
237dto_field_id_template = Template("""
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200238 jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}");""")
Marek Gradzkid85036f2016-04-26 12:09:05 +0200239
240default_dto_field_setter_template = Template("""
241 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name});
242""")
243
Marek Gradzki34e77722016-05-30 21:52:37 +0200244u16_dto_field_setter_template = Template("""
245 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u16(mp->${c_name}));
246""")
247
Marek Gradzkid85036f2016-04-26 12:09:05 +0200248u32_dto_field_setter_template = Template("""
249 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name}));
250""")
251
252u64_dto_field_setter_template = Template("""
253 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name}));
254""")
255
256u8_array_dto_field_setter_template = Template("""
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200257 jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length});
258 (*env)->SetByteArrayRegion(env, ${java_name}, 0, ${field_length}, (const jbyte*)mp->${c_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200259 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
260""")
261
Marek Gradzki97d99512016-06-06 12:36:09 +0200262u32_array_dto_field_setter_template = Template("""
263 {
264 jintArray ${java_name} = (*env)->NewIntArray(env, ${field_length});
265 jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL);
266 unsigned int _i;
267 for (_i = 0; _i < ${field_length}; _i++) {
268 ${java_name}ArrayElements[_i] = clib_net_to_host_u32(mp->${c_name}[_i]);
269 }
270 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
271 }
272""")
273
Marek Gradzkid85036f2016-04-26 12:09:05 +0200274# For each u64 array we get its elements. Then we convert values to host byte order.
Marek Gradzki97d99512016-06-06 12:36:09 +0200275# All changes to jlong* buffer are written to jlongArray (isCopy is set to NULL)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200276u64_array_dto_field_setter_template = Template("""
Marek Gradzkid85036f2016-04-26 12:09:05 +0200277 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200278 jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200279 jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL);
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200280 unsigned int _i;
281 for (_i = 0; _i < ${field_length}; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200282 ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]);
283 }
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200284 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200285 }
Marek Gradzkid85036f2016-04-26 12:09:05 +0200286""")
287
288dto_field_setter_templates = {'u8': default_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200289 'u16': u16_dto_field_setter_template,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200290 'u32': u32_dto_field_setter_template,
291 'i32': u32_dto_field_setter_template,
292 'u64': u64_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200293 'f64': default_dto_field_setter_template, #fixme
Marek Gradzki97d99512016-06-06 12:36:09 +0200294 'u8[]': u8_array_dto_field_setter_template,
295 'u32[]': u32_array_dto_field_setter_template,
296 'u64[]': u64_array_dto_field_setter_template
Marek Gradzkid85036f2016-04-26 12:09:05 +0200297 }
298
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200299# code fragment for checking result of the operation before sending request reply
300callback_err_handler_template = Template("""
301 // for negative result don't send callback message but send error callback
302 if (mp->retval<0) {
303 CallOnError("${handler_name}",mp->context,mp->retval);
304 return;
305 }
306 if (mp->retval == VNET_API_ERROR_IN_PROGRESS) {
307 clib_warning("Result in progress");
308 return;
309 }
310""")
311
Marek Gradzkid85036f2016-04-26 12:09:05 +0200312msg_handler_template = Template("""
313/**
314 * Handler for ${handler_name} vpe.api message.
315 * Generated based on $inputfile preparsed data:
316$api_data
317 */
318static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp)
319{
320 vppjni_main_t * jm = &vppjni_main;
321 JNIEnv *env = jm->jenv;
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200322 $err_handler
Marek Gradzkid85036f2016-04-26 12:09:05 +0200323
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200324 jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V");
Marek Gradzkid85036f2016-04-26 12:09:05 +0200325 jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V");
326
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200327 jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor);
Marek Gradzkid85036f2016-04-26 12:09:05 +0200328 $dto_setters
329 (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
330}""")
331
332def generate_msg_handlers(func_list, inputfile):
333 handlers = []
334 for f in func_list:
335 handler_name = f['name']
336 dto_name = util.underscore_to_camelcase_upper(handler_name)
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200337 ref_name = util.underscore_to_camelcase(handler_name)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200338
Maros Marsalek7becd082016-05-31 17:45:16 +0200339 if is_manually_generated(handler_name) or util.is_ignored(handler_name):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200340 continue
341
Maros Marsalek7becd082016-05-31 17:45:16 +0200342 if not util.is_reply(dto_name) and not util.is_notification(handler_name):
343 continue
344
345 if util.is_notification(handler_name):
346 dto_name = util.add_notification_suffix(dto_name)
347 ref_name = util.add_notification_suffix(ref_name)
348
Marek Gradzkid85036f2016-04-26 12:09:05 +0200349 dto_setters = ''
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200350 err_handler = ''
Marek Gradzkid85036f2016-04-26 12:09:05 +0200351 # dto setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200352 for t in zip(f['c_types'], f['types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200353 c_type = t[0]
354 jni_type = t[1]
355 c_name = t[2]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200356 field_length = t[3]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200357
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200358 # for retval don't generate setters and generate retval check
359 if util.is_retval_field(c_name):
360 err_handler = callback_err_handler_template.substitute(
361 handler_name=handler_name
362 )
363 continue
364
Marek Gradzkid85036f2016-04-26 12:09:05 +0200365 java_field_name = util.underscore_to_camelcase(c_name)
366 jni_signature = util.jni_2_signature_mapping[jni_type]
367 jni_setter = util.jni_field_accessors[jni_type]
368
369 dto_setters += dto_field_id_template.substitute(
370 java_name=java_field_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200371 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200372 jni_signature=jni_signature)
373
374 dto_setter_template = dto_field_setter_templates[c_type]
375
376 dto_setters += dto_setter_template.substitute(
377 java_name=java_field_name,
378 jni_signature=jni_signature,
379 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200380 jni_setter=jni_setter,
381 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200382
383 handlers.append(msg_handler_template.substitute(
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200384 inputfile=inputfile,
385 api_data=util.api_message_to_javadoc(f),
386 handler_name=handler_name,
387 dto_name=dto_name,
388 class_ref_name=ref_name,
389 dto_setters=dto_setters,
390 err_handler=err_handler))
Marek Gradzkid85036f2016-04-26 12:09:05 +0200391
392 return "\n".join(handlers)
393
394
395handler_registration_template = Template("""_(${upercase_name}, ${name}) \\
396""")
397
398
399def generate_handler_registration(func_list):
400 handler_registration = ["#define foreach_vpe_api_msg \\\n"]
401 for f in func_list:
402 name = f['name']
403 camelcase_name = util.underscore_to_camelcase(f['name'])
404
Maros Marsalek7becd082016-05-31 17:45:16 +0200405 if (not util.is_reply(camelcase_name) and not util.is_notification(name)) or util.is_ignored(name):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200406 continue
407
408 handler_registration.append(handler_registration_template.substitute(
Maros Marsalek7becd082016-05-31 17:45:16 +0200409 name=name,
410 upercase_name=name.upper()))
Marek Gradzkid85036f2016-04-26 12:09:05 +0200411
412 return "".join(handler_registration)
413
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200414
415jvpp_c_template = Template("""/**
Marek Gradzkid85036f2016-04-26 12:09:05 +0200416 * This file contains JNI bindings for jvpp Java API.
417 * It was generated by jvpp_c_gen.py based on $inputfile
418 * (python representation of vpe.api generated by vppapigen).
419 */
420
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200421void CallOnError(const char* call, int context, int retval);
422
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200423// JAVA class reference cache
424$class_cache
425
Marek Gradzkid85036f2016-04-26 12:09:05 +0200426// JNI bindings
427$jni_implementations
428
429// Message handlers
430$msg_handlers
431
432// Registration of message handlers in vlib
433$handler_registration
434""")
435
436def generate_jvpp(func_list, inputfile):
437 """ Generates jvpp C file """
438 print "Generating jvpp C"
439
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200440 class_cache = generate_class_cache(func_list)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200441 jni_impl = generate_jni_impl(func_list, inputfile)
442 msg_handlers = generate_msg_handlers(func_list, inputfile)
443 handler_registration = generate_handler_registration(func_list)
444
445 jvpp_c_file = open("jvpp_gen.h", 'w')
446 jvpp_c_file.write(jvpp_c_template.substitute(
447 inputfile=inputfile,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200448 class_cache=class_cache,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200449 jni_implementations=jni_impl,
450 msg_handlers=msg_handlers,
451 handler_registration=handler_registration))
452 jvpp_c_file.flush()
453 jvpp_c_file.close()
454