blob: 476a768d9a0563e1e7fd171edea5d29fce5e90f1 [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
239# For each u64 array we get its elements. Then we convert values to host byte order.
240# All changes to jint* buffer are written to jlongArray (isCopy is set to NULL)
241u64_array_dto_field_setter_template = Template("""
Marek Gradzkid85036f2016-04-26 12:09:05 +0200242 {
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200243 jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200244 jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL);
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200245 unsigned int _i;
246 for (_i = 0; _i < ${field_length}; _i++) {
Marek Gradzkid85036f2016-04-26 12:09:05 +0200247 ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]);
248 }
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200249 (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name});
Marek Gradzkid85036f2016-04-26 12:09:05 +0200250 }
Marek Gradzkid85036f2016-04-26 12:09:05 +0200251""")
252
253dto_field_setter_templates = {'u8': default_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200254 'u16': u16_dto_field_setter_template,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200255 'u32': u32_dto_field_setter_template,
256 'i32': u32_dto_field_setter_template,
257 'u64': u64_dto_field_setter_template,
Marek Gradzki34e77722016-05-30 21:52:37 +0200258 'f64': default_dto_field_setter_template, #fixme
Marek Gradzkid85036f2016-04-26 12:09:05 +0200259 'u64[]': u64_array_dto_field_setter_template,
260 'u8[]': u8_array_dto_field_setter_template
261 }
262
263msg_handler_template = Template("""
264/**
265 * Handler for ${handler_name} vpe.api message.
266 * Generated based on $inputfile preparsed data:
267$api_data
268 */
269static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp)
270{
271 vppjni_main_t * jm = &vppjni_main;
272 JNIEnv *env = jm->jenv;
273
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200274 jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V");
Marek Gradzkid85036f2016-04-26 12:09:05 +0200275 jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V");
276
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200277 jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor);
Marek Gradzkid85036f2016-04-26 12:09:05 +0200278 $dto_setters
279 (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto);
280}""")
281
282def generate_msg_handlers(func_list, inputfile):
283 handlers = []
284 for f in func_list:
285 handler_name = f['name']
286 dto_name = util.underscore_to_camelcase_upper(handler_name)
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200287 ref_name = util.underscore_to_camelcase(handler_name)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200288
289 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):
290 # TODO handle notifications
291 continue
292
293 dto_setters = ''
294 # dto setters
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200295 for t in zip(f['c_types'], f['types'], f['args'], f['lengths']):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200296 c_type = t[0]
297 jni_type = t[1]
298 c_name = t[2]
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200299 field_length = t[3]
Marek Gradzkid85036f2016-04-26 12:09:05 +0200300
301 java_field_name = util.underscore_to_camelcase(c_name)
302 jni_signature = util.jni_2_signature_mapping[jni_type]
303 jni_setter = util.jni_field_accessors[jni_type]
304
305 dto_setters += dto_field_id_template.substitute(
306 java_name=java_field_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200307 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200308 jni_signature=jni_signature)
309
310 dto_setter_template = dto_field_setter_templates[c_type]
311
312 dto_setters += dto_setter_template.substitute(
313 java_name=java_field_name,
314 jni_signature=jni_signature,
315 c_name=c_name,
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200316 jni_setter=jni_setter,
317 field_length=field_length)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200318
319 handlers.append(msg_handler_template.substitute(
320 inputfile=inputfile,
321 api_data=util.api_message_to_javadoc(f),
322 handler_name=handler_name,
323 dto_name=dto_name,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200324 class_ref_name=ref_name,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200325 dto_setters=dto_setters))
326
327 return "\n".join(handlers)
328
329
330handler_registration_template = Template("""_(${upercase_name}, ${name}) \\
331""")
332
333
334def generate_handler_registration(func_list):
335 handler_registration = ["#define foreach_vpe_api_msg \\\n"]
336 for f in func_list:
337 name = f['name']
338 camelcase_name = util.underscore_to_camelcase(f['name'])
339
340 if not util.is_reply(camelcase_name) or util.is_ignored(name) or util.is_notification(name):
341 # TODO handle notifications
342 continue
343
344 handler_registration.append(handler_registration_template.substitute(
345 name=name,
346 upercase_name=name.upper()))
347
348 return "".join(handler_registration)
349
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200350
351jvpp_c_template = Template("""/**
Marek Gradzkid85036f2016-04-26 12:09:05 +0200352 * This file contains JNI bindings for jvpp Java API.
353 * It was generated by jvpp_c_gen.py based on $inputfile
354 * (python representation of vpe.api generated by vppapigen).
355 */
356
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200357// JAVA class reference cache
358$class_cache
359
Marek Gradzkid85036f2016-04-26 12:09:05 +0200360// JNI bindings
361$jni_implementations
362
363// Message handlers
364$msg_handlers
365
366// Registration of message handlers in vlib
367$handler_registration
368""")
369
370def generate_jvpp(func_list, inputfile):
371 """ Generates jvpp C file """
372 print "Generating jvpp C"
373
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200374 class_cache = generate_class_cache(func_list)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200375 jni_impl = generate_jni_impl(func_list, inputfile)
376 msg_handlers = generate_msg_handlers(func_list, inputfile)
377 handler_registration = generate_handler_registration(func_list)
378
379 jvpp_c_file = open("jvpp_gen.h", 'w')
380 jvpp_c_file.write(jvpp_c_template.substitute(
381 inputfile=inputfile,
Marek Gradzki0aaf92f2016-05-03 17:05:27 +0200382 class_cache=class_cache,
Marek Gradzkid85036f2016-04-26 12:09:05 +0200383 jni_implementations=jni_impl,
384 msg_handlers=msg_handlers,
385 handler_registration=handler_registration))
386 jvpp_c_file.flush()
387 jvpp_c_file.close()
388
389