VPP-330 Track pending map-requests with a fifo
[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 $base_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 jvpp.$method_name($args);
43     }\n""")
44
45
46 def generate_dtos(func_list, base_package, 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']):
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 = "vpe.api 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>" % (base_package, dto_package, request_dto_name + "Dump")
80                     generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name,
81                                             camel_case_method_name, func)
82                 else:
83                     base_type += "JVppReply<%s.%s.%s>" % (base_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                                                    args=args)
89                 if util.is_dump(camel_case_dto_name):
90                     base_type += "JVppDump"
91                     description = "vpe.api dump request DTO"
92                 else:
93                     base_type += "JVppRequest"
94                     description = "vpe.api request DTO"
95
96             write_dto_file(base_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
97                            inputfile, methods)
98
99         # for structures that are also used as notifications, generate dedicated notification DTO
100         if util.is_notification(func["name"]):
101             base_type = "JVppNotification"
102             description = "vpe.api notification DTO"
103             camel_case_dto_name = util.add_notification_suffix(camel_case_dto_name)
104             methods = ""
105             dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")
106             write_dto_file(base_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
107                            inputfile, methods)
108
109     flush_dump_reply_dtos(inputfile)
110
111
112 def write_dto_file(base_package, base_type, camel_case_dto_name, description, dto_package, dto_path, fields, func,
113                    inputfile, methods):
114     dto_file = open(dto_path, 'w')
115     dto_file.write(dto_template.substitute(inputfile=inputfile,
116                                            description=description,
117                                            docs=util.api_message_to_javadoc(func),
118                                            cls_name=camel_case_dto_name,
119                                            fields=fields,
120                                            methods=methods,
121                                            base_package=base_package,
122                                            base_type=base_type,
123                                            dto_package=dto_package))
124     dto_file.flush()
125     dto_file.close()
126
127
128 dump_dto_suffix = "ReplyDump"
129 dump_reply_artificial_dtos = {}
130
131 # Returns request name or special one from unconventional_naming_rep_req map
132 def get_request_name(camel_case_dto_name, func_name):
133     return util.underscore_to_camelcase_upper(
134         util.unconventional_naming_rep_req[func_name]) if func_name in util.unconventional_naming_rep_req \
135         else util.remove_reply_suffix(camel_case_dto_name)
136
137
138 def flush_dump_reply_dtos(inputfile):
139     for dump_reply_artificial_dto in dump_reply_artificial_dtos.values():
140         dto_path = os.path.join(dump_reply_artificial_dto['dto_package'],
141                                 dump_reply_artificial_dto['cls_name'] + ".java")
142         dto_file = open(dto_path, 'w')
143         dto_file.write(dto_template.substitute(inputfile=inputfile,
144                                                description="vpe.api dump reply wrapper",
145                                                docs=dump_reply_artificial_dto['docs'],
146                                                cls_name=dump_reply_artificial_dto['cls_name'],
147                                                fields=dump_reply_artificial_dto['fields'],
148                                                methods=dump_reply_artificial_dto['methods'],
149                                                base_package=dump_reply_artificial_dto['base_package'],
150                                                base_type=dump_reply_artificial_dto['base_type'],
151                                                dto_package=dump_reply_artificial_dto['dto_package']))
152         dto_file.flush()
153         dto_file.close()
154
155
156 def generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, camel_case_method_name,
157                             func):
158     base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % (
159         base_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump",
160         base_package, dto_package, camel_case_dto_name)
161     fields = "    public java.util.List<%s> %s = new java.util.ArrayList<>();" % (camel_case_dto_name, camel_case_method_name)
162     cls_name = camel_case_dto_name + dump_dto_suffix
163
164     # In case of already existing artificial reply dump DTO, just update it
165     # Used for sub-dump dtos
166     if request_dto_name in dump_reply_artificial_dtos.keys():
167         dump_reply_artificial_dtos[request_dto_name]['fields'] = \
168             dump_reply_artificial_dtos[request_dto_name]['fields'] + '\n' + fields
169     else:
170         dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
171                                                          'cls_name': cls_name,
172                                                          'fields': fields,
173                                                          'methods': "",
174                                                          'base_package': base_package,
175                                                          'base_type': base_type,
176                                                          'dto_package': dto_package,
177                                                          })