blob: c02c826fc49a4bc67f9652abc85598c586b09e8a [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
34class_cache_template = Template("""
35$class_references
36static int cache_class_references(JNIEnv* env) {
37 $find_class_invocations
38 return 0;
39}""")
40
41def generate_class_cache(func_list):
42 class_references = []
43 find_class_invocations = []
44 for f in func_list:
45 c_name = f['name']
46 class_name = util.underscore_to_camelcase_upper(c_name)
47 ref_name = util.underscore_to_camelcase(c_name)
48
49 if not util.is_reply(class_name) or util.is_ignored(c_name) or util.is_notification(c_name):
50 # TODO handle notifications
51 continue
52
53 class_references.append(class_reference_template.substitute(
54 ref_name=ref_name))
55
56 find_class_invocations.append(find_class_invocation_template.substitute(
57 ref_name=ref_name,
58 class_name=class_name))
59
60 return class_cache_template.substitute(
61 class_references="".join(class_references), find_class_invocations="".join(find_class_invocations))
62
63
64# TODO: cache method and field identifiers to achieve better performance
Marek Gradzkid85036f2016-04-26 12:09:05 +020065# https://jira.fd.io/browse/HONEYCOMB-42
66request_class_template = Template("""
67 jclass requestClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${java_name_upper}");""")
68
69request_field_identifier_template = Template("""
70 jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, requestClass, "${java_name}", "${jni_signature}");
71 ${jni_type} ${java_name} = (*env)->Get${jni_getter}(env, request, ${java_name}FieldId);
72 """)
73
74u8_struct_setter_template = Template("""
75 mp->${c_name} = ${java_name};""")
76
77u16_struct_setter_template = Template("""
78 mp->${c_name} = clib_host_to_net_u16(${java_name});""")
79
80u32_struct_setter_template = Template("""
81 mp->${c_name} = clib_host_to_net_u32(${java_name});""")
82
83i32_struct_setter_template = Template("""
84 mp->${c_name} = clib_host_to_net_i32(${java_name});!""")
85
86u64_struct_setter_template = Template("""
87 mp->${c_name} = clib_host_to_net_u64(${java_name});""")
88
89u8_array_struct_setter_template = Template("""
90 {
91 jsize cnt = (*env)->GetArrayLength (env, ${java_name});
Marek Gradzkic4cb44c2016-05-24 13:32:26 +020092 size_t max_size = ${field_length};
93 if (max_size != 0 && cnt > max_size) cnt = max_size;
Marek Gradzkid85036f2016-04-26 12:09:05 +020094 (*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name});
95 }
96""")
97
98u32_array_struct_setter_template = Template("""
99 jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL);
100 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200101 size_t _i;
102 jsize cnt = (*env)->GetArrayLength (env, ${java_name});
103 size_t max_size = ${field_length};
104 if (max_size != 0 && cnt > max_size) cnt = max_size;
105 for (_i = 0; _i < cnt; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200106 mp->${c_name}[_i] = clib_host_to_net_u32(${java_name}ArrayElements[_i]);
107 }
108 }
109 (*env)->ReleaseIntArrayElements (env, ${java_name}, ${java_name}ArrayElements, 0);
110 """)
111
112vl_api_ip4_fib_counter_t_array_struct_setter_template = Template("""
113 // vl_api_ip4_fib_counter_t_array_field_setter_template FIXME""")
114
115vl_api_ip6_fib_counter_t_array_struct_setter_template = Template("""
116 // vl_api_ip6_fib_counter_t_array_field_setter_template FIXME""")
117
118struct_setter_templates = {'u8': u8_struct_setter_template,
119 'u16': u32_struct_setter_template,
120 'u32': u32_struct_setter_template,
121 'i32': u32_struct_setter_template,
122 'u64': u64_struct_setter_template,
123 'u8[]': u8_array_struct_setter_template,
124 'u32[]': u32_array_struct_setter_template,
125 'vl_api_ip4_fib_counter_t[]': vl_api_ip4_fib_counter_t_array_struct_setter_template,
126 'vl_api_ip6_fib_counter_t[]': vl_api_ip6_fib_counter_t_array_struct_setter_template
127 }
128
129jni_impl_template = Template("""
130/**
131 * JNI binding for sending ${c_name} vpe.api message.
132 * Generated based on $inputfile preparsed data:
133$api_data
134 */
135JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppImpl_${java_name}0
136(JNIEnv * env, jclass clazz$args) {
137 vppjni_main_t *jm = &vppjni_main;
138 vl_api_${c_name}_t * mp;
139 u32 my_context_id;
140 int rv;
141 rv = vppjni_sanity_check (jm);
142 if (rv) return rv;
143 my_context_id = vppjni_get_context_id (jm);
144 $request_class
145 $field_identifiers
146 M(${c_name_uppercase}, ${c_name});
147 mp->context = clib_host_to_net_u32 (my_context_id);
148 $struct_setters
149 S;
150 return my_context_id;
151}""")
152
153def generate_jni_impl(func_list, inputfile):
154 jni_impl = []
155 for f in func_list:
156 f_name = f['name']
157 camel_case_function_name = util.underscore_to_camelcase(f_name)
158 if is_manually_generated(f_name) or util.is_reply(camel_case_function_name) \
159 or util.is_ignored(f_name) or util.is_notification(f_name):
160 continue
161
162 arguments = ''
163 request_class = ''
164 field_identifiers = ''
165 struct_setters = ''
166 f_name_uppercase = f_name.upper()
167
168 if f['args']:
169 arguments = ', jobject request'
170 camel_case_function_name_upper = util.underscore_to_camelcase_upper(f_name)
171
172 request_class = request_class_template.substitute(java_name_upper=camel_case_function_name_upper)
173
174 # field identifiers
175 for t in zip(f['types'], f['args']):
176 jni_type = t[0]
177 java_field_name = util.underscore_to_camelcase(t[1])
178 jni_signature = util.jni_2_signature_mapping[jni_type]
179 jni_getter = util.jni_field_accessors[jni_type]
180 field_identifiers += request_field_identifier_template.substitute(
181 jni_type=jni_type,
182 java_name=java_field_name,
183 jni_signature=jni_signature,
184 jni_getter=jni_getter)
185
186 # field setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200187 for t in zip(f['c_types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200188 c_type = t[0]
189 c_name = t[1]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200190 field_length = t[2]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200191 java_field_name = util.underscore_to_camelcase(c_name)
192
193 struct_setter_template = struct_setter_templates[c_type]
194
195 struct_setters += struct_setter_template.substitute(
196 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200197 java_name=java_field_name,
198 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200199
200 jni_impl.append(jni_impl_template.substitute(
201 inputfile=inputfile,
202 api_data=util.api_message_to_javadoc(f),
203 java_name=camel_case_function_name,
204 c_name_uppercase=f_name_uppercase,
205 c_name=f_name,
206 request_class=request_class,
207 field_identifiers=field_identifiers,
208 struct_setters=struct_setters,
209 args=arguments))
210
211 return "\n".join(jni_impl)
212
213
214dto_field_id_template = Template("""
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200215 jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}");""")
Marek Gradzkid85036f2016-04-26 12:09:05 +0200216
217default_dto_field_setter_template = Template("""
218 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name});
219""")
220
221u32_dto_field_setter_template = Template("""
222 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name}));
223""")
224
225u64_dto_field_setter_template = Template("""
226 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name}));
227""")
228
229u8_array_dto_field_setter_template = Template("""
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200230 jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length});
231 (*env)->SetByteArrayRegion(env, ${java_name}, 0, ${field_length}, (const jbyte*)mp->${c_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200232 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
233""")
234
235# For each u64 array we get its elements. Then we convert values to host byte order.
236# All changes to jint* buffer are written to jlongArray (isCopy is set to NULL)
237u64_array_dto_field_setter_template = Template("""
Marek Gradzkid85036f2016-04-26 12:09:05 +0200238 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200239 jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200240 jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL);
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200241 unsigned int _i;
242 for (_i = 0; _i < ${field_length}; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200243 ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]);
244 }
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200245 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200246 }
Marek Gradzkid85036f2016-04-26 12:09:05 +0200247""")
248
249dto_field_setter_templates = {'u8': default_dto_field_setter_template,
250 'u16': u32_dto_field_setter_template,
251 'u32': u32_dto_field_setter_template,
252 'i32': u32_dto_field_setter_template,
253 'u64': u64_dto_field_setter_template,
254 'f64': default_dto_field_setter_template,
255 'u64[]': u64_array_dto_field_setter_template,
256 'u8[]': u8_array_dto_field_setter_template
257 }
258
259msg_handler_template = Template("""
260/**
261 * Handler for ${handler_name} vpe.api message.
262 * Generated based on $inputfile preparsed data:
263$api_data
264 */
265static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp)
266{
267 vppjni_main_t * jm = &vppjni_main;
268 JNIEnv *env = jm->jenv;
269
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200270 jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V");
Marek Gradzkid85036f2016-04-26 12:09:05 +0200271 jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V");
272
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200273 jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor);
Marek Gradzkid85036f2016-04-26 12:09:05 +0200274 $dto_setters
275 (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
276}""")
277
278def generate_msg_handlers(func_list, inputfile):
279 handlers = []
280 for f in func_list:
281 handler_name = f['name']
282 dto_name = util.underscore_to_camelcase_upper(handler_name)
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200283 ref_name = util.underscore_to_camelcase(handler_name)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200284
285 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):
286 # TODO handle notifications
287 continue
288
289 dto_setters = ''
290 # dto setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200291 for t in zip(f['c_types'], f['types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200292 c_type = t[0]
293 jni_type = t[1]
294 c_name = t[2]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200295 field_length = t[3]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200296
297 java_field_name = util.underscore_to_camelcase(c_name)
298 jni_signature = util.jni_2_signature_mapping[jni_type]
299 jni_setter = util.jni_field_accessors[jni_type]
300
301 dto_setters += dto_field_id_template.substitute(
302 java_name=java_field_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200303 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200304 jni_signature=jni_signature)
305
306 dto_setter_template = dto_field_setter_templates[c_type]
307
308 dto_setters += dto_setter_template.substitute(
309 java_name=java_field_name,
310 jni_signature=jni_signature,
311 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200312 jni_setter=jni_setter,
313 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200314
315 handlers.append(msg_handler_template.substitute(
316 inputfile=inputfile,
317 api_data=util.api_message_to_javadoc(f),
318 handler_name=handler_name,
319 dto_name=dto_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200320 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200321 dto_setters=dto_setters))
322
323 return "\n".join(handlers)
324
325
326handler_registration_template = Template("""_(${upercase_name}, ${name}) \\
327""")
328
329
330def generate_handler_registration(func_list):
331 handler_registration = ["#define foreach_vpe_api_msg \\\n"]
332 for f in func_list:
333 name = f['name']
334 camelcase_name = util.underscore_to_camelcase(f['name'])
335
336 if not util.is_reply(camelcase_name) or util.is_ignored(name) or util.is_notification(name):
337 # TODO handle notifications
338 continue
339
340 handler_registration.append(handler_registration_template.substitute(
341 name=name,
342 upercase_name=name.upper()))
343
344 return "".join(handler_registration)
345
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200346
347jvpp_c_template = Template("""/**
Marek Gradzkid85036f2016-04-26 12:09:05 +0200348 * This file contains JNI bindings for jvpp Java API.
349 * It was generated by jvpp_c_gen.py based on $inputfile
350 * (python representation of vpe.api generated by vppapigen).
351 */
352
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200353// JAVA class reference cache
354$class_cache
355
Marek Gradzkid85036f2016-04-26 12:09:05 +0200356// JNI bindings
357$jni_implementations
358
359// Message handlers
360$msg_handlers
361
362// Registration of message handlers in vlib
363$handler_registration
364""")
365
366def generate_jvpp(func_list, inputfile):
367 """ Generates jvpp C file """
368 print "Generating jvpp C"
369
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200370 class_cache = generate_class_cache(func_list)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200371 jni_impl = generate_jni_impl(func_list, inputfile)
372 msg_handlers = generate_msg_handlers(func_list, inputfile)
373 handler_registration = generate_handler_registration(func_list)
374
375 jvpp_c_file = open("jvpp_gen.h", 'w')
376 jvpp_c_file.write(jvpp_c_template.substitute(
377 inputfile=inputfile,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200378 class_cache=class_cache,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200379 jni_implementations=jni_impl,
380 msg_handlers=msg_handlers,
381 handler_registration=handler_registration))
382 jvpp_c_file.flush()
383 jvpp_c_file.close()
384
385