diff options
author | Ole Troan <ot@cisco.com> | 2018-11-28 11:36:05 +0100 |
---|---|---|
committer | Neale Ranns <nranns@cisco.com> | 2018-12-13 12:11:50 +0000 |
commit | 413f4a5b2123c1625d615315db293a080078482b (patch) | |
tree | 6cfd8376c1d84b93793b062731ec9594487dc95e /extras | |
parent | 6f666ad99ae1e384aa851af5e0feed3d2a25e709 (diff) |
API: Use string type instead of u8.
The new string type is modelled after string in proto3.
It is always variable length.
Change-Id: I64884067e28a80072c8dac31b7c7c82d6e306051
Signed-off-by: Ole Troan <ot@cisco.com>
Signed-off-by: Michal Cmarada <mcmarada@cisco.com>
Signed-off-by: Ole Troan <ot@cisco.com>
Diffstat (limited to 'extras')
15 files changed, 122 insertions, 40 deletions
diff --git a/extras/japi/java/jvpp-common/jvpp_common.h b/extras/japi/java/jvpp-common/jvpp_common.h index dd48138a536..e12141b08ba 100644 --- a/extras/japi/java/jvpp-common/jvpp_common.h +++ b/extras/japi/java/jvpp-common/jvpp_common.h @@ -17,6 +17,7 @@ // #include <vppinfra/types.h> #include <vlibapi/api.h> +#include <vlibapi/api_types.h> #include <vlibmemory/api.h> #include <jni.h> diff --git a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackApiExample.java b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackApiExample.java index b99979cf301..b6558ffaa7e 100644 --- a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackApiExample.java +++ b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackApiExample.java @@ -77,17 +77,17 @@ public class CallbackApiExample { System.out.printf("Received ShowVersionReply: context=%d, program=%s, version=%s, " + "buildDate=%s, buildDirectory=%s%n", msg.context, - new String(msg.program, StandardCharsets.UTF_8), - new String(msg.version, StandardCharsets.UTF_8), - new String(msg.buildDate, StandardCharsets.UTF_8), - new String(msg.buildDirectory, StandardCharsets.UTF_8)); + msg.program, + msg.version, + msg.buildDate, + msg.buildDirectory); } @Override public void onSwInterfaceDetails(final SwInterfaceDetails msg) { System.out.printf("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, " + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n", - new String(msg.interfaceName, StandardCharsets.UTF_8), msg.l2AddressLength, msg.adminUpDown, + msg.interfaceName, msg.l2AddressLength, msg.adminUpDown, msg.linkUpDown, msg.linkSpeed, (int) msg.linkMtu); } diff --git a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackJVppFacadeExample.java b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackJVppFacadeExample.java index dc2bdcba569..a8b91860b75 100644 --- a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackJVppFacadeExample.java +++ b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/CallbackJVppFacadeExample.java @@ -39,10 +39,10 @@ public class CallbackJVppFacadeExample { public void onShowVersionReply(final ShowVersionReply msg) { System.out.printf("ShowVersionCallback1 received ShowVersionReply: context=%d, program=%s," + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, - new String(msg.program, StandardCharsets.UTF_8), - new String(msg.version, StandardCharsets.UTF_8), - new String(msg.buildDate, StandardCharsets.UTF_8), - new String(msg.buildDirectory, StandardCharsets.UTF_8)); + msg.program, + msg.version, + msg.buildDate, + msg.buildDirectory); } @Override @@ -57,10 +57,10 @@ public class CallbackJVppFacadeExample { public void onShowVersionReply(final ShowVersionReply msg) { System.out.printf("ShowVersionCallback2 received ShowVersionReply: context=%d, program=%s," + "version=%s, buildDate=%s, buildDirectory=%s%n", msg.context, - new String(msg.program, StandardCharsets.UTF_8), - new String(msg.version, StandardCharsets.UTF_8), - new String(msg.buildDate, StandardCharsets.UTF_8), - new String(msg.buildDirectory, StandardCharsets.UTF_8)); + msg.program, + msg.version, + msg.buildDate, + msg.buildDirectory); } @Override diff --git a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/FutureApiExample.java b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/FutureApiExample.java index 931c9b337aa..030e68995a2 100644 --- a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/FutureApiExample.java +++ b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/FutureApiExample.java @@ -47,10 +47,10 @@ public class FutureApiExample { LOG.info( String.format( "Received ShowVersionReply: context=%d, program=%s, version=%s, buildDate=%s, buildDirectory=%s%n", - reply.context, new String(reply.program, StandardCharsets.UTF_8), - new String(reply.version, StandardCharsets.UTF_8), - new String(reply.buildDate, StandardCharsets.UTF_8), - new String(reply.buildDirectory, StandardCharsets.UTF_8))); + reply.context, reply.program, + reply.version, + reply.buildDate, + reply.buildDirectory)); } private static void testEmptyBridgeDomainDump(final FutureJVppCoreFacade jvpp) throws Exception { @@ -100,7 +100,7 @@ public class FutureApiExample { LOG.info( String.format("Received SwInterfaceDetails: interfaceName=%s, l2AddressLength=%d, adminUpDown=%d, " + "linkUpDown=%d, linkSpeed=%d, linkMtu=%d%n", - new String(details.interfaceName, StandardCharsets.UTF_8), + details.interfaceName, details.l2AddressLength, details.adminUpDown, details.linkUpDown, details.linkSpeed, (int) details.linkMtu)); } diff --git a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/L2AclExample.java b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/L2AclExample.java index 9a17136a6d9..0be85d4f17f 100644 --- a/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/L2AclExample.java +++ b/extras/japi/java/jvpp-core/io/fd/vpp/jvpp/core/examples/L2AclExample.java @@ -75,7 +75,7 @@ public class L2AclExample { hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } - return new String(hexChars); + return new java.lang.String(hexChars); } private static ClassifyTableInfo createClassifyTableInfoRequest(final int tableId) { diff --git a/extras/japi/java/jvpp-core/jvpp_core.c b/extras/japi/java/jvpp-core/jvpp_core.c index 09d753026d7..0df8589702c 100644 --- a/extras/japi/java/jvpp-core/jvpp_core.c +++ b/extras/japi/java/jvpp-core/jvpp_core.c @@ -14,7 +14,7 @@ */ #include <vnet/vnet.h> - +#include <jvpp-common/jvpp_common.h> #include <vpp/api/vpe_msg_enum.h> #define vl_typedefs /* define message structures */ #include <vpp/api/vpe_all_api_h.h> @@ -24,8 +24,7 @@ #include <vlibapi/api.h> #include <vlibmemory/api.h> #include <jni.h> - -#include <jvpp-common/jvpp_common.h> +#include <jvpp_core.h> // TODO: generate jvpp_plugin_name.c files (or at least reuse plugin's main structure) typedef struct { diff --git a/extras/japi/java/jvpp-core/jvpp_core.h b/extras/japi/java/jvpp-core/jvpp_core.h new file mode 100644 index 00000000000..bbf7a71fa31 --- /dev/null +++ b/extras/japi/java/jvpp-core/jvpp_core.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef VPP_JVPP_CORE_H +#define VPP_JVPP_CORE_H + +#include <vlibapi/api_types.h> + +#endif //VPP_JVPP_CORE_H + +// /** +// * Host to network byte order conversion for string type. Converts String in Java to VPP string type. +// * typedef struct +// * { +// * u32 length; +// * u8 buf[0]; +// * } __attribute__ ((packed)) vl_api_string_t; +// */ +static void _host_to_net_string(JNIEnv * env, jstring javaString, vl_api_string_t * _net) +{ + const char *nativeString = (*env)->GetStringUTFChars(env, javaString, 0); + int len = strlen(nativeString); + + vl_api_string_t * vl_api_string = (vl_api_string_t *)calloc(1, (sizeof(vl_api_string_t) + len * sizeof(u8))); + if (NULL == vl_api_string) + return; + + vl_api_string->length = len; + memcpy(vl_api_string->buf, nativeString, len); + + _net = vl_api_string; + (*env)->ReleaseStringUTFChars(env, javaString, nativeString); +} + +// +// /** +// * Network to host byte order conversion for string type. Converts VPP string type to String in Java +// * typedef struct +// * { +// * u32 length; +// * u8 buf[0]; +// * } __attribute__ ((packed)) vl_api_string_t; +// */ +static jstring _net_to_host_string(JNIEnv * env, const vl_api_string_t * _net) +{ + jstring jstr = (*env)->NewStringUTF(env, (char *)_net->buf); + + return jstr; +} diff --git a/extras/japi/java/jvpp/gen/jvppgen/dto_gen.py b/extras/japi/java/jvpp/gen/jvppgen/dto_gen.py index ca015a30456..cbd969d51fe 100755 --- a/extras/japi/java/jvpp/gen/jvppgen/dto_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/dto_gen.py @@ -221,7 +221,7 @@ public final class ${details_class}ReplyDump implements io.fd.vpp.jvpp.dto.JVppR } @Override - public String toString() { + public java.lang.String toString() { return "${details_class}ReplyDump{" + "${details_field}=" + ${details_field} + "}"; } diff --git a/extras/japi/java/jvpp/gen/jvppgen/jni_common_gen.py b/extras/japi/java/jvpp/gen/jvppgen/jni_common_gen.py index 708cc1cba81..b52e5ff2d2c 100755 --- a/extras/japi/java/jvpp/gen/jvppgen/jni_common_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jni_common_gen.py @@ -165,6 +165,7 @@ def _generate_field_length_check(field): else: return "" + # Make sure we do not write more elements that are expected _FIELD_LENGTH_CHECK = Template(""" size_t max_size = ${field_length}; @@ -177,9 +178,13 @@ def _generate_j2c_scalar_swap(field, struct_ref_name, is_alias): host = field.java_name if not is_alias: net = "%s->%s" % (struct_ref_name, field.name) - return " %s;" % field_type.get_host_to_net_function(host, net) + if field_type.name == "string": + net = "%s->%s" % (struct_ref_name, field.name) + return " _host_to_net_%s(env, %s, (vl_api_string_t *) &%s);" % (field_type.name, host, net) + else: + return " %s;" % field_type.get_host_to_net_function(host, net) else: - net = "%s" % (struct_ref_name) + net = "%s" % struct_ref_name return " *%s;" % field_type.get_host_to_net_function(host, net) else: return " %s->%s = %s;" % (struct_ref_name, field.name, field.java_name) @@ -424,7 +429,10 @@ _C2J_ENUM_SWAP_TEMPLATE = Template(""" def _generate_c2j_primitive_type_swap(msg_java_name, field, object_ref_name, struct_ref_name, is_alias): field_type = field.type if not is_alias: - template = _C2J_PRIMITIVE_TYPE_SWAP_TEMPLATE + if field_type.name == "string": + template = _C2J_STRING_TYPE_SWAP_TEMPLATE + else: + template = _C2J_PRIMITIVE_TYPE_SWAP_TEMPLATE else: template = _C2J_ALIAS_PRIMITIVE_TYPE_SWAP_TEMPLATE return template.substitute( @@ -444,6 +452,12 @@ _C2J_PRIMITIVE_TYPE_SWAP_TEMPLATE = Template(""" """) +_C2J_STRING_TYPE_SWAP_TEMPLATE = Template(""" + jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}"); + (*env)->Set${jni_accessor}Field(env, ${object_ref_name}, ${java_name}FieldId, ${net_to_host_function}(env, (const vl_api_string_t *) &${struct_ref_name}->${c_name})); +""") + + _C2J_ALIAS_PRIMITIVE_TYPE_SWAP_TEMPLATE = Template(""" jfieldID ${java_name}FieldId = (*env)->GetFieldID(env, ${class_ref_name}Class, "${java_name}", "${jni_signature}"); (*env)->Set${jni_accessor}Field(env, ${object_ref_name}, ${java_name}FieldId, ${net_to_host_function}(*${struct_ref_name})); diff --git a/extras/japi/java/jvpp/gen/jvppgen/jvpp_callback_facade_gen.py b/extras/japi/java/jvpp/gen/jvppgen/jvpp_callback_facade_gen.py index c5634de2e48..ebc552bfda6 100644 --- a/extras/japi/java/jvpp/gen/jvppgen/jvpp_callback_facade_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jvpp_callback_facade_gen.py @@ -210,7 +210,7 @@ public final class CallbackJVpp${plugin_name}FacadeCallback implements $plugin_p failedCall.onError(reply); } catch(RuntimeException ex) { ex.addSuppressed(reply); - LOG.log(java.util.logging.Level.WARNING, String.format("Callback: %s failed while handling exception: %s", failedCall, reply), ex); + LOG.log(java.util.logging.Level.WARNING, java.lang.String.format("Callback: %s failed while handling exception: %s", failedCall, reply), ex); } } } @@ -265,7 +265,7 @@ _CALLBACK_METHOD_TEMPLATE = Template(""" $plugin_package.callback.$callback callback; final int replyId = reply.context; if (LOG.isLoggable(java.util.logging.Level.FINE)) { - LOG.fine(String.format("Received ${message} event message: %s", reply)); + LOG.fine(java.lang.String.format("Received ${message} event message: %s", reply)); } synchronized(requests) { callback = ($plugin_package.callback.$callback) requests.remove(replyId); @@ -282,7 +282,7 @@ _CALLBACK_EVENT_METHOD_TEMPLATE = Template(""" @SuppressWarnings("unchecked") public void on${message}($plugin_package.dto.${message} notification) { if (LOG.isLoggable(java.util.logging.Level.FINE)) { - LOG.fine(String.format("Received ${message} event message: %s", notification)); + LOG.fine(java.lang.String.format("Received ${message} event message: %s", notification)); } eventCallback.on${message}(notification); } diff --git a/extras/japi/java/jvpp/gen/jvppgen/jvpp_common_gen.py b/extras/japi/java/jvpp/gen/jvppgen/jvpp_common_gen.py index 83226ea78ac..499adbc3f56 100755 --- a/extras/japi/java/jvpp/gen/jvppgen/jvpp_common_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jvpp_common_gen.py @@ -103,7 +103,7 @@ def generate_to_string(class_name, fields): _TO_STRING_TEMPLATE = Template(""" @Override - public String toString() { + public java.lang.String toString() { return "$class_name{" + $to_string_fields }""") diff --git a/extras/japi/java/jvpp/gen/jvppgen/jvpp_future_facade_gen.py b/extras/japi/java/jvpp/gen/jvppgen/jvpp_future_facade_gen.py index 47a9985144d..3da367a4d5f 100644 --- a/extras/japi/java/jvpp/gen/jvppgen/jvpp_future_facade_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jvpp_future_facade_gen.py @@ -280,7 +280,7 @@ _FUTURE_JVPP_FACADE_DETAILS_CALLBACK_TEMPLATE = Template(""" io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.dto.${callback_dto}ReplyDump> completableFuture; final int replyId = reply.context; if (LOG.isLoggable(java.util.logging.Level.FINE)) { - LOG.fine(String.format("Received $callback_dto event message: %s", reply)); + LOG.fine(java.lang.String.format("Received $callback_dto event message: %s", reply)); } synchronized(requests) { completableFuture = (io.fd.vpp.jvpp.future.AbstractFutureJVppInvoker.CompletableDumpFuture<$plugin_package.dto.${callback_dto}ReplyDump>) requests.get(replyId); @@ -305,7 +305,7 @@ _FUTURE_JVPP_FACADE_REPLY_CALLBACK_TEMPLATE = Template(""" java.util.concurrent.CompletableFuture<io.fd.vpp.jvpp.dto.JVppReply<$plugin_package.dto.$request_dto>> completableFuture; final int replyId = reply.context; if (LOG.isLoggable(java.util.logging.Level.FINE)) { - LOG.fine(String.format("Received $callback_dto event message: %s", reply)); + LOG.fine(java.lang.String.format("Received $callback_dto event message: %s", reply)); } synchronized(requests) { completableFuture = @@ -331,7 +331,7 @@ _FUTURE_JVPP_FACADE_EVENT_CALLBACK_TEMPLATE = Template(""" @Override public void on$callback_dto($plugin_package.dto.$callback_dto notification) { if (LOG.isLoggable(java.util.logging.Level.FINE)) { - LOG.fine(String.format("Received $callback_dto event message: %s", notification)); + LOG.fine(java.lang.String.format("Received $callback_dto event message: %s", notification)); } notificationCallback.on$callback_dto(notification); } diff --git a/extras/japi/java/jvpp/gen/jvppgen/jvpp_impl_gen.py b/extras/japi/java/jvpp/gen/jvppgen/jvpp_impl_gen.py index aff8d7fc99c..376952b620e 100755 --- a/extras/japi/java/jvpp/gen/jvppgen/jvpp_impl_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jvpp_impl_gen.py @@ -74,7 +74,7 @@ import io.fd.vpp.jvpp.JVppRegistry; public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugin_name} { private final static Logger LOG = Logger.getLogger(JVpp${plugin_name}Impl.class.getName()); - private static final String LIBNAME = "libjvpp_${plugin_name_underscore}.so"; + private static final java.lang.String LIBNAME = "libjvpp_${plugin_name_underscore}.so"; // FIXME using NativeLibraryLoader makes load fail could not find (WantInterfaceEventsReply). static { @@ -151,7 +151,7 @@ _JVPP_IMPL_METHOD_TEMPLATE = Template(""" java.util.Objects.requireNonNull(request, "Null request object"); connection.checkActive(); if (LOG.isLoggable(Level.FINE)) { - LOG.fine(String.format("Sending $type event message: %s", request)); + LOG.fine(java.lang.String.format("Sending $type event message: %s", request)); } int result=${name}0(request); if (result<0){ diff --git a/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py b/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py index 16099689880..da1e01f5a63 100755 --- a/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py +++ b/extras/japi/java/jvpp/gen/jvppgen/jvpp_model.py @@ -92,9 +92,11 @@ class SimpleType(Type): # TODO(VPP-1187): add array host to net functions to reduce number of members and simplify JNI generation class Array(Type): - def __init__(self, base_type): + def __init__(self, base_type, name=None): + if name is None: + name = base_type.name + _ARRAY_SUFFIX super(Array, self).__init__( - name=base_type.name + _ARRAY_SUFFIX, + name=name, java_name=base_type.java_name + _ARRAY_SUFFIX, java_name_fqn=base_type.java_name_fqn + _ARRAY_SUFFIX, jni_signature="[%s" % base_type.jni_signature, @@ -341,6 +343,8 @@ class JVppModel(object): self._parse_types(types) def _parse_aliases(self, types): + + # model aliases for alias_name in self._aliases: alias = self._aliases[alias_name] alias_type = {"type": "type"} @@ -442,7 +446,10 @@ class JVppModel(object): 'i64': SimpleType('i64', 'long', 'J', 'jlong', 'Long', host_to_net_function='clib_host_to_net_i64', net_to_host_function='clib_net_to_host_i64'), - 'f64': SimpleType('f64', 'double', 'D', 'jdouble', 'Double') + 'f64': SimpleType('f64', 'double', 'D', 'jdouble', 'Double'), + 'string': SimpleType('string', 'String', 'l', 'jstring', 'Object', + host_to_net_function='_host_to_net_string', + net_to_host_function='_net_to_host_string') }) for n, t in self._types_by_name.items(): diff --git a/extras/japi/java/jvpp/gen/jvppgen/notification_gen.py b/extras/japi/java/jvpp/gen/jvppgen/notification_gen.py index 69e870ed33d..fa86fe4bbf2 100644 --- a/extras/japi/java/jvpp/gen/jvppgen/notification_gen.py +++ b/extras/japi/java/jvpp/gen/jvppgen/notification_gen.py @@ -117,7 +117,7 @@ _HANDLER_IMPL_TEMPLATE = Template(""" public void on$notification( final $plugin_package.dto.$notification notification) { if (LOG.isLoggable(java.util.logging.Level.FINE)) { - LOG.fine(String.format("Received $notification event message: %s", notification)); + LOG.fine(java.lang.String.format("Received $notification event message: %s", notification)); } final io.fd.vpp.jvpp.callback.JVppCallback jVppCallback = registeredCallbacks.get($plugin_package.dto.$notification.class); if (null != jVppCallback) { @@ -153,7 +153,7 @@ public final class ${plugin_name}EventRegistryImpl implements ${plugin_name}Even @Override public void onError(io.fd.vpp.jvpp.VppCallbackException ex) { java.util.logging.Logger LOG = java.util.logging.Logger.getLogger(${plugin_name}EventRegistryImpl.class.getName()); - LOG.log(java.util.logging.Level.WARNING, String.format("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(), + LOG.log(java.util.logging.Level.WARNING, java.lang.String.format("Received onError exception: call=%s, context=%d, retval=%d%n", ex.getMethodName(), ex.getCtxId(), ex.getErrorCode()), ex); } } |