Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 1 | #!/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 | |
| 16 | import os, util |
| 17 | from string import Template |
| 18 | |
| 19 | jvpp_ifc_template = Template(""" |
| 20 | package $base_package; |
| 21 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 22 | |
| 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 Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 28 | public interface JVpp extends java.lang.AutoCloseable { |
| 29 | |
| 30 | /** |
Tibor Sirovatka | 071d610 | 2016-05-13 10:17:51 +0200 | [diff] [blame^] | 31 | * 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 Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 41 | * Generic dispatch method for sending requests to VPP |
| 42 | */ |
| 43 | int send($base_package.$dto_package.JVppRequest request); |
| 44 | |
| 45 | @Override |
| 46 | void close(); |
| 47 | |
| 48 | $methods |
| 49 | } |
| 50 | """) |
| 51 | |
| 52 | jvpp_impl_template = Template(""" |
| 53 | package $base_package; |
| 54 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 55 | /** |
| 56 | * <p>Default implementation of JVpp interface. |
| 57 | * <br>It was generated by jvpp_impl_gen.py based on $inputfile |
| 58 | * <br>(python representation of vpe.api generated by vppapigen). |
| 59 | */ |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 60 | public final class JVppImpl implements $base_package.JVpp { |
| 61 | |
| 62 | private final $base_package.VppConnection connection; |
| 63 | |
| 64 | public JVppImpl(final $base_package.VppConnection connection) { |
Tibor Sirovatka | c288066 | 2016-05-09 16:41:31 +0200 | [diff] [blame] | 65 | this.connection = java.util.Objects.requireNonNull(connection,"Connection is null"); |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
Tibor Sirovatka | 071d610 | 2016-05-13 10:17:51 +0200 | [diff] [blame^] | 69 | public void connect($base_package.callback.JVppCallback callback) throws java.io.IOException { |
| 70 | connection.connect(callback); |
| 71 | } |
| 72 | |
| 73 | @Override |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 74 | public void close() { |
| 75 | connection.close(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int send($base_package.$dto_package.JVppRequest request) { |
| 80 | return request.send(this); |
| 81 | } |
| 82 | |
| 83 | $methods |
| 84 | } |
| 85 | """) |
| 86 | |
| 87 | method_template = Template(""" int $name($base_package.$dto_package.$request request);""") |
| 88 | method_native_template = Template( |
| 89 | """ private static native int ${name}0($base_package.$dto_package.$request request);""") |
| 90 | method_impl_template = Template(""" public final int $name($base_package.$dto_package.$request request) { |
| 91 | if(request == null) { |
| 92 | throw new java.lang.NullPointerException("Null request object"); |
| 93 | } |
| 94 | connection.checkActive(); |
| 95 | return ${name}0(request); |
| 96 | } |
| 97 | """) |
| 98 | |
| 99 | no_arg_method_template = Template(""" int $name();""") |
| 100 | no_arg_method_native_template = Template(""" private static native int ${name}0();""") |
| 101 | no_arg_method_impl_template = Template(""" public final int $name() { |
| 102 | connection.checkActive(); |
| 103 | return ${name}0(); |
| 104 | } |
| 105 | """) |
| 106 | |
| 107 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 108 | def generate_jvpp(func_list, base_package, dto_package, inputfile): |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 109 | """ Generates JVpp interface and JNI implementation """ |
| 110 | print "Generating JVpp" |
| 111 | |
| 112 | methods = [] |
| 113 | methods_impl = [] |
| 114 | for func in func_list: |
| 115 | |
| 116 | if util.is_notification(func['name']) or util.is_ignored(func['name']): |
| 117 | # TODO handle notifications |
| 118 | continue |
| 119 | |
| 120 | camel_case_name = util.underscore_to_camelcase(func['name']) |
| 121 | camel_case_name_upper = util.underscore_to_camelcase_upper(func['name']) |
| 122 | if util.is_reply(camel_case_name): |
| 123 | continue |
| 124 | |
| 125 | if len(func['args']) == 0: |
| 126 | methods.append(no_arg_method_template.substitute(name=camel_case_name, |
| 127 | base_package=base_package, |
| 128 | dto_package=dto_package)) |
| 129 | methods_impl.append( |
| 130 | no_arg_method_native_template.substitute(name=camel_case_name, |
| 131 | base_package=base_package, |
| 132 | dto_package=dto_package)) |
| 133 | methods_impl.append(no_arg_method_impl_template.substitute(name=camel_case_name, |
| 134 | base_package=base_package, |
| 135 | dto_package=dto_package)) |
| 136 | else: |
| 137 | methods.append(method_template.substitute(name=camel_case_name, |
| 138 | request=camel_case_name_upper, |
| 139 | base_package=base_package, |
| 140 | dto_package=dto_package)) |
| 141 | methods_impl.append(method_native_template.substitute(name=camel_case_name, |
| 142 | request=camel_case_name_upper, |
| 143 | base_package=base_package, |
| 144 | dto_package=dto_package)) |
| 145 | methods_impl.append(method_impl_template.substitute(name=camel_case_name, |
| 146 | request=camel_case_name_upper, |
| 147 | base_package=base_package, |
| 148 | dto_package=dto_package)) |
| 149 | |
| 150 | jvpp_file = open("JVpp.java", 'w') |
| 151 | jvpp_file.write( |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 152 | jvpp_ifc_template.substitute(inputfile=inputfile, |
| 153 | methods="\n".join(methods), |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 154 | base_package=base_package, |
| 155 | dto_package=dto_package)) |
| 156 | jvpp_file.flush() |
| 157 | jvpp_file.close() |
| 158 | |
| 159 | jvpp_file = open("JVppImpl.java", 'w') |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 160 | jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile, |
| 161 | methods="\n".join(methods_impl), |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 162 | base_package=base_package, |
| 163 | dto_package=dto_package)) |
| 164 | jvpp_file.flush() |
| 165 | jvpp_file.close() |