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.
17 from string import Template
21 dto_template = Template("""
22 package $plugin_package.$dto_package;
25 * <p>This class represents $description.
26 * <br>It was generated by dto_gen.py based on $inputfile preparsed data:
31 public final class $cls_name implements $base_package.$dto_package.$base_type {
38 field_template = Template(""" public $type $name;\n""")
40 send_template = Template(""" @Override
41 public int send(final $base_package.JVpp jvpp) throws io.fd.vpp.jvpp.VppInvocationException {
42 return (($plugin_package.JVpp${plugin_name})jvpp).$method_name($args);
46 def generate_dtos(func_list, base_package, plugin_package, plugin_name, dto_package, inputfile):
47 """ Generates dto objects in a dedicated package """
48 print "Generating DTOs"
50 if not os.path.exists(dto_package):
51 raise Exception("%s folder is missing" % dto_package)
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")
58 if util.is_ignored(func['name']) or util.is_control_ping(camel_case_dto_name):
61 fields = generate_dto_fields(camel_case_dto_name, func)
62 methods = generate_dto_base_methods(camel_case_dto_name, func)
65 # 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):
68 description = "reply DTO"
69 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
72 base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name + "Dump")
73 generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package,
74 camel_case_dto_name, camel_case_method_name, func)
76 base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name)
78 args = "" if fields is "" else "this"
79 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,
84 if util.is_dump(camel_case_dto_name):
85 base_type += "JVppDump"
86 description = "dump request DTO"
88 base_type += "JVppRequest"
89 description = "request DTO"
91 write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package,
92 dto_path, fields, func, inputfile, methods)
94 # for structures that are also used as notifications, generate dedicated notification DTO
95 if util.is_notification(func["name"]):
96 base_type = "JVppNotification"
97 description = "notification DTO"
98 camel_case_dto_name = util.add_notification_suffix(camel_case_dto_name)
99 dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")
100 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)
104 flush_dump_reply_dtos(inputfile)
107 def 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)
114 def generate_dto_fields(camel_case_dto_name, func):
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):
121 fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]],
126 tostring_field_template = Template(""" \"$field_name=\" + $field_name + ", " +\n""")
127 tostring_array_field_template = Template(""" \"$field_name=\" + java.util.Arrays.toString($field_name) + ", " +\n""")
128 tostring_template = Template(""" @Override
129 public String toString() {
130 return "$cls_name{" +
131 $fields_tostring "}";
135 def generate_dto_tostring(camel_case_dto_name, func):
137 for t in zip(func['types'], func['args']):
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):
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)
148 tostring_fields += tostring_field_template.substitute(field_name=field_name)
150 return tostring_template.substitute(cls_name=camel_case_dto_name,
151 fields_tostring=tostring_fields[:-8])
154 equals_field_template = Template(""" if (!java.util.Objects.equals(this.$field_name, other.$field_name)) {
157 equals_array_field_template = Template(""" if (!java.util.Arrays.equals(this.$field_name, other.$field_name)) {
160 equals_template = Template(""" @Override
161 public boolean equals(final Object o) {
165 if (o == null || getClass() != o.getClass()) {
169 final $cls_name other = ($cls_name) o;
176 def generate_dto_equals(camel_case_dto_name, func):
178 for t in zip(func['types'], func['args']):
179 field_name = util.underscore_to_camelcase(t[1])
180 # for retval don't generate dto field in Reply
181 if util.is_retval_field(field_name):
185 if util.is_array(util.jni_2_java_type_mapping[t[0]]):
186 equals_fields += equals_array_field_template.substitute(field_name=field_name)
188 equals_fields += equals_field_template.substitute(field_name=field_name)
190 return equals_template.substitute(cls_name=camel_case_dto_name,
191 comparisons=equals_fields)
194 hash_template = Template(""" @Override
195 public int hashCode() {
196 return java.util.Objects.hash($fields);
198 hash_single_array_type_template = Template(""" @Override
199 public int hashCode() {
200 return java.util.Arrays.hashCode($fields);
204 def generate_dto_hash(func):
207 # Special handling for hashCode in case just a single array field is present. Cannot use Objects.equals since the
208 # array is mistaken for a varargs parameter. Instead use Arrays.hashCode in such case.
209 if len(func['args']) == 1:
210 single_type = func['types'][0]
211 single_type_name = func['args'][0]
212 if util.is_array(util.jni_2_java_type_mapping[single_type]):
213 return hash_single_array_type_template.substitute(fields=util.underscore_to_camelcase(single_type_name))
215 for t in zip(func['types'], func['args']):
216 field_name = util.underscore_to_camelcase(t[1])
217 # for retval don't generate dto field in Reply
218 if util.is_retval_field(field_name):
221 hash_fields += field_name + ", "
223 return hash_template.substitute(fields=hash_fields[:-2])
226 def write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path,
227 fields, func, inputfile, methods):
228 dto_file = open(dto_path, 'w')
229 dto_file.write(dto_template.substitute(inputfile=inputfile,
230 description=description,
231 docs=util.api_message_to_javadoc(func),
232 cls_name=camel_case_dto_name,
235 base_package=base_package,
236 plugin_package=plugin_package,
238 dto_package=dto_package))
243 dump_dto_suffix = "ReplyDump"
244 dump_reply_artificial_dtos = {}
247 # Returns request name or special one from unconventional_naming_rep_req map
248 def get_request_name(camel_case_dto_name, func_name):
249 return util.underscore_to_camelcase_upper(
250 util.unconventional_naming_rep_req[func_name]) if func_name in util.unconventional_naming_rep_req \
251 else util.remove_reply_suffix(camel_case_dto_name)
254 def flush_dump_reply_dtos(inputfile):
255 for dump_reply_artificial_dto in dump_reply_artificial_dtos.values():
256 dto_path = os.path.join(dump_reply_artificial_dto['dto_package'],
257 dump_reply_artificial_dto['cls_name'] + ".java")
258 dto_file = open(dto_path, 'w')
259 dto_file.write(dto_template.substitute(inputfile=inputfile,
260 description="dump reply wrapper",
261 docs=dump_reply_artificial_dto['docs'],
262 cls_name=dump_reply_artificial_dto['cls_name'],
263 fields=dump_reply_artificial_dto['fields'],
264 methods=dump_reply_artificial_dto['methods'],
265 plugin_package=dump_reply_artificial_dto['plugin_package'],
266 base_package=dump_reply_artificial_dto['base_package'],
267 base_type=dump_reply_artificial_dto['base_type'],
268 dto_package=dump_reply_artificial_dto['dto_package']))
273 def generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package, camel_case_dto_name,
274 camel_case_method_name, func):
275 base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % (
276 plugin_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump",
277 plugin_package, dto_package, camel_case_dto_name)
278 fields = " public java.util.List<%s> %s = new java.util.ArrayList<>();" % (camel_case_dto_name, camel_case_method_name)
279 cls_name = camel_case_dto_name + dump_dto_suffix
280 # using artificial type for fields, just to bypass the is_array check in base methods generators
281 # the type is not really used
282 artificial_type = 'jstring'
284 # In case of already existing artificial reply dump DTO, just update it
285 # Used for sub-dump dtos
286 if request_dto_name in dump_reply_artificial_dtos.keys():
287 dump_reply_artificial_dtos[request_dto_name]['fields'] += '\n' + fields
288 dump_reply_artificial_dtos[request_dto_name]['field_names'].append(func['name'])
289 dump_reply_artificial_dtos[request_dto_name]['field_types'].append(artificial_type)
290 methods = '\n' + generate_dto_base_methods(dump_reply_artificial_dtos[request_dto_name]['cls_name'],
291 {'args': dump_reply_artificial_dtos[request_dto_name]['field_names'],
292 'types': dump_reply_artificial_dtos[request_dto_name]['field_types']})
293 dump_reply_artificial_dtos[request_dto_name]['methods'] = methods
295 methods = '\n' + generate_dto_base_methods(cls_name, {'args': [func['name']],
296 'types': [artificial_type]})
297 dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
298 'cls_name': cls_name,
300 'field_names': [func['name']],
301 'field_types': [artificial_type],
302 # strip too many newlines at the end of base method block
304 'plugin_package': plugin_package,
305 'base_package': base_package,
306 'base_type': base_type,
307 'dto_package': dto_package})