aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-api/java/jvpp/gen/jvpp_impl_gen.py
blob: 08ebddfcf978be06eac0e515d34ee48501661324 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
#
# Copyright (c) 2016 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 os, util
from string import Template

jvpp_ifc_template = Template("""
package $base_package;


/**
 * <p>Java representation of vpe.api.
 * <br>It was generated by jvpp_impl_gen.py based on $inputfile
 * <br>(python representation of vpe.api generated by vppapigen).
 */
public interface JVpp extends java.lang.AutoCloseable {

    /**
     * Generic dispatch method for sending requests to VPP
     */
    int send($base_package.$dto_package.JVppRequest request);

    @Override
    void close();

$methods
}
""")

jvpp_impl_template = Template("""
package $base_package;

/**
 * <p>Default implementation of JVpp interface.
 * <br>It was generated by jvpp_impl_gen.py based on $inputfile
 * <br>(python representation of vpe.api generated by vppapigen).
 */
public final class JVppImpl implements $base_package.JVpp {

    private final $base_package.VppConnection connection;

    public JVppImpl(final $base_package.VppConnection connection) {
        if(connection == null) {
            throw new java.lang.NullPointerException("Connection is null");
        }
        this.connection = connection;
    }

    @Override
    public void close() {
        connection.close();
    }

    @Override
    public int send($base_package.$dto_package.JVppRequest request) {
        return request.send(this);
    }

$methods
}
""")

method_template = Template("""    int $name($base_package.$dto_package.$request request);""")
method_native_template = Template(
    """    private static native int ${name}0($base_package.$dto_package.$request request);""")
method_impl_template = Template("""    public final int $name($base_package.$dto_package.$request request) {
        if(request == null) {
            throw new java.lang.NullPointerException("Null request object");
        }
        connection.checkActive();
        return ${name}0(request);
    }
""")

no_arg_method_template = Template("""    int $name();""")
no_arg_method_native_template = Template("""    private static native int ${name}0();""")
no_arg_method_impl_template = Template("""    public final int $name() {
        connection.checkActive();
        return ${name}0();
    }
""")


def generate_jvpp(func_list, base_package, dto_package, inputfile):
    """ Generates JVpp interface and JNI implementation """
    print "Generating JVpp"

    methods = []
    methods_impl = []
    for func in func_list:

        if util.is_notification(func['name']) or util.is_ignored(func['name']):
            # TODO handle notifications
            continue

        camel_case_name = util.underscore_to_camelcase(func['name'])
        camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
        if util.is_reply(camel_case_name):
            continue

        if len(func['args']) == 0:
            methods.append(no_arg_method_template.substitute(name=camel_case_name,
                                                             base_package=base_package,
                                                             dto_package=dto_package))
            methods_impl.append(
                no_arg_method_native_template.substitute(name=camel_case_name,
                                                         base_package=base_package,
                                                         dto_package=dto_package))
            methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
                                                                       base_package=base_package,
                                                                       dto_package=dto_package))
        else:
            methods.append(method_template.substitute(name=camel_case_name,
                                                      request=camel_case_name_upper,
                                                      base_package=base_package,
                                                      dto_package=dto_package))
            methods_impl.append(method_native_template.substitute(name=camel_case_name,
                                                                  request=camel_case_name_upper,
                                                                  base_package=base_package,
                                                                  dto_package=dto_package))
            methods_impl.append(method_impl_template.substitute(name=camel_case_name,
                                                                request=camel_case_name_upper,
                                                                base_package=base_package,
                                                                dto_package=dto_package))

    jvpp_file = open("JVpp.java", 'w')
    jvpp_file.write(
        jvpp_ifc_template.substitute(inputfile=inputfile,
                                     methods="\n".join(methods),
                                     base_package=base_package,
                                     dto_package=dto_package))
    jvpp_file.flush()
    jvpp_file.close()

    jvpp_file = open("JVppImpl.java", 'w')
    jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
                                                  methods="\n".join(methods_impl),
                                                  base_package=base_package,
                                                  dto_package=dto_package))
    jvpp_file.flush()
    jvpp_file.close()