blob: dfec6a743def9d438f73cdd5c0ffd1a354c0be3f [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
Marek Gradzkid85036f2016-04-26 12:09:05 +020022
23/**
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).
27 */
Maros Marsalek45a42b52016-04-28 12:29:33 +020028public interface JVpp extends java.lang.AutoCloseable {
29
30 /**
Tibor Sirovatka071d6102016-05-13 10:17:51 +020031 * Generic connect with $base_package.callback.JVppCallback callback handler
32 * providing connecting to VPP
33 *
34 * @param callback JVppCallback instance providing callback handling
35 *
36 * @throws java.io.IOException if connection cannot be initiated
37 */
38 void connect($base_package.callback.JVppCallback callback) throws java.io.IOException;
39
40 /**
Maros Marsalek45a42b52016-04-28 12:29:33 +020041 * Generic dispatch method for sending requests to VPP
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020042 *
43 * @throws org.openvpp.jvpp.VppInvocationException if send request had failed
Maros Marsalek45a42b52016-04-28 12:29:33 +020044 */
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020045 int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException;
Maros Marsalek45a42b52016-04-28 12:29:33 +020046
47 @Override
48 void close();
49
50$methods
51}
52""")
53
54jvpp_impl_template = Template("""
55package $base_package;
56
Marek Gradzkid85036f2016-04-26 12:09:05 +020057/**
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).
61 */
Maros Marsalek45a42b52016-04-28 12:29:33 +020062public final class JVppImpl implements $base_package.JVpp {
63
64 private final $base_package.VppConnection connection;
65
66 public JVppImpl(final $base_package.VppConnection connection) {
Tibor Sirovatkac2880662016-05-09 16:41:31 +020067 this.connection = java.util.Objects.requireNonNull(connection,"Connection is null");
Maros Marsalek45a42b52016-04-28 12:29:33 +020068 }
69
70 @Override
Tibor Sirovatka071d6102016-05-13 10:17:51 +020071 public void connect($base_package.callback.JVppCallback callback) throws java.io.IOException {
72 connection.connect(callback);
73 }
74
75 @Override
Maros Marsalek45a42b52016-04-28 12:29:33 +020076 public void close() {
77 connection.close();
78 }
79
80 @Override
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020081 public int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException {
Maros Marsalek45a42b52016-04-28 12:29:33 +020082 return request.send(this);
83 }
84
85$methods
86}
87""")
88
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020089method_template = Template(""" int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException;""")
Maros Marsalek45a42b52016-04-28 12:29:33 +020090method_native_template = Template(
91 """ private static native int ${name}0($base_package.$dto_package.$request request);""")
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020092method_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");
Maros Marsalek45a42b52016-04-28 12:29:33 +020094 connection.checkActive();
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020095 int result=${name}0(request);
96 if(result<0){
97 throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
98 }
99 return result;
Maros Marsalek45a42b52016-04-28 12:29:33 +0200100 }
101""")
102
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200103no_arg_method_template = Template(""" int $name() throws org.openvpp.jvpp.VppInvocationException;""")
104no_arg_method_native_template = Template(""" private static native int ${name}0() throws org.openvpp.jvpp.VppInvocationException;""")
105no_arg_method_impl_template = Template(""" public final int $name() throws org.openvpp.jvpp.VppInvocationException {
Maros Marsalek45a42b52016-04-28 12:29:33 +0200106 connection.checkActive();
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +0200107 int result=${name}0();
108 if(result<0){
109 throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
110 }
111 return result;
Maros Marsalek45a42b52016-04-28 12:29:33 +0200112 }
113""")
114
115
Marek Gradzkid85036f2016-04-26 12:09:05 +0200116def generate_jvpp(func_list, base_package, dto_package, inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +0200117 """ Generates JVpp interface and JNI implementation """
118 print "Generating JVpp"
119
120 methods = []
121 methods_impl = []
122 for func in func_list:
123
124 if util.is_notification(func['name']) or util.is_ignored(func['name']):
125 # TODO handle notifications
126 continue
127
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):
131 continue
132
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))
137 methods_impl.append(
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))
144 else:
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))
157
158 jvpp_file = open("JVpp.java", 'w')
159 jvpp_file.write(
Marek Gradzkid85036f2016-04-26 12:09:05 +0200160 jvpp_ifc_template.substitute(inputfile=inputfile,
161 methods="\n".join(methods),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200162 base_package=base_package,
163 dto_package=dto_package))
164 jvpp_file.flush()
165 jvpp_file.close()
166
167 jvpp_file = open("JVppImpl.java", 'w')
Marek Gradzkid85036f2016-04-26 12:09:05 +0200168 jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
169 methods="\n".join(methods_impl),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200170 base_package=base_package,
171 dto_package=dto_package))
172 jvpp_file.flush()
173 jvpp_file.close()