Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 1 | #!/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 | |
| 17 | import os, util |
| 18 | from string import Template |
| 19 | |
| 20 | def is_manually_generated(f_name): |
| 21 | return f_name in {'control_ping_reply'} |
| 22 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 23 | |
| 24 | class_reference_template = Template("""jclass ${ref_name}Class; |
| 25 | """) |
| 26 | |
| 27 | find_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 | |
| 34 | class_cache_template = Template(""" |
| 35 | $class_references |
| 36 | static int cache_class_references(JNIEnv* env) { |
| 37 | $find_class_invocations |
| 38 | return 0; |
| 39 | }""") |
| 40 | |
| 41 | def 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 Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 65 | # https://jira.fd.io/browse/HONEYCOMB-42 |
| 66 | request_class_template = Template(""" |
| 67 | jclass requestClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${java_name_upper}");""") |
| 68 | |
| 69 | request_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 | |
| 74 | u8_struct_setter_template = Template(""" |
| 75 | mp->${c_name} = ${java_name};""") |
| 76 | |
| 77 | u16_struct_setter_template = Template(""" |
| 78 | mp->${c_name} = clib_host_to_net_u16(${java_name});""") |
| 79 | |
| 80 | u32_struct_setter_template = Template(""" |
| 81 | mp->${c_name} = clib_host_to_net_u32(${java_name});""") |
| 82 | |
| 83 | i32_struct_setter_template = Template(""" |
| 84 | mp->${c_name} = clib_host_to_net_i32(${java_name});!""") |
| 85 | |
| 86 | u64_struct_setter_template = Template(""" |
| 87 | mp->${c_name} = clib_host_to_net_u64(${java_name});""") |
| 88 | |
| 89 | u8_array_struct_setter_template = Template(""" |
| 90 | { |
| 91 | jsize cnt = (*env)->GetArrayLength (env, ${java_name}); |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 92 | size_t max_size = ${field_length}; |
| 93 | if (max_size != 0 && cnt > max_size) cnt = max_size; |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 94 | (*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name}); |
| 95 | } |
| 96 | """) |
| 97 | |
| 98 | u32_array_struct_setter_template = Template(""" |
| 99 | jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL); |
| 100 | { |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 101 | 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 Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 106 | 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 | |
| 112 | vl_api_ip4_fib_counter_t_array_struct_setter_template = Template(""" |
| 113 | // vl_api_ip4_fib_counter_t_array_field_setter_template FIXME""") |
| 114 | |
| 115 | vl_api_ip6_fib_counter_t_array_struct_setter_template = Template(""" |
| 116 | // vl_api_ip6_fib_counter_t_array_field_setter_template FIXME""") |
| 117 | |
| 118 | struct_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 | |
| 129 | jni_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 | */ |
| 135 | JNIEXPORT 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 | |
| 153 | def 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 Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 187 | for t in zip(f['c_types'], f['args'], f['lengths']): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 188 | c_type = t[0] |
| 189 | c_name = t[1] |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 190 | field_length = t[2] |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 191 | 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 Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 197 | java_name=java_field_name, |
| 198 | field_length=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 199 | |
| 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 | |
| 214 | dto_field_id_template = Template(""" |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 215 | jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}");""") |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 216 | |
| 217 | default_dto_field_setter_template = Template(""" |
| 218 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name}); |
| 219 | """) |
| 220 | |
| 221 | u32_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 | |
| 225 | u64_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 | |
| 229 | u8_array_dto_field_setter_template = Template(""" |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 230 | jbyteArray ${java_name} = (*env)->NewByteArray(env, ${field_length}); |
| 231 | (*env)->SetByteArrayRegion(env, ${java_name}, 0, ${field_length}, (const jbyte*)mp->${c_name}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 232 | (*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) |
| 237 | u64_array_dto_field_setter_template = Template(""" |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 238 | { |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 239 | jlongArray ${java_name} = (*env)->NewLongArray(env, ${field_length}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 240 | jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL); |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 241 | unsigned int _i; |
| 242 | for (_i = 0; _i < ${field_length}; _i++) { |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 243 | ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]); |
| 244 | } |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 245 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 246 | } |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 247 | """) |
| 248 | |
| 249 | dto_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 | |
| 259 | msg_handler_template = Template(""" |
| 260 | /** |
| 261 | * Handler for ${handler_name} vpe.api message. |
| 262 | * Generated based on $inputfile preparsed data: |
| 263 | $api_data |
| 264 | */ |
| 265 | static 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 Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 270 | jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V"); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 271 | jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V"); |
| 272 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 273 | jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor); |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 274 | $dto_setters |
| 275 | (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto); |
| 276 | }""") |
| 277 | |
| 278 | def 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 Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 283 | ref_name = util.underscore_to_camelcase(handler_name) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 284 | |
| 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 Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 291 | for t in zip(f['c_types'], f['types'], f['args'], f['lengths']): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 292 | c_type = t[0] |
| 293 | jni_type = t[1] |
| 294 | c_name = t[2] |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 295 | field_length = t[3] |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 296 | |
| 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 Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 303 | class_ref_name=ref_name, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 304 | 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 Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame^] | 312 | jni_setter=jni_setter, |
| 313 | field_length=field_length) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 314 | |
| 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 Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 320 | class_ref_name=ref_name, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 321 | dto_setters=dto_setters)) |
| 322 | |
| 323 | return "\n".join(handlers) |
| 324 | |
| 325 | |
| 326 | handler_registration_template = Template("""_(${upercase_name}, ${name}) \\ |
| 327 | """) |
| 328 | |
| 329 | |
| 330 | def 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 Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 346 | |
| 347 | jvpp_c_template = Template("""/** |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 348 | * 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 Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 353 | // JAVA class reference cache |
| 354 | $class_cache |
| 355 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 356 | // 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 | |
| 366 | def generate_jvpp(func_list, inputfile): |
| 367 | """ Generates jvpp C file """ |
| 368 | print "Generating jvpp C" |
| 369 | |
Marek Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 370 | class_cache = generate_class_cache(func_list) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 371 | 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 Gradzki | 0aaf92f | 2016-05-03 17:05:27 +0200 | [diff] [blame] | 378 | class_cache=class_cache, |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 379 | 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 | |