Move java api to extras/
[vpp.git] / extras / japi / java / jvpp-common / jvpp_common.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 #define _GNU_SOURCE /* for strcasestr(3) */
16
17 #include <vnet/api_errno.h>
18 #include "jvpp_common.h"
19
20 #ifndef JVPP_DEBUG
21 #define JVPP_DEBUG 0
22 #endif
23
24 #if JVPP_DEBUG == 1
25 #define DEBUG_LOG(...) clib_warning(__VA_ARGS__)
26 #else
27 #define DEBUG_LOG(...)
28 #endif
29
30 #define _(error,errorCode,msg)  \
31 if (errorCode == code)          \
32     message = msg;              \
33 else
34
35 #define get_error_message(errno)    \
36 int code = errno;                   \
37 foreach_vnet_api_error              \
38     message = "Reason unknown";
39
40 /* shared jvpp main structure */
41 jvpp_main_t jvpp_main __attribute__((aligned (64)));
42
43 void call_on_error(const char* callName, int contextId, int retval,
44         jclass callbackClass, jobject callbackObject,
45         jclass callbackExceptionClass) {
46     DEBUG_LOG("\nCallOnError : callback=%s, retval=%d, context=%d\n", callName,
47             clib_net_to_host_u32(retval), clib_net_to_host_u32(context));
48     JNIEnv *env = jvpp_main.jenv;
49     if (!callbackClass) {
50         DEBUG_LOG("CallOnError : jm->callbackClass is null!\n");
51         return;
52     }
53     jmethodID excConstructor = (*env)->GetMethodID(env, callbackExceptionClass,
54             "<init>", "(Ljava/lang/String;Ljava/lang/String;II)V");
55     if (!excConstructor) {
56         DEBUG_LOG("CallOnError : excConstructor is null!\n");
57         return;
58     }
59     jmethodID callbackExcMethod = (*env)->GetMethodID(env, callbackClass,
60             "onError", "(Lio/fd/vpp/jvpp/VppCallbackException;)V");
61     if (!callbackExcMethod) {
62         DEBUG_LOG("CallOnError : callbackExcMethod is null!\n");
63         return;
64     }
65
66     char *message;
67     get_error_message(clib_net_to_host_u32(retval));
68     jobject excObject = (*env)->NewObject(env, callbackExceptionClass,
69             excConstructor, (*env)->NewStringUTF(env, callName),
70             (*env)->NewStringUTF(env, message),
71             clib_net_to_host_u32(contextId), clib_net_to_host_u32(retval));
72     if (!excObject) {
73         DEBUG_LOG("CallOnError : excObject is null!\n");
74         return;
75     }
76
77     (*env)->CallVoidMethod(env, callbackObject, callbackExcMethod, excObject);
78     DEBUG_LOG("CallOnError : Response sent\n");
79 }
80 #undef _
81
82 u32 get_message_id(JNIEnv *env, const char *key) {
83     uword *p = hash_get(jvpp_main.messages_hash, key);
84     if (!p) {
85         jclass exClass = (*env)->FindClass(env, "java/lang/IllegalStateException");
86         char *msgBuf  = clib_mem_alloc(strlen(key) + 70);
87         strcpy(msgBuf, "API mismatch detected: ");
88         strcat(msgBuf, key);
89         strcat(msgBuf, " is missing in global name_crc hash table.");
90         DEBUG_LOG("%s", msgBuf);
91         DEBUG_LOG("Possible reasons:");
92         DEBUG_LOG("1) incompatible VPP version used");
93         DEBUG_LOG("2) message present in JSON file but not in global name_crc table");
94         (*env)->ThrowNew(env, exClass, msgBuf);
95         clib_mem_free(msgBuf);
96         return 0;
97     }
98     return (u32) p[0];
99 }