b0577cf83ceb2397b58d02689ac5ad9963ac3ce5
[vpp.git] / extras / japi / java / jvpp-nsh / jvpp_nsh.c
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/vnet.h>
17
18 #define vl_typedefs             /* define message structures */
19 #include <nsh/nsh.api.h>
20 #undef vl_typedefs
21
22 #include <vnet/api_errno.h>
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25
26 #if VPPJNI_DEBUG == 1
27   #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
28 #else
29   #define DEBUG_LOG(...)
30 #endif
31
32 #include <jvpp-common/jvpp_common.h>
33
34 #include "jvpp-nsh/io_fd_vpp_jvpp_nsh_JVppNshImpl.h"
35 #include "jvpp_nsh.h"
36 #include "jvpp-nsh/jvpp_nsh_gen.h"
37
38 /*
39  * Class:     io_fd_vpp_jvpp_nsh_JVppnshImpl
40  * Method:    init0
41  * Signature: (JI)V
42  */
43 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_init0
44   (JNIEnv *env, jclass clazz, jobject callback, jlong queue_address, jint my_client_index) {
45   nsh_main_t * plugin_main = &nsh_main;
46   clib_warning ("Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_init0");
47
48   plugin_main->my_client_index = my_client_index;
49   plugin_main->vl_input_queue = (svm_queue_t *)queue_address;
50
51   plugin_main->callbackObject = (*env)->NewGlobalRef(env, callback);
52   plugin_main->callbackClass = (jclass)(*env)->NewGlobalRef(env, (*env)->GetObjectClass(env, callback));
53
54   // verify API has not changed since jar generation
55   #define _(N)             \
56       if (get_message_id(env, #N) == 0) return;
57       foreach_supported_api_message;
58   #undef _
59
60   #define _(N,n)                                  \
61       vl_msg_api_set_handlers(get_message_id(env, #N), #n,     \
62               vl_api_##n##_t_handler,             \
63               vl_noop_handler,                    \
64               vl_noop_handler,                    \
65               vl_noop_handler,                    \
66               sizeof(vl_api_##n##_t), 1);
67       foreach_api_reply_handler;
68   #undef _
69 }
70
71 JNIEXPORT void JNICALL Java_io_fd_vpp_jvpp_nsh_JVppNshImpl_close0
72 (JNIEnv *env, jclass clazz) {
73   nsh_main_t * plugin_main = &nsh_main;
74
75     // cleanup:
76     (*env)->DeleteGlobalRef(env, plugin_main->callbackClass);
77     (*env)->DeleteGlobalRef(env, plugin_main->callbackObject);
78
79     plugin_main->callbackClass = NULL;
80     plugin_main->callbackObject = NULL;
81
82
83 /* Attach thread to JVM and cache class references when initiating JVPP ACL */
84 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
85     JNIEnv* env;
86
87     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
88         return JNI_EVERSION;
89     }
90
91     if (cache_class_references(env) != 0) {
92         clib_warning ("Failed to cache class references\n");
93         return JNI_ERR;
94     }
95
96     return JNI_VERSION_1_8;
97 }
98
99 /* Clean up cached references when disposing JVPP ACL */
100 void JNI_OnUnload(JavaVM *vm, void *reserved) {
101     JNIEnv* env;
102     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_8) != JNI_OK) {
103         return;
104     }
105     delete_class_references(env);
106 }