blob: ca2d2624d50147df1eff605bfd4e4dace7579996 [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
Marek Gradzki4746a5d2017-01-27 08:57:40 +01009#
Marek Gradzkid85036f2016-04-26 12:09:05 +020010# 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
Marek Gradzkid85036f2016-04-26 12:09:05 +020018import sys
Ed Warnickeadeb7492016-07-11 10:29:41 -070019import os
Marek Gradzki2fee4c82016-11-25 08:38:29 +010020import json
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
Ole Troan5c749732017-03-13 13:39:52 +010032blacklist = [ "memclnt.api", "flowprobe.api" ]
Damjan Marion7cd468a2016-12-19 23:05:39 +010033
Marek Gradzkid85036f2016-04-26 12:09:05 +020034# Invocation:
Marek Gradzkie85581c2016-09-28 10:12:04 +020035# ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp
36# ~/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 +020037#
38# Compilation:
Marek Gradzkie85581c2016-09-28 10:12:04 +020039# ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ javac *.java dto/*.java callback/*.java
Maros Marsalek7becd082016-05-31 17:45:16 +020040#
Marek Gradzkid85036f2016-04-26 12:09:05 +020041# where
42# defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
Marek Gradzkid85036f2016-04-26 12:09:05 +020043
44parser = argparse.ArgumentParser(description='VPP Java API generator')
Marek Gradzki2fee4c82016-11-25 08:38:29 +010045parser.add_argument('-i', action="store", dest="inputfiles", nargs='+')
Marek Gradzki66ea26b2016-07-26 15:28:22 +020046parser.add_argument('--plugin_name', action="store", dest="plugin_name")
Damjan Marioncb034b92016-12-28 18:38:59 +010047parser.add_argument('--root_dir', action="store", dest="root_dir")
Marek Gradzkid85036f2016-04-26 12:09:05 +020048args = parser.parse_args()
49
50sys.path.append(".")
Damjan Marioncb034b92016-12-28 18:38:59 +010051cwd = os.getcwd()
Marek Gradzkid85036f2016-04-26 12:09:05 +020052
Marek Gradzki2fee4c82016-11-25 08:38:29 +010053print "Generating Java API for %s" % args.inputfiles
54print "inputfiles %s" % args.inputfiles
Marek Gradzki66ea26b2016-07-26 15:28:22 +020055plugin_name = args.plugin_name
56print "plugin_name %s" % plugin_name
Marek Gradzki2fee4c82016-11-25 08:38:29 +010057
58cfg = {}
59
Damjan Marioncb034b92016-12-28 18:38:59 +010060base_package = 'io.fd.vpp.jvpp'
61plugin_package = base_package + '.' + plugin_name
62root_dir = os.path.abspath(args.root_dir)
63print "root_dir %s" % root_dir
64work_dir = root_dir + "/target/" + plugin_package.replace(".","/")
65
66try:
67 os.makedirs(work_dir)
68except OSError:
69 if not os.path.isdir(work_dir):
70 raise
71
72os.chdir(work_dir)
73
Marek Gradzki2fee4c82016-11-25 08:38:29 +010074for inputfile in args.inputfiles:
Damjan Marion7cd468a2016-12-19 23:05:39 +010075 if any(substring in inputfile for substring in blacklist):
76 print "WARNING: Imput file %s blacklisted" % inputfile
77 continue
Damjan Marioncb034b92016-12-28 18:38:59 +010078 _cfg = json.load(open(cwd + "/" + inputfile, 'r'))
Marek Gradzki2fee4c82016-11-25 08:38:29 +010079 if 'types' in cfg:
80 cfg['types'].extend(_cfg['types'])
81 else:
82 cfg['types'] = _cfg['types']
83 if 'messages' in cfg:
84 cfg['messages'].extend(_cfg['messages'])
85 else:
86 cfg['messages'] = _cfg['messages']
87
Marek Gradzkid85036f2016-04-26 12:09:05 +020088
Marek Gradzkid85036f2016-04-26 12:09:05 +020089def is_request_field(field_name):
90 return field_name not in {'_vl_msg_id', 'client_index', 'context'}
91
92
93def is_response_field(field_name):
94 return field_name not in {'_vl_msg_id'}
95
96
97def get_args(t, filter):
Marek Gradzkied76d5f2017-05-12 18:24:34 +020098 arg_names = []
99 arg_types = []
Marek Gradzkid85036f2016-04-26 12:09:05 +0200100 for i in t:
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100101 if is_crc(i):
102 continue
Marek Gradzkid85036f2016-04-26 12:09:05 +0200103 if not filter(i[1]):
104 continue
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200105 arg_types.append(i[0])
106 arg_names.append(i[1])
107 return arg_types, arg_names
Marek Gradzkid85036f2016-04-26 12:09:05 +0200108
109
110def get_types(t, filter):
111 types_list = []
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200112 lengths_list = []
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100113 crc = None
Marek Gradzkid85036f2016-04-26 12:09:05 +0200114 for i in t:
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100115 if is_crc(i):
116 crc = ('crc', i['crc'][2:])
117 continue
Marek Gradzkid85036f2016-04-26 12:09:05 +0200118 if not filter(i[1]):
119 continue
120 if len(i) is 3: # array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200121 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +0200122 lengths_list.append((i[2], False))
123 elif len(i) is 4: # variable length array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200124 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +0200125 lengths_list.append((i[3], True))
Marek Gradzkid85036f2016-04-26 12:09:05 +0200126 else: # primitive type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200127 types_list.append(i[0])
Marek Gradzkifa42e252016-06-15 16:38:33 +0200128 lengths_list.append((0, False))
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100129 return types_list, lengths_list, crc
130
131
132def is_crc(arg):
133 """ Check whether the argument inside message definition is just crc """
134 return 'crc' in arg
Marek Gradzkid85036f2016-04-26 12:09:05 +0200135
136
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200137def get_definitions(defs):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200138 # Pass 1
139 func_list = []
140 func_name = {}
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200141 for a in defs:
Marek Gradzkid85036f2016-04-26 12:09:05 +0200142 java_name = util.underscore_to_camelcase(a[0])
143
144 # For replies include all the arguments except message_id
145 if util.is_reply(java_name):
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100146 types, lengths, crc = get_types(a[1:], is_response_field)
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200147 args = get_args(a[1:], is_response_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200148 func_name[a[0]] = dict(
149 [('name', a[0]), ('java_name', java_name),
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200150 ('args', args[1]), ('arg_types', args[0]),
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100151 ('types', types), ('lengths', lengths), crc])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200152 # For requests skip message_id, client_id and context
153 else:
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100154 types, lengths, crc = get_types(a[1:], is_request_field)
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200155 args = get_args(a[1:], is_request_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200156 func_name[a[0]] = dict(
157 [('name', a[0]), ('java_name', java_name),
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200158 ('args', args[1]), ('arg_types', args[0]),
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100159 ('types', types), ('lengths', lengths), crc])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200160
161 # Indexed by name
162 func_list.append(func_name[a[0]])
163 return func_list, func_name
164
165
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200166types_package = 'types'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200167dto_package = 'dto'
168callback_package = 'callback'
Maros Marsalek7becd082016-05-31 17:45:16 +0200169notification_package = 'notification'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200170future_package = 'future'
171# TODO find better package name
172callback_facade_package = 'callfacade'
173
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100174types_list, types_name = get_definitions(cfg['types'])
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200175
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100176types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles)
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200177
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100178func_list, func_name = get_definitions(cfg['messages'])
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200179
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100180dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles)
181jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles)
182callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfiles)
183notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfiles)
Damjan Marioncb034b92016-12-28 18:38:59 +0100184jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles, root_dir)
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100185jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, future_package, args.inputfiles)
186jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, callback_facade_package, args.inputfiles)
187
188print "Java API for %s generated successfully" % args.inputfiles