diff options
Diffstat (limited to 'vpp-api/java/jvpp/gen')
-rw-r--r-- | vpp-api/java/jvpp/gen/callback_gen.py | 89 | ||||
-rw-r--r-- | vpp-api/java/jvpp/gen/dto_gen.py | 143 | ||||
-rw-r--r-- | vpp-api/java/jvpp/gen/jvpp_callback_facade_gen.py | 236 | ||||
-rw-r--r-- | vpp-api/java/jvpp/gen/jvpp_future_facade_gen.py | 184 | ||||
-rw-r--r-- | vpp-api/java/jvpp/gen/jvpp_impl_gen.py | 140 | ||||
-rw-r--r-- | vpp-api/java/jvpp/gen/util.py | 173 |
6 files changed, 965 insertions, 0 deletions
diff --git a/vpp-api/java/jvpp/gen/callback_gen.py b/vpp-api/java/jvpp/gen/callback_gen.py new file mode 100644 index 00000000000..218ac62298b --- /dev/null +++ b/vpp-api/java/jvpp/gen/callback_gen.py @@ -0,0 +1,89 @@ +#!/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 +import util +from string import Template + +from util import remove_suffix + +callback_suffix = "Callback" + +callback_template = Template(""" +package $base_package.$callback_package; + +/** + * $docs + */ +public interface $cls_name extends $base_package.$callback_package.JVppCallback { + + $callback_method + +} +""") + +global_callback_template = Template(""" +package $base_package.$callback_package; + +/** + * + * + * Global aggregated callback interface + */ +public interface JVppGlobalCallback extends $callbacks { +} +""") + + +def generate_callbacks(func_list, base_package, callback_package, dto_package): + """ Generates callback interfaces """ + print "Generating Callback interfaces" + + if not os.path.exists(callback_package): + raise Exception("%s folder is missing" % callback_package) + + callbacks = [] + for func in func_list: + + if util.is_notification(func['name']) or util.is_ignored(func['name']): + # FIXME handle notifications + continue + + camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) + if not util.is_reply(camel_case_name_with_suffix): + continue + + camel_case_name = util.remove_reply_suffix(camel_case_name_with_suffix) + callbacks.append("{0}.{1}.{2}".format(base_package, callback_package, camel_case_name + callback_suffix)) + callback_path = os.path.join(callback_package, camel_case_name + callback_suffix + ".java") + callback_file = open(callback_path, 'w') + + reply_type = "%s.%s.%s" % (base_package, dto_package, camel_case_name_with_suffix) + method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type) + callback_file.write( + callback_template.substitute(docs='Generated from ' + str(func), + cls_name=camel_case_name + callback_suffix, + callback_method=method, + base_package=base_package, + callback_package=callback_package)) + callback_file.flush() + callback_file.close() + + callback_file = open(os.path.join(callback_package, "JVppGlobalCallback.java"), 'w') + callback_file.write(global_callback_template.substitute(callbacks=", ".join(callbacks), + base_package=base_package, + callback_package=callback_package)) + callback_file.flush() + callback_file.close() diff --git a/vpp-api/java/jvpp/gen/dto_gen.py b/vpp-api/java/jvpp/gen/dto_gen.py new file mode 100644 index 00000000000..17fde68a26d --- /dev/null +++ b/vpp-api/java/jvpp/gen/dto_gen.py @@ -0,0 +1,143 @@ +#!/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 + +dto_template = Template(""" +package $base_package.$dto_package; + +/** + * $docs + */ +public final class $cls_name implements $base_package.$dto_package.$base_type { + +$fields +$methods +} +""") + +field_template = Template(""" public $type $name;\n""") + +send_template = Template(""" @Override + public int send(final $base_package.JVpp jvpp) { + return jvpp.$method_name($args); + }\n""") + + +def generate_dtos(func_list, base_package, dto_package): + """ Generates dto objects in a dedicated package """ + print "Generating DTOs" + + if not os.path.exists(dto_package): + raise Exception("%s folder is missing" % dto_package) + + for func in func_list: + camel_case_dto_name = util.underscore_to_camelcase_upper(func['name']) + camel_case_method_name = util.underscore_to_camelcase(func['name']) + dto_path = os.path.join(dto_package, camel_case_dto_name + ".java") + + if util.is_notification(func['name']) or util.is_ignored(func['name']): + # TODO handle notifications + continue + + fields = "" + for t in zip(func['types'], func['args']): + fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]], + name=util.underscore_to_camelcase(t[1])) + methods = "" + base_type = "" + if util.is_reply(camel_case_dto_name): + request_dto_name = get_request_name(camel_case_dto_name, func['name']) + if util.is_details(camel_case_dto_name): + # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api + base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name + "Dump") + generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, + camel_case_method_name, func) + else: + base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name) + else: + args = "" if fields is "" else "this" + methods = send_template.substitute(method_name=camel_case_method_name, + base_package=base_package, + args=args) + if util.is_dump(camel_case_dto_name): + base_type += "JVppDump" + else: + base_type += "JVppRequest" + + dto_file = open(dto_path, 'w') + dto_file.write(dto_template.substitute(docs='Generated from ' + str(func), + cls_name=camel_case_dto_name, + fields=fields, + methods=methods, + base_package=base_package, + base_type=base_type, + dto_package=dto_package)) + dto_file.flush() + dto_file.close() + + flush_dump_reply_dtos() + + +dump_dto_suffix = "ReplyDump" +dump_reply_artificial_dtos = {} + + +# Returns request name or special one from unconventional_naming_rep_req map +def get_request_name(camel_case_dto_name, func_name): + return util.underscore_to_camelcase_upper( + util.unconventional_naming_rep_req[func_name]) if func_name in util.unconventional_naming_rep_req \ + else util.remove_reply_suffix(camel_case_dto_name) + + +def flush_dump_reply_dtos(): + for dump_reply_artificial_dto in dump_reply_artificial_dtos.values(): + dto_path = os.path.join(dump_reply_artificial_dto['dto_package'], + dump_reply_artificial_dto['cls_name'] + ".java") + dto_file = open(dto_path, 'w') + dto_file.write(dto_template.substitute(docs=dump_reply_artificial_dto['docs'], + cls_name=dump_reply_artificial_dto['cls_name'], + fields=dump_reply_artificial_dto['fields'], + methods=dump_reply_artificial_dto['methods'], + base_package=dump_reply_artificial_dto['base_package'], + base_type=dump_reply_artificial_dto['base_type'], + dto_package=dump_reply_artificial_dto['dto_package'])) + dto_file.flush() + dto_file.close() + + +def generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, camel_case_method_name, + func): + base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % ( + base_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump", + base_package, dto_package, camel_case_dto_name) + fields = " public java.util.List<%s> %s = new java.util.ArrayList<>();" % (camel_case_dto_name, camel_case_method_name) + cls_name = camel_case_dto_name + dump_dto_suffix + + # In case of already existing artificial reply dump DTO, just update it + # Used for sub-dump dtos + if request_dto_name in dump_reply_artificial_dtos.keys(): + dump_reply_artificial_dtos[request_dto_name]['fields'] = \ + dump_reply_artificial_dtos[request_dto_name]['fields'] + '\n' + fields + else: + dump_reply_artificial_dtos[request_dto_name] = ({'docs': 'Dump reply wrapper generated from ' + str(func), + 'cls_name': cls_name, + 'fields': fields, + 'methods': "", + 'base_package': base_package, + 'base_type': base_type, + 'dto_package': dto_package, + }) diff --git a/vpp-api/java/jvpp/gen/jvpp_callback_facade_gen.py b/vpp-api/java/jvpp/gen/jvpp_callback_facade_gen.py new file mode 100644 index 00000000000..731bd894cee --- /dev/null +++ b/vpp-api/java/jvpp/gen/jvpp_callback_facade_gen.py @@ -0,0 +1,236 @@ +#!/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 + +import callback_gen +import dto_gen + +jvpp_ifc_template = Template(""" +package $base_package.$callback_facade_package; + +public interface CallbackJVpp extends java.lang.AutoCloseable { + + @Override + void close(); + + // TODO add send + +$methods +} +""") + +jvpp_impl_template = Template(""" +package $base_package.$callback_facade_package; + +public final class CallbackJVppFacade implements $base_package.$callback_facade_package.CallbackJVpp { + + private final $base_package.JVpp jvpp; + private final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> callbacks; + + public CallbackJVppFacade(final $base_package.JVpp jvpp, + java.util.Map<Integer, $base_package.$callback_package.JVppCallback> callbacks) { + if(jvpp == null) { + throw new java.lang.NullPointerException("jvpp is null"); + } + this.jvpp = jvpp; + this.callbacks = callbacks; + } + + @Override + public void close() { + } + + // TODO add send() + +$methods +} +""") + +method_template = Template( + """ void $name($base_package.$dto_package.$request request, $base_package.$callback_package.$callback callback);""") +method_impl_template = Template(""" public final void $name($base_package.$dto_package.$request request, $base_package.$callback_package.$callback callback) { + synchronized (callbacks) { + callbacks.put(jvpp.$name(request), callback); + } + } +""") + +no_arg_method_template = Template(""" void $name($base_package.$callback_package.$callback callback);""") +no_arg_method_impl_template = Template(""" public final void $name($base_package.$callback_package.$callback callback) { + synchronized (callbacks) { + callbacks.put(jvpp.$name(), callback); + } + } +""") + + +def generate_jvpp(func_list, base_package, dto_package, callback_package, callback_facade_package): + """ Generates callback facade """ + print "Generating JVpp callback facade" + + if os.path.exists(callback_facade_package): + util.remove_folder(callback_facade_package) + + os.mkdir(callback_facade_package) + + 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 + + # Strip suffix for dump calls + callback_type = get_request_name(camel_case_name_upper, func['name']) + callback_gen.callback_suffix + + if len(func['args']) == 0: + methods.append(no_arg_method_template.substitute(name=camel_case_name, + base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + callback=callback_type)) + methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name, + base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + callback=callback_type)) + else: + methods.append(method_template.substitute(name=camel_case_name, + request=camel_case_name_upper, + base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + callback=callback_type)) + methods_impl.append(method_impl_template.substitute(name=camel_case_name, + request=camel_case_name_upper, + base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + callback=callback_type)) + + join = os.path.join(callback_facade_package, "CallbackJVpp.java") + jvpp_file = open(join, 'w') + jvpp_file.write( + jvpp_ifc_template.substitute(methods="\n".join(methods), + base_package=base_package, + dto_package=dto_package, + callback_facade_package=callback_facade_package)) + jvpp_file.flush() + jvpp_file.close() + + jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacade.java"), 'w') + jvpp_file.write(jvpp_impl_template.substitute(methods="\n".join(methods_impl), + base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + callback_facade_package=callback_facade_package)) + jvpp_file.flush() + jvpp_file.close() + + generate_callback(func_list, base_package, dto_package, callback_package, callback_facade_package) + + +jvpp_facade_callback_template = Template(""" +package $base_package.$callback_facade_package; + +/** + * Async facade callback setting values to future objects + */ +public final class CallbackJVppFacadeCallback implements $base_package.$callback_package.JVppGlobalCallback { + + private final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requests; + + public CallbackJVppFacadeCallback(final java.util.Map<Integer, $base_package.$callback_package.JVppCallback> requestMap) { + this.requests = requestMap; + } + +$methods +} +""") + +jvpp_facade_callback_method_template = Template(""" + @Override + @SuppressWarnings("unchecked") + public void on$callback_dto($base_package.$dto_package.$callback_dto reply) { + + $base_package.$callback_package.$callback callback; + synchronized(requests) { + callback = ($base_package.$callback_package.$callback) requests.remove(reply.context); + } + + if(callback != null) { + callback.on$callback_dto(reply); + } + } +""") + + +def generate_callback(func_list, base_package, dto_package, callback_package, callback_facade_package): + callbacks = [] + for func in func_list: + + if util.is_notification(func['name']) or util.is_ignored(func['name']): + # TODO handle notifications + continue + + camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) + if not util.is_reply(camel_case_name_with_suffix): + continue + + callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + callback=util.remove_reply_suffix(camel_case_name_with_suffix) + callback_gen.callback_suffix, + callback_dto=camel_case_name_with_suffix)) + + jvpp_file = open(os.path.join(callback_facade_package, "CallbackJVppFacadeCallback.java"), 'w') + jvpp_file.write(jvpp_facade_callback_template.substitute(base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + methods="".join(callbacks), + callback_facade_package=callback_facade_package)) + jvpp_file.flush() + jvpp_file.close() + + +# Returns request name or special one from unconventional_naming_rep_req map +def get_request_name(camel_case_dto_name, func_name): + if func_name in reverse_dict(util.unconventional_naming_rep_req): + request_name = util.underscore_to_camelcase_upper(reverse_dict(util.unconventional_naming_rep_req)[func_name]) + else: + request_name = camel_case_dto_name + return remove_suffix(request_name) + + +def reverse_dict(map): + return dict((v, k) for k, v in map.iteritems()) + + +def remove_suffix(name): + if util.is_reply(name): + return util.remove_reply_suffix(name) + else: + if util.is_dump(name): + return util.remove_suffix(name, util.dump_suffix) + else: + return name diff --git a/vpp-api/java/jvpp/gen/jvpp_future_facade_gen.py b/vpp-api/java/jvpp/gen/jvpp_future_facade_gen.py new file mode 100644 index 00000000000..2aae5b8018a --- /dev/null +++ b/vpp-api/java/jvpp/gen/jvpp_future_facade_gen.py @@ -0,0 +1,184 @@ +#!/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 + +import dto_gen + +jvpp_facade_callback_template = Template(""" +package $base_package.$future_package; + +/** + * Async facade callback setting values to future objects + */ +public final class FutureJVppFacadeCallback implements $base_package.$callback_package.JVppGlobalCallback { + + private final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requests; + + public FutureJVppFacadeCallback(final java.util.Map<java.lang.Integer, java.util.concurrent.CompletableFuture<? extends $base_package.$dto_package.JVppReply<?>>> requestMap) { + this.requests = requestMap; + } + +$methods +} +""") + +jvpp_facade_callback_method_template = Template(""" + @Override + @SuppressWarnings("unchecked") + public void on$callback_dto($base_package.$dto_package.$callback_dto reply) { + final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture; + + synchronized(requests) { + completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.context); + } + + if(completableFuture != null) { + if(reply.retval < 0) { + completableFuture.completeExceptionally(new Exception("Invocation of " + $base_package.$dto_package.$callback_dto.class + + " failed with value " + reply.retval)); + } else { + completableFuture.complete(reply); + } + + synchronized(requests) { + requests.remove(reply.context); + } + } + } +""") + +# TODO reuse common parts with generic method callback +jvpp_facade_control_ping_method_template = Template(""" + @Override + @SuppressWarnings("unchecked") + public void on$callback_dto($base_package.$dto_package.$callback_dto reply) { + final java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>> completableFuture; + + synchronized(requests) { + completableFuture = (java.util.concurrent.CompletableFuture<$base_package.$dto_package.JVppReply<?>>) requests.get(reply.context); + } + + if(completableFuture != null) { + // Finish dump call + if (completableFuture instanceof $base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) { + completableFuture.complete((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getReplyDump()); + // Remove future mapped to dump call context id + synchronized(requests) { + requests.remove((($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture) completableFuture).getContextId()); + } + } else { + if(reply.retval < 0) { + completableFuture.completeExceptionally(new Exception("Invocation of " + $base_package.$dto_package.$callback_dto.class + + " failed with value " + reply.retval)); + } else { + completableFuture.complete(reply); + } + } + + synchronized(requests) { + requests.remove(reply.context); + } + } + } +""") + +jvpp_facade_details_callback_method_template = Template(""" + @Override + @SuppressWarnings("unchecked") + public void on$callback_dto($base_package.$dto_package.$callback_dto reply) { + final FutureJVppFacade.CompletableDumpFuture<$base_package.$dto_package.$callback_dto_reply_dump> completableFuture; + + synchronized(requests) { + completableFuture = ($base_package.$future_package.FutureJVppFacade.CompletableDumpFuture<$base_package.$dto_package.$callback_dto_reply_dump>) requests.get(reply.context); + } + + if(completableFuture != null) { + $base_package.$dto_package.$callback_dto_reply_dump replyDump = completableFuture.getReplyDump(); + if(replyDump == null) { + replyDump = new $base_package.$dto_package.$callback_dto_reply_dump(); + completableFuture.setReplyDump(replyDump); + } + + replyDump.$callback_dto_field.add(reply); + } + } +""") + + +def generate_jvpp(func_list, base_package, dto_package, callback_package, future_facade_package): + """ Generates JVpp interface and JNI implementation """ + print "Generating JVpp future facade" + + if not os.path.exists(future_facade_package): + raise Exception("%s folder is missing" % future_facade_package) + + callbacks = [] + for func in func_list: + + if util.is_notification(func['name']) or util.is_ignored(func['name']): + # TODO handle notifications + continue + + camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name']) + if not util.is_reply(camel_case_name_with_suffix): + continue + + if util.is_details(camel_case_name_with_suffix): + camel_case_method_name = util.underscore_to_camelcase(func['name']) + camel_case_request_name = get_standard_dump_reply_name(util.underscore_to_camelcase_upper(func['name']), + func['name']) + callbacks.append(jvpp_facade_details_callback_method_template.substitute(base_package=base_package, + dto_package=dto_package, + callback_dto=camel_case_name_with_suffix, + callback_dto_field=camel_case_method_name, + callback_dto_reply_dump=camel_case_request_name + dto_gen.dump_dto_suffix, + future_package=future_facade_package)) + else: + if util.is_control_ping(camel_case_name_with_suffix): + callbacks.append(jvpp_facade_control_ping_method_template.substitute(base_package=base_package, + dto_package=dto_package, + callback_dto=camel_case_name_with_suffix, + future_package=future_facade_package)) + else: + callbacks.append(jvpp_facade_callback_method_template.substitute(base_package=base_package, + dto_package=dto_package, + callback_dto=camel_case_name_with_suffix)) + + jvpp_file = open(os.path.join(future_facade_package, "FutureJVppFacadeCallback.java"), 'w') + jvpp_file.write(jvpp_facade_callback_template.substitute(base_package=base_package, + dto_package=dto_package, + callback_package=callback_package, + methods="".join(callbacks), + future_package=future_facade_package)) + jvpp_file.flush() + jvpp_file.close() + + +# Returns request name or special one from unconventional_naming_rep_req map +def get_standard_dump_reply_name(camel_case_dto_name, func_name): + # FIXME this is a hotfix for sub-details callbacks + # FIXME also for L2FibTableEntry + # It's all because unclear mapping between + # request -> reply, + # dump -> reply, details, + # notification_start -> reply, notifications + + # vpe.api needs to be "standardized" so we can parse the information and create maps before generating java code + suffix = func_name.split("_")[-1] + return util.underscore_to_camelcase_upper( + util.unconventional_naming_rep_req[func_name]) + util.underscore_to_camelcase_upper(suffix) if func_name in util.unconventional_naming_rep_req \ + else camel_case_dto_name 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() diff --git a/vpp-api/java/jvpp/gen/util.py b/vpp-api/java/jvpp/gen/util.py new file mode 100644 index 00000000000..17dc2ed90a9 --- /dev/null +++ b/vpp-api/java/jvpp/gen/util.py @@ -0,0 +1,173 @@ +#!/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 +from os import removedirs + + +def underscore_to_camelcase(name): + name = name.title().replace("_", "") + return name[0].lower() + name[1:] + + +def underscore_to_camelcase_upper(name): + name = name.title().replace("_", "") + return name[0].upper() + name[1:] + + +def remove_folder(folder): + """ Remove folder with all its files """ + for root, dirs, files in os.walk(folder, topdown=False): + for name in files: + os.remove(os.path.join(root, name)) + removedirs(folder) + + +reply_suffixes = ("reply", "details", "l2fibtableentry") + + +def is_reply(name): + return name.lower().endswith(reply_suffixes) + + +def is_details(name): + return name.lower().endswith(reply_suffixes[1]) or name.lower().endswith(reply_suffixes[2]) + +dump_suffix = "dump" + + +def is_dump(name): + return name.lower().endswith(dump_suffix) + + +def get_reply_suffix(name): + for reply_suffix in reply_suffixes: + if name.lower().endswith(reply_suffix): + if reply_suffix == reply_suffixes[2]: + # FIXME workaround for l2_fib_table_entry + return 'entry' + else: + return reply_suffix + +# http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html +jni_2_java_type_mapping = {'jbyte': 'byte', + 'jbyteArray': 'byte[]', + 'jchar': 'char', + 'jcharArray': 'char[]', + 'jshort': 'short', + 'jshortArray': 'short[]', + 'jint': 'int', + 'jintArray': 'int[]', + 'jlong': 'long', + 'jlongArray': 'long[]', + 'jdouble': 'double', + 'jdoubleArray': 'double[]', + 'jfloat': 'float', + 'jfloatArray': 'float[]', + 'void': 'void', + 'jstring': 'java.lang.String', + 'jobject': 'java.lang.Object', + 'jobjectArray': 'java.lang.Object[]' + } + +# https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html#type_signatures +jni_2_signature_mapping = {'jbyte': 'B', + 'jbyteArray': '[B', + 'jchar': 'C', + 'jcharArray': '[C', + 'jshort': 'S', + 'jshortArray': '[S', + 'jint': 'I', + 'jintArray': '[I', + 'jlong': 'J', + 'jlongArray': '[J', + 'jdouble': 'D', + 'jdoubleArray': '[D', + 'jfloat': 'F', + 'jfloatArray': '[F' + } + +# https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#Get_type_Field_routines +jni_field_accessors = { + 'jbyte': 'ByteField', + 'jbyteArray': 'ObjectField', + 'jchar': 'CharField', + 'jcharArray': 'ObjectField', + 'jshort': 'ShortField', + 'jshortArray': 'ObjectField', + 'jint': 'IntField', + 'jintArray': 'ObjectField', + 'jlong': 'LongField', + 'jlongArray': 'ObjectField', + 'jdouble': 'DoubleField', + 'jdoubleArray': 'ObjectField', + 'jfloat': 'FloatField', + 'jfloatArray': 'ObjectField' +} + +# TODO watch out for unsigned types +# http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html +vpp_2_jni_type_mapping = {'u8': 'jbyte', # fixme + 'i8': 'jbyte', + 'u16': 'jchar', + 'i16': 'jshort', + 'u32': 'jint', # fixme + 'i32': 'jint', + 'u64': 'jlong', # fixme + 'i64': 'jlong', + 'f64': 'jdouble' + } + +# vpe.api calls that do not follow naming conventions and have to be handled exceptionally when finding reply -> request mapping +# FIXME in vpe.api +unconventional_naming_rep_req = { + 'cli_reply': 'cli_request', + 'vnet_summary_stats_reply': 'vnet_get_summary_stats', + # This below is actually a sub-details callback. We cannot derive the mapping of dump request + # belonging to this sub-details from naming conventions. We need special mapping + 'bridge_domain_sw_if_details': 'bridge_domain', + # This is standard dump call + details reply. However it's not called details but entry + 'l2_fib_table_entry': 'l2_fib_table' + } + +# +# FIXME no convention in the naming of events (notifications) in vpe.api +notifications_message_suffixes = ("event", "counters") +notification_messages = ["from_netconf_client", "from_netconf_server", "to_netconf_client", "to_netconf_server"] + +# messages that must be ignored. These messages are INSUFFICIENTLY marked as disabled in vpe.api +# FIXME +ignored_messages = ["is_address_reachable"] + + +def is_notification(param): + return param.lower().endswith(notifications_message_suffixes) or param.lower() in notification_messages + + +def is_ignored(param): + return param.lower() in ignored_messages + + +def remove_reply_suffix(camel_case_name_with_suffix): + return remove_suffix(camel_case_name_with_suffix, get_reply_suffix(camel_case_name_with_suffix)) + + +def remove_suffix(camel_case_name_with_suffix, suffix): + suffix_length = len(suffix) + return camel_case_name_with_suffix[:-suffix_length] if suffix_length != 0 else camel_case_name_with_suffix + + +def is_control_ping(camel_case_name_with_suffix): + return "controlping" in camel_case_name_with_suffix.lower() |