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