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 |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 42 | * |
| 43 | * @throws org.openvpp.jvpp.VppInvocationException if send request had failed |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 44 | */ |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 45 | int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException; |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 46 | |
| 47 | @Override |
| 48 | void close(); |
| 49 | |
| 50 | $methods |
| 51 | } |
| 52 | """) |
| 53 | |
| 54 | jvpp_impl_template = Template(""" |
| 55 | package $base_package; |
| 56 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 57 | /** |
| 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 Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 62 | public 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 Sirovatka | c288066 | 2016-05-09 16:41:31 +0200 | [diff] [blame] | 67 | this.connection = java.util.Objects.requireNonNull(connection,"Connection is null"); |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Tibor Sirovatka | 071d610 | 2016-05-13 10:17:51 +0200 | [diff] [blame] | 71 | public void connect($base_package.callback.JVppCallback callback) throws java.io.IOException { |
| 72 | connection.connect(callback); |
| 73 | } |
| 74 | |
| 75 | @Override |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 76 | public void close() { |
| 77 | connection.close(); |
| 78 | } |
| 79 | |
| 80 | @Override |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 81 | public int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException { |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 82 | return request.send(this); |
| 83 | } |
| 84 | |
| 85 | $methods |
| 86 | } |
| 87 | """) |
| 88 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 89 | method_template = Template(""" int $name($base_package.$dto_package.$request request) throws org.openvpp.jvpp.VppInvocationException;""") |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 90 | method_native_template = Template( |
| 91 | """ private static native int ${name}0($base_package.$dto_package.$request request);""") |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 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"); |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 94 | connection.checkActive(); |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 95 | int result=${name}0(request); |
| 96 | if(result<0){ |
| 97 | throw new org.openvpp.jvpp.VppInvocationException("${name}",result); |
| 98 | } |
| 99 | return result; |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 100 | } |
| 101 | """) |
| 102 | |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 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 { |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 106 | connection.checkActive(); |
Tibor Sirovatka | 42bb61f | 2016-05-18 14:54:50 +0200 | [diff] [blame^] | 107 | int result=${name}0(); |
| 108 | if(result<0){ |
| 109 | throw new org.openvpp.jvpp.VppInvocationException("${name}",result); |
| 110 | } |
| 111 | return result; |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 112 | } |
| 113 | """) |
| 114 | |
| 115 | |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 116 | def generate_jvpp(func_list, base_package, dto_package, inputfile): |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 117 | """ 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 Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 160 | jvpp_ifc_template.substitute(inputfile=inputfile, |
| 161 | methods="\n".join(methods), |
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() |
| 166 | |
| 167 | jvpp_file = open("JVppImpl.java", 'w') |
Marek Gradzki | d85036f | 2016-04-26 12:09:05 +0200 | [diff] [blame] | 168 | jvpp_file.write(jvpp_impl_template.substitute(inputfile=inputfile, |
| 169 | methods="\n".join(methods_impl), |
Maros Marsalek | 45a42b5 | 2016-04-28 12:29:33 +0200 | [diff] [blame] | 170 | base_package=base_package, |
| 171 | dto_package=dto_package)) |
| 172 | jvpp_file.flush() |
| 173 | jvpp_file.close() |