blob: 551ce7d264c19e538188df1c05cf4ace57edfce3 [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
Ed Warnickeadeb7492016-07-11 10:29:41 -070020import os
Marek Gradzkid85036f2016-04-26 12:09:05 +020021
Ed Warnickeadeb7492016-07-11 10:29:41 -070022from jvppgen import callback_gen
23from jvppgen import notification_gen
24from jvppgen import dto_gen
25from jvppgen import jvpp_callback_facade_gen
26from jvppgen import jvpp_future_facade_gen
27from jvppgen import jvpp_impl_gen
28from jvppgen import jvpp_c_gen
29from jvppgen import util
Marek Gradzkid85036f2016-04-26 12:09:05 +020030
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
Maros Marsalek7becd082016-05-31 17:45:16 +020037#
Marek Gradzkid85036f2016-04-26 12:09:05 +020038# where
39# defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
Ed Warnickeadeb7492016-07-11 10:29:41 -070040from jvppgen.util import vpp_2_jni_type_mapping
Marek Gradzkid85036f2016-04-26 12:09:05 +020041
42parser = argparse.ArgumentParser(description='VPP Java API generator')
43parser.add_argument('-i', action="store", dest="inputfile")
Marek Gradzki66ea26b2016-07-26 15:28:22 +020044parser.add_argument('--plugin_name', action="store", dest="plugin_name")
45parser.add_argument('--control_ping_class', action="store", dest="control_ping_class", default="ControlPing")
Marek Gradzkid85036f2016-04-26 12:09:05 +020046args = parser.parse_args()
47
48sys.path.append(".")
49
Ed Warnickeadeb7492016-07-11 10:29:41 -070050print "args.inputfile %s" % args.inputfile
51importdir = os.path.dirname(args.inputfile)
52print "importdir %s" % importdir
53inputfile = os.path.basename(args.inputfile)
54inputfile = inputfile.replace('.py', '')
55print "inputfile %s" % inputfile
Marek Gradzki66ea26b2016-07-26 15:28:22 +020056plugin_name = args.plugin_name
57print "plugin_name %s" % plugin_name
58control_ping_class = args.control_ping_class
59print "control_ping_class %s" % control_ping_class
Ed Warnickeadeb7492016-07-11 10:29:41 -070060sys.path.append(importdir)
Marek Gradzkid85036f2016-04-26 12:09:05 +020061cfg = importlib.import_module(inputfile, package=None)
62
63
64# FIXME: functions unsupported due to problems with vpe.api
65def is_supported(f_name):
66 return f_name not in {'vnet_ip4_fib_counters', 'vnet_ip6_fib_counters'}
67
68
69def is_request_field(field_name):
70 return field_name not in {'_vl_msg_id', 'client_index', 'context'}
71
72
73def is_response_field(field_name):
74 return field_name not in {'_vl_msg_id'}
75
76
77def get_args(t, filter):
78 arg_list = []
79 for i in t:
80 if not filter(i[1]):
81 continue
82 arg_list.append(i[1])
83 return arg_list
84
85
86def get_types(t, filter):
87 types_list = []
88 c_types_list = []
Marek Gradzkic4cb44c2016-05-24 13:32:26 +020089 lengths_list = []
Marek Gradzkid85036f2016-04-26 12:09:05 +020090 for i in t:
91 if not filter(i[1]):
92 continue
93 if len(i) is 3: # array type
94 types_list.append(vpp_2_jni_type_mapping[i[0]] + 'Array')
95 c_types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +020096 lengths_list.append((i[2], False))
97 elif len(i) is 4: # variable length array type
98 types_list.append(vpp_2_jni_type_mapping[i[0]] + 'Array')
99 c_types_list.append(i[0] + '[]')
100 lengths_list.append((i[3], True))
Marek Gradzkid85036f2016-04-26 12:09:05 +0200101 else: # primitive type
102 types_list.append(vpp_2_jni_type_mapping[i[0]])
103 c_types_list.append(i[0])
Marek Gradzkifa42e252016-06-15 16:38:33 +0200104 lengths_list.append((0, False))
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200105 return types_list, c_types_list, lengths_list
Marek Gradzkid85036f2016-04-26 12:09:05 +0200106
107
108def get_definitions():
109 # Pass 1
110 func_list = []
111 func_name = {}
112 for a in cfg.vppapidef:
113 if not is_supported(a[0]):
114 continue
115
116 java_name = util.underscore_to_camelcase(a[0])
117
118 # For replies include all the arguments except message_id
119 if util.is_reply(java_name):
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200120 types, c_types, lengths = get_types(a[1:], is_response_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200121 func_name[a[0]] = dict(
122 [('name', a[0]), ('java_name', java_name),
123 ('args', get_args(a[1:], is_response_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200124 ('types', types), ('c_types', c_types), ('lengths', lengths)])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200125 # For requests skip message_id, client_id and context
126 else:
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200127 types, c_types, lengths = get_types(a[1:], is_request_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200128 func_name[a[0]] = dict(
129 [('name', a[0]), ('java_name', java_name),
130 ('args', get_args(a[1:], is_request_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200131 ('types', types), ('c_types', c_types), ('lengths', lengths)])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200132
133 # Indexed by name
134 func_list.append(func_name[a[0]])
135 return func_list, func_name
136
137
138func_list, func_name = get_definitions()
139
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200140base_package = 'org.openvpp.jvpp'
141plugin_package = base_package + '.' + plugin_name
Marek Gradzkid85036f2016-04-26 12:09:05 +0200142dto_package = 'dto'
143callback_package = 'callback'
Maros Marsalek7becd082016-05-31 17:45:16 +0200144notification_package = 'notification'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200145future_package = 'future'
146# TODO find better package name
147callback_facade_package = 'callfacade'
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200148control_ping_class_fqn = "%s.%s.%s" % (plugin_package, dto_package, control_ping_class)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200149
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200150dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfile)
151jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, control_ping_class_fqn, dto_package, args.inputfile)
152callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfile)
153notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfile)
154jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfile)
155jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, future_package, args.inputfile)
156jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, callback_facade_package, args.inputfile)