blob: 5f8df2a9602063d45c3812a4185f1d80d502eca2 [file] [log] [blame]
Marek Gradzkid85036f2016-04-26 12:09:05 +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# 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
17import argparse
18import importlib
19import sys
Ed Warnickeadeb7492016-07-11 10:29:41 -070020import os
Marek Gradzki2fee4c82016-11-25 08:38:29 +010021import json
Marek Gradzkid85036f2016-04-26 12:09:05 +020022
Marek Gradzki81c7dfc2016-09-29 13:22:35 +020023from jvppgen import types_gen
Ed Warnickeadeb7492016-07-11 10:29:41 -070024from jvppgen import callback_gen
25from jvppgen import notification_gen
26from jvppgen import dto_gen
27from jvppgen import jvpp_callback_facade_gen
28from jvppgen import jvpp_future_facade_gen
29from jvppgen import jvpp_impl_gen
30from jvppgen import jvpp_c_gen
31from jvppgen import util
Marek Gradzkid85036f2016-04-26 12:09:05 +020032
Damjan Marion7cd468a2016-12-19 23:05:39 +010033blacklist = [ "memclnt.api", "flowperpkt.api" ]
34
Marek Gradzkid85036f2016-04-26 12:09:05 +020035# Invocation:
Marek Gradzkie85581c2016-09-28 10:12:04 +020036# ~/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
Marek Gradzkid85036f2016-04-26 12:09:05 +020038#
39# Compilation:
Marek Gradzkie85581c2016-09-28 10:12:04 +020040# ~/Projects/vpp/vpp-api/jvpp/gen/java/io/fd/vpp/jvpp$ javac *.java dto/*.java callback/*.java
Maros Marsalek7becd082016-05-31 17:45:16 +020041#
Marek Gradzkid85036f2016-04-26 12:09:05 +020042# where
43# defs_api_vpp_papi.py - vpe.api in python format (generated by vppapigen)
Marek Gradzkid85036f2016-04-26 12:09:05 +020044
45parser = argparse.ArgumentParser(description='VPP Java API generator')
Marek Gradzki2fee4c82016-11-25 08:38:29 +010046parser.add_argument('-i', action="store", dest="inputfiles", nargs='+')
Marek Gradzki66ea26b2016-07-26 15:28:22 +020047parser.add_argument('--plugin_name', action="store", dest="plugin_name")
Marek Gradzkid85036f2016-04-26 12:09:05 +020048args = parser.parse_args()
49
50sys.path.append(".")
51
Marek Gradzki2fee4c82016-11-25 08:38:29 +010052print "Generating Java API for %s" % args.inputfiles
53print "inputfiles %s" % args.inputfiles
Marek Gradzki66ea26b2016-07-26 15:28:22 +020054plugin_name = args.plugin_name
55print "plugin_name %s" % plugin_name
Marek Gradzki2fee4c82016-11-25 08:38:29 +010056
57cfg = {}
58
59for inputfile in args.inputfiles:
Damjan Marion7cd468a2016-12-19 23:05:39 +010060 if any(substring in inputfile for substring in blacklist):
61 print "WARNING: Imput file %s blacklisted" % inputfile
62 continue
Marek Gradzki2fee4c82016-11-25 08:38:29 +010063 _cfg = json.load(open(inputfile, 'r'))
64 if 'types' in cfg:
65 cfg['types'].extend(_cfg['types'])
66 else:
67 cfg['types'] = _cfg['types']
68 if 'messages' in cfg:
69 cfg['messages'].extend(_cfg['messages'])
70 else:
71 cfg['messages'] = _cfg['messages']
72
Marek Gradzkid85036f2016-04-26 12:09:05 +020073
Marek Gradzkid85036f2016-04-26 12:09:05 +020074def is_request_field(field_name):
75 return field_name not in {'_vl_msg_id', 'client_index', 'context'}
76
77
78def is_response_field(field_name):
79 return field_name not in {'_vl_msg_id'}
80
81
82def get_args(t, filter):
83 arg_list = []
84 for i in t:
Marek Gradzki2fee4c82016-11-25 08:38:29 +010085 if is_crc(i):
86 continue
Marek Gradzkid85036f2016-04-26 12:09:05 +020087 if not filter(i[1]):
88 continue
89 arg_list.append(i[1])
90 return arg_list
91
92
93def get_types(t, filter):
94 types_list = []
Marek Gradzkic4cb44c2016-05-24 13:32:26 +020095 lengths_list = []
Marek Gradzki2fee4c82016-11-25 08:38:29 +010096 crc = None
Marek Gradzkid85036f2016-04-26 12:09:05 +020097 for i in t:
Marek Gradzki2fee4c82016-11-25 08:38:29 +010098 if is_crc(i):
99 crc = ('crc', i['crc'][2:])
100 continue
Marek Gradzkid85036f2016-04-26 12:09:05 +0200101 if not filter(i[1]):
102 continue
103 if len(i) is 3: # array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200104 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +0200105 lengths_list.append((i[2], False))
106 elif len(i) is 4: # variable length array type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200107 types_list.append(i[0] + '[]')
Marek Gradzkifa42e252016-06-15 16:38:33 +0200108 lengths_list.append((i[3], True))
Marek Gradzkid85036f2016-04-26 12:09:05 +0200109 else: # primitive type
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200110 types_list.append(i[0])
Marek Gradzkifa42e252016-06-15 16:38:33 +0200111 lengths_list.append((0, False))
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100112 return types_list, lengths_list, crc
113
114
115def is_crc(arg):
116 """ Check whether the argument inside message definition is just crc """
117 return 'crc' in arg
Marek Gradzkid85036f2016-04-26 12:09:05 +0200118
119
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200120def get_definitions(defs):
Marek Gradzkid85036f2016-04-26 12:09:05 +0200121 # Pass 1
122 func_list = []
123 func_name = {}
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200124 for a in defs:
Marek Gradzkid85036f2016-04-26 12:09:05 +0200125 java_name = util.underscore_to_camelcase(a[0])
126
127 # For replies include all the arguments except message_id
128 if util.is_reply(java_name):
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100129 types, lengths, crc = get_types(a[1:], is_response_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200130 func_name[a[0]] = dict(
131 [('name', a[0]), ('java_name', java_name),
132 ('args', get_args(a[1:], is_response_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100133 ('types', types), ('lengths', lengths), crc])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200134 # For requests skip message_id, client_id and context
135 else:
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100136 types, lengths, crc = get_types(a[1:], is_request_field)
Marek Gradzkid85036f2016-04-26 12:09:05 +0200137 func_name[a[0]] = dict(
138 [('name', a[0]), ('java_name', java_name),
139 ('args', get_args(a[1:], is_request_field)), ('full_args', get_args(a[1:], lambda x: True)),
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100140 ('types', types), ('lengths', lengths), crc])
Marek Gradzkid85036f2016-04-26 12:09:05 +0200141
142 # Indexed by name
143 func_list.append(func_name[a[0]])
144 return func_list, func_name
145
146
Marek Gradzkie85581c2016-09-28 10:12:04 +0200147base_package = 'io.fd.vpp.jvpp'
Marek Gradzki66ea26b2016-07-26 15:28:22 +0200148plugin_package = base_package + '.' + plugin_name
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200149types_package = 'types'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200150dto_package = 'dto'
151callback_package = 'callback'
Maros Marsalek7becd082016-05-31 17:45:16 +0200152notification_package = 'notification'
Marek Gradzkid85036f2016-04-26 12:09:05 +0200153future_package = 'future'
154# TODO find better package name
155callback_facade_package = 'callfacade'
156
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100157types_list, types_name = get_definitions(cfg['types'])
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200158
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100159types_gen.generate_types(types_list, plugin_package, types_package, args.inputfiles)
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200160
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100161func_list, func_name = get_definitions(cfg['messages'])
Marek Gradzki81c7dfc2016-09-29 13:22:35 +0200162
Marek Gradzki2fee4c82016-11-25 08:38:29 +0100163dto_gen.generate_dtos(func_list, base_package, plugin_package, plugin_name.title(), dto_package, args.inputfiles)
164jvpp_impl_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name, dto_package, args.inputfiles)
165callback_gen.generate_callbacks(func_list, base_package, plugin_package, plugin_name.title(), callback_package, dto_package, args.inputfiles)
166notification_gen.generate_notification_registry(func_list, base_package, plugin_package, plugin_name.title(), notification_package, callback_package, dto_package, args.inputfiles)
167jvpp_c_gen.generate_jvpp(func_list, plugin_name, args.inputfiles)
168jvpp_future_facade_gen.generate_jvpp(func_list, base_package, plugin_package, plugin_name.title(), dto_package, callback_package, notification_package, future_package, args.inputfiles)
169jvpp_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)
170
171print "Java API for %s generated successfully" % args.inputfiles