blob: 378d279c6d0bf40571c8a91c79379c9e1b82a1e2 [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
16import os, util
17from string import Template
18
19dto_template = Template("""
20package $base_package.$dto_package;
21
22/**
Marek Gradzkid85036f2016-04-26 12:09:05 +020023 * <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>
Maros Marsalek45a42b52016-04-28 12:29:33 +020028 */
29public final class $cls_name implements $base_package.$dto_package.$base_type {
30
31$fields
32$methods
33}
34""")
35
36field_template = Template(""" public $type $name;\n""")
37
38send_template = Template(""" @Override
39 public int send(final $base_package.JVpp jvpp) {
40 return jvpp.$method_name($args);
41 }\n""")
42
Marek Gradzkid85036f2016-04-26 12:09:05 +020043def generate_dtos(func_list, base_package, dto_package, inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +020044 """ 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):
Marek Gradzkid85036f2016-04-26 12:09:05 +020066 description = "vpe.api reply DTO"
Maros Marsalek45a42b52016-04-28 12:29:33 +020067 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"
Marek Gradzkid85036f2016-04-26 12:09:05 +020082 description = "vpe.api dump request DTO"
Maros Marsalek45a42b52016-04-28 12:29:33 +020083 else:
84 base_type += "JVppRequest"
Marek Gradzkid85036f2016-04-26 12:09:05 +020085 description = "vpe.api request DTO"
Maros Marsalek45a42b52016-04-28 12:29:33 +020086
87 dto_file = open(dto_path, 'w')
Marek Gradzkid85036f2016-04-26 12:09:05 +020088 dto_file.write(dto_template.substitute(inputfile=inputfile,
89 description=description,
90 docs=util.api_message_to_javadoc(func),
Maros Marsalek45a42b52016-04-28 12:29:33 +020091 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
Marek Gradzkid85036f2016-04-26 12:09:05 +0200100 flush_dump_reply_dtos(inputfile)
Maros Marsalek45a42b52016-04-28 12:29:33 +0200101
102
103dump_dto_suffix = "ReplyDump"
104dump_reply_artificial_dtos = {}
105
Maros Marsalek45a42b52016-04-28 12:29:33 +0200106# Returns request name or special one from unconventional_naming_rep_req map
107def 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
Marek Gradzkid85036f2016-04-26 12:09:05 +0200113def flush_dump_reply_dtos(inputfile):
Maros Marsalek45a42b52016-04-28 12:29:33 +0200114 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')
Marek Gradzkid85036f2016-04-26 12:09:05 +0200118 dto_file.write(dto_template.substitute(inputfile=inputfile,
119 description="vpe.api dump reply wrapper",
120 docs=dump_reply_artificial_dto['docs'],
Maros Marsalek45a42b52016-04-28 12:29:33 +0200121 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
131def 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:
Marek Gradzkid85036f2016-04-26 12:09:05 +0200145 dump_reply_artificial_dtos[request_dto_name] = ({'docs': util.api_message_to_javadoc(func),
Maros Marsalek45a42b52016-04-28 12:29:33 +0200146 '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 })