6f531defc9a4671a0411f4aa2b8e537aaff20b5d
[vpp.git] / vpp-api / java / jvpp / gen / jvpp_gen.py
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
20 import os
21
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
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
37 #
38 # where
39 # defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
40 from jvppgen.util import vpp_2_jni_type_mapping
41
42 parser = argparse.ArgumentParser(description='VPP Java API generator')
43 parser.add_argument('-i', action="store", dest="inputfile")
44 parser.add_argument('--base_package', action="store", dest="base_package", default='org.openvpp.jvpp')
45 args = parser.parse_args()
46
47 sys.path.append(".")
48
49 print "args.inputfile %s" % args.inputfile
50 importdir = os.path.dirname(args.inputfile)
51 print "importdir %s" % importdir
52 inputfile = os.path.basename(args.inputfile)
53 inputfile = inputfile.replace('.py', '')
54 print "inputfile %s" % inputfile
55 base_package = args.base_package
56 sys.path.append(importdir)
57 cfg = importlib.import_module(inputfile, package=None)
58
59
60 # FIXME: functions unsupported due to problems with vpe.api
61 def is_supported(f_name):
62     return f_name not in {'vnet_ip4_fib_counters', 'vnet_ip6_fib_counters'}
63
64
65 def is_request_field(field_name):
66     return field_name not in {'_vl_msg_id', 'client_index', 'context'}
67
68
69 def is_response_field(field_name):
70     return field_name not in {'_vl_msg_id'}
71
72
73 def get_args(t, filter):
74     arg_list = []
75     for i in t:
76         if not filter(i[1]):
77             continue
78         arg_list.append(i[1])
79     return arg_list
80
81
82 def get_types(t, filter):
83     types_list = []
84     c_types_list = []
85     lengths_list = []
86     for i in t:
87         if not filter(i[1]):
88             continue
89         if len(i) is 3:  # array type
90             types_list.append(vpp_2_jni_type_mapping[i[0]] + 'Array')
91             c_types_list.append(i[0] + '[]')
92             lengths_list.append((i[2], False))
93         elif len(i) is 4:  # variable length array type
94             types_list.append(vpp_2_jni_type_mapping[i[0]] + 'Array')
95             c_types_list.append(i[0] + '[]')
96             lengths_list.append((i[3], True))
97         else:  # primitive type
98             types_list.append(vpp_2_jni_type_mapping[i[0]])
99             c_types_list.append(i[0])
100             lengths_list.append((0, False))
101     return types_list, c_types_list, lengths_list
102
103
104 def get_definitions():
105     # Pass 1
106     func_list = []
107     func_name = {}
108     for a in cfg.vppapidef:
109         if not is_supported(a[0]):
110             continue
111
112         java_name = util.underscore_to_camelcase(a[0])
113
114         # For replies include all the arguments except message_id
115         if util.is_reply(java_name):
116             types, c_types, lengths = get_types(a[1:], is_response_field)
117             func_name[a[0]] = dict(
118                 [('name', a[0]), ('java_name', java_name),
119                  ('args', get_args(a[1:], is_response_field)), ('full_args', get_args(a[1:], lambda x: True)),
120                  ('types', types), ('c_types', c_types), ('lengths', lengths)])
121         # For requests skip message_id, client_id and context
122         else:
123             types, c_types, lengths = get_types(a[1:], is_request_field)
124             func_name[a[0]] = dict(
125                 [('name', a[0]), ('java_name', java_name),
126                  ('args', get_args(a[1:], is_request_field)), ('full_args', get_args(a[1:], lambda x: True)),
127                  ('types', types), ('c_types', c_types), ('lengths', lengths)])
128
129         # Indexed by name
130         func_list.append(func_name[a[0]])
131     return func_list, func_name
132
133
134 func_list, func_name = get_definitions()
135
136 dto_package = 'dto'
137 callback_package = 'callback'
138 notification_package = 'notification'
139 future_package = 'future'
140 # TODO find better package name
141 callback_facade_package = 'callfacade'
142
143 dto_gen.generate_dtos(func_list, base_package, dto_package, args.inputfile)
144 jvpp_impl_gen.generate_jvpp(func_list, base_package, dto_package, args.inputfile)
145 callback_gen.generate_callbacks(func_list, base_package, callback_package, dto_package, args.inputfile)
146 notification_gen.generate_notification_registry(func_list, base_package, notification_package, callback_package, dto_package, args.inputfile)
147 jvpp_c_gen.generate_jvpp(func_list, args.inputfile)
148 jvpp_future_facade_gen.generate_jvpp(func_list, base_package, dto_package, callback_package, notification_package, future_package, args.inputfile)
149 jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, dto_package, callback_package, notification_package, callback_facade_package, args.inputfile)