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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 from jvppgen import types_gen
24 from jvppgen import callback_gen
25 from jvppgen import notification_gen
26 from jvppgen import dto_gen
27 from jvppgen import jvpp_callback_facade_gen
28 from jvppgen import jvpp_future_facade_gen
29 from jvppgen import jvpp_impl_gen
30 from jvppgen import jvpp_c_gen
31 from jvppgen import util
34 # ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp
35 # ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ ../../../../jvpp_gen.py -idefs_api_vpp_papi.py
38 # ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ javac *.java dto/*.java callback/*.java
41 # defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
43 parser = argparse.ArgumentParser(description='VPP Java API generator')
44 parser.add_argument('-i', action="store", dest="inputfiles", nargs='+')
45 parser.add_argument('--plugin_name', action="store", dest="plugin_name")
46 parser.add_argument('--root_dir', action="store", dest="root_dir")
47 args = parser.parse_args()
54 verbose = int(os.getenv("V", 0))
58 log_level = logging.WARNING
60 log_level = logging.INFO
62 log_level = logging.DEBUG
64 logging.basicConfig(stream=sys.stdout, level=log_level)
65 logger = logging.getLogger("JVPP GEN")
66 logger.setLevel(log_level)
69 logger.info("Generating Java API for %s" % args.inputfiles)
70 plugin_name = args.plugin_name
71 logger.debug("plugin_name: %s" % plugin_name)
75 base_package = 'io.fd.vpp.jvpp'
76 plugin_package = base_package + '.' + plugin_name
77 root_dir = os.path.abspath(args.root_dir)
78 logger.debug("root_dir: %s" % root_dir)
79 work_dir = root_dir + "/target/" + plugin_package.replace(".","/")
84 if not os.path.isdir(work_dir):
89 for inputfile in args.inputfiles:
90 _cfg = json.load(open(cwd + "/" + inputfile, 'r'))
92 cfg['types'].extend(_cfg['types'])
94 cfg['types'] = _cfg['types']
96 cfg['messages'].extend(_cfg['messages'])
98 cfg['messages'] = _cfg['messages']
101 def is_request_field(field_name):
102 return field_name not in {'_vl_msg_id', 'client_index', 'context'}
105 def is_response_field(field_name):
106 return field_name not in {'_vl_msg_id'}
109 def get_args(t, filter):
117 arg_types.append(i[0])
118 arg_names.append(i[1])
119 return arg_types, arg_names
122 def get_types(t, filter):
128 crc = ('crc', i['crc'][2:])
132 if len(i) is 3: # array type
133 types_list.append(i[0] + '[]')
134 lengths_list.append((i[2], False))
135 elif len(i) is 4: # variable length array type
136 types_list.append(i[0] + '[]')
137 lengths_list.append((i[3], True))
138 else: # primitive type
139 types_list.append(i[0])
140 lengths_list.append((0, False))
141 return types_list, lengths_list, crc
145 """ Check whether the argument inside message definition is just crc """
149 def get_definitions(defs):
154 java_name = util.underscore_to_camelcase(a[0])
156 # For replies include all the arguments except message_id
157 if util.is_reply(java_name):
158 types, lengths, crc = get_types(a[1:], is_response_field)
159 args = get_args(a[1:], is_response_field)
160 func_name[a[0]] = dict(
161 [('name', a[0]), ('java_name', java_name),
162 ('args', args[1]), ('arg_types', args[0]),
163 ('types', types), ('lengths', lengths), crc])
164 # For requests skip message_id, client_id and context
166 types, lengths, crc = get_types(a[1:], is_request_field)
167 args = get_args(a[1:], is_request_field)
168 func_name[a[0]] = dict(
169 [('name', a[0]), ('java_name', java_name),
170 ('args', args[1]), ('arg_types', args[0]),
171 ('types', types), ('lengths', lengths), crc])
174 func_list.append(func_name[a[0]])
175 return func_list, func_name
178 types_package = 'types'
180 callback_package = 'callback'
181 notification_package = 'notification'
182 future_package = 'future'
183 # TODO find better package name
184 callback_facade_package = 'callfacade'
186 types_list, types_name = get_definitions(cfg['types'])
188 types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles, logger)
190 func_list, func_name = get_definitions(cfg['messages'])
192 dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles,
194 jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles, logger)
195 callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package,
196 dto_package, args.inputfiles, logger)
197 notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(),
198 notification_package, callback_package, dto_package, args.inputfiles,
200 jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles, root_dir, logger)
201 jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package,
202 callback_package, notification_package, future_package, args.inputfiles, logger)
203 jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package,
204 callback_package, notification_package, callback_facade_package, args.inputfiles,
207 logger.info("Java API for %s generated successfully" % args.inputfiles)