blob: a58c5809a6d72d8681a925b74f83ecfec404b854 [file] [log] [blame]
Marek Gradzkia51ccb52018-02-09 13:42:12 +01001#!/usr/bin/env python2
Marek Gradzkid85036f2016-04-26 12:09:05 +02002#
Marek Gradzkia51ccb52018-02-09 13:42:12 +01003# Copyright (c) 2016,2018 Cisco and/or its affiliates.
Marek Gradzkid85036f2016-04-26 12:09:05 +02004# 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#
Marek Gradzkid85036f2016-04-26 12:09:05 +020016import argparse
Marek Gradzki568cc602018-01-26 11:39:27 +010017import logging
Marek Gradzkia51ccb52018-02-09 13:42:12 +010018import os
19import sys
Marek Gradzkid85036f2016-04-26 12:09:05 +020020
Marek Gradzkia51ccb52018-02-09 13:42:12 +010021from jvppgen.types_gen import generate_types
22from jvppgen.dto_gen import generate_dtos
23from jvppgen.jvpp_ifc_gen import generate_java_ifc
24from jvppgen.jvpp_impl_gen import generate_java_impl
25from jvppgen.callback_gen import generate_callbacks
26from jvppgen.jni_gen import generate_jni
27from jvppgen.notification_gen import generate_notifications
28from jvppgen.jvpp_future_facade_gen import generate_future_facade
29from jvppgen.jvpp_callback_facade_gen import generate_callback_facade
30from jvppgen.jvpp_model import JVppModel
Marek Gradzki568cc602018-01-26 11:39:27 +010031
32
Marek Gradzkia51ccb52018-02-09 13:42:12 +010033def generate_jvpp(root_dir, model, logger):
34 base_dir = "%s/target/%s" % (root_dir, model.plugin_package.replace(".", "/"))
35 generate_types(_work_dir(base_dir, "types"), model, logger)
36 generate_dtos(_work_dir(base_dir, "dto"), model, logger)
37 generate_java_ifc(_work_dir(base_dir), model, logger)
38 generate_java_impl(_work_dir(base_dir), model, logger)
39 generate_callbacks(_work_dir(base_dir, "callback"), model, logger)
40 generate_jni(root_dir, model, logger)
41 generate_notifications(_work_dir(base_dir, "notification"), model, logger)
42 generate_future_facade(_work_dir(base_dir, "future"), model, logger)
43 generate_callback_facade(_work_dir(base_dir, "callfacade"), model, logger)
Marek Gradzki2fee4c82016-11-25 08:38:29 +010044
Marek Gradzkid85036f2016-04-26 12:09:05 +020045
Marek Gradzkia51ccb52018-02-09 13:42:12 +010046def _work_dir(work_dir, sub_dir=None):
47 if sub_dir:
48 work_dir = "%s/%s" % (work_dir, sub_dir)
49 try:
50 os.makedirs(work_dir)
51 except OSError:
52 if not os.path.isdir(work_dir):
53 raise
54 return work_dir
Marek Gradzkid85036f2016-04-26 12:09:05 +020055
56
Marek Gradzkia51ccb52018-02-09 13:42:12 +010057def _init_logger():
58 try:
59 verbose = int(os.getenv("V", 0))
60 except:
61 verbose = 0
62
63 log_level = logging.WARNING
64 if verbose == 1:
65 log_level = logging.INFO
66 elif verbose >= 2:
67 log_level = logging.DEBUG
68
69 logging.basicConfig(stream=sys.stdout, level=log_level)
70 logger = logging.getLogger("JVPP GEN")
71 logger.setLevel(log_level)
72 return logger
Marek Gradzkid85036f2016-04-26 12:09:05 +020073
74
Marek Gradzkia51ccb52018-02-09 13:42:12 +010075if __name__ == '__main__':
76 logger = _init_logger()
Marek Gradzkid85036f2016-04-26 12:09:05 +020077
Marek Gradzkia51ccb52018-02-09 13:42:12 +010078 argparser = argparse.ArgumentParser(description="VPP Java API generator")
79 argparser.add_argument('-i', nargs='+', metavar='api_file.json', help="json vpp api file(s)")
80 argparser.add_argument('--plugin_name')
81 argparser.add_argument('--root_dir')
82 args = argparser.parse_args()
Marek Gradzkid85036f2016-04-26 12:09:05 +020083
Marek Gradzkia51ccb52018-02-09 13:42:12 +010084 logger.info("Generating Java API for %s" % args.i)
85 logger.debug("plugin_name: %s" % args.plugin_name)
86 logger.debug("root_dir: %s" % args.root_dir)
Marek Gradzki2fee4c82016-11-25 08:38:29 +010087
Marek Gradzkia51ccb52018-02-09 13:42:12 +010088 model = JVppModel(logger, args.i, args.plugin_name)
89 generate_jvpp(args.root_dir, model, logger)