blob: 5446a694c1bdb2ebcd57bce74b1c593008f98b9c [file] [log] [blame]
Maros Marsalek45a42b52016-04-28 12:29:33 +02001#!/usr/bin/env python
2#
3# Copyright (c) 2016 Cisco and/or its affiliates.
4# 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
9#
10# 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
16import os, util
17from string import Template
18
19jvpp_ifc_template = Template("""
20package $base_package;
21
22public interface JVpp extends java.lang.AutoCloseable {
23
24 /**
25 * Generic dispatch method for sending requests to VPP
26 */
27 int send($base_package.$dto_package.JVppRequest request);
28
29 @Override
30 void close();
31
32$methods
33}
34""")
35
36jvpp_impl_template = Template("""
37package $base_package;
38
39public final class JVppImpl implements $base_package.JVpp {
40
41 private final $base_package.VppConnection connection;
42
43 public JVppImpl(final $base_package.VppConnection connection) {
44 if(connection == null) {
45 throw new java.lang.NullPointerException("Connection is null");
46 }
47 this.connection = connection;
48 }
49
50 @Override
51 public void close() {
52 connection.close();
53 }
54
55 @Override
56 public int send($base_package.$dto_package.JVppRequest request) {
57 return request.send(this);
58 }
59
60$methods
61}
62""")
63
64method_template = Template(""" int $name($base_package.$dto_package.$request request);""")
65method_native_template = Template(
66 """ private static native int ${name}0($base_package.$dto_package.$request request);""")
67method_impl_template = Template(""" public final int $name($base_package.$dto_package.$request request) {
68 if(request == null) {
69 throw new java.lang.NullPointerException("Null request object");
70 }
71 connection.checkActive();
72 return ${name}0(request);
73 }
74""")
75
76no_arg_method_template = Template(""" int $name();""")
77no_arg_method_native_template = Template(""" private static native int ${name}0();""")
78no_arg_method_impl_template = Template(""" public final int $name() {
79 connection.checkActive();
80 return ${name}0();
81 }
82""")
83
84
85def generate_jvpp(func_list, base_package, dto_package):
86 """ Generates JVpp interface and JNI implementation """
87 print "Generating JVpp"
88
89 methods = []
90 methods_impl = []
91 for func in func_list:
92
93 if util.is_notification(func['name']) or util.is_ignored(func['name']):
94 # TODO handle notifications
95 continue
96
97 camel_case_name = util.underscore_to_camelcase(func['name'])
98 camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
99 if util.is_reply(camel_case_name):
100 continue
101
102 if len(func['args']) == 0:
103 methods.append(no_arg_method_template.substitute(name=camel_case_name,
104 base_package=base_package,
105 dto_package=dto_package))
106 methods_impl.append(
107 no_arg_method_native_template.substitute(name=camel_case_name,
108 base_package=base_package,
109 dto_package=dto_package))
110 methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
111 base_package=base_package,
112 dto_package=dto_package))
113 else:
114 methods.append(method_template.substitute(name=camel_case_name,
115 request=camel_case_name_upper,
116 base_package=base_package,
117 dto_package=dto_package))
118 methods_impl.append(method_native_template.substitute(name=camel_case_name,
119 request=camel_case_name_upper,
120 base_package=base_package,
121 dto_package=dto_package))
122 methods_impl.append(method_impl_template.substitute(name=camel_case_name,
123 request=camel_case_name_upper,
124 base_package=base_package,
125 dto_package=dto_package))
126
127 jvpp_file = open("JVpp.java", 'w')
128 jvpp_file.write(
129 jvpp_ifc_template.substitute(methods="\n".join(methods),
130 base_package=base_package,
131 dto_package=dto_package))
132 jvpp_file.flush()
133 jvpp_file.close()
134
135 jvpp_file = open("JVppImpl.java", 'w')
136 jvpp_file.write(jvpp_impl_template.substitute(methods="\n".join(methods_impl),
137 base_package=base_package,
138 dto_package=dto_package))
139 jvpp_file.flush()
140 jvpp_file.close()