blob: 426cd96b2acc7bdd8a6437b6bb3f88d09d440cbb [file] [log] [blame]
Maros Marsalek45a42b52016-04-28 12:29:33 +02001#!/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
Maros Marsalek7becd082016-05-31 17:45:16 +020016import os
Maros Marsalek45a42b52016-04-28 12:29:33 +020017from string import Template
18
Maros Marsalek7becd082016-05-31 17:45:16 +020019import util
20
Maros Marsalek45a42b52016-04-28 12:29:33 +020021dto_template = Template("""
22package $base_package.$dto_package;
23
24/**
Marek Gradzkid85036f2016-04-26 12:09:05 +020025 * <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>
Maros Marsalek45a42b52016-04-28 12:29:33 +020030 */
31public final class $cls_name implements $base_package.$dto_package.$base_type {
32
33$fields
34$methods
35}
36""")
37
38field_template = Template(""" public $type $name;\n""")
39
40send_template = Template(""" @Override
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020041 public int send(final $base_package.JVpp jvpp) throws org.openvpp.jvpp.VppInvocationException {
Maros Marsalek45a42b52016-04-28 12:29:33 +020042 return jvpp.$method_name($args);
43 }\n""")
44
Maros Marsalek7becd082016-05-31 17:45:16 +020045
Marek Gradzkid85036f2016-04-26 12:09:05 +020046def generate_dtos(func_list, base_package, dto_package, inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +020047 """ 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
Maros Marsalek7becd082016-05-31 17:45:16 +020058 if util.is_ignored(func['name']):
Maros Marsalek45a42b52016-04-28 12:29:33 +020059 continue
60
61 fields = ""
62 for t in zip(func['types'], func['args']):
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020063 # 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
Maros Marsalek45a42b52016-04-28 12:29:33 +020067 fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]],
Tibor Sirovatka42bb61f2016-05-18 14:54:50 +020068 name=field_name)
Maros Marsalek45a42b52016-04-28 12:29:33 +020069 methods = ""
70 base_type = ""
Maros Marsalek45a42b52016-04-28 12:29:33 +020071
Maros Marsalek7becd082016-05-31 17:45:16 +020072 # 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)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200108
Marek Gradzkid85036f2016-04-26 12:09:05 +0200109 flush_dump_reply_dtos(inputfile)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200110
111
Maros Marsalek7becd082016-05-31 17:45:16 +0200112def 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
Maros Marsalek45a42b52016-04-28 12:29:33 +0200128dump_dto_suffix = "ReplyDump"
129dump_reply_artificial_dtos = {}
130
Maros Marsalek45a42b52016-04-28 12:29:33 +0200131# Returns request name or special one from unconventional_naming_rep_req map
132def 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
Marek Gradzkid85036f2016-04-26 12:09:05 +0200138def flush_dump_reply_dtos(inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +0200139 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')
Marek Gradzkid85036f2016-04-26 12:09:05 +0200143 dto_file.write(dto_template.substitute(inputfile=inputfile,
144 description="vpe.api dump reply wrapper",
145 docs=dump_reply_artificial_dto['docs'],
Maros Marsalek45a42b52016-04-28 12:29:33 +0200146 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
156def 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:
Marek Gradzkid85036f2016-04-26 12:09:05 +0200170 dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200171 '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 })