diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2018-02-09 13:42:12 +0100 |
---|---|---|
committer | Neale Ranns <nranns@cisco.com> | 2018-03-02 15:22:34 +0000 |
commit | a51ccb5bb56fad29f68f9acafd458fada69bd825 (patch) | |
tree | c155c143fc1289ab33d48abbe4d7fba8f88add1b /src/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py | |
parent | 371ca50a74a9c4f1b74c4c1b65c6fdec610fcfc3 (diff) |
jvpp: object model for jvpp generator (VPP-1184)
Introduces JSON parser which builds object model of Java API.
Also rewrites JNI translation of typedefs
to use per type translation functions
instead of code inlining.
Not covered:
- integrate with vappigen plugin (VPP-1154) or vapi parser (VPP-1155)
- use better templating engine (VPP-480)
- improvements of generator structure (e.g. VPP-1186)
Change-Id: I9e12d76c2f3c6ee041669f58e8a37917f656aa90
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'src/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py')
-rwxr-xr-x[-rw-r--r--] | src/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py | 158 |
1 files changed, 56 insertions, 102 deletions
diff --git a/src/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py b/src/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py index 69eb67e8357..aff8d7fc99c 100644..100755 --- a/src/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py +++ b/src/vpp-api/java/jvpp/gen/jvppgen/jvpp_impl_gen.py @@ -1,6 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 # -# Copyright (c) 2016 Cisco and/or its affiliates. +# Copyright (c) 2016,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: @@ -12,33 +12,45 @@ # 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 util +# from string import Template -jvpp_ifc_template = Template(""" -package $plugin_package; +from jvpp_model import is_request, is_dump, is_event -/** - * <p>Java representation of plugin's api file. - * <br>It was generated by jvpp_impl_gen.py based on $inputfile - * <br>(python representation of api file generated by vppapigen). - */ -public interface JVpp${plugin_name} extends $base_package.JVpp { - /** - * Generic dispatch method for sending requests to VPP - * - * @throws io.fd.vpp.jvpp.VppInvocationException if send request had failed - */ - int send($base_package.$dto_package.JVppRequest request) throws io.fd.vpp.jvpp.VppInvocationException; +def generate_java_impl(work_dir, model, logger): + logger.debug("Generating JVpp implementation for %s" % model.json_api_files) + messages = filter(_jvpp_impl_filter, model.messages) + plugin_package = model.plugin_package + methods = [] + for msg in messages: + if msg.has_fields: + methods.append(_JVPP_IMPL_METHOD_TEMPLATE.substitute( + name=msg.java_name_lower, + plugin_package=plugin_package, + type=msg.java_name_upper)) + else: + methods.append(_JVPP_IMPL_NO_ARG_METHOD_TEMPLATE.substitute( + name=msg.java_name_lower, + type=msg.java_name_upper)) + + plugin_name = model.plugin_java_name + jvpp_impl = _JVPP_IMPL_TEMPLATE.substitute( + plugin_package=plugin_package, + json_filename=model.json_api_files, + plugin_name=model.plugin_java_name, + plugin_name_underscore=model.plugin_name, + methods="\n".join(methods)) -$methods -} -""") + with open("%s/JVpp%sImpl.java" % (work_dir, plugin_name), "w") as f: + f.write(jvpp_impl) + + +def _jvpp_impl_filter(msg): + return is_request(msg) or is_dump(msg) or is_event(msg) -jvpp_impl_template = Template(""" -package $plugin_package; + +_JVPP_IMPL_TEMPLATE = Template("""package $plugin_package; import java.io.IOException; import java.io.InputStream; @@ -50,14 +62,14 @@ import java.nio.file.attribute.PosixFilePermissions; import java.util.Set; import java.util.logging.Logger; import java.util.logging.Level; -import $base_package.callback.JVppCallback; -import $base_package.VppConnection; -import $base_package.JVppRegistry; +import io.fd.vpp.jvpp.callback.JVppCallback; +import io.fd.vpp.jvpp.VppConnection; +import io.fd.vpp.jvpp.JVppRegistry; /** * <p>Default implementation of JVpp interface. - * <br>It was generated by jvpp_impl_gen.py based on $inputfile - * <br>(python representation of api file generated by vppapigen). + * <br>It was generated by jvpp_impl_gen.py based on $json_filename. + * <br>(python representation of api file generated by vppapigen) */ public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugin_name} { @@ -121,7 +133,7 @@ public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugi } @Override - public int send($base_package.$dto_package.JVppRequest request) throws io.fd.vpp.jvpp.VppInvocationException { + public int send(io.fd.vpp.jvpp.dto.JVppRequest request) throws io.fd.vpp.jvpp.VppInvocationException { return request.send(this); } @@ -129,91 +141,33 @@ public final class JVpp${plugin_name}Impl implements $plugin_package.JVpp${plugi public final int controlPing(final io.fd.vpp.jvpp.dto.ControlPing controlPing) throws io.fd.vpp.jvpp.VppInvocationException { return registry.controlPing(JVpp${plugin_name}Impl.class); } - $methods } """) -method_template = Template(""" int $name($plugin_package.$dto_package.$request request) throws io.fd.vpp.jvpp.VppInvocationException;""") -method_native_template = Template( - """ private static native int ${name}0($plugin_package.$dto_package.$request request);""") -method_impl_template = Template(""" public final int $name($plugin_package.$dto_package.$request request) throws io.fd.vpp.jvpp.VppInvocationException { - java.util.Objects.requireNonNull(request,"Null request object"); +_JVPP_IMPL_METHOD_TEMPLATE = Template(""" + private static native int ${name}0($plugin_package.dto.$type request); + public final int $name($plugin_package.dto.$type request) throws io.fd.vpp.jvpp.VppInvocationException { + java.util.Objects.requireNonNull(request, "Null request object"); connection.checkActive(); - if(LOG.isLoggable(Level.FINE)) { - LOG.fine(String.format("Sending $name event message: %s", request)); + if (LOG.isLoggable(Level.FINE)) { + LOG.fine(String.format("Sending $type event message: %s", request)); } int result=${name}0(request); - if(result<0){ - throw new io.fd.vpp.jvpp.VppInvocationException("${name}",result); + if (result<0){ + throw new io.fd.vpp.jvpp.VppInvocationException("${name}", result); } return result; - } -""") + }""") -no_arg_method_template = Template(""" int $name() throws io.fd.vpp.jvpp.VppInvocationException;""") -no_arg_method_native_template = Template(""" private static native int ${name}0() throws io.fd.vpp.jvpp.VppInvocationException;""") -no_arg_method_impl_template = Template(""" public final int $name() throws io.fd.vpp.jvpp.VppInvocationException { +_JVPP_IMPL_NO_ARG_METHOD_TEMPLATE = Template(""" + private static native int ${name}0() throws io.fd.vpp.jvpp.VppInvocationException; + public final int $name() throws io.fd.vpp.jvpp.VppInvocationException { connection.checkActive(); - LOG.fine("Sending $name event message"); + LOG.fine("Sending $type event message"); int result=${name}0(); if(result<0){ - throw new io.fd.vpp.jvpp.VppInvocationException("${name}",result); + throw new io.fd.vpp.jvpp.VppInvocationException("${name}", result); } return result; - } -""") - - -def generate_jvpp(func_list, base_package, plugin_package, plugin_name_underscore, dto_package, inputfile, logger): - """ Generates JVpp interface and JNI implementation """ - logger.debug("Generating JVpp interface implementation for %s" % inputfile) - plugin_name = util.underscore_to_camelcase_upper(plugin_name_underscore) - - methods = [] - methods_impl = [] - for func in func_list: - 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)) - methods_impl.append(no_arg_method_native_template.substitute(name=camel_case_name)) - methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name)) - else: - methods.append(method_template.substitute(name=camel_case_name, - request=camel_case_name_upper, - plugin_package=plugin_package, - dto_package=dto_package)) - methods_impl.append(method_native_template.substitute(name=camel_case_name, - request=camel_case_name_upper, - plugin_package=plugin_package, - dto_package=dto_package)) - methods_impl.append(method_impl_template.substitute(name=camel_case_name, - request=camel_case_name_upper, - plugin_package=plugin_package, - dto_package=dto_package)) - - jvpp_file = open("JVpp%s.java" % plugin_name, 'w') - jvpp_file.write( - jvpp_ifc_template.substitute(inputfile=inputfile, - methods="\n".join(methods), - base_package=base_package, - plugin_package=plugin_package, - plugin_name=plugin_name, - dto_package=dto_package)) - jvpp_file.flush() - jvpp_file.close() - - jvpp_file = open("JVpp%sImpl.java" % plugin_name, 'w') - jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile, - methods="\n".join(methods_impl), - base_package=base_package, - plugin_package=plugin_package, - plugin_name=plugin_name, - plugin_name_underscore=plugin_name_underscore, - dto_package=dto_package)) - jvpp_file.flush() - jvpp_file.close() + }""") |