3 # Copyright (c) 2018 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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from string import Template
18 from jni_common_gen import generate_c2j_swap
19 from jvpp_model import is_dump, is_request, is_control_ping, is_control_ping_reply, is_retval
22 def generate_jni_handlers(model):
24 Generates msg handlers for all messages except for dumps and requests (handled by vpp, not client).
25 :param model: meta-model of VPP API used for jVPP generation.
28 for msg in model.messages:
30 if is_control_ping(msg) or is_control_ping_reply(msg):
31 # Skip control ping managed by jvpp registry.
33 if is_dump(msg) or is_request(msg):
36 jni_impl.append(_MSG_HANDLER_TEMPLATE.substitute(
38 json_filename=model.json_api_files,
39 json_definition=msg.doc,
40 plugin_name=model.plugin_name,
41 err_handler=_generate_error_handler(msg),
42 class_ref_name=msg.java_name_lower,
43 dto_name=msg.java_name_upper,
44 dto_setters=generate_c2j_swap(msg, object_ref_name="dto", struct_ref_name="mp")
46 return "".join(jni_impl)
48 _MSG_HANDLER_TEMPLATE = Template("""
50 * Handler for ${c_name} message.
51 * Generated based on $json_filename:
54 static void vl_api_${c_name}_t_handler (vl_api_${c_name}_t * mp)
56 ${plugin_name}_main_t *plugin_main = &${plugin_name}_main;
57 JNIEnv *env = jvpp_main.jenv;
62 clib_warning ("Received ${c_name} event message");
64 jmethodID constructor = (*env)->GetMethodID(env, ${class_ref_name}Class, "<init>", "()V");
66 // User does not have to provide callbacks for all VPP messages.
67 // We are ignoring messages that are not supported by user.
68 (*env)->ExceptionClear(env); // just in case exception occurred in different place and was not properly cleared
69 jmethodID callbackMethod = (*env)->GetMethodID(env, plugin_main->callbackClass, "on${dto_name}", "(Lio/fd/vpp/jvpp/${plugin_name}/dto/${dto_name};)V");
70 exc = (*env)->ExceptionOccurred(env);
72 clib_warning("Unable to extract on${dto_name} method reference from ${plugin_name} plugin's callbackClass. Ignoring message.\\n");
73 (*env)->ExceptionDescribe(env);
74 (*env)->ExceptionClear(env);
78 jobject dto = (*env)->NewObject(env, ${class_ref_name}Class, constructor);
81 (*env)->CallVoidMethod(env, plugin_main->callbackObject, callbackMethod, dto);
82 // free DTO as per http://stackoverflow.com/questions/1340938/memory-leak-when-calling-java-code-from-c-using-jni
83 (*env)->DeleteLocalRef(env, dto);
87 def _generate_error_handler(msg):
89 for field in msg.fields:
91 err_handler = _ERR_HANDLER_TEMPLATE.substitute(name=msg.name)
94 # Code fragment for checking result of the operation before sending request reply.
95 # Error checking is optional (some messages, e.g. detail messages do not have retval field).
96 _ERR_HANDLER_TEMPLATE = Template("""
97 // for negative result don't send callback message but send error callback
99 call_on_error("${name}", mp->context, mp->retval, plugin_main->callbackClass, plugin_main->callbackObject, callbackExceptionClass);
102 if (mp->retval == VNET_API_ERROR_IN_PROGRESS) {
103 clib_warning("Result in progress");