jvpp: include all api files from @top_builddir@/vpp
[vpp.git] / src / vpp-api / java / jvpp / gen / jvpp_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
17 import argparse
18 import sys
19 import os
20 import json
21
22 from jvppgen import types_gen
23 from jvppgen import callback_gen
24 from jvppgen import notification_gen
25 from jvppgen import dto_gen
26 from jvppgen import jvpp_callback_facade_gen
27 from jvppgen import jvpp_future_facade_gen
28 from jvppgen import jvpp_impl_gen
29 from jvppgen import jvpp_c_gen
30 from jvppgen import util
31
32 # Invocation:
33 # ~/Projects/vpp/vpp-api/jvpp/gen$ mkdir -p java/io/fd/vpp/jvpp && cd java/io/fd/vpp/jvpp
34 # ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ ../../../../jvpp_gen.py -idefs_api_vpp_papi.py
35 #
36 # Compilation:
37 # ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ javac *.java dto/*.java callback/*.java
38 #
39 # where
40 # defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
41
42 parser = argparse.ArgumentParser(description='VPP Java API generator')
43 parser.add_argument('-i', action="store", dest="inputfiles", nargs='+')
44 parser.add_argument('--plugin_name', action="store", dest="plugin_name")
45 parser.add_argument('--root_dir', action="store", dest="root_dir")
46 args = parser.parse_args()
47
48 sys.path.append(".")
49 cwd = os.getcwd()
50
51 print "Generating Java API for %s" % args.inputfiles
52 print "inputfiles %s" % args.inputfiles
53 plugin_name = args.plugin_name
54 print "plugin_name %s" % plugin_name
55
56 cfg = {}
57
58 base_package = 'io.fd.vpp.jvpp'
59 plugin_package = base_package + '.' + plugin_name
60 root_dir = os.path.abspath(args.root_dir)
61 print "root_dir %s" % root_dir
62 work_dir = root_dir + "/target/" + plugin_package.replace(".","/")
63
64 try:
65     os.makedirs(work_dir)
66 except OSError:
67     if not os.path.isdir(work_dir):
68         raise
69
70 os.chdir(work_dir)
71
72 for inputfile in args.inputfiles:
73     _cfg = json.load(open(cwd + "/" + inputfile, 'r'))
74     if 'types' in cfg:
75         cfg['types'].extend(_cfg['types'])
76     else:
77         cfg['types'] = _cfg['types']
78     if 'messages' in cfg:
79         cfg['messages'].extend(_cfg['messages'])
80     else:
81         cfg['messages'] = _cfg['messages']
82
83
84 def is_request_field(field_name):
85     return field_name not in {'_vl_msg_id', 'client_index', 'context'}
86
87
88 def is_response_field(field_name):
89     return field_name not in {'_vl_msg_id'}
90
91
92 def get_args(t, filter):
93     arg_names = []
94     arg_types = []
95     for i in t:
96         if is_crc(i):
97             continue
98         if not filter(i[1]):
99             continue
100         arg_types.append(i[0])
101         arg_names.append(i[1])
102     return arg_types, arg_names
103
104
105 def get_types(t, filter):
106     types_list = []
107     lengths_list = []
108     crc = None
109     for i in t:
110         if is_crc(i):
111             crc = ('crc', i['crc'][2:])
112             continue
113         if not filter(i[1]):
114             continue
115         if len(i) is 3:  # array type
116             types_list.append(i[0] + '[]')
117             lengths_list.append((i[2], False))
118         elif len(i) is 4:  # variable length array type
119             types_list.append(i[0] + '[]')
120             lengths_list.append((i[3], True))
121         else:  # primitive type
122             types_list.append(i[0])
123             lengths_list.append((0, False))
124     return types_list, lengths_list, crc
125
126
127 def is_crc(arg):
128     """ Check whether the argument inside message definition is just crc """
129     return 'crc' in arg
130
131
132 def get_definitions(defs):
133     # Pass 1
134     func_list = []
135     func_name = {}
136     for a in defs:
137         java_name = util.underscore_to_camelcase(a[0])
138
139         # For replies include all the arguments except message_id
140         if util.is_reply(java_name):
141             types, lengths, crc = get_types(a[1:], is_response_field)
142             args = get_args(a[1:], is_response_field)
143             func_name[a[0]] = dict(
144                 [('name', a[0]), ('java_name', java_name),
145                  ('args', args[1]), ('arg_types', args[0]),
146                  ('types', types), ('lengths', lengths), crc])
147         # For requests skip message_id, client_id and context
148         else:
149             types, lengths, crc = get_types(a[1:], is_request_field)
150             args = get_args(a[1:], is_request_field)
151             func_name[a[0]] = dict(
152                 [('name', a[0]), ('java_name', java_name),
153                  ('args', args[1]), ('arg_types', args[0]),
154                  ('types', types), ('lengths', lengths), crc])
155
156         # Indexed by name
157         func_list.append(func_name[a[0]])
158     return func_list, func_name
159
160
161 types_package = 'types'
162 dto_package = 'dto'
163 callback_package = 'callback'
164 notification_package = 'notification'
165 future_package = 'future'
166 # TODO find better package name
167 callback_facade_package = 'callfacade'
168
169 types_list, types_name = get_definitions(cfg['types'])
170
171 types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles)
172
173 func_list, func_name = get_definitions(cfg['messages'])
174
175 dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles)
176 jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles)
177 callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfiles)
178 notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfiles)
179 jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles, root_dir)
180 jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, future_package, args.inputfiles)
181 jvpp_callback_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, callback_facade_package, args.inputfiles)
182
183 print "Java API for %s generated successfully" % args.inputfiles