93ffd0fb35916344e3147afe4865f0538aa449dd
[vpp.git] / vpp-api / java / jvpp / gen / jvpp_impl_gen.py
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
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  */
28 public interface JVpp extends java.lang.AutoCloseable {
29
30     /**
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     /**
41      * Generic dispatch method for sending requests to VPP
42      *
43      * @throws org.openvpp.jvpp.VppInvocationException if send request had failed
44      */
45     int send($base_package.$dto_package.JVppRequest request) throws org.openvpp.jvpp.VppInvocationException;
46
47     @Override
48     void close();
49
50 $methods
51 }
52 """)
53
54 jvpp_impl_template = Template("""
55 package $base_package;
56
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  */
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) {
67         this.connection = java.util.Objects.requireNonNull(connection,"Connection is null");
68     }
69
70     @Override
71     public void connect($base_package.callback.JVppCallback callback) throws java.io.IOException {
72         connection.connect(callback);
73     }
74
75     @Override
76     public void close() {
77         connection.close();
78     }
79
80     @Override
81     public int send($base_package.$dto_package.JVppRequest request)  throws org.openvpp.jvpp.VppInvocationException {
82         return request.send(this);
83     }
84
85 $methods
86 }
87 """)
88
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);
96         if(result<0){
97             throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
98         }
99         return result;
100     }
101 """)
102
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();
108         if(result<0){
109             throw new org.openvpp.jvpp.VppInvocationException("${name}",result);
110         }
111         return result;
112     }
113 """)
114
115
116 def generate_jvpp(func_list, base_package, dto_package, inputfile):
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         # Skip structures that are used only as notifications
125         if util.is_just_notification(func['name']) or util.is_ignored(func['name']):
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(
160         jvpp_ifc_template.substitute(inputfile=inputfile,
161                                      methods="\n".join(methods),
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')
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))
172     jvpp_file.flush()
173     jvpp_file.close()