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 | 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 | |
| 33 | # Invocation: |
Marek Gradzki | e85581c | 2016-09-28 10:12:04 +0200 | [diff] [blame] | 34 | # ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp |
| 35 | # ~/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] | 36 | # |
| 37 | # Compilation: |
Marek Gradzki | e85581c | 2016-09-28 10:12:04 +0200 | [diff] [blame] | 38 | # ~/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] | 39 | # |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 40 | # where |
| 41 | # 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] | 42 | |
| 43 | parser = argparse.ArgumentParser(description='VPP Java API generator') |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 44 | parser.add_argument('-i', action="store", dest="inputfiles", nargs='+') |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 45 | parser.add_argument('--plugin_name', action="store", dest="plugin_name") |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 46 | args = parser.parse_args() |
| 47 | |
| 48 | sys.path.append(".") |
| 49 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 50 | print "Generating Java API for %s" % args.inputfiles |
| 51 | print "inputfiles %s" % args.inputfiles |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 52 | plugin_name = args.plugin_name |
| 53 | print "plugin_name %s" % plugin_name |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 54 | |
| 55 | cfg = {} |
| 56 | |
| 57 | for inputfile in args.inputfiles: |
| 58 | _cfg = json.load(open(inputfile, 'r')) |
| 59 | if 'types' in cfg: |
| 60 | cfg['types'].extend(_cfg['types']) |
| 61 | else: |
| 62 | cfg['types'] = _cfg['types'] |
| 63 | if 'messages' in cfg: |
| 64 | cfg['messages'].extend(_cfg['messages']) |
| 65 | else: |
| 66 | cfg['messages'] = _cfg['messages'] |
| 67 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 68 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 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: |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 80 | if is_crc(i): |
| 81 | continue |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 82 | if not filter(i[1]): |
| 83 | continue |
| 84 | arg_list.append(i[1]) |
| 85 | return arg_list |
| 86 | |
| 87 | |
| 88 | def get_types(t, filter): |
| 89 | types_list = [] |
Marek Gradzki | c4cb44c | 2016-05-24 13:32:26 +0200 | [diff] [blame] | 90 | lengths_list = [] |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 91 | crc = None |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 92 | for i in t: |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 93 | if is_crc(i): |
| 94 | crc = ('crc', i['crc'][2:]) |
| 95 | continue |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 96 | if not filter(i[1]): |
| 97 | continue |
| 98 | if len(i) is 3: # array type |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 99 | types_list.append(i[0] + '[]') |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 100 | lengths_list.append((i[2], False)) |
| 101 | elif len(i) is 4: # variable length array type |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 102 | types_list.append(i[0] + '[]') |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 103 | lengths_list.append((i[3], True)) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 104 | else: # primitive type |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 105 | types_list.append(i[0]) |
Marek Gradzki | fa42e25 | 2016-06-15 16:38:33 +0200 | [diff] [blame] | 106 | lengths_list.append((0, False)) |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 107 | return types_list, lengths_list, crc |
| 108 | |
| 109 | |
| 110 | def is_crc(arg): |
| 111 | """ Check whether the argument inside message definition is just crc """ |
| 112 | return 'crc' in arg |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 113 | |
| 114 | |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 115 | def get_definitions(defs): |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 116 | # Pass 1 |
| 117 | func_list = [] |
| 118 | func_name = {} |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 119 | for a in defs: |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 120 | java_name = util.underscore_to_camelcase(a[0]) |
| 121 | |
| 122 | # For replies include all the arguments except message_id |
| 123 | if util.is_reply(java_name): |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 124 | types, lengths, crc = get_types(a[1:], is_response_field) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 125 | func_name[a[0]] = dict( |
| 126 | [('name', a[0]), ('java_name', java_name), |
| 127 | ('args', get_args(a[1:], is_response_field)), ('full_args', get_args(a[1:], lambda x: True)), |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 128 | ('types', types), ('lengths', lengths), crc]) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 129 | # For requests skip message_id, client_id and context |
| 130 | else: |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 131 | types, lengths, crc = get_types(a[1:], is_request_field) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 132 | func_name[a[0]] = dict( |
| 133 | [('name', a[0]), ('java_name', java_name), |
| 134 | ('args', get_args(a[1:], is_request_field)), ('full_args', get_args(a[1:], lambda x: True)), |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 135 | ('types', types), ('lengths', lengths), crc]) |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 136 | |
| 137 | # Indexed by name |
| 138 | func_list.append(func_name[a[0]]) |
| 139 | return func_list, func_name |
| 140 | |
| 141 | |
Marek Gradzki | e85581c | 2016-09-28 10:12:04 +0200 | [diff] [blame] | 142 | base_package = 'io.fd.vpp.jvpp' |
Marek Gradzki | 66ea26b | 2016-07-26 15:28:22 +0200 | [diff] [blame] | 143 | plugin_package = base_package + '.' + plugin_name |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 144 | types_package = 'types' |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 145 | dto_package = 'dto' |
| 146 | callback_package = 'callback' |
Maros Marsalek | 7becd08 | 2016-05-31 17:45:16 +0200 | [diff] [blame] | 147 | notification_package = 'notification' |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 148 | future_package = 'future' |
| 149 | # TODO find better package name |
| 150 | callback_facade_package = 'callfacade' |
| 151 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 152 | types_list, types_name = get_definitions(cfg['types']) |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 153 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 154 | types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles) |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 155 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 156 | func_list, func_name = get_definitions(cfg['messages']) |
Marek Gradzki | 81c7dfc | 2016-09-29 13:22:35 +0200 | [diff] [blame] | 157 | |
Marek Gradzki | 2fee4c8 | 2016-11-25 08:38:29 +0100 | [diff] [blame] | 158 | dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles) |
| 159 | jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles) |
| 160 | callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfiles) |
| 161 | notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfiles) |
| 162 | jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles) |
| 163 | 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) |
| 164 | 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) |
| 165 | |
| 166 | print "Java API for %s generated successfully" % args.inputfiles |