blob: 2536b4eea42370cee094affaaee59667af1900f6 [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 Gradzki568cc602018-01-26 11:39:27 +010021import logging
Marek Gradzkid85036f2016-04-26 12:09:05 +020022
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020023from jvppgen import types_gen
Ed Warnickeadeb7492016-07-11 10:29:41 -070024from jvppgen import callback_gen
25from jvppgen import notification_gen
26from jvppgen import dto_gen
27from jvppgen import jvpp_callback_facade_gen
28from jvppgen import jvpp_future_facade_gen
29from jvppgen import jvpp_impl_gen
30from jvppgen import jvpp_c_gen
31from jvppgen import util
Marek Gradzkid1660e92018-01-24 16:35:11 +010032from jvppgen import enum_gen
Marek Gradzkid85036f2016-04-26 12:09:05 +020033
34# 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 Gradzki568cc602018-01-26 11:39:27 +010053# Initialize logger
54try:
55 verbose = int(os.getenv("V", 0))
56except:
57 verbose = 0
58
59log_level = logging.WARNING
60if verbose == 1:
61 log_level = logging.INFO
62elif verbose >= 2:
63 log_level = logging.DEBUG
64
65logging.basicConfig(stream=sys.stdout, level=log_level)
66logger = logging.getLogger("JVPP GEN")
67logger.setLevel(log_level)
68
69
70logger.info("Generating Java API for %s" % args.inputfiles)
Marek Gradzki66ea26b2016-07-26 15:28:22 +020071plugin_name = args.plugin_name
Marek Gradzki568cc602018-01-26 11:39:27 +010072logger.debug("plugin_name: %s" % plugin_name)
Marek Gradzki2fee4c82016-11-25 08:38:29 +010073
74cfg = {}
75
Damjan Marioncb034b92016-12-28 18:38:59 +010076base_package = 'io.fd.vpp.jvpp'
77plugin_package = base_package + '.' + plugin_name
78root_dir = os.path.abspath(args.root_dir)
Marek Gradzki568cc602018-01-26 11:39:27 +010079logger.debug("root_dir: %s" % root_dir)
Damjan Marioncb034b92016-12-28 18:38:59 +010080work_dir = root_dir + "/target/" + plugin_package.replace(".","/")
81
82try:
83 os.makedirs(work_dir)
84except OSError:
85 if not os.path.isdir(work_dir):
86 raise
87
88os.chdir(work_dir)
89
Marek Gradzki2fee4c82016-11-25 08:38:29 +010090for inputfile in args.inputfiles:
Damjan Marioncb034b92016-12-28 18:38:59 +010091 _cfg = json.load(open(cwd + "/" + inputfile, 'r'))
Marek Gradzkid1660e92018-01-24 16:35:11 +010092 if 'enums' in cfg:
93 cfg['enums'].extend(_cfg['enums'])
94 else:
95 cfg['enums'] = _cfg['enums']
Marek Gradzki2fee4c82016-11-25 08:38:29 +010096 if 'types' in cfg:
97 cfg['types'].extend(_cfg['types'])
98 else:
99 cfg['types'] = _cfg['types']
100 if 'messages' in cfg:
101 cfg['messages'].extend(_cfg['messages'])
102 else:
103 cfg['messages'] = _cfg['messages']
104
Marek Gradzkid85036f2016-04-26 12:09:05 +0200105
Marek Gradzkid85036f2016-04-26 12:09:05 +0200106def is_request_field(field_name):
107 return field_name not in {'_vl_msg_id', 'client_index', 'context'}
108
109
110def is_response_field(field_name):
111 return field_name not in {'_vl_msg_id'}
112
113
114def get_args(t, filter):
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200115 arg_names = []
116 arg_types = []
Marek Gradzkid85036f2016-04-26 12:09:05 +0200117 for i in t:
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100118 if is_crc(i):
119 continue
Marek Gradzkid85036f2016-04-26 12:09:05 +0200120 if not filter(i[1]):
121 continue
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200122 arg_types.append(i[0])
123 arg_names.append(i[1])
124 return arg_types, arg_names
Marek Gradzkid85036f2016-04-26 12:09:05 +0200125
126
127def get_types(t, filter):
128 types_list = []
Marek Gradzkic4cb44c2016-05-24 13:32:26 +0200129 lengths_list = []
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100130 crc = None
Marek Gradzkid85036f2016-04-26 12:09:05 +0200131 for i in t:
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100132 if is_crc(i):
133 crc = ('crc', i['crc'][2:])
134 continue
Marek Gradzkid85036f2016-04-26 12:09:05 +0200135 if not filter(i[1]):
136 continue
137 if len(i) is 3: # array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200138 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +0200139 lengths_list.append((i[2], False))
140 elif len(i) is 4: # variable length array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200141 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +0200142 lengths_list.append((i[3], True))
Marek Gradzkid85036f2016-04-26 12:09:05 +0200143 else: # primitive type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200144 types_list.append(i[0])
Marek Gradzkifa42e252016-06-15 16:38:33 +0200145 lengths_list.append((0, False))
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100146 return types_list, lengths_list, crc
147
148
149def is_crc(arg):
150 """ Check whether the argument inside message definition is just crc """
151 return 'crc' in arg
Marek Gradzkid85036f2016-04-26 12:09:05 +0200152
153
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200154def get_definitions(defs):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200155 # Pass 1
156 func_list = []
157 func_name = {}
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200158 for a in defs:
Marek Gradzkid85036f2016-04-26 12:09:05 +0200159 java_name = util.underscore_to_camelcase(a[0])
160
161 # For replies include all the arguments except message_id
162 if util.is_reply(java_name):
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100163 types, lengths, crc = get_types(a[1:], is_response_field)
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200164 args = get_args(a[1:], is_response_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200165 func_name[a[0]] = dict(
166 [('name', a[0]), ('java_name', java_name),
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200167 ('args', args[1]), ('arg_types', args[0]),
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100168 ('types', types), ('lengths', lengths), crc])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200169 # For requests skip message_id, client_id and context
170 else:
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100171 types, lengths, crc = get_types(a[1:], is_request_field)
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200172 args = get_args(a[1:], is_request_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200173 func_name[a[0]] = dict(
174 [('name', a[0]), ('java_name', java_name),
Marek Gradzkied76d5f2017-05-12 18:24:34 +0200175 ('args', args[1]), ('arg_types', args[0]),
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100176 ('types', types), ('lengths', lengths), crc])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200177
178 # Indexed by name
179 func_list.append(func_name[a[0]])
180 return func_list, func_name
181
182
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200183types_package = 'types'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200184dto_package = 'dto'
185callback_package = 'callback'
Maros Marsalek7becd082016-05-31 17:45:16 +0200186notification_package = 'notification'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200187future_package = 'future'
188# TODO find better package name
189callback_facade_package = 'callfacade'
190
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100191types_list, types_name = get_definitions(cfg['types'])
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100192func_list, func_name = get_definitions(cfg['messages'])
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200193
Marek Gradzkid1660e92018-01-24 16:35:11 +0100194enum_gen.generate_enums(cfg['enums'], args.inputfiles, logger)
195types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles, logger)
Marek Gradzki568cc602018-01-26 11:39:27 +0100196dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles,
197 logger)
198jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles, logger)
199callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package,
200 dto_package, args.inputfiles, logger)
201notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(),
202 notification_package, callback_package, dto_package, args.inputfiles,
203 logger)
204jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles, root_dir, logger)
205jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package,
206 callback_package, notification_package, future_package, args.inputfiles, logger)
207jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package,
208 callback_package, notification_package, callback_facade_package, args.inputfiles,
209 logger)
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100210
Marek Gradzki568cc602018-01-26 11:39:27 +0100211logger.info("Java API for %s generated successfully" % args.inputfiles)