blob: 3be83824e9c907c8a3b0272c308d65026c399056 [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
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020022from jvppgen import types_gen
Ed Warnickeadeb7492016-07-11 10:29:41 -070023from jvppgen import callback_gen
24from jvppgen import notification_gen
25from jvppgen import dto_gen
26from jvppgen import jvpp_callback_facade_gen
27from jvppgen import jvpp_future_facade_gen
28from jvppgen import jvpp_impl_gen
29from jvppgen import jvpp_c_gen
30from jvppgen import util
Marek Gradzkid85036f2016-04-26 12:09:05 +020031
32# Invocation:
Marek Gradzkie85581c2016-09-28 10:12:04 +020033# ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp
34# ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ ../../../../jvpp_gen.py -idefs_api_vpp_papi.py
Marek Gradzkid85036f2016-04-26 12:09:05 +020035#
36# Compilation:
Marek Gradzkie85581c2016-09-28 10:12:04 +020037# ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ javac *.java dto/*.java callback/*.java
Maros Marsalek7becd082016-05-31 17:45:16 +020038#
Marek Gradzkid85036f2016-04-26 12:09:05 +020039# where
40# defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
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
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020050print "Generating Java API for %s" % args.inputfile
Ed Warnickeadeb7492016-07-11 10:29:41 -070051importdir = 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
Marek Gradzkid85036f2016-04-26 12:09:05 +020063def is_request_field(field_name):
64 return field_name not in {'_vl_msg_id', 'client_index', 'context'}
65
66
67def is_response_field(field_name):
68 return field_name not in {'_vl_msg_id'}
69
70
71def get_args(t, filter):
72 arg_list = []
73 for i in t:
74 if not filter(i[1]):
75 continue
76 arg_list.append(i[1])
77 return arg_list
78
79
80def get_types(t, filter):
81 types_list = []
Marek Gradzkic4cb44c2016-05-24 13:32:26 +020082 lengths_list = []
Marek Gradzkid85036f2016-04-26 12:09:05 +020083 for i in t:
84 if not filter(i[1]):
85 continue
86 if len(i) is 3: # array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020087 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +020088 lengths_list.append((i[2], False))
89 elif len(i) is 4: # variable length array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020090 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +020091 lengths_list.append((i[3], True))
Marek Gradzkid85036f2016-04-26 12:09:05 +020092 else: # primitive type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020093 types_list.append(i[0])
Marek Gradzkifa42e252016-06-15 16:38:33 +020094 lengths_list.append((0, False))
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020095 return types_list, lengths_list
Marek Gradzkid85036f2016-04-26 12:09:05 +020096
97
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020098def get_definitions(defs):
Marek Gradzkid85036f2016-04-26 12:09:05 +020099 # Pass 1
100 func_list = []
101 func_name = {}
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200102 for a in defs:
Marek Gradzkid85036f2016-04-26 12:09:05 +0200103 java_name = util.underscore_to_camelcase(a[0])
104
105 # For replies include all the arguments except message_id
106 if util.is_reply(java_name):
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200107 types, lengths = get_types(a[1:], is_response_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200108 func_name[a[0]] = dict(
109 [('name', a[0]), ('java_name', java_name),
110 ('args', get_args(a[1:], is_response_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200111 ('types', types), ('lengths', lengths)])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200112 # For requests skip message_id, client_id and context
113 else:
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200114 types, lengths = get_types(a[1:], is_request_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200115 func_name[a[0]] = dict(
116 [('name', a[0]), ('java_name', java_name),
117 ('args', get_args(a[1:], is_request_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200118 ('types', types), ('lengths', lengths)])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200119
120 # Indexed by name
121 func_list.append(func_name[a[0]])
122 return func_list, func_name
123
124
Marek Gradzkie85581c2016-09-28 10:12:04 +0200125base_package = 'io.fd.vpp.jvpp'
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200126plugin_package = base_package + '.' + plugin_name
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200127types_package = 'types'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200128dto_package = 'dto'
129callback_package = 'callback'
Maros Marsalek7becd082016-05-31 17:45:16 +0200130notification_package = 'notification'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200131future_package = 'future'
132# TODO find better package name
133callback_facade_package = 'callfacade'
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200134control_ping_class_fqn = "%s.%s.%s" % (plugin_package, dto_package, control_ping_class)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200135
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200136types_list, types_name = get_definitions(cfg.types)
137
138types_gen.generate_types(types_list, plugin_package, types_package, inputfile)
139
140func_list, func_name = get_definitions(cfg.messages)
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200141dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfile)
142jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, control_ping_class_fqn, dto_package, args.inputfile)
143callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfile)
144notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfile)
145jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfile)
146jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, future_package, args.inputfile)
147jvpp_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)
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200148
149print "Java API for %s generated successfully" % args.inputfile