blob: 4e2625714a90f84bc691a5abfd71a637f333b3f3 [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 /**
31 * Generic dispatch method for sending requests to VPP
32 */
33 int send($base_package.$dto_package.JVppRequest request);
34
35 @Override
36 void close();
37
38$methods
39}
40""")
41
42jvpp_impl_template = Template("""
43package $base_package;
44
Marek Gradzkid85036f2016-04-26 12:09:05 +020045/**
46 * <p>Default implementation of JVpp interface.
47 * <br>It was generated by jvpp_impl_gen.py based on $inputfile
48 * <br>(python representation of vpe.api generated by vppapigen).
49 */
Maros Marsalek45a42b52016-04-28 12:29:33 +020050public final class JVppImpl implements $base_package.JVpp {
51
52 private final $base_package.VppConnection connection;
53
54 public JVppImpl(final $base_package.VppConnection connection) {
Tibor Sirovatkac2880662016-05-09 16:41:31 +020055 this.connection = java.util.Objects.requireNonNull(connection,"Connection is null");
Maros Marsalek45a42b52016-04-28 12:29:33 +020056 }
57
58 @Override
59 public void close() {
60 connection.close();
61 }
62
63 @Override
64 public int send($base_package.$dto_package.JVppRequest request) {
65 return request.send(this);
66 }
67
68$methods
69}
70""")
71
72method_template = Template(""" int $name($base_package.$dto_package.$request request);""")
73method_native_template = Template(
74 """ private static native int ${name}0($base_package.$dto_package.$request request);""")
75method_impl_template = Template(""" public final int $name($base_package.$dto_package.$request request) {
76 if(request == null) {
77 throw new java.lang.NullPointerException("Null request object");
78 }
79 connection.checkActive();
80 return ${name}0(request);
81 }
82""")
83
84no_arg_method_template = Template(""" int $name();""")
85no_arg_method_native_template = Template(""" private static native int ${name}0();""")
86no_arg_method_impl_template = Template(""" public final int $name() {
87 connection.checkActive();
88 return ${name}0();
89 }
90""")
91
92
Marek Gradzkid85036f2016-04-26 12:09:05 +020093def generate_jvpp(func_list, base_package, dto_package, inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +020094 """ Generates JVpp interface and JNI implementation """
95 print "Generating JVpp"
96
97 methods = []
98 methods_impl = []
99 for func in func_list:
100
101 if util.is_notification(func['name']) or util.is_ignored(func['name']):
102 # TODO handle notifications
103 continue
104
105 camel_case_name = util.underscore_to_camelcase(func['name'])
106 camel_case_name_upper = util.underscore_to_camelcase_upper(func['name'])
107 if util.is_reply(camel_case_name):
108 continue
109
110 if len(func['args']) == 0:
111 methods.append(no_arg_method_template.substitute(name=camel_case_name,
112 base_package=base_package,
113 dto_package=dto_package))
114 methods_impl.append(
115 no_arg_method_native_template.substitute(name=camel_case_name,
116 base_package=base_package,
117 dto_package=dto_package))
118 methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name,
119 base_package=base_package,
120 dto_package=dto_package))
121 else:
122 methods.append(method_template.substitute(name=camel_case_name,
123 request=camel_case_name_upper,
124 base_package=base_package,
125 dto_package=dto_package))
126 methods_impl.append(method_native_template.substitute(name=camel_case_name,
127 request=camel_case_name_upper,
128 base_package=base_package,
129 dto_package=dto_package))
130 methods_impl.append(method_impl_template.substitute(name=camel_case_name,
131 request=camel_case_name_upper,
132 base_package=base_package,
133 dto_package=dto_package))
134
135 jvpp_file = open("JVpp.java", 'w')
136 jvpp_file.write(
Marek Gradzkid85036f2016-04-26 12:09:05 +0200137 jvpp_ifc_template.substitute(inputfile=inputfile,
138 methods="\n".join(methods),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200139 base_package=base_package,
140 dto_package=dto_package))
141 jvpp_file.flush()
142 jvpp_file.close()
143
144 jvpp_file = open("JVppImpl.java", 'w')
Marek Gradzkid85036f2016-04-26 12:09:05 +0200145 jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile,
146 methods="\n".join(methods_impl),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200147 base_package=base_package,
148 dto_package=dto_package))
149 jvpp_file.flush()
150 jvpp_file.close()