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;
24 * <p>Java representation of vpe.api.
25 * <br>It was generated by jvpp_impl_gen.py based on $inputfile
26 * <br>(python representation of vpe.api generated by vppapigen).
28 public interface JVpp extends java.lang.AutoCloseable {
31 * Generic connect with $base_package.callback.JVppCallback callback handler
32 * providing connecting to VPP
34 * @param callback JVppCallback instance providing callback handling
36 * @throws java.io.IOException if connection cannot be initiated
38 void connect($base_package.callback.JVppCallback callback) throws java.io.IOException;
41 * Generic dispatch method for sending requests to VPP
43 * @throws org.openvpp.jvpp.VppInvocationException if send request had failed
45 int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException;
54 jvpp_impl_template = Template("""
55 package $base_package;
58 * <p>Default implementation of JVpp interface.
59 * <br>It was generated by jvpp_impl_gen.py based on $inputfile
60 * <br>(python representation of vpe.api generated by vppapigen).
62 public final class JVppImpl implements $base_package.JVpp {
64 private final $base_package.VppConnection connection;
66 public JVppImpl(final $base_package.VppConnection connection) {
67 this.connection = java.util.Objects.requireNonNull(connection,"Connection is null");
71 public void connect($base_package.callback.JVppCallback callback) throws java.io.IOException {
72 connection.connect(callback);
81 public int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException {
82 return request.send(this);
89 method_template = Template(""" int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException;""")
90 method_native_template = Template(
91 """ private static native int ${name}0($base_package.$dto_package.$request request);""")
92 method_impl_template = Template(""" public final int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException {
93 java.util.Objects.requireNonNull(request,"Null request object");
94 connection.checkActive();
95 int result=${name}0(request);
97 throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
103 no_arg_method_template = Template(""" int $name() throws org.openvpp.jvpp.VppInvocationException;""")
104 no_arg_method_native_template = Template(""" private static native int ${name}0() throws org.openvpp.jvpp.VppInvocationException;""")
105 no_arg_method_impl_template = Template(""" public final int $name() throws org.openvpp.jvpp.VppInvocationException {
106 connection.checkActive();
107 int result=${name}0();
109 throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
116 def generate_jvpp(func_list, base_package, dto_package, inputfile):
117 """ Generates JVpp interface and JNI implementation """
118 print "Generating JVpp"
122 for func in func_list:
124 # Skip structures that are used only as notifications
125 if util.is_just_notification(func['name']) or util.is_ignored(func['name']):
128 camel_case_name = util.underscore_to_camelcase(func['name'])
129 camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
130 if util.is_reply(camel_case_name):
133 if len(func['args']) == 0:
134 methods.append(no_arg_method_template.substitute(name=camel_case_name,
135 base_package=base_package,
136 dto_package=dto_package))
138 no_arg_method_native_template.substitute(name=camel_case_name,
139 base_package=base_package,
140 dto_package=dto_package))
141 methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
142 base_package=base_package,
143 dto_package=dto_package))
145 methods.append(method_template.substitute(name=camel_case_name,
146 request=camel_case_name_upper,
147 base_package=base_package,
148 dto_package=dto_package))
149 methods_impl.append(method_native_template.substitute(name=camel_case_name,
150 request=camel_case_name_upper,
151 base_package=base_package,
152 dto_package=dto_package))
153 methods_impl.append(method_impl_template.substitute(name=camel_case_name,
154 request=camel_case_name_upper,
155 base_package=base_package,
156 dto_package=dto_package))
158 jvpp_file = open("JVpp.java", 'w')
160 jvpp_ifc_template.substitute(inputfile=inputfile,
161 methods="\n".join(methods),
162 base_package=base_package,
163 dto_package=dto_package))
167 jvpp_file = open("JVppImpl.java", 'w')
168 jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
169 methods="\n".join(methods_impl),
170 base_package=base_package,
171 dto_package=dto_package))