blob: 17fde68a26d68fefb4f7392a25c8fe8968c8d11f [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/**
23 * $docs
24 */
25public final class $cls_name implements $base_package.$dto_package.$base_type {
26
27$fields
28$methods
29}
30""")
31
32field_template = Template(""" public $type $name;\n""")
33
34send_template = Template(""" @Override
35 public int send(final $base_package.JVpp jvpp) {
36 return jvpp.$method_name($args);
37 }\n""")
38
39
40def generate_dtos(func_list, base_package, dto_package):
41 """ Generates dto objects in a dedicated package """
42 print "Generating DTOs"
43
44 if not os.path.exists(dto_package):
45 raise Exception("%s folder is missing" % dto_package)
46
47 for func in func_list:
48 camel_case_dto_name = util.underscore_to_camelcase_upper(func['name'])
49 camel_case_method_name = util.underscore_to_camelcase(func['name'])
50 dto_path = os.path.join(dto_package, camel_case_dto_name + ".java")
51
52 if util.is_notification(func['name']) or util.is_ignored(func['name']):
53 # TODO handle notifications
54 continue
55
56 fields = ""
57 for t in zip(func['types'], func['args']):
58 fields += field_template.substitute(type=util.jni_2_java_type_mapping[t[0]],
59 name=util.underscore_to_camelcase(t[1]))
60 methods = ""
61 base_type = ""
62 if util.is_reply(camel_case_dto_name):
63 request_dto_name = get_request_name(camel_case_dto_name, func['name'])
64 if util.is_details(camel_case_dto_name):
65 # FIXME assumption that dump calls end with "Dump" suffix. Not enforced in vpe.api
66 base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name + "Dump")
67 generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name,
68 camel_case_method_name, func)
69 else:
70 base_type += "JVppReply<%s.%s.%s>" % (base_package, dto_package, request_dto_name)
71 else:
72 args = "" if fields is "" else "this"
73 methods = send_template.substitute(method_name=camel_case_method_name,
74 base_package=base_package,
75 args=args)
76 if util.is_dump(camel_case_dto_name):
77 base_type += "JVppDump"
78 else:
79 base_type += "JVppRequest"
80
81 dto_file = open(dto_path, 'w')
82 dto_file.write(dto_template.substitute(docs='Generated from ' + str(func),
83 cls_name=camel_case_dto_name,
84 fields=fields,
85 methods=methods,
86 base_package=base_package,
87 base_type=base_type,
88 dto_package=dto_package))
89 dto_file.flush()
90 dto_file.close()
91
92 flush_dump_reply_dtos()
93
94
95dump_dto_suffix = "ReplyDump"
96dump_reply_artificial_dtos = {}
97
98
99# Returns request name or special one from unconventional_naming_rep_req map
100def get_request_name(camel_case_dto_name, func_name):
101 return util.underscore_to_camelcase_upper(
102 util.unconventional_naming_rep_req[func_name]) if func_name in util.unconventional_naming_rep_req \
103 else util.remove_reply_suffix(camel_case_dto_name)
104
105
106def flush_dump_reply_dtos():
107 for dump_reply_artificial_dto in dump_reply_artificial_dtos.values():
108 dto_path = os.path.join(dump_reply_artificial_dto['dto_package'],
109 dump_reply_artificial_dto['cls_name'] + ".java")
110 dto_file = open(dto_path, 'w')
111 dto_file.write(dto_template.substitute(docs=dump_reply_artificial_dto['docs'],
112 cls_name=dump_reply_artificial_dto['cls_name'],
113 fields=dump_reply_artificial_dto['fields'],
114 methods=dump_reply_artificial_dto['methods'],
115 base_package=dump_reply_artificial_dto['base_package'],
116 base_type=dump_reply_artificial_dto['base_type'],
117 dto_package=dump_reply_artificial_dto['dto_package']))
118 dto_file.flush()
119 dto_file.close()
120
121
122def generate_dump_reply_dto(request_dto_name, base_package, dto_package, camel_case_dto_name, camel_case_method_name,
123 func):
124 base_type = "JVppReplyDump<%s.%s.%s, %s.%s.%s>" % (
125 base_package, dto_package, util.remove_reply_suffix(camel_case_dto_name) + "Dump",
126 base_package, dto_package, camel_case_dto_name)
127 fields = " public java.util.List<%s> %s = new java.util.ArrayList<>();" % (camel_case_dto_name, camel_case_method_name)
128 cls_name = camel_case_dto_name + dump_dto_suffix
129
130 # In case of already existing artificial reply dump DTO, just update it
131 # Used for sub-dump dtos
132 if request_dto_name in dump_reply_artificial_dtos.keys():
133 dump_reply_artificial_dtos[request_dto_name]['fields'] = \
134 dump_reply_artificial_dtos[request_dto_name]['fields'] + '\n' + fields
135 else:
136 dump_reply_artificial_dtos[request_dto_name] = ({'docs': 'Dump reply wrapper generated from ' + str(func),
137 'cls_name': cls_name,
138 'fields': fields,
139 'methods': "",
140 'base_package': base_package,
141 'base_type': base_type,
142 'dto_package': dto_package,
143 })