blob: 785e47e9ef77624e19c86be9eb6ce20603309370 [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
Maros Marsalek7becd082016-05-31 17:45:16 +020016import os
Maros Marsalek45a42b52016-04-28 12:29:33 +020017from string import Template
18
Maros Marsalek7becd082016-05-31 17:45:16 +020019import util
20
Maros Marsalek45a42b52016-04-28 12:29:33 +020021dto_template = Template("""
Marek Gradzki66ea26b2016-07-26 15:28:22 +020022package $plugin_package.$dto_package;
Maros Marsalek45a42b52016-04-28 12:29:33 +020023
24/**
Marek Gradzkid85036f2016-04-26 12:09:05 +020025 * <p>This class represents $description.
26 * <br>It was generated by dto_gen.py based on $inputfile preparsed data:
27 * <pre>
28$docs
29 * </pre>
Maros Marsalek45a42b52016-04-28 12:29:33 +020030 */
31public final class $cls_name implements $base_package.$dto_package.$base_type {
32
33$fields
34$methods
35}
36""")
37
38field_template = Template(""" public $type $name;\n""")
39
40send_template = Template(""" @Override
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020041 public int send(final $base_package.JVpp jvpp) throws org.openvpp.jvpp.VppInvocationException {
Marek Gradzki66ea26b2016-07-26 15:28:22 +020042 return (($plugin_package.JVpp${plugin_name})jvpp).$method_name($args);
Maros Marsalek45a42b52016-04-28 12:29:33 +020043 }\n""")
44
Maros Marsalek7becd082016-05-31 17:45:16 +020045
Marek Gradzki66ea26b2016-07-26 15:28:22 +020046def generate_dtos(func_list, base_package, plugin_package, plugin_name, dto_package, inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +020047 """ Generates dto objects in a dedicated package """
48 print "Generating DTOs"
49
50 if not os.path.exists(dto_package):
51 raise Exception("%s folder is missing" % dto_package)
52
53 for func in func_list:
54 camel_case_dto_name = util.underscore_to_camelcase_upper(func['name'])
55 camel_case_method_name = util.underscore_to_camelcase(func['name'])
56 dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")
57
Marek Gradzki66ea26b2016-07-26 15:28:22 +020058 if util.is_ignored(func['name']) or util.is_control_ping(camel_case_dto_name):
Maros Marsalek45a42b52016-04-28 12:29:33 +020059 continue
60
61 fields = ""
62 for t in zip(func['types'], func['args']):
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020063 # for retval don't generate dto field in Reply
64 field_name = util.underscore_to_camelcase(t[1])
65 if util.is_reply(camel_case_dto_name) and util.is_retval_field(field_name):
66 continue
Maros Marsalek45a42b52016-04-28 12:29:33 +020067 fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]],
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020068 name=field_name)
Maros Marsalek45a42b52016-04-28 12:29:33 +020069 methods = ""
70 base_type = ""
Maros Marsalek45a42b52016-04-28 12:29:33 +020071
Maros Marsalek7becd082016-05-31 17:45:16 +020072 # Generate request/reply or dump/dumpReply even if structure can be used as notification
73 if not util.is_just_notification(func["name"]):
74 if util.is_reply(camel_case_dto_name):
Marek Gradzki66ea26b2016-07-26 15:28:22 +020075 description = "reply DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +020076 request_dto_name = get_request_name(camel_case_dto_name, func['name'])
77 if util.is_details(camel_case_dto_name):
78 # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api
Marek Gradzki66ea26b2016-07-26 15:28:22 +020079 base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name + "Dump")
80 generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package, camel_case_dto_name,
Maros Marsalek7becd082016-05-31 17:45:16 +020081 camel_case_method_name, func)
82 else:
Marek Gradzki66ea26b2016-07-26 15:28:22 +020083 base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name)
Maros Marsalek7becd082016-05-31 17:45:16 +020084 else:
85 args = "" if fields is "" else "this"
86 methods = send_template.substitute(method_name=camel_case_method_name,
87 base_package=base_package,
Marek Gradzki66ea26b2016-07-26 15:28:22 +020088 plugin_package=plugin_package,
89 plugin_name=plugin_name,
Maros Marsalek7becd082016-05-31 17:45:16 +020090 args=args)
91 if util.is_dump(camel_case_dto_name):
92 base_type += "JVppDump"
Marek Gradzki66ea26b2016-07-26 15:28:22 +020093 description = "dump request DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +020094 else:
95 base_type += "JVppRequest"
Marek Gradzki66ea26b2016-07-26 15:28:22 +020096 description = "request DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +020097
Marek Gradzki66ea26b2016-07-26 15:28:22 +020098 write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
Maros Marsalek7becd082016-05-31 17:45:16 +020099 inputfile, methods)
100
101 # for structures that are also used as notifications, generate dedicated notification DTO
102 if util.is_notification(func["name"]):
103 base_type = "JVppNotification"
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200104 description = "notification DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +0200105 camel_case_dto_name = util.add_notification_suffix(camel_case_dto_name)
106 methods = ""
107 dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200108 write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
Maros Marsalek7becd082016-05-31 17:45:16 +0200109 inputfile, methods)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200110
Marek Gradzkid85036f2016-04-26 12:09:05 +0200111 flush_dump_reply_dtos(inputfile)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200112
113
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200114def write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
Maros Marsalek7becd082016-05-31 17:45:16 +0200115 inputfile, methods):
116 dto_file = open(dto_path, 'w')
117 dto_file.write(dto_template.substitute(inputfile=inputfile,
118 description=description,
119 docs=util.api_message_to_javadoc(func),
120 cls_name=camel_case_dto_name,
121 fields=fields,
122 methods=methods,
123 base_package=base_package,
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200124 plugin_package=plugin_package,
Maros Marsalek7becd082016-05-31 17:45:16 +0200125 base_type=base_type,
126 dto_package=dto_package))
127 dto_file.flush()
128 dto_file.close()
129
130
Maros Marsalek45a42b52016-04-28 12:29:33 +0200131dump_dto_suffix = "ReplyDump"
132dump_reply_artificial_dtos = {}
133
Maros Marsalek45a42b52016-04-28 12:29:33 +0200134# Returns request name or special one from unconventional_naming_rep_req map
135def get_request_name(camel_case_dto_name, func_name):
136 return util.underscore_to_camelcase_upper(
137 util.unconventional_naming_rep_req[func_name]) if func_name in util.unconventional_naming_rep_req \
138 else util.remove_reply_suffix(camel_case_dto_name)
139
140
Marek Gradzkid85036f2016-04-26 12:09:05 +0200141def flush_dump_reply_dtos(inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +0200142 for dump_reply_artificial_dto in dump_reply_artificial_dtos.values():
143 dto_path = os.path.join(dump_reply_artificial_dto['dto_package'],
144 dump_reply_artificial_dto['cls_name'] + ".java")
145 dto_file = open(dto_path, 'w')
Marek Gradzkid85036f2016-04-26 12:09:05 +0200146 dto_file.write(dto_template.substitute(inputfile=inputfile,
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200147 description="dump reply wrapper",
Marek Gradzkid85036f2016-04-26 12:09:05 +0200148 docs=dump_reply_artificial_dto['docs'],
Maros Marsalek45a42b52016-04-28 12:29:33 +0200149 cls_name=dump_reply_artificial_dto['cls_name'],
150 fields=dump_reply_artificial_dto['fields'],
151 methods=dump_reply_artificial_dto['methods'],
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200152 plugin_package=dump_reply_artificial_dto['plugin_package'],
Maros Marsalek45a42b52016-04-28 12:29:33 +0200153 base_package=dump_reply_artificial_dto['base_package'],
154 base_type=dump_reply_artificial_dto['base_type'],
155 dto_package=dump_reply_artificial_dto['dto_package']))
156 dto_file.flush()
157 dto_file.close()
158
159
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200160def generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package, camel_case_dto_name, camel_case_method_name,
Maros Marsalek45a42b52016-04-28 12:29:33 +0200161 func):
162 base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % (
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200163 plugin_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump",
164 plugin_package, dto_package, camel_case_dto_name)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200165 fields = " public java.util.List<%s> %s = new java.util.ArrayList<>();" % (camel_case_dto_name, camel_case_method_name)
166 cls_name = camel_case_dto_name + dump_dto_suffix
167
168 # In case of already existing artificial reply dump DTO, just update it
169 # Used for sub-dump dtos
170 if request_dto_name in dump_reply_artificial_dtos.keys():
171 dump_reply_artificial_dtos[request_dto_name]['fields'] = \
172 dump_reply_artificial_dtos[request_dto_name]['fields'] + '\n' + fields
173 else:
Marek Gradzkid85036f2016-04-26 12:09:05 +0200174 dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200175 'cls_name': cls_name,
176 'fields': fields,
177 'methods': "",
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200178 'plugin_package': plugin_package,
Maros Marsalek45a42b52016-04-28 12:29:33 +0200179 'base_package': base_package,
180 'base_type': base_type,
181 'dto_package': dto_package,
182 })