785e47e9ef77624e19c86be9eb6ce20603309370
[vpp.git] / vpp-api / java / jvpp / gen / jvppgen / dto_gen.py
1 #!/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
16 import os
17 from string import Template
18
19 import util
20
21 dto_template = Template("""
22 package $plugin_package.$dto_package;
23
24 /**
25  * <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>
30  */
31 public final class $cls_name implements $base_package.$dto_package.$base_type {
32
33 $fields
34 $methods
35 }
36 """)
37
38 field_template = Template("""    public $type $name;\n""")
39
40 send_template = Template("""    @Override
41     public int send(final $base_package.JVpp jvpp) throws org.openvpp.jvpp.VppInvocationException {
42         return (($plugin_package.JVpp${plugin_name})jvpp).$method_name($args);
43     }\n""")
44
45
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"
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
58         if util.is_ignored(func['name']) or util.is_control_ping(camel_case_dto_name):
59             continue
60
61         fields = ""
62         for t in zip(func['types'], func['args']):
63             # 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
67             fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]],
68                                                 name=field_name)
69         methods = ""
70         base_type = ""
71
72         # 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):
75                 description = "reply DTO"
76                 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
79                     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,
81                                             camel_case_method_name, func)
82                 else:
83                     base_type += "JVppReply<%s.%s.%s>" % (plugin_package, dto_package, request_dto_name)
84             else:
85                 args = "" if fields is "" else "this"
86                 methods = send_template.substitute(method_name=camel_case_method_name,
87                                                    base_package=base_package,
88                                                    plugin_package=plugin_package,
89                                                    plugin_name=plugin_name,
90                                                    args=args)
91                 if util.is_dump(camel_case_dto_name):
92                     base_type += "JVppDump"
93                     description = "dump request DTO"
94                 else:
95                     base_type += "JVppRequest"
96                     description = "request DTO"
97
98             write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
99                            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"
104             description = "notification DTO"
105             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")
108             write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
109                            inputfile, methods)
110
111     flush_dump_reply_dtos(inputfile)
112
113
114 def write_dto_file(base_package, plugin_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
115                    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,
124                                            plugin_package=plugin_package,
125                                            base_type=base_type,
126                                            dto_package=dto_package))
127     dto_file.flush()
128     dto_file.close()
129
130
131 dump_dto_suffix = "ReplyDump"
132 dump_reply_artificial_dtos = {}
133
134 # Returns request name or special one from unconventional_naming_rep_req map
135 def 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
141 def flush_dump_reply_dtos(inputfile):
142     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')
146         dto_file.write(dto_template.substitute(inputfile=inputfile,
147                                                description="dump reply wrapper",
148                                                docs=dump_reply_artificial_dto['docs'],
149                                                cls_name=dump_reply_artificial_dto['cls_name'],
150                                                fields=dump_reply_artificial_dto['fields'],
151                                                methods=dump_reply_artificial_dto['methods'],
152                                                plugin_package=dump_reply_artificial_dto['plugin_package'],
153                                                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
160 def generate_dump_reply_dto(request_dto_name, base_package, plugin_package, dto_package, camel_case_dto_name, camel_case_method_name,
161                             func):
162     base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % (
163         plugin_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump",
164         plugin_package, dto_package, camel_case_dto_name)
165     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:
174         dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
175                                                          'cls_name': cls_name,
176                                                          'fields': fields,
177                                                          'methods': "",
178                                                          'plugin_package': plugin_package,
179                                                          'base_package': base_package,
180                                                          'base_type': base_type,
181                                                          'dto_package': dto_package,
182                                                          })