Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2016 Cisco and/or its affiliates. |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at: |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # l |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | import argparse |
| 18 | import importlib |
| 19 | import sys |
Ed Warnicke | adeb749 | 2016-07-11 10:29:41 -0700 | [diff] [blame] | 20 | import os |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 21 | |
Ed Warnicke | adeb749 | 2016-07-11 10:29:41 -0700 | [diff] [blame] | 22 | from jvppgen import callback_gen |
| 23 | from jvppgen import notification_gen |
| 24 | from jvppgen import dto_gen |
| 25 | from jvppgen import jvpp_callback_facade_gen |
| 26 | from jvppgen import jvpp_future_facade_gen |
| 27 | from jvppgen import jvpp_impl_gen |
| 28 | from jvppgen import jvpp_c_gen |
| 29 | from jvppgen import util |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 30 | |
| 31 | # Invocation: |
| 32 | # ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/org/openvpp/jvpp && cd java/org/openvpp/jvpp |
| 33 | # ~/Projects/vpp/vpp-api/jvpp/gen/java/org/openvpp/jvpp$ ../../../../jvpp_gen.py -idefs_api_vpp_papi.py |
| 34 | # |
| 35 | # Compilation: |
| 36 | # ~/Projects/vpp/vpp-api/jvpp/gen/java/org/openvpp/jvpp$ javac *.java dto/*.java callback/*.java |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 37 | # |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 38 | # where |
| 39 | # defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen) |
Ed Warnicke | adeb749 | 2016-07-11 10:29:41 -0700 | [diff] [blame] | 40 | from jvppgen.util import vpp_2_jni_type_mapping |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 41 | |
| 42 | parser = argparse.ArgumentParser(description='VPP Java API generator') |
| 43 | parser.add_argument('-i', action="store", dest="inputfile") |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 44 | parser.add_argument('--plugin_name', action="store", dest="plugin_name") |
| 45 | parser.add_argument('--control_ping_class', action="store", dest="control_ping_class", default="ControlPing") |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 46 | args = parser.parse_args() |
| 47 | |
| 48 | sys.path.append(".") |
| 49 | |
Ed Warnicke | adeb749 | 2016-07-11 10:29:41 -0700 | [diff] [blame] | 50 | print "args.inputfile %s" % args.inputfile |
| 51 | importdir = os.path.dirname(args.inputfile) |
| 52 | print "importdir %s" % importdir |
| 53 | inputfile = os.path.basename(args.inputfile) |
| 54 | inputfile = inputfile.replace('.py', '') |
| 55 | print "inputfile %s" % inputfile |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 56 | plugin_name = args.plugin_name |
| 57 | print "plugin_name %s" % plugin_name |
| 58 | control_ping_class = args.control_ping_class |
| 59 | print "control_ping_class %s" % control_ping_class |
Ed Warnicke | adeb749 | 2016-07-11 10:29:41 -0700 | [diff] [blame] | 60 | sys.path.append(importdir) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 61 | cfg = importlib.import_module(inputfile, package=None) |
| 62 | |
| 63 | |
| 64 | # FIXME: functions unsupported due to problems with vpe.api |
| 65 | def is_supported(f_name): |
| 66 | return f_name not in {'vnet_ip4_fib_counters', 'vnet_ip6_fib_counters'} |
| 67 | |
| 68 | |
| 69 | def is_request_field(field_name): |
| 70 | return field_name not in {'_vl_msg_id', 'client_index', 'context'} |
| 71 | |
| 72 | |
| 73 | def is_response_field(field_name): |
| 74 | return field_name not in {'_vl_msg_id'} |
| 75 | |
| 76 | |
| 77 | def get_args(t, filter): |
| 78 | arg_list = [] |
| 79 | for i in t: |
| 80 | if not filter(i[1]): |
| 81 | continue |
| 82 | arg_list.append(i[1]) |
| 83 | return arg_list |
| 84 | |
| 85 | |
| 86 | def get_types(t, filter): |
| 87 | types_list = [] |
| 88 | c_types_list = [] |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 89 | lengths_list = [] |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 90 | for i in t: |
| 91 | if not filter(i[1]): |
| 92 | continue |
| 93 | if len(i) is 3: # array type |
| 94 | types_list.append(vpp_2_jni_type_mapping[i[0]] + 'Array') |
| 95 | c_types_list.append(i[0] + '[]') |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 96 | lengths_list.append((i[2], False)) |
| 97 | elif len(i) is 4: # variable length array type |
| 98 | types_list.append(vpp_2_jni_type_mapping[i[0]] + 'Array') |
| 99 | c_types_list.append(i[0] + '[]') |
| 100 | lengths_list.append((i[3], True)) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 101 | else: # primitive type |
| 102 | types_list.append(vpp_2_jni_type_mapping[i[0]]) |
| 103 | c_types_list.append(i[0]) |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 104 | lengths_list.append((0, False)) |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 105 | return types_list, c_types_list, lengths_list |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 106 | |
| 107 | |
| 108 | def get_definitions(): |
| 109 | # Pass 1 |
| 110 | func_list = [] |
| 111 | func_name = {} |
| 112 | for a in cfg.vppapidef: |
| 113 | if not is_supported(a[0]): |
| 114 | continue |
| 115 | |
| 116 | java_name = util.underscore_to_camelcase(a[0]) |
| 117 | |
| 118 | # For replies include all the arguments except message_id |
| 119 | if util.is_reply(java_name): |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 120 | types, c_types, lengths = get_types(a[1:], is_response_field) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 121 | func_name[a[0]] = dict( |
| 122 | [('name', a[0]), ('java_name', java_name), |
| 123 | ('args', get_args(a[1:], is_response_field)), ('full_args', get_args(a[1:], lambda x: True)), |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 124 | ('types', types), ('c_types', c_types), ('lengths', lengths)]) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 125 | # For requests skip message_id, client_id and context |
| 126 | else: |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 127 | types, c_types, lengths = get_types(a[1:], is_request_field) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 128 | func_name[a[0]] = dict( |
| 129 | [('name', a[0]), ('java_name', java_name), |
| 130 | ('args', get_args(a[1:], is_request_field)), ('full_args', get_args(a[1:], lambda x: True)), |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 131 | ('types', types), ('c_types', c_types), ('lengths', lengths)]) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 132 | |
| 133 | # Indexed by name |
| 134 | func_list.append(func_name[a[0]]) |
| 135 | return func_list, func_name |
| 136 | |
| 137 | |
| 138 | func_list, func_name = get_definitions() |
| 139 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 140 | base_package = 'org.openvpp.jvpp' |
| 141 | plugin_package = base_package + '.' + plugin_name |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 142 | dto_package = 'dto' |
| 143 | callback_package = 'callback' |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 144 | notification_package = 'notification' |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 145 | future_package = 'future' |
| 146 | # TODO find better package name |
| 147 | callback_facade_package = 'callfacade' |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 148 | control_ping_class_fqn = "%s.%s.%s" % (plugin_package, dto_package, control_ping_class) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 149 | |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 150 | dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfile) |
| 151 | jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, control_ping_class_fqn, dto_package, args.inputfile) |
| 152 | callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfile) |
| 153 | notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfile) |
| 154 | jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfile) |
| 155 | jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, future_package, args.inputfile) |
| 156 | jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, callback_facade_package, args.inputfile) |