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 | |
| 23 | # TODO: cache class/method/field identifiers to achieve better performance |
| 24 | # https://jira.fd.io/browse/HONEYCOMB-42 |
| 25 | request_class_template = Template(""" |
| 26 | jclass requestClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${java_name_upper}");""") |
| 27 | |
| 28 | request_field_identifier_template = Template(""" |
| 29 | jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, requestClass, "${java_name}", "${jni_signature}"); |
| 30 | ${jni_type} ${java_name} = (*env)->Get${jni_getter}(env, request, ${java_name}FieldId); |
| 31 | """) |
| 32 | |
| 33 | u8_struct_setter_template = Template(""" |
| 34 | mp->${c_name} = ${java_name};""") |
| 35 | |
| 36 | u16_struct_setter_template = Template(""" |
| 37 | mp->${c_name} = clib_host_to_net_u16(${java_name});""") |
| 38 | |
| 39 | u32_struct_setter_template = Template(""" |
| 40 | mp->${c_name} = clib_host_to_net_u32(${java_name});""") |
| 41 | |
| 42 | i32_struct_setter_template = Template(""" |
| 43 | mp->${c_name} = clib_host_to_net_i32(${java_name});!""") |
| 44 | |
| 45 | u64_struct_setter_template = Template(""" |
| 46 | mp->${c_name} = clib_host_to_net_u64(${java_name});""") |
| 47 | |
| 48 | u8_array_struct_setter_template = Template(""" |
| 49 | { |
| 50 | jsize cnt = (*env)->GetArrayLength (env, ${java_name}); |
| 51 | if (cnt > sizeof(mp->${c_name})) cnt = sizeof(mp->${c_name}); |
| 52 | (*env)->GetByteArrayRegion(env, ${java_name}, 0, cnt, (jbyte *)mp->${c_name}); |
| 53 | } |
| 54 | """) |
| 55 | |
| 56 | u32_array_struct_setter_template = Template(""" |
| 57 | jint * ${java_name}ArrayElements = (*env)->GetIntArrayElements(env, ${java_name}, NULL); |
| 58 | { |
| 59 | int _i; |
| 60 | for (_i = 0; _i < 0; _i++) { |
| 61 | mp->${c_name}[_i] = clib_host_to_net_u32(${java_name}ArrayElements[_i]); |
| 62 | } |
| 63 | } |
| 64 | (*env)->ReleaseIntArrayElements (env, ${java_name}, ${java_name}ArrayElements, 0); |
| 65 | """) |
| 66 | |
| 67 | vl_api_ip4_fib_counter_t_array_struct_setter_template = Template(""" |
| 68 | // vl_api_ip4_fib_counter_t_array_field_setter_template FIXME""") |
| 69 | |
| 70 | vl_api_ip6_fib_counter_t_array_struct_setter_template = Template(""" |
| 71 | // vl_api_ip6_fib_counter_t_array_field_setter_template FIXME""") |
| 72 | |
| 73 | struct_setter_templates = {'u8': u8_struct_setter_template, |
| 74 | 'u16': u32_struct_setter_template, |
| 75 | 'u32': u32_struct_setter_template, |
| 76 | 'i32': u32_struct_setter_template, |
| 77 | 'u64': u64_struct_setter_template, |
| 78 | 'u8[]': u8_array_struct_setter_template, |
| 79 | 'u32[]': u32_array_struct_setter_template, |
| 80 | 'vl_api_ip4_fib_counter_t[]': vl_api_ip4_fib_counter_t_array_struct_setter_template, |
| 81 | 'vl_api_ip6_fib_counter_t[]': vl_api_ip6_fib_counter_t_array_struct_setter_template |
| 82 | } |
| 83 | |
| 84 | jni_impl_template = Template(""" |
| 85 | /** |
| 86 | * JNI binding for sending ${c_name} vpe.api message. |
| 87 | * Generated based on $inputfile preparsed data: |
| 88 | $api_data |
| 89 | */ |
| 90 | JNIEXPORT jint JNICALL Java_org_openvpp_jvpp_JVppImpl_${java_name}0 |
| 91 | (JNIEnv * env, jclass clazz$args) { |
| 92 | vppjni_main_t *jm = &vppjni_main; |
| 93 | vl_api_${c_name}_t * mp; |
| 94 | u32 my_context_id; |
| 95 | int rv; |
| 96 | rv = vppjni_sanity_check (jm); |
| 97 | if (rv) return rv; |
| 98 | my_context_id = vppjni_get_context_id (jm); |
| 99 | $request_class |
| 100 | $field_identifiers |
| 101 | M(${c_name_uppercase}, ${c_name}); |
| 102 | mp->context = clib_host_to_net_u32 (my_context_id); |
| 103 | $struct_setters |
| 104 | S; |
| 105 | return my_context_id; |
| 106 | }""") |
| 107 | |
| 108 | def generate_jni_impl(func_list, inputfile): |
| 109 | jni_impl = [] |
| 110 | for f in func_list: |
| 111 | f_name = f['name'] |
| 112 | camel_case_function_name = util.underscore_to_camelcase(f_name) |
| 113 | if is_manually_generated(f_name) or util.is_reply(camel_case_function_name) \ |
| 114 | or util.is_ignored(f_name) or util.is_notification(f_name): |
| 115 | continue |
| 116 | |
| 117 | arguments = '' |
| 118 | request_class = '' |
| 119 | field_identifiers = '' |
| 120 | struct_setters = '' |
| 121 | f_name_uppercase = f_name.upper() |
| 122 | |
| 123 | if f['args']: |
| 124 | arguments = ', jobject request' |
| 125 | camel_case_function_name_upper = util.underscore_to_camelcase_upper(f_name) |
| 126 | |
| 127 | request_class = request_class_template.substitute(java_name_upper=camel_case_function_name_upper) |
| 128 | |
| 129 | # field identifiers |
| 130 | for t in zip(f['types'], f['args']): |
| 131 | jni_type = t[0] |
| 132 | java_field_name = util.underscore_to_camelcase(t[1]) |
| 133 | jni_signature = util.jni_2_signature_mapping[jni_type] |
| 134 | jni_getter = util.jni_field_accessors[jni_type] |
| 135 | field_identifiers += request_field_identifier_template.substitute( |
| 136 | jni_type=jni_type, |
| 137 | java_name=java_field_name, |
| 138 | jni_signature=jni_signature, |
| 139 | jni_getter=jni_getter) |
| 140 | |
| 141 | # field setters |
| 142 | for t in zip(f['c_types'], f['args']): |
| 143 | c_type = t[0] |
| 144 | c_name = t[1] |
| 145 | java_field_name = util.underscore_to_camelcase(c_name) |
| 146 | |
| 147 | struct_setter_template = struct_setter_templates[c_type] |
| 148 | |
| 149 | struct_setters += struct_setter_template.substitute( |
| 150 | c_name=c_name, |
| 151 | java_name=java_field_name) |
| 152 | |
| 153 | jni_impl.append(jni_impl_template.substitute( |
| 154 | inputfile=inputfile, |
| 155 | api_data=util.api_message_to_javadoc(f), |
| 156 | java_name=camel_case_function_name, |
| 157 | c_name_uppercase=f_name_uppercase, |
| 158 | c_name=f_name, |
| 159 | request_class=request_class, |
| 160 | field_identifiers=field_identifiers, |
| 161 | struct_setters=struct_setters, |
| 162 | args=arguments)) |
| 163 | |
| 164 | return "\n".join(jni_impl) |
| 165 | |
| 166 | |
| 167 | dto_field_id_template = Template(""" |
| 168 | jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, dtoClass, "${java_name}", "${jni_signature}");""") |
| 169 | |
| 170 | default_dto_field_setter_template = Template(""" |
| 171 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, mp->${c_name}); |
| 172 | """) |
| 173 | |
| 174 | u32_dto_field_setter_template = Template(""" |
| 175 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u32(mp->${c_name})); |
| 176 | """) |
| 177 | |
| 178 | u64_dto_field_setter_template = Template(""" |
| 179 | (*env)->Set${jni_setter}(env, dto, ${java_name}FieldId, clib_net_to_host_u64(mp->${c_name})); |
| 180 | """) |
| 181 | |
| 182 | u8_array_dto_field_setter_template = Template(""" |
| 183 | jbyteArray ${java_name} = (*env)->NewByteArray(env, sizeof(mp->${c_name})); |
| 184 | (*env)->SetByteArrayRegion(env, ${java_name}, 0, sizeof(mp->${c_name}), (const jbyte*)mp->${c_name}); |
| 185 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
| 186 | """) |
| 187 | |
| 188 | # For each u64 array we get its elements. Then we convert values to host byte order. |
| 189 | # All changes to jint* buffer are written to jlongArray (isCopy is set to NULL) |
| 190 | u64_array_dto_field_setter_template = Template(""" |
| 191 | jlongArray ${java_name} = (*env)->NewLongArray(env, sizeof(mp->${c_name})); |
| 192 | { |
| 193 | jlong * ${java_name}ArrayElements = (*env)->GetLongArrayElements(env, ${java_name}, NULL); |
| 194 | int _i; |
| 195 | for (_i = 0; _i < 0; _i++) { |
| 196 | ${java_name}ArrayElements[_i] = clib_net_to_host_u64(mp->${c_name}[_i]); |
| 197 | } |
| 198 | } |
| 199 | (*env)->SetObjectField(env, dto, ${java_name}FieldId, ${java_name}); |
| 200 | """) |
| 201 | |
| 202 | dto_field_setter_templates = {'u8': default_dto_field_setter_template, |
| 203 | 'u16': u32_dto_field_setter_template, |
| 204 | 'u32': u32_dto_field_setter_template, |
| 205 | 'i32': u32_dto_field_setter_template, |
| 206 | 'u64': u64_dto_field_setter_template, |
| 207 | 'f64': default_dto_field_setter_template, |
| 208 | 'u64[]': u64_array_dto_field_setter_template, |
| 209 | 'u8[]': u8_array_dto_field_setter_template |
| 210 | } |
| 211 | |
| 212 | msg_handler_template = Template(""" |
| 213 | /** |
| 214 | * Handler for ${handler_name} vpe.api message. |
| 215 | * Generated based on $inputfile preparsed data: |
| 216 | $api_data |
| 217 | */ |
| 218 | static void vl_api_${handler_name}_t_handler (vl_api_${handler_name}_t * mp) |
| 219 | { |
| 220 | vppjni_main_t * jm = &vppjni_main; |
| 221 | JNIEnv *env = jm->jenv; |
| 222 | |
| 223 | jclass dtoClass = (*env)->FindClass(env, "org/openvpp/jvpp/dto/${dto_name}"); |
| 224 | |
| 225 | jmethodID constructor = (*env)->GetMethodID(env, dtoClass, "<init>", "()V"); |
| 226 | jmethodID callbackMethod = (*env)->GetMethodID(env, jm->callbackClass, "on${dto_name}", "(Lorg/openvpp/jvpp/dto/${dto_name};)V"); |
| 227 | |
| 228 | jobject dto = (*env)->NewObject(env, dtoClass, constructor); |
| 229 | $dto_setters |
| 230 | (*env)->CallVoidMethod(env, jm->callback, callbackMethod, dto); |
| 231 | }""") |
| 232 | |
| 233 | def generate_msg_handlers(func_list, inputfile): |
| 234 | handlers = [] |
| 235 | for f in func_list: |
| 236 | handler_name = f['name'] |
| 237 | dto_name = util.underscore_to_camelcase_upper(handler_name) |
| 238 | |
| 239 | 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): |
| 240 | # TODO handle notifications |
| 241 | continue |
| 242 | |
| 243 | dto_setters = '' |
| 244 | # dto setters |
| 245 | for t in zip(f['c_types'], f['types'], f['args']): |
| 246 | c_type = t[0] |
| 247 | jni_type = t[1] |
| 248 | c_name = t[2] |
| 249 | |
| 250 | java_field_name = util.underscore_to_camelcase(c_name) |
| 251 | jni_signature = util.jni_2_signature_mapping[jni_type] |
| 252 | jni_setter = util.jni_field_accessors[jni_type] |
| 253 | |
| 254 | dto_setters += dto_field_id_template.substitute( |
| 255 | java_name=java_field_name, |
| 256 | jni_signature=jni_signature) |
| 257 | |
| 258 | dto_setter_template = dto_field_setter_templates[c_type] |
| 259 | |
| 260 | dto_setters += dto_setter_template.substitute( |
| 261 | java_name=java_field_name, |
| 262 | jni_signature=jni_signature, |
| 263 | c_name=c_name, |
| 264 | jni_setter=jni_setter) |
| 265 | |
| 266 | handlers.append(msg_handler_template.substitute( |
| 267 | inputfile=inputfile, |
| 268 | api_data=util.api_message_to_javadoc(f), |
| 269 | handler_name=handler_name, |
| 270 | dto_name=dto_name, |
| 271 | dto_setters=dto_setters)) |
| 272 | |
| 273 | return "\n".join(handlers) |
| 274 | |
| 275 | |
| 276 | handler_registration_template = Template("""_(${upercase_name}, ${name}) \\ |
| 277 | """) |
| 278 | |
| 279 | |
| 280 | def generate_handler_registration(func_list): |
| 281 | handler_registration = ["#define foreach_vpe_api_msg \\\n"] |
| 282 | for f in func_list: |
| 283 | name = f['name'] |
| 284 | camelcase_name = util.underscore_to_camelcase(f['name']) |
| 285 | |
| 286 | if not util.is_reply(camelcase_name) or util.is_ignored(name) or util.is_notification(name): |
| 287 | # TODO handle notifications |
| 288 | continue |
| 289 | |
| 290 | handler_registration.append(handler_registration_template.substitute( |
| 291 | name=name, |
| 292 | upercase_name=name.upper())) |
| 293 | |
| 294 | return "".join(handler_registration) |
| 295 | |
| 296 | jvpp_c_template = Template(""" |
| 297 | /** |
| 298 | * This file contains JNI bindings for jvpp Java API. |
| 299 | * It was generated by jvpp_c_gen.py based on $inputfile |
| 300 | * (python representation of vpe.api generated by vppapigen). |
| 301 | */ |
| 302 | |
| 303 | // JNI bindings |
| 304 | $jni_implementations |
| 305 | |
| 306 | // Message handlers |
| 307 | $msg_handlers |
| 308 | |
| 309 | // Registration of message handlers in vlib |
| 310 | $handler_registration |
| 311 | """) |
| 312 | |
| 313 | def generate_jvpp(func_list, inputfile): |
| 314 | """ Generates jvpp C file """ |
| 315 | print "Generating jvpp C" |
| 316 | |
| 317 | jni_impl = generate_jni_impl(func_list, inputfile) |
| 318 | msg_handlers = generate_msg_handlers(func_list, inputfile) |
| 319 | handler_registration = generate_handler_registration(func_list) |
| 320 | |
| 321 | jvpp_c_file = open("jvpp_gen.h", 'w') |
| 322 | jvpp_c_file.write(jvpp_c_template.substitute( |
| 323 | inputfile=inputfile, |
| 324 | jni_implementations=jni_impl, |
| 325 | msg_handlers=msg_handlers, |
| 326 | handler_registration=handler_registration)) |
| 327 | jvpp_c_file.flush() |
| 328 | jvpp_c_file.close() |
| 329 | |
| 330 | |