jvpp: use Python's logging API
[vpp.git] / src / vpp-api / java / jvpp / gen / jvppgen / callback_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
17 import util
18 from string import Template
19
20 callback_suffix = "Callback"
21
22 callback_template = Template("""
23 package $plugin_package.$callback_package;
24
25 /**
26  * <p>Represents callback for plugin's api file message.
27  * <br>It was generated by callback_gen.py based on $inputfile preparsed data:
28  * <pre>
29 $docs
30  * </pre>
31  */
32 public interface $cls_name extends $base_package.$callback_package.$callback_type {
33
34     $callback_method
35
36 }
37 """)
38
39 global_callback_template = Template("""
40 package $plugin_package.$callback_package;
41
42 /**
43  * <p>Global aggregated callback interface.
44  * <br>It was generated by callback_gen.py based on $inputfile
45  * <br>(python representation of api file generated by vppapigen).
46  */
47 public interface JVpp${plugin_name}GlobalCallback extends $base_package.$callback_package.ControlPingCallback, $callbacks {
48 }
49 """)
50
51
52 def generate_callbacks(func_list, base_package, plugin_package, plugin_name, callback_package, dto_package, inputfile,
53                        logger):
54     """ Generates callback interfaces """
55     logger.debug("Generating Callback interfaces for %s" % inputfile)
56
57     if not os.path.exists(callback_package):
58         os.mkdir(callback_package)
59
60     callbacks = []
61     for func in func_list:
62         camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
63
64         if util.is_control_ping(camel_case_name_with_suffix):
65             # Skip control_ping managed by jvpp registry.
66             continue
67         if util.is_dump(func['name']) or util.is_request(func['name'], func_list):
68             continue
69
70         # Generate callbacks for all messages except for dumps and requests (handled by vpp, not client).
71         callback_type = "JVppCallback"
72         callbacks.append("{0}.{1}.{2}".format(plugin_package, callback_package, camel_case_name_with_suffix + callback_suffix))
73         callback_path = os.path.join(callback_package, camel_case_name_with_suffix + callback_suffix + ".java")
74         callback_file = open(callback_path, 'w')
75
76         reply_type = "%s.%s.%s" % (plugin_package, dto_package, camel_case_name_with_suffix)
77         method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type)
78         callback_file.write(
79             callback_template.substitute(inputfile=inputfile,
80                                          docs=util.api_message_to_javadoc(func),
81                                          cls_name=camel_case_name_with_suffix + callback_suffix,
82                                          callback_method=method,
83                                          base_package=base_package,
84                                          plugin_package=plugin_package,
85                                          callback_package=callback_package,
86                                          callback_type=callback_type))
87         callback_file.flush()
88         callback_file.close()
89
90     callback_file = open(os.path.join(callback_package, "JVpp%sGlobalCallback.java" % plugin_name), 'w')
91     callback_file.write(global_callback_template.substitute(inputfile=inputfile,
92                                                             callbacks=", ".join(callbacks),
93                                                             base_package=base_package,
94                                                             plugin_package=plugin_package,
95                                                             plugin_name=plugin_name,
96                                                             callback_package=callback_package))
97     callback_file.flush()
98     callback_file.close()