blob: a92dd69c853472e486f747670931d0b1fce076de [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,
Marek Gradzki34e77722016-05-30 21:52:37 +0200119 'u16': u16_struct_setter_template,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200120 '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
Marek Gradzki34e77722016-05-30 21:52:37 +0200221u16_dto_field_setter_template = Template("""
222 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u16(mp->${c_name}));
223""")
224
Marek Gradzkid85036f2016-04-26 12:09:05 +0200225u32_dto_field_setter_template = Template("""
226 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name}));
227""")
228
229u64_dto_field_setter_template = Template("""
230 (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name}));
231""")
232
233u8_array_dto_field_setter_template = Template("""
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200234 jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length});
235 (*env)->SetByteArrayRegion(env, ${java_name}, 0, ${field_length}, (const jbyte*)mp->${c_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200236 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
237""")
238
Marek Gradzki97d99512016-06-06 12:36:09 +0200239u32_array_dto_field_setter_template = Template("""
240 {
241 jintArray ${java_name} = (*env)->NewIntArray(env, ${field_length});
242 jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL);
243 unsigned int _i;
244 for (_i = 0; _i < ${field_length}; _i++) {
245 ${java_name}ArrayElements[_i] = clib_net_to_host_u32(mp->${c_name}[_i]);
246 }
247 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
248 }
249""")
250
Marek Gradzkid85036f2016-04-26 12:09:05 +0200251# For each u64 array we get its elements. Then we convert values to host byte order.
Marek Gradzki97d99512016-06-06 12:36:09 +0200252# All changes to jlong* buffer are written to jlongArray (isCopy is set to NULL)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200253u64_array_dto_field_setter_template = Template("""
Marek Gradzkid85036f2016-04-26 12:09:05 +0200254 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200255 jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200256 jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL);
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200257 unsigned int _i;
258 for (_i = 0; _i < ${field_length}; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200259 ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]);
260 }
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200261 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200262 }
Marek Gradzkid85036f2016-04-26 12:09:05 +0200263""")
264
265dto_field_setter_templates = {'u8': default_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200266 'u16': u16_dto_field_setter_template,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200267 'u32': u32_dto_field_setter_template,
268 'i32': u32_dto_field_setter_template,
269 'u64': u64_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200270 'f64': default_dto_field_setter_template, #fixme
Marek Gradzki97d99512016-06-06 12:36:09 +0200271 'u8[]': u8_array_dto_field_setter_template,
272 'u32[]': u32_array_dto_field_setter_template,
273 'u64[]': u64_array_dto_field_setter_template
Marek Gradzkid85036f2016-04-26 12:09:05 +0200274 }
275
276msg_handler_template = Template("""
277/**
278 * Handler for ${handler_name} vpe.api message.
279 * Generated based on $inputfile preparsed data:
280$api_data
281 */
282static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp)
283{
284 vppjni_main_t * jm = &vppjni_main;
285 JNIEnv *env = jm->jenv;
286
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200287 jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V");
Marek Gradzkid85036f2016-04-26 12:09:05 +0200288 jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V");
289
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200290 jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor);
Marek Gradzkid85036f2016-04-26 12:09:05 +0200291 $dto_setters
292 (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
293}""")
294
295def generate_msg_handlers(func_list, inputfile):
296 handlers = []
297 for f in func_list:
298 handler_name = f['name']
299 dto_name = util.underscore_to_camelcase_upper(handler_name)
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200300 ref_name = util.underscore_to_camelcase(handler_name)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200301
302 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):
303 # TODO handle notifications
304 continue
305
306 dto_setters = ''
307 # dto setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200308 for t in zip(f['c_types'], f['types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200309 c_type = t[0]
310 jni_type = t[1]
311 c_name = t[2]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200312 field_length = t[3]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200313
314 java_field_name = util.underscore_to_camelcase(c_name)
315 jni_signature = util.jni_2_signature_mapping[jni_type]
316 jni_setter = util.jni_field_accessors[jni_type]
317
318 dto_setters += dto_field_id_template.substitute(
319 java_name=java_field_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200320 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200321 jni_signature=jni_signature)
322
323 dto_setter_template = dto_field_setter_templates[c_type]
324
325 dto_setters += dto_setter_template.substitute(
326 java_name=java_field_name,
327 jni_signature=jni_signature,
328 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200329 jni_setter=jni_setter,
330 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200331
332 handlers.append(msg_handler_template.substitute(
333 inputfile=inputfile,
334 api_data=util.api_message_to_javadoc(f),
335 handler_name=handler_name,
336 dto_name=dto_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200337 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200338 dto_setters=dto_setters))
339
340 return "\n".join(handlers)
341
342
343handler_registration_template = Template("""_(${upercase_name}, ${name}) \\
344""")
345
346
347def generate_handler_registration(func_list):
348 handler_registration = ["#define foreach_vpe_api_msg \\\n"]
349 for f in func_list:
350 name = f['name']
351 camelcase_name = util.underscore_to_camelcase(f['name'])
352
353 if not util.is_reply(camelcase_name) or util.is_ignored(name) or util.is_notification(name):
354 # TODO handle notifications
355 continue
356
357 handler_registration.append(handler_registration_template.substitute(
358 name=name,
359 upercase_name=name.upper()))
360
361 return "".join(handler_registration)
362
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200363
364jvpp_c_template = Template("""/**
Marek Gradzkid85036f2016-04-26 12:09:05 +0200365 * This file contains JNI bindings for jvpp Java API.
366 * It was generated by jvpp_c_gen.py based on $inputfile
367 * (python representation of vpe.api generated by vppapigen).
368 */
369
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200370// JAVA class reference cache
371$class_cache
372
Marek Gradzkid85036f2016-04-26 12:09:05 +0200373// JNI bindings
374$jni_implementations
375
376// Message handlers
377$msg_handlers
378
379// Registration of message handlers in vlib
380$handler_registration
381""")
382
383def generate_jvpp(func_list, inputfile):
384 """ Generates jvpp C file """
385 print "Generating jvpp C"
386
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200387 class_cache = generate_class_cache(func_list)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200388 jni_impl = generate_jni_impl(func_list, inputfile)
389 msg_handlers = generate_msg_handlers(func_list, inputfile)
390 handler_registration = generate_handler_registration(func_list)
391
392 jvpp_c_file = open("jvpp_gen.h", 'w')
393 jvpp_c_file.write(jvpp_c_template.substitute(
394 inputfile=inputfile,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200395 class_cache=class_cache,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200396 jni_implementations=jni_impl,
397 msg_handlers=msg_handlers,
398 handler_registration=handler_registration))
399 jvpp_c_file.flush()
400 jvpp_c_file.close()
401
402