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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from string import Template
20 from util import remove_suffix
22 callback_suffix = "Callback"
24 callback_template = Template("""
25 package $base_package.$callback_package;
28 * <p>Represents callback for vpe.api message.
29 * <br>It was generated by callback_gen.py based on $inputfile preparsed data:
34 public interface $cls_name extends $base_package.$callback_package.JVppCallback {
41 global_callback_template = Template("""
42 package $base_package.$callback_package;
45 * <p>Global aggregated callback interface.
46 * <br>It was generated by callback_gen.py based on $inputfile
47 * <br>(python representation of vpe.api generated by vppapigen).
49 public interface JVppGlobalCallback extends $callbacks {
54 def generate_callbacks(func_list, base_package, callback_package, dto_package, inputfile):
55 """ Generates callback interfaces """
56 print "Generating Callback interfaces"
58 if not os.path.exists(callback_package):
59 raise Exception("%s folder is missing" % callback_package)
62 for func in func_list:
64 if util.is_notification(func['name']) or util.is_ignored(func['name']):
65 # FIXME handle notifications
68 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
69 if not util.is_reply(camel_case_name_with_suffix):
72 camel_case_name = util.remove_reply_suffix(camel_case_name_with_suffix)
73 callbacks.append("{0}.{1}.{2}".format(base_package, callback_package, camel_case_name + callback_suffix))
74 callback_path = os.path.join(callback_package, camel_case_name + callback_suffix + ".java")
75 callback_file = open(callback_path, 'w')
77 reply_type = "%s.%s.%s" % (base_package, dto_package, camel_case_name_with_suffix)
78 method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type)
80 callback_template.substitute(inputfile=inputfile,
81 docs=util.api_message_to_javadoc(func),
82 cls_name=camel_case_name + callback_suffix,
83 callback_method=method,
84 base_package=base_package,
85 callback_package=callback_package))
89 callback_file = open(os.path.join(callback_package, "JVppGlobalCallback.java"), 'w')
90 callback_file.write(global_callback_template.substitute(inputfile=inputfile,
91 callbacks=", ".join(callbacks),
92 base_package=base_package,
93 callback_package=callback_package))