From 45a42b5d7f6940fbd93d6b3413c2d943e497a0b1 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Thu, 28 Apr 2016 12:29:33 +0200 Subject: HONEYCOMB-10: jVpp - the new java API. Java code generation The old japi has two main drawbacks: * it is not fully generated (requres manual coding for every new api call that returns data other thanstatus code) * it is not asynchronous from Java perspective (requires active wait loops - big overhead due to JNI boundary being crossed lots of times). The new api is lightweight (fully generated except for connect, disconenct and ping) and truly asynchronous (uses callbacks, utilities that offer java.util.concurrent.Future interface are also provided). Change-Id: I531080ef651e8a74f19210490c71d161221ab600 Signed-off-by: Marek Gradzki Signed-off-by: Maros Marsalek Signed-off-by: Ed Warnicke --- vpp-api/java/jvpp/gen/jvpp_impl_gen.py | 140 +++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 vpp-api/java/jvpp/gen/jvpp_impl_gen.py (limited to 'vpp-api/java/jvpp/gen/jvpp_impl_gen.py') diff --git a/vpp-api/java/jvpp/gen/jvpp_impl_gen.py b/vpp-api/java/jvpp/gen/jvpp_impl_gen.py new file mode 100644 index 00000000000..5446a694c1b --- /dev/null +++ b/vpp-api/java/jvpp/gen/jvpp_impl_gen.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python +# +# Copyright (c) 2016 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. + +import os, util +from string import Template + +jvpp_ifc_template = Template(""" +package $base_package; + +public interface JVpp extends java.lang.AutoCloseable { + + /** + * Generic dispatch method for sending requests to VPP + */ + int send($base_package.$dto_package.JVppRequest request); + + @Override + void close(); + +$methods +} +""") + +jvpp_impl_template = Template(""" +package $base_package; + +public final class JVppImpl implements $base_package.JVpp { + + private final $base_package.VppConnection connection; + + public JVppImpl(final $base_package.VppConnection connection) { + if(connection == null) { + throw new java.lang.NullPointerException("Connection is null"); + } + this.connection = connection; + } + + @Override + public void close() { + connection.close(); + } + + @Override + public int send($base_package.$dto_package.JVppRequest request) { + return request.send(this); + } + +$methods +} +""") + +method_template = Template(""" int $name($base_package.$dto_package.$request request);""") +method_native_template = Template( + """ private static native int ${name}0($base_package.$dto_package.$request request);""") +method_impl_template = Template(""" public final int $name($base_package.$dto_package.$request request) { + if(request == null) { + throw new java.lang.NullPointerException("Null request object"); + } + connection.checkActive(); + return ${name}0(request); + } +""") + +no_arg_method_template = Template(""" int $name();""") +no_arg_method_native_template = Template(""" private static native int ${name}0();""") +no_arg_method_impl_template = Template(""" public final int $name() { + connection.checkActive(); + return ${name}0(); + } +""") + + +def generate_jvpp(func_list, base_package, dto_package): + """ Generates JVpp interface and JNI implementation """ + print "Generating JVpp" + + methods = [] + methods_impl = [] + for func in func_list: + + if util.is_notification(func['name']) or util.is_ignored(func['name']): + # TODO handle notifications + continue + + camel_case_name = util.underscore_to_camelcase(func['name']) + camel_case_name_upper = util.underscore_to_camelcase_upper(func['name']) + if util.is_reply(camel_case_name): + continue + + if len(func['args']) == 0: + methods.append(no_arg_method_template.substitute(name=camel_case_name, + base_package=base_package, + dto_package=dto_package)) + methods_impl.append( + no_arg_method_native_template.substitute(name=camel_case_name, + base_package=base_package, + dto_package=dto_package)) + methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name, + base_package=base_package, + dto_package=dto_package)) + else: + methods.append(method_template.substitute(name=camel_case_name, + request=camel_case_name_upper, + base_package=base_package, + dto_package=dto_package)) + methods_impl.append(method_native_template.substitute(name=camel_case_name, + request=camel_case_name_upper, + base_package=base_package, + dto_package=dto_package)) + methods_impl.append(method_impl_template.substitute(name=camel_case_name, + request=camel_case_name_upper, + base_package=base_package, + dto_package=dto_package)) + + jvpp_file = open("JVpp.java", 'w') + jvpp_file.write( + jvpp_ifc_template.substitute(methods="\n".join(methods), + base_package=base_package, + dto_package=dto_package)) + jvpp_file.flush() + jvpp_file.close() + + jvpp_file = open("JVppImpl.java", 'w') + jvpp_file.write(jvpp_impl_template.substitute(methods="\n".join(methods_impl), + base_package=base_package, + dto_package=dto_package)) + jvpp_file.flush() + jvpp_file.close() -- cgit 1.2.3-korg