blob: e2ff2adcf14433d48961c4ea3caf985c751ec76b [file] [log] [blame]
Marek Gradzkid85036f2016-04-26 12:09:05 +02001#!/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
17import argparse
18import importlib
19import sys
20
21import callback_gen
22import dto_gen
23import jvpp_callback_facade_gen
24import jvpp_future_facade_gen
25import jvpp_impl_gen
26import jvpp_c_gen
27import util
28
29# Invocation:
30# ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/org/openvpp/jvpp && cd java/org/openvpp/jvpp
31# ~/Projects/vpp/vpp-api/jvpp/gen/java/org/openvpp/jvpp$ ../../../../jvpp_gen.py -idefs_api_vpp_papi.py
32#
33# Compilation:
34# ~/Projects/vpp/vpp-api/jvpp/gen/java/org/openvpp/jvpp$ javac *.java dto/*.java callback/*.java
35#
36# where
37# defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
38from util import vpp_2_jni_type_mapping
39
40parser = argparse.ArgumentParser(description='VPP Java API generator')
41parser.add_argument('-i', action="store", dest="inputfile")
42args = parser.parse_args()
43
44sys.path.append(".")
45
46inputfile = args.inputfile.replace('.py', '')
47cfg = importlib.import_module(inputfile, package=None)
48
49
50# FIXME: functions unsupported due to problems with vpe.api
51def is_supported(f_name):
52 return f_name not in {'vnet_ip4_fib_counters', 'vnet_ip6_fib_counters'}
53
54
55def is_request_field(field_name):
56 return field_name not in {'_vl_msg_id', 'client_index', 'context'}
57
58
59def is_response_field(field_name):
60 return field_name not in {'_vl_msg_id'}
61
62
63def get_args(t, filter):
64 arg_list = []
65 for i in t:
66 if not filter(i[1]):
67 continue
68 arg_list.append(i[1])
69 return arg_list
70
71
72def get_types(t, filter):
73 types_list = []
74 c_types_list = []
Marek Gradzkic4cb44c2016-05-24 13:32:26 +020075 lengths_list = []
Marek Gradzkid85036f2016-04-26 12:09:05 +020076 for i in t:
77 if not filter(i[1]):
78 continue
79 if len(i) is 3: # array type
80 types_list.append(vpp_2_jni_type_mapping[i[0]] + 'Array')
81 c_types_list.append(i[0] + '[]')
Marek Gradzkic4cb44c2016-05-24 13:32:26 +020082 lengths_list.append(i[2])
Marek Gradzkid85036f2016-04-26 12:09:05 +020083 else: # primitive type
84 types_list.append(vpp_2_jni_type_mapping[i[0]])
85 c_types_list.append(i[0])
Marek Gradzkic4cb44c2016-05-24 13:32:26 +020086 lengths_list.append(0)
87 return types_list, c_types_list, lengths_list
Marek Gradzkid85036f2016-04-26 12:09:05 +020088
89
90def get_definitions():
91 # Pass 1
92 func_list = []
93 func_name = {}
94 for a in cfg.vppapidef:
95 if not is_supported(a[0]):
96 continue
97
98 java_name = util.underscore_to_camelcase(a[0])
99
100 # For replies include all the arguments except message_id
101 if util.is_reply(java_name):
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200102 types, c_types, lengths = get_types(a[1:], is_response_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200103 func_name[a[0]] = dict(
104 [('name', a[0]), ('java_name', java_name),
105 ('args', get_args(a[1:], is_response_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200106 ('types', types), ('c_types', c_types), ('lengths', lengths)])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200107 # For requests skip message_id, client_id and context
108 else:
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200109 types, c_types, lengths = get_types(a[1:], is_request_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200110 func_name[a[0]] = dict(
111 [('name', a[0]), ('java_name', java_name),
112 ('args', get_args(a[1:], is_request_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200113 ('types', types), ('c_types', c_types), ('lengths', lengths)])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200114
115 # Indexed by name
116 func_list.append(func_name[a[0]])
117 return func_list, func_name
118
119
120func_list, func_name = get_definitions()
121
122base_package = 'org.openvpp.jvpp'
123dto_package = 'dto'
124callback_package = 'callback'
125future_package = 'future'
126# TODO find better package name
127callback_facade_package = 'callfacade'
128
129dto_gen.generate_dtos(func_list, base_package, dto_package, args.inputfile)
130jvpp_impl_gen.generate_jvpp(func_list, base_package, dto_package, args.inputfile)
131callback_gen.generate_callbacks(func_list, base_package, callback_package, dto_package, args.inputfile)
132jvpp_c_gen.generate_jvpp(func_list, args.inputfile)
133jvpp_future_facade_gen.generate_jvpp(func_list, base_package, dto_package, callback_package, future_package, args.inputfile)
134jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, dto_package, callback_package, callback_facade_package, args.inputfile)