aboutsummaryrefslogtreecommitdiffstats
path: root/src/vpp-api/java/jvpp/gen/jvpp_gen.py
blob: 067a92f86be498cb8c48441df799c6c32ad29b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python2
#
# 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:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import logging
import os
import sys

from jvppgen.types_gen import generate_types
from jvppgen.enums_gen import generate_enums
from jvppgen.unions_gen import generate_unions
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_enums(_work_dir(base_dir, "types"), model, logger)
    generate_unions(_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)