blob: a043c9459b07035ea4283a86c90533345a41b232 [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
Marek Gradzkie85581c2016-09-28 10:12:04 +020041 public int send(final $base_package.JVpp jvpp) throws io.fd.vpp.jvpp.VppInvocationException {
Marek Gradzki66ea26b2016-07-26 15:28:22 +020042 return (($plugin_package.JVpp${plugin_name})jvpp).$method_name($args);
Maros Marsaleka53b0e22016-09-16 16:17:58 +020043 }""")
Maros Marsalek45a42b52016-04-28 12:29:33 +020044
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
Maros Marsaleka53b0e22016-09-16 16:17:58 +020061 fields = generate_dto_fields(camel_case_dto_name, func)
62 methods = generate_dto_base_methods(camel_case_dto_name, func)
Maros Marsalek45a42b52016-04-28 12:29:33 +020063 base_type = ""
Maros Marsalek45a42b52016-04-28 12:29:33 +020064
Maros Marsalek7becd082016-05-31 17:45:16 +020065 # Generate request/reply or dump/dumpReply even if structure can be used as notification
66 if not util.is_just_notification(func["name"]):
67 if util.is_reply(camel_case_dto_name):
Marek Gradzki66ea26b2016-07-26 15:28:22 +020068 description = "reply DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +020069 request_dto_name = get_request_name(camel_case_dto_name, func['name'])
70 if util.is_details(camel_case_dto_name):
71 # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api
Marek Gradzki66ea26b2016-07-26 15:28:22 +020072 base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name + "Dump")
Maros Marsaleka53b0e22016-09-16 16:17:58 +020073 generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package,
74 camel_case_dto_name, camel_case_method_name, func)
Maros Marsalek7becd082016-05-31 17:45:16 +020075 else:
Marek Gradzki66ea26b2016-07-26 15:28:22 +020076 base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name)
Maros Marsalek7becd082016-05-31 17:45:16 +020077 else:
78 args = "" if fields is "" else "this"
Maros Marsaleka53b0e22016-09-16 16:17:58 +020079 methods += send_template.substitute(method_name=camel_case_method_name,
80 base_package=base_package,
81 plugin_package=plugin_package,
82 plugin_name=plugin_name,
83 args=args)
Maros Marsalek7becd082016-05-31 17:45:16 +020084 if util.is_dump(camel_case_dto_name):
85 base_type += "JVppDump"
Marek Gradzki66ea26b2016-07-26 15:28:22 +020086 description = "dump request DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +020087 else:
88 base_type += "JVppRequest"
Marek Gradzki66ea26b2016-07-26 15:28:22 +020089 description = "request DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +020090
Maros Marsaleka53b0e22016-09-16 16:17:58 +020091 write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package,
92 dto_path, fields, func, inputfile, methods)
Maros Marsalek7becd082016-05-31 17:45:16 +020093
94 # for structures that are also used as notifications, generate dedicated notification DTO
95 if util.is_notification(func["name"]):
96 base_type = "JVppNotification"
Marek Gradzki66ea26b2016-07-26 15:28:22 +020097 description = "notification DTO"
Maros Marsalek7becd082016-05-31 17:45:16 +020098 camel_case_dto_name = util.add_notification_suffix(camel_case_dto_name)
Maros Marsalek7becd082016-05-31 17:45:16 +020099 dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200100 methods = generate_dto_base_methods(camel_case_dto_name, func)
101 write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package,
102 dto_path, fields, func, inputfile, methods)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200103
Marek Gradzkid85036f2016-04-26 12:09:05 +0200104 flush_dump_reply_dtos(inputfile)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200105
106
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200107def generate_dto_base_methods(camel_case_dto_name, func):
108 methods = generate_dto_hash(func)
109 methods += generate_dto_equals(camel_case_dto_name, func)
110 methods += generate_dto_tostring(camel_case_dto_name, func)
111 return methods
112
113
114def generate_dto_fields(camel_case_dto_name, func):
115 fields = ""
116 for t in zip(func['types'], func['args']):
117 # for retval don't generate dto field in Reply
118 field_name = util.underscore_to_camelcase(t[1])
119 if util.is_reply(camel_case_dto_name) and util.is_retval_field(field_name):
120 continue
121 fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]],
122 name=field_name)
123 return fields
124
125
126tostring_field_template = Template(""" \"$field_name=\" + $field_name + ", " +\n""")
127tostring_array_field_template = Template(""" \"$field_name=\" + java.util.Arrays.toString($field_name) + ", " +\n""")
128tostring_template = Template(""" @Override
129 public String toString() {
130 return "$cls_name{" +
131$fields_tostring "}";
132 }\n\n""")
133
134
135def generate_dto_tostring(camel_case_dto_name, func):
136 tostring_fields = ""
137 for t in zip(func['types'], func['args']):
138
139 field_name = util.underscore_to_camelcase(t[1])
140 # for retval don't generate dto field in Reply
141 if util.is_retval_field(field_name):
142 continue
143
144 # handle array types
145 if util.is_array(util.jni_2_java_type_mapping[t[0]]):
146 tostring_fields += tostring_array_field_template.substitute(field_name=field_name)
147 else:
148 tostring_fields += tostring_field_template.substitute(field_name=field_name)
149
150 return tostring_template.substitute(cls_name=camel_case_dto_name,
151 fields_tostring=tostring_fields[:-8])
152
Marek Gradzkiaf0c70c2016-10-25 07:57:30 +0200153equals_other_template = Template("""
154 final $cls_name other = ($cls_name) o;
155\n""")
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200156equals_field_template = Template(""" if (!java.util.Objects.equals(this.$field_name, other.$field_name)) {
157 return false;
158 }\n""")
159equals_array_field_template = Template(""" if (!java.util.Arrays.equals(this.$field_name, other.$field_name)) {
160 return false;
161 }\n""")
162equals_template = Template(""" @Override
163 public boolean equals(final Object o) {
164 if (this == o) {
165 return true;
166 }
167 if (o == null || getClass() != o.getClass()) {
168 return false;
169 }
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200170$comparisons
171 return true;
172 }\n\n""")
173
174
175def generate_dto_equals(camel_case_dto_name, func):
176 equals_fields = ""
177 for t in zip(func['types'], func['args']):
178 field_name = util.underscore_to_camelcase(t[1])
179 # for retval don't generate dto field in Reply
180 if util.is_retval_field(field_name):
181 continue
182
183 # handle array types
184 if util.is_array(util.jni_2_java_type_mapping[t[0]]):
185 equals_fields += equals_array_field_template.substitute(field_name=field_name)
186 else:
187 equals_fields += equals_field_template.substitute(field_name=field_name)
188
Marek Gradzkiaf0c70c2016-10-25 07:57:30 +0200189 if equals_fields != "":
190 equals_fields = equals_other_template.substitute(cls_name=camel_case_dto_name) + equals_fields
191
192 return equals_template.substitute(comparisons=equals_fields)
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200193
194
195hash_template = Template(""" @Override
196 public int hashCode() {
197 return java.util.Objects.hash($fields);
198 }\n\n""")
199hash_single_array_type_template = Template(""" @Override
200 public int hashCode() {
201 return java.util.Arrays.hashCode($fields);
202 }\n\n""")
203
204
205def generate_dto_hash(func):
206 hash_fields = ""
207
208 # Special handling for hashCode in case just a single array field is present. Cannot use Objects.equals since the
209 # array is mistaken for a varargs parameter. Instead use Arrays.hashCode in such case.
210 if len(func['args']) == 1:
211 single_type = func['types'][0]
212 single_type_name = func['args'][0]
213 if util.is_array(util.jni_2_java_type_mapping[single_type]):
214 return hash_single_array_type_template.substitute(fields=util.underscore_to_camelcase(single_type_name))
215
216 for t in zip(func['types'], func['args']):
217 field_name = util.underscore_to_camelcase(t[1])
218 # for retval don't generate dto field in Reply
219 if util.is_retval_field(field_name):
220 continue
221
222 hash_fields += field_name + ", "
223
224 return hash_template.substitute(fields=hash_fields[:-2])
225
226
227def write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path,
228 fields, func, inputfile, methods):
Maros Marsalek7becd082016-05-31 17:45:16 +0200229 dto_file = open(dto_path, 'w')
230 dto_file.write(dto_template.substitute(inputfile=inputfile,
231 description=description,
232 docs=util.api_message_to_javadoc(func),
233 cls_name=camel_case_dto_name,
234 fields=fields,
235 methods=methods,
236 base_package=base_package,
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200237 plugin_package=plugin_package,
Maros Marsalek7becd082016-05-31 17:45:16 +0200238 base_type=base_type,
239 dto_package=dto_package))
240 dto_file.flush()
241 dto_file.close()
242
243
Maros Marsalek45a42b52016-04-28 12:29:33 +0200244dump_dto_suffix = "ReplyDump"
245dump_reply_artificial_dtos = {}
246
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200247
Maros Marsalek45a42b52016-04-28 12:29:33 +0200248# Returns request name or special one from unconventional_naming_rep_req map
249def get_request_name(camel_case_dto_name, func_name):
250 return util.underscore_to_camelcase_upper(
251 util.unconventional_naming_rep_req[func_name]) if func_name in util.unconventional_naming_rep_req \
252 else util.remove_reply_suffix(camel_case_dto_name)
253
254
Marek Gradzkid85036f2016-04-26 12:09:05 +0200255def flush_dump_reply_dtos(inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +0200256 for dump_reply_artificial_dto in dump_reply_artificial_dtos.values():
257 dto_path = os.path.join(dump_reply_artificial_dto['dto_package'],
258 dump_reply_artificial_dto['cls_name'] + ".java")
259 dto_file = open(dto_path, 'w')
Marek Gradzkid85036f2016-04-26 12:09:05 +0200260 dto_file.write(dto_template.substitute(inputfile=inputfile,
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200261 description="dump reply wrapper",
Marek Gradzkid85036f2016-04-26 12:09:05 +0200262 docs=dump_reply_artificial_dto['docs'],
Maros Marsalek45a42b52016-04-28 12:29:33 +0200263 cls_name=dump_reply_artificial_dto['cls_name'],
264 fields=dump_reply_artificial_dto['fields'],
265 methods=dump_reply_artificial_dto['methods'],
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200266 plugin_package=dump_reply_artificial_dto['plugin_package'],
Maros Marsalek45a42b52016-04-28 12:29:33 +0200267 base_package=dump_reply_artificial_dto['base_package'],
268 base_type=dump_reply_artificial_dto['base_type'],
269 dto_package=dump_reply_artificial_dto['dto_package']))
270 dto_file.flush()
271 dto_file.close()
272
273
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200274def generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package, camel_case_dto_name,
275 camel_case_method_name, func):
Maros Marsalek45a42b52016-04-28 12:29:33 +0200276 base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % (
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200277 plugin_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump",
278 plugin_package, dto_package, camel_case_dto_name)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200279 fields = " public java.util.List<%s> %s = new java.util.ArrayList<>();" % (camel_case_dto_name, camel_case_method_name)
280 cls_name = camel_case_dto_name + dump_dto_suffix
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200281 # using artificial type for fields, just to bypass the is_array check in base methods generators
282 # the type is not really used
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200283 artificial_type = 'u8'
Maros Marsalek45a42b52016-04-28 12:29:33 +0200284
285 # In case of already existing artificial reply dump DTO, just update it
286 # Used for sub-dump dtos
287 if request_dto_name in dump_reply_artificial_dtos.keys():
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200288 dump_reply_artificial_dtos[request_dto_name]['fields'] += '\n' + fields
289 dump_reply_artificial_dtos[request_dto_name]['field_names'].append(func['name'])
290 dump_reply_artificial_dtos[request_dto_name]['field_types'].append(artificial_type)
291 methods = '\n' + generate_dto_base_methods(dump_reply_artificial_dtos[request_dto_name]['cls_name'],
292 {'args': dump_reply_artificial_dtos[request_dto_name]['field_names'],
293 'types': dump_reply_artificial_dtos[request_dto_name]['field_types']})
294 dump_reply_artificial_dtos[request_dto_name]['methods'] = methods
Maros Marsalek45a42b52016-04-28 12:29:33 +0200295 else:
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200296 methods = '\n' + generate_dto_base_methods(cls_name, {'args': [func['name']],
297 'types': [artificial_type]})
Marek Gradzkid85036f2016-04-26 12:09:05 +0200298 dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200299 'cls_name': cls_name,
300 'fields': fields,
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200301 'field_names': [func['name']],
302 'field_types': [artificial_type],
303 # strip too many newlines at the end of base method block
304 'methods': methods,
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200305 'plugin_package': plugin_package,
Maros Marsalek45a42b52016-04-28 12:29:33 +0200306 'base_package': base_package,
307 'base_type': base_type,
Maros Marsaleka53b0e22016-09-16 16:17:58 +0200308 'dto_package': dto_package})