blob: eadf3b5c50d316db70d83faac6c49d40fbc9aa93 [file] [log] [blame]
Maros Marsalek45a42b52016-04-28 12:29:33 +02001#!/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
16import os
17import util
18from string import Template
19
20from util import remove_suffix
21
22callback_suffix = "Callback"
23
24callback_template = Template("""
25package $base_package.$callback_package;
26
27/**
Marek Gradzkid85036f2016-04-26 12:09:05 +020028 * <p>Represents callback for vpe.api message.
29 * <br>It was generated by callback_gen.py based on $inputfile preparsed data:
30 * <pre>
31$docs
32 * </pre>
Maros Marsalek45a42b52016-04-28 12:29:33 +020033 */
Maros Marsalek7becd082016-05-31 17:45:16 +020034public interface $cls_name extends $base_package.$callback_package.$callback_type {
Maros Marsalek45a42b52016-04-28 12:29:33 +020035
36 $callback_method
37
38}
39""")
40
41global_callback_template = Template("""
42package $base_package.$callback_package;
43
44/**
Marek Gradzkid85036f2016-04-26 12:09:05 +020045 * <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).
Maros Marsalek45a42b52016-04-28 12:29:33 +020048 */
49public interface JVppGlobalCallback extends $callbacks {
50}
51""")
52
53
Marek Gradzkid85036f2016-04-26 12:09:05 +020054def generate_callbacks(func_list, base_package, callback_package, dto_package, inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +020055 """ Generates callback interfaces """
56 print "Generating Callback interfaces"
57
58 if not os.path.exists(callback_package):
59 raise Exception("%s folder is missing" % callback_package)
60
61 callbacks = []
62 for func in func_list:
63
Maros Marsalek7becd082016-05-31 17:45:16 +020064 if util.is_ignored(func['name']):
Maros Marsalek45a42b52016-04-28 12:29:33 +020065 continue
66
67 camel_case_name_with_suffix = util.underscore_to_camelcase_upper(func['name'])
Maros Marsalek7becd082016-05-31 17:45:16 +020068 if not util.is_reply(camel_case_name_with_suffix) and not util.is_notification(func['name']):
Maros Marsalek45a42b52016-04-28 12:29:33 +020069 continue
70
Maros Marsalek7becd082016-05-31 17:45:16 +020071 if util.is_reply(camel_case_name_with_suffix):
72 camel_case_name = util.remove_reply_suffix(camel_case_name_with_suffix)
73 callback_type = "JVppCallback"
74 else:
75 camel_case_name_with_suffix = util.add_notification_suffix(camel_case_name_with_suffix)
76 camel_case_name = camel_case_name_with_suffix
77 callback_type = "JVppNotificationCallback"
78
Maros Marsalek45a42b52016-04-28 12:29:33 +020079 callbacks.append("{0}.{1}.{2}".format(base_package, callback_package, camel_case_name + callback_suffix))
80 callback_path = os.path.join(callback_package, camel_case_name + callback_suffix + ".java")
81 callback_file = open(callback_path, 'w')
82
83 reply_type = "%s.%s.%s" % (base_package, dto_package, camel_case_name_with_suffix)
84 method = "void on{0}({1} reply);".format(camel_case_name_with_suffix, reply_type)
85 callback_file.write(
Marek Gradzkid85036f2016-04-26 12:09:05 +020086 callback_template.substitute(inputfile=inputfile,
87 docs=util.api_message_to_javadoc(func),
Maros Marsalek45a42b52016-04-28 12:29:33 +020088 cls_name=camel_case_name + callback_suffix,
89 callback_method=method,
90 base_package=base_package,
Maros Marsalek7becd082016-05-31 17:45:16 +020091 callback_package=callback_package,
92 callback_type=callback_type))
Maros Marsalek45a42b52016-04-28 12:29:33 +020093 callback_file.flush()
94 callback_file.close()
95
96 callback_file = open(os.path.join(callback_package, "JVppGlobalCallback.java"), 'w')
Marek Gradzkid85036f2016-04-26 12:09:05 +020097 callback_file.write(global_callback_template.substitute(inputfile=inputfile,
98 callbacks=", ".join(callbacks),
Maros Marsalek45a42b52016-04-28 12:29:33 +020099 base_package=base_package,
100 callback_package=callback_package))
101 callback_file.flush()
102 callback_file.close()