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