HONEYCOMB-10: jVpp - the new java API. C code and jar file generation
[vpp.git] / vpp-api / java / jvpp / gen / 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, util
17 from string import Template
18
19 dto_template = Template("""
20 package $base_package.$dto_package;
21
22 /**
23  * <p>This class represents $description.
24  * <br>It was generated by dto_gen.py based on $inputfile preparsed data:
25  * <pre>
26 $docs
27  * </pre>
28  */
29 public final class $cls_name implements $base_package.$dto_package.$base_type {
30
31 $fields
32 $methods
33 }
34 """)
35
36 field_template = Template("""    public $type $name;\n""")
37
38 send_template = Template("""    @Override
39     public int send(final $base_package.JVpp jvpp) {
40         return jvpp.$method_name($args);
41     }\n""")
42
43 def generate_dtos(func_list, base_package, dto_package, inputfile):
44     """ Generates dto objects in a dedicated package """
45     print "Generating DTOs"
46
47     if not os.path.exists(dto_package):
48         raise Exception("%s folder is missing" % dto_package)
49
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")
54
55         if util.is_notification(func['name']) or util.is_ignored(func['name']):
56             # TODO handle notifications
57             continue
58
59         fields = ""
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]))
63         methods = ""
64         base_type = ""
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)
73             else:
74                 base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name)
75         else:
76             args = "" if fields is "" else "this"
77             methods = send_template.substitute(method_name=camel_case_method_name,
78                                                base_package=base_package,
79                                                args=args)
80             if util.is_dump(camel_case_dto_name):
81                 base_type += "JVppDump"
82                 description = "vpe.api dump request DTO"
83             else:
84                 base_type += "JVppRequest"
85                 description = "vpe.api request DTO"
86
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,
92                                                fields=fields,
93                                                methods=methods,
94                                                base_package=base_package,
95                                                base_type=base_type,
96                                                dto_package=dto_package))
97         dto_file.flush()
98         dto_file.close()
99
100     flush_dump_reply_dtos(inputfile)
101
102
103 dump_dto_suffix = "ReplyDump"
104 dump_reply_artificial_dtos = {}
105
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)
111
112
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']))
127         dto_file.flush()
128         dto_file.close()
129
130
131 def generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, camel_case_method_name,
132                             func):
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
138
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
144     else:
145         dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
146                                                          'cls_name': cls_name,
147                                                          'fields': fields,
148                                                          'methods': "",
149                                                          'base_package': base_package,
150                                                          'base_type': base_type,
151                                                          'dto_package': dto_package,
152                                                          })