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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from string import Template
19 jvpp_ifc_template = Template("""
20 package $base_package;
22 public interface JVpp extends java.lang.AutoCloseable {
25 * Generic dispatch method for sending requests to VPP
27 int send($base_package.$dto_package.JVppRequest request);
36 jvpp_impl_template = Template("""
37 package $base_package;
39 public final class JVppImpl implements $base_package.JVpp {
41 private final $base_package.VppConnection connection;
43 public JVppImpl(final $base_package.VppConnection connection) {
44 if(connection == null) {
45 throw new java.lang.NullPointerException("Connection is null");
47 this.connection = connection;
56 public int send($base_package.$dto_package.JVppRequest request) {
57 return request.send(this);
64 method_template = Template(""" int $name($base_package.$dto_package.$request request);""")
65 method_native_template = Template(
66 """ private static native int ${name}0($base_package.$dto_package.$request request);""")
67 method_impl_template = Template(""" public final int $name($base_package.$dto_package.$request request) {
69 throw new java.lang.NullPointerException("Null request object");
71 connection.checkActive();
72 return ${name}0(request);
76 no_arg_method_template = Template(""" int $name();""")
77 no_arg_method_native_template = Template(""" private static native int ${name}0();""")
78 no_arg_method_impl_template = Template(""" public final int $name() {
79 connection.checkActive();
85 def generate_jvpp(func_list, base_package, dto_package):
86 """ Generates JVpp interface and JNI implementation """
87 print "Generating JVpp"
91 for func in func_list:
93 if util.is_notification(func['name']) or util.is_ignored(func['name']):
94 # TODO handle notifications
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):
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))
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))
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))
127 jvpp_file = open("JVpp.java", 'w')
129 jvpp_ifc_template.substitute(methods="\n".join(methods),
130 base_package=base_package,
131 dto_package=dto_package))
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))