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 |
Marek Gradzki | 4746a5d | 2017-01-27 08:57:40 +0100 | [diff] [blame] | 9 | # |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 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 | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 21 | import json |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 22 | |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 23 | from jvppgen import types_gen |
Ed Warnicke | adeb749 | 2016-07-11 10:29:41 -0700 | [diff] [blame] | 24 | from jvppgen import callback_gen |
| 25 | from jvppgen import notification_gen |
| 26 | from jvppgen import dto_gen |
| 27 | from jvppgen import jvpp_callback_facade_gen |
| 28 | from jvppgen import jvpp_future_facade_gen |
| 29 | from jvppgen import jvpp_impl_gen |
| 30 | from jvppgen import jvpp_c_gen |
| 31 | from jvppgen import util |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 32 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 33 | blacklist = [ "memclnt.api", "flowperpkt.api" ] |
| 34 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 35 | # Invocation: |
Marek Gradzki | e85581c | 2016-09-28 10:12:04 +0200 | [diff] [blame] | 36 | # ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp |
| 37 | # ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ ../../../../jvpp_gen.py -idefs_api_vpp_papi.py |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 38 | # |
| 39 | # Compilation: |
Marek Gradzki | e85581c | 2016-09-28 10:12:04 +0200 | [diff] [blame] | 40 | # ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ javac *.java dto/*.java callback/*.java |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 41 | # |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 42 | # where |
| 43 | # defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 44 | |
| 45 | parser = argparse.ArgumentParser(description='VPP Java API generator') |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 46 | parser.add_argument('-i', action="store", dest="inputfiles", nargs='+') |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 47 | parser.add_argument('--plugin_name', action="store", dest="plugin_name") |
Damjan Marion | cb034b9 | 2016-12-28 18:38:59 +0100 | [diff] [blame] | 48 | parser.add_argument('--root_dir', action="store", dest="root_dir") |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 49 | args = parser.parse_args() |
| 50 | |
| 51 | sys.path.append(".") |
Damjan Marion | cb034b9 | 2016-12-28 18:38:59 +0100 | [diff] [blame] | 52 | cwd = os.getcwd() |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 53 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 54 | print "Generating Java API for %s" % args.inputfiles |
| 55 | print "inputfiles %s" % args.inputfiles |
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 |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 58 | |
| 59 | cfg = {} |
| 60 | |
Damjan Marion | cb034b9 | 2016-12-28 18:38:59 +0100 | [diff] [blame] | 61 | base_package = 'io.fd.vpp.jvpp' |
| 62 | plugin_package = base_package + '.' + plugin_name |
| 63 | root_dir = os.path.abspath(args.root_dir) |
| 64 | print "root_dir %s" % root_dir |
| 65 | work_dir = root_dir + "/target/" + plugin_package.replace(".","/") |
| 66 | |
| 67 | try: |
| 68 | os.makedirs(work_dir) |
| 69 | except OSError: |
| 70 | if not os.path.isdir(work_dir): |
| 71 | raise |
| 72 | |
| 73 | os.chdir(work_dir) |
| 74 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 75 | for inputfile in args.inputfiles: |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 76 | if any(substring in inputfile for substring in blacklist): |
| 77 | print "WARNING: Imput file %s blacklisted" % inputfile |
| 78 | continue |
Damjan Marion | cb034b9 | 2016-12-28 18:38:59 +0100 | [diff] [blame] | 79 | _cfg = json.load(open(cwd + "/" + inputfile, 'r')) |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 80 | if 'types' in cfg: |
| 81 | cfg['types'].extend(_cfg['types']) |
| 82 | else: |
| 83 | cfg['types'] = _cfg['types'] |
| 84 | if 'messages' in cfg: |
| 85 | cfg['messages'].extend(_cfg['messages']) |
| 86 | else: |
| 87 | cfg['messages'] = _cfg['messages'] |
| 88 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 89 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 90 | def is_request_field(field_name): |
| 91 | return field_name not in {'_vl_msg_id', 'client_index', 'context'} |
| 92 | |
| 93 | |
| 94 | def is_response_field(field_name): |
| 95 | return field_name not in {'_vl_msg_id'} |
| 96 | |
| 97 | |
| 98 | def get_args(t, filter): |
Marek Gradzki | ed76d5f | 2017-05-12 18:24:34 +0200 | [diff] [blame] | 99 | arg_names = [] |
| 100 | arg_types = [] |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 101 | for i in t: |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 102 | if is_crc(i): |
| 103 | continue |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 104 | if not filter(i[1]): |
| 105 | continue |
Marek Gradzki | ed76d5f | 2017-05-12 18:24:34 +0200 | [diff] [blame] | 106 | arg_types.append(i[0]) |
| 107 | arg_names.append(i[1]) |
| 108 | return arg_types, arg_names |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def get_types(t, filter): |
| 112 | types_list = [] |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 113 | lengths_list = [] |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 114 | crc = None |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 115 | for i in t: |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 116 | if is_crc(i): |
| 117 | crc = ('crc', i['crc'][2:]) |
| 118 | continue |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 119 | if not filter(i[1]): |
| 120 | continue |
| 121 | if len(i) is 3: # array type |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 122 | types_list.append(i[0] + '[]') |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 123 | lengths_list.append((i[2], False)) |
| 124 | elif len(i) is 4: # variable length array type |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 125 | types_list.append(i[0] + '[]') |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 126 | lengths_list.append((i[3], True)) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 127 | else: # primitive type |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 128 | types_list.append(i[0]) |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 129 | lengths_list.append((0, False)) |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 130 | return types_list, lengths_list, crc |
| 131 | |
| 132 | |
| 133 | def is_crc(arg): |
| 134 | """ Check whether the argument inside message definition is just crc """ |
| 135 | return 'crc' in arg |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 136 | |
| 137 | |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 138 | def get_definitions(defs): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 139 | # Pass 1 |
| 140 | func_list = [] |
| 141 | func_name = {} |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 142 | for a in defs: |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 143 | java_name = util.underscore_to_camelcase(a[0]) |
| 144 | |
| 145 | # For replies include all the arguments except message_id |
| 146 | if util.is_reply(java_name): |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 147 | types, lengths, crc = get_types(a[1:], is_response_field) |
Marek Gradzki | ed76d5f | 2017-05-12 18:24:34 +0200 | [diff] [blame] | 148 | args = get_args(a[1:], is_response_field) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 149 | func_name[a[0]] = dict( |
| 150 | [('name', a[0]), ('java_name', java_name), |
Marek Gradzki | ed76d5f | 2017-05-12 18:24:34 +0200 | [diff] [blame] | 151 | ('args', args[1]), ('arg_types', args[0]), |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 152 | ('types', types), ('lengths', lengths), crc]) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 153 | # For requests skip message_id, client_id and context |
| 154 | else: |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 155 | types, lengths, crc = get_types(a[1:], is_request_field) |
Marek Gradzki | ed76d5f | 2017-05-12 18:24:34 +0200 | [diff] [blame] | 156 | args = get_args(a[1:], is_request_field) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 157 | func_name[a[0]] = dict( |
| 158 | [('name', a[0]), ('java_name', java_name), |
Marek Gradzki | ed76d5f | 2017-05-12 18:24:34 +0200 | [diff] [blame] | 159 | ('args', args[1]), ('arg_types', args[0]), |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 160 | ('types', types), ('lengths', lengths), crc]) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 161 | |
| 162 | # Indexed by name |
| 163 | func_list.append(func_name[a[0]]) |
| 164 | return func_list, func_name |
| 165 | |
| 166 | |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 167 | types_package = 'types' |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 168 | dto_package = 'dto' |
| 169 | callback_package = 'callback' |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 170 | notification_package = 'notification' |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 171 | future_package = 'future' |
| 172 | # TODO find better package name |
| 173 | callback_facade_package = 'callfacade' |
| 174 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 175 | types_list, types_name = get_definitions(cfg['types']) |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 176 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 177 | types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles) |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 178 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 179 | func_list, func_name = get_definitions(cfg['messages']) |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 180 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 181 | dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles) |
| 182 | jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles) |
| 183 | callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfiles) |
| 184 | notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfiles) |
Damjan Marion | cb034b9 | 2016-12-28 18:38:59 +0100 | [diff] [blame] | 185 | jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles, root_dir) |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 186 | jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, future_package, args.inputfiles) |
| 187 | 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.inputfiles) |
| 188 | |
| 189 | print "Java API for %s generated successfully" % args.inputfiles |