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
19 dto_template = Template("""
20 package $base_package.$dto_package;
23 * <p>This class represents $description.
24 * <br>It was generated by dto_gen.py based on $inputfile preparsed data:
29 public final class $cls_name implements $base_package.$dto_package.$base_type {
36 field_template = Template(""" public $type $name;\n""")
38 send_template = Template(""" @Override
39 public int send(final $base_package.JVpp jvpp) {
40 return jvpp.$method_name($args);
43 def generate_dtos(func_list, base_package, dto_package, inputfile):
44 """ Generates dto objects in a dedicated package """
45 print "Generating DTOs"
47 if not os.path.exists(dto_package):
48 raise Exception("%s folder is missing" % dto_package)
50 for func in func_list:
51 camel_case_dto_name = util.underscore_to_camelcase_upper(func['name'])
52 camel_case_method_name = util.underscore_to_camelcase(func['name'])
53 dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")
55 if util.is_notification(func['name']) or util.is_ignored(func['name']):
56 # TODO handle notifications
60 for t in zip(func['types'], func['args']):
61 fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]],
62 name=util.underscore_to_camelcase(t[1]))
65 if util.is_reply(camel_case_dto_name):
66 description = "vpe.api reply DTO"
67 request_dto_name = get_request_name(camel_case_dto_name, func['name'])
68 if util.is_details(camel_case_dto_name):
69 # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api
70 base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name + "Dump")
71 generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name,
72 camel_case_method_name, func)
74 base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name)
76 args = "" if fields is "" else "this"
77 methods = send_template.substitute(method_name=camel_case_method_name,
78 base_package=base_package,
80 if util.is_dump(camel_case_dto_name):
81 base_type += "JVppDump"
82 description = "vpe.api dump request DTO"
84 base_type += "JVppRequest"
85 description = "vpe.api request DTO"
87 dto_file = open(dto_path, 'w')
88 dto_file.write(dto_template.substitute(inputfile=inputfile,
89 description=description,
90 docs=util.api_message_to_javadoc(func),
91 cls_name=camel_case_dto_name,
94 base_package=base_package,
96 dto_package=dto_package))
100 flush_dump_reply_dtos(inputfile)
103 dump_dto_suffix = "ReplyDump"
104 dump_reply_artificial_dtos = {}
106 # Returns request name or special one from unconventional_naming_rep_req map
107 def get_request_name(camel_case_dto_name, func_name):
108 return util.underscore_to_camelcase_upper(
109 util.unconventional_naming_rep_req[func_name]) if func_name in util.unconventional_naming_rep_req \
110 else util.remove_reply_suffix(camel_case_dto_name)
113 def flush_dump_reply_dtos(inputfile):
114 for dump_reply_artificial_dto in dump_reply_artificial_dtos.values():
115 dto_path = os.path.join(dump_reply_artificial_dto['dto_package'],
116 dump_reply_artificial_dto['cls_name'] + ".java")
117 dto_file = open(dto_path, 'w')
118 dto_file.write(dto_template.substitute(inputfile=inputfile,
119 description="vpe.api dump reply wrapper",
120 docs=dump_reply_artificial_dto['docs'],
121 cls_name=dump_reply_artificial_dto['cls_name'],
122 fields=dump_reply_artificial_dto['fields'],
123 methods=dump_reply_artificial_dto['methods'],
124 base_package=dump_reply_artificial_dto['base_package'],
125 base_type=dump_reply_artificial_dto['base_type'],
126 dto_package=dump_reply_artificial_dto['dto_package']))
131 def generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, camel_case_method_name,
133 base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % (
134 base_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump",
135 base_package, dto_package, camel_case_dto_name)
136 fields = " public java.util.List<%s> %s = new java.util.ArrayList<>();" % (camel_case_dto_name, camel_case_method_name)
137 cls_name = camel_case_dto_name + dump_dto_suffix
139 # In case of already existing artificial reply dump DTO, just update it
140 # Used for sub-dump dtos
141 if request_dto_name in dump_reply_artificial_dtos.keys():
142 dump_reply_artificial_dtos[request_dto_name]['fields'] = \
143 dump_reply_artificial_dtos[request_dto_name]['fields'] + '\n' + fields
145 dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
146 'cls_name': cls_name,
149 'base_package': base_package,
150 'base_type': base_type,
151 'dto_package': dto_package,