blob: 067a92f86be498cb8c48441df799c6c32ad29b4f [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
Marek Gradzki3e2d57d2018-06-20 14:49:39 +020022from jvppgen.enums_gen import generate_enums
Marek Gradzki5b3d95b2018-06-27 10:18:18 +020023from jvppgen.unions_gen import generate_unions
Marek Gradzkia51ccb52018-02-09 13:42:12 +010024from jvppgen.dto_gen import generate_dtos
25from jvppgen.jvpp_ifc_gen import generate_java_ifc
26from jvppgen.jvpp_impl_gen import generate_java_impl
27from jvppgen.callback_gen import generate_callbacks
28from jvppgen.jni_gen import generate_jni
29from jvppgen.notification_gen import generate_notifications
30from jvppgen.jvpp_future_facade_gen import generate_future_facade
31from jvppgen.jvpp_callback_facade_gen import generate_callback_facade
32from jvppgen.jvpp_model import JVppModel
Marek Gradzki568cc602018-01-26 11:39:27 +010033
34
Marek Gradzkia51ccb52018-02-09 13:42:12 +010035def generate_jvpp(root_dir, model, logger):
36 base_dir = "%s/target/%s" % (root_dir, model.plugin_package.replace(".", "/"))
37 generate_types(_work_dir(base_dir, "types"), model, logger)
Marek Gradzki3e2d57d2018-06-20 14:49:39 +020038 generate_enums(_work_dir(base_dir, "types"), model, logger)
Marek Gradzki5b3d95b2018-06-27 10:18:18 +020039 generate_unions(_work_dir(base_dir, "types"), model, logger)
Marek Gradzkia51ccb52018-02-09 13:42:12 +010040 generate_dtos(_work_dir(base_dir, "dto"), model, logger)
41 generate_java_ifc(_work_dir(base_dir), model, logger)
42 generate_java_impl(_work_dir(base_dir), model, logger)
43 generate_callbacks(_work_dir(base_dir, "callback"), model, logger)
44 generate_jni(root_dir, model, logger)
45 generate_notifications(_work_dir(base_dir, "notification"), model, logger)
46 generate_future_facade(_work_dir(base_dir, "future"), model, logger)
47 generate_callback_facade(_work_dir(base_dir, "callfacade"), model, logger)
Marek Gradzki2fee4c82016-11-25 08:38:29 +010048
Marek Gradzkid85036f2016-04-26 12:09:05 +020049
Marek Gradzkia51ccb52018-02-09 13:42:12 +010050def _work_dir(work_dir, sub_dir=None):
51 if sub_dir:
52 work_dir = "%s/%s" % (work_dir, sub_dir)
53 try:
54 os.makedirs(work_dir)
55 except OSError:
56 if not os.path.isdir(work_dir):
57 raise
58 return work_dir
Marek Gradzkid85036f2016-04-26 12:09:05 +020059
60
Marek Gradzkia51ccb52018-02-09 13:42:12 +010061def _init_logger():
62 try:
63 verbose = int(os.getenv("V", 0))
64 except:
65 verbose = 0
66
67 log_level = logging.WARNING
68 if verbose == 1:
69 log_level = logging.INFO
70 elif verbose >= 2:
71 log_level = logging.DEBUG
72
73 logging.basicConfig(stream=sys.stdout, level=log_level)
74 logger = logging.getLogger("JVPP GEN")
75 logger.setLevel(log_level)
76 return logger
Marek Gradzkid85036f2016-04-26 12:09:05 +020077
78
Marek Gradzkia51ccb52018-02-09 13:42:12 +010079if __name__ == '__main__':
80 logger = _init_logger()
Marek Gradzkid85036f2016-04-26 12:09:05 +020081
Marek Gradzkia51ccb52018-02-09 13:42:12 +010082 argparser = argparse.ArgumentParser(description="VPP Java API generator")
83 argparser.add_argument('-i', nargs='+', metavar='api_file.json', help="json vpp api file(s)")
84 argparser.add_argument('--plugin_name')
85 argparser.add_argument('--root_dir')
86 args = argparser.parse_args()
Marek Gradzkid85036f2016-04-26 12:09:05 +020087
Marek Gradzkia51ccb52018-02-09 13:42:12 +010088 logger.info("Generating Java API for %s" % args.i)
89 logger.debug("plugin_name: %s" % args.plugin_name)
90 logger.debug("root_dir: %s" % args.root_dir)
Marek Gradzki2fee4c82016-11-25 08:38:29 +010091
Marek Gradzkia51ccb52018-02-09 13:42:12 +010092 model = JVppModel(logger, args.i, args.plugin_name)
93 generate_jvpp(args.root_dir, model, logger)