diff options
Diffstat (limited to 'src/vpp-api/java/jvpp/gen/jvpp_gen.py')
-rwxr-xr-x | src/vpp-api/java/jvpp/gen/jvpp_gen.py | 268 |
1 files changed, 73 insertions, 195 deletions
diff --git a/src/vpp-api/java/jvpp/gen/jvpp_gen.py b/src/vpp-api/java/jvpp/gen/jvpp_gen.py index 2536b4eea42..a58c5809a6d 100755 --- a/src/vpp-api/java/jvpp/gen/jvpp_gen.py +++ b/src/vpp-api/java/jvpp/gen/jvpp_gen.py @@ -1,6 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 # -# Copyright (c) 2016 Cisco and/or its affiliates. +# Copyright (c) 2016,2018 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -13,199 +13,77 @@ # See the License for the specific language governing permissions and # limitations under the License. # - import argparse -import sys -import os -import json import logging +import os +import sys -from jvppgen import types_gen -from jvppgen import callback_gen -from jvppgen import notification_gen -from jvppgen import dto_gen -from jvppgen import jvpp_callback_facade_gen -from jvppgen import jvpp_future_facade_gen -from jvppgen import jvpp_impl_gen -from jvppgen import jvpp_c_gen -from jvppgen import util -from jvppgen import enum_gen - -# Invocation: -# ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp -# ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ ../../../../jvpp_gen.py -idefs_api_vpp_papi.py -# -# Compilation: -# ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ javac *.java dto/*.java callback/*.java -# -# where -# defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen) - -parser = argparse.ArgumentParser(description='VPP Java API generator') -parser.add_argument('-i', action="store", dest="inputfiles", nargs='+') -parser.add_argument('--plugin_name', action="store", dest="plugin_name") -parser.add_argument('--root_dir', action="store", dest="root_dir") -args = parser.parse_args() - -sys.path.append(".") -cwd = os.getcwd() - -# Initialize logger -try: - verbose = int(os.getenv("V", 0)) -except: - verbose = 0 - -log_level = logging.WARNING -if verbose == 1: - log_level = logging.INFO -elif verbose >= 2: - log_level = logging.DEBUG - -logging.basicConfig(stream=sys.stdout, level=log_level) -logger = logging.getLogger("JVPP GEN") -logger.setLevel(log_level) - - -logger.info("Generating Java API for %s" % args.inputfiles) -plugin_name = args.plugin_name -logger.debug("plugin_name: %s" % plugin_name) - -cfg = {} - -base_package = 'io.fd.vpp.jvpp' -plugin_package = base_package + '.' + plugin_name -root_dir = os.path.abspath(args.root_dir) -logger.debug("root_dir: %s" % root_dir) -work_dir = root_dir + "/target/" + plugin_package.replace(".","/") - -try: - os.makedirs(work_dir) -except OSError: - if not os.path.isdir(work_dir): - raise - -os.chdir(work_dir) - -for inputfile in args.inputfiles: - _cfg = json.load(open(cwd + "/" + inputfile, 'r')) - if 'enums' in cfg: - cfg['enums'].extend(_cfg['enums']) - else: - cfg['enums'] = _cfg['enums'] - if 'types' in cfg: - cfg['types'].extend(_cfg['types']) - else: - cfg['types'] = _cfg['types'] - if 'messages' in cfg: - cfg['messages'].extend(_cfg['messages']) - else: - cfg['messages'] = _cfg['messages'] - - -def is_request_field(field_name): - return field_name not in {'_vl_msg_id', 'client_index', 'context'} - - -def is_response_field(field_name): - return field_name not in {'_vl_msg_id'} - - -def get_args(t, filter): - arg_names = [] - arg_types = [] - for i in t: - if is_crc(i): - continue - if not filter(i[1]): - continue - arg_types.append(i[0]) - arg_names.append(i[1]) - return arg_types, arg_names - - -def get_types(t, filter): - types_list = [] - lengths_list = [] - crc = None - for i in t: - if is_crc(i): - crc = ('crc', i['crc'][2:]) - continue - if not filter(i[1]): - continue - if len(i) is 3: # array type - types_list.append(i[0] + '[]') - lengths_list.append((i[2], False)) - elif len(i) is 4: # variable length array type - types_list.append(i[0] + '[]') - lengths_list.append((i[3], True)) - else: # primitive type - types_list.append(i[0]) - lengths_list.append((0, False)) - return types_list, lengths_list, crc - - -def is_crc(arg): - """ Check whether the argument inside message definition is just crc """ - return 'crc' in arg - - -def get_definitions(defs): - # Pass 1 - func_list = [] - func_name = {} - for a in defs: - java_name = util.underscore_to_camelcase(a[0]) - - # For replies include all the arguments except message_id - if util.is_reply(java_name): - types, lengths, crc = get_types(a[1:], is_response_field) - args = get_args(a[1:], is_response_field) - func_name[a[0]] = dict( - [('name', a[0]), ('java_name', java_name), - ('args', args[1]), ('arg_types', args[0]), - ('types', types), ('lengths', lengths), crc]) - # For requests skip message_id, client_id and context - else: - types, lengths, crc = get_types(a[1:], is_request_field) - args = get_args(a[1:], is_request_field) - func_name[a[0]] = dict( - [('name', a[0]), ('java_name', java_name), - ('args', args[1]), ('arg_types', args[0]), - ('types', types), ('lengths', lengths), crc]) - - # Indexed by name - func_list.append(func_name[a[0]]) - return func_list, func_name - - -types_package = 'types' -dto_package = 'dto' -callback_package = 'callback' -notification_package = 'notification' -future_package = 'future' -# TODO find better package name -callback_facade_package = 'callfacade' - -types_list, types_name = get_definitions(cfg['types']) -func_list, func_name = get_definitions(cfg['messages']) - -enum_gen.generate_enums(cfg['enums'], args.inputfiles, logger) -types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles, logger) -dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles, - logger) -jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles, logger) -callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, - dto_package, args.inputfiles, logger) -notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), - notification_package, callback_package, dto_package, args.inputfiles, - logger) -jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles, root_dir, logger) -jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, - callback_package, notification_package, future_package, args.inputfiles, logger) -jvpp_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, - logger) - -logger.info("Java API for %s generated successfully" % args.inputfiles) +from jvppgen.types_gen import generate_types +from jvppgen.dto_gen import generate_dtos +from jvppgen.jvpp_ifc_gen import generate_java_ifc +from jvppgen.jvpp_impl_gen import generate_java_impl +from jvppgen.callback_gen import generate_callbacks +from jvppgen.jni_gen import generate_jni +from jvppgen.notification_gen import generate_notifications +from jvppgen.jvpp_future_facade_gen import generate_future_facade +from jvppgen.jvpp_callback_facade_gen import generate_callback_facade +from jvppgen.jvpp_model import JVppModel + + +def generate_jvpp(root_dir, model, logger): + base_dir = "%s/target/%s" % (root_dir, model.plugin_package.replace(".", "/")) + generate_types(_work_dir(base_dir, "types"), model, logger) + generate_dtos(_work_dir(base_dir, "dto"), model, logger) + generate_java_ifc(_work_dir(base_dir), model, logger) + generate_java_impl(_work_dir(base_dir), model, logger) + generate_callbacks(_work_dir(base_dir, "callback"), model, logger) + generate_jni(root_dir, model, logger) + generate_notifications(_work_dir(base_dir, "notification"), model, logger) + generate_future_facade(_work_dir(base_dir, "future"), model, logger) + generate_callback_facade(_work_dir(base_dir, "callfacade"), model, logger) + + +def _work_dir(work_dir, sub_dir=None): + if sub_dir: + work_dir = "%s/%s" % (work_dir, sub_dir) + try: + os.makedirs(work_dir) + except OSError: + if not os.path.isdir(work_dir): + raise + return work_dir + + +def _init_logger(): + try: + verbose = int(os.getenv("V", 0)) + except: + verbose = 0 + + log_level = logging.WARNING + if verbose == 1: + log_level = logging.INFO + elif verbose >= 2: + log_level = logging.DEBUG + + logging.basicConfig(stream=sys.stdout, level=log_level) + logger = logging.getLogger("JVPP GEN") + logger.setLevel(log_level) + return logger + + +if __name__ == '__main__': + logger = _init_logger() + + argparser = argparse.ArgumentParser(description="VPP Java API generator") + argparser.add_argument('-i', nargs='+', metavar='api_file.json', help="json vpp api file(s)") + argparser.add_argument('--plugin_name') + argparser.add_argument('--root_dir') + args = argparser.parse_args() + + logger.info("Generating Java API for %s" % args.i) + logger.debug("plugin_name: %s" % args.plugin_name) + logger.debug("root_dir: %s" % args.root_dir) + + model = JVppModel(logger, args.i, args.plugin_name) + generate_jvpp(args.root_dir, model, logger) |