blob: 6d2db422c96c22cbc7ce1dddb78dc7c18976621f [file] [log] [blame]
Ole Troan1b1ccad2019-10-25 18:30:40 +02001#!/usr/bin/env python3
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02002
3import json
4
5
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02006class ParseError (Exception):
7 pass
8
9
10magic_prefix = "vl_api_"
11magic_suffix = "_t"
12
13
14def remove_magic(what):
15 if what.startswith(magic_prefix) and what.endswith(magic_suffix):
16 return what[len(magic_prefix): - len(magic_suffix)]
17 return what
18
19
Klement Sekera958b7502017-09-28 06:31:53 +020020class Field(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020021
Klement Sekera2108c0c2018-08-24 11:43:20 +020022 def __init__(self, field_name, field_type, array_len=None,
23 nelem_field=None):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020024 self.name = field_name
25 self.type = field_type
26 self.len = array_len
27 self.nelem_field = nelem_field
28
29 def __str__(self):
30 if self.len is None:
Klement Sekera34a962b2018-09-06 19:31:36 +020031 return "Field(name: %s, type: %s)" % (self.name, self.type)
Paul Vinciguerrad7a32eb2020-05-01 10:09:58 -040032 elif type(self.len) == dict:
33 return "Field(name: %s, type: %s, length: %s)" % (self.name,
34 self.type,
35 self.len)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020036 elif self.len > 0:
Klement Sekera34a962b2018-09-06 19:31:36 +020037 return "Field(name: %s, type: %s, length: %s)" % (self.name,
38 self.type,
39 self.len)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020040 else:
Klement Sekera34a962b2018-09-06 19:31:36 +020041 return (
42 "Field(name: %s, type: %s, variable length stored in: %s)" %
43 (self.name, self.type, self.nelem_field))
44
45 def is_vla(self):
46 return self.nelem_field is not None
47
48 def has_vla(self):
49 return self.is_vla() or self.type.has_vla()
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020050
51
Ole Troan53fffa12018-11-13 12:36:56 +010052class Alias(Field):
53 pass
54
55
Klement Sekera958b7502017-09-28 06:31:53 +020056class Type(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020057 def __init__(self, name):
58 self.name = name
59
Klement Sekera32a9d7b2017-12-10 05:15:41 +010060 def __str__(self):
61 return self.name
62
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020063
64class SimpleType (Type):
65
Klement Sekera34a962b2018-09-06 19:31:36 +020066 def has_vla(self):
67 return False
68
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020069
Klement Sekera2108c0c2018-08-24 11:43:20 +020070def get_msg_header_defs(struct_type_class, field_class, json_parser, logger):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020071 return [
72 struct_type_class(['msg_header1_t',
73 ['u16', '_vl_msg_id'],
74 ['u32', 'context'],
75 ],
Klement Sekera2108c0c2018-08-24 11:43:20 +020076 json_parser, field_class, logger
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020077 ),
78 struct_type_class(['msg_header2_t',
79 ['u16', '_vl_msg_id'],
80 ['u32', 'client_index'],
81 ['u32', 'context'],
82 ],
Klement Sekera2108c0c2018-08-24 11:43:20 +020083 json_parser, field_class, logger
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020084 ),
85 ]
86
87
Klement Sekera958b7502017-09-28 06:31:53 +020088class Struct(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020089
90 def __init__(self, name, fields):
91 self.name = name
92 self.fields = fields
93 self.field_names = [n.name for n in self.fields]
Klement Sekera2108c0c2018-08-24 11:43:20 +020094 self.depends = [f.type for f in self.fields]
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020095
Klement Sekera32a9d7b2017-12-10 05:15:41 +010096 def __str__(self):
97 return "[%s]" % "], [".join([str(f) for f in self.fields])
98
Klement Sekera34a962b2018-09-06 19:31:36 +020099 def has_vla(self):
100 for f in self.fields:
101 if f.has_vla():
102 return True
103 return False
104
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200105
Klement Sekera2108c0c2018-08-24 11:43:20 +0200106class Enum(SimpleType):
107 def __init__(self, name, value_pairs, enumtype):
108 super(Enum, self).__init__(name)
109 self.type = enumtype
110 self.value_pairs = value_pairs
111
112 def __str__(self):
113 return "Enum(%s, [%s])" % (
114 self.name,
115 "], [" .join(["%s => %s" % (i, j) for i, j in self.value_pairs])
116 )
117
118
119class Union(Type):
120 def __init__(self, name, type_pairs, crc):
121 Type.__init__(self, name)
122 self.crc = crc
123 self.type_pairs = type_pairs
124 self.depends = [t for t, _ in self.type_pairs]
125
126 def __str__(self):
127 return "Union(%s, [%s])" % (
128 self.name,
129 "], [" .join(["%s %s" % (i, j) for i, j in self.type_pairs])
130 )
131
Klement Sekera34a962b2018-09-06 19:31:36 +0200132 def has_vla(self):
133 return False
134
Klement Sekera2108c0c2018-08-24 11:43:20 +0200135
Klement Sekera958b7502017-09-28 06:31:53 +0200136class Message(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200137
Klement Sekera2108c0c2018-08-24 11:43:20 +0200138 def __init__(self, logger, definition, json_parser):
139 struct_type_class = json_parser.struct_type_class
140 field_class = json_parser.field_class
Klement Sekeradc15be22017-06-12 06:49:33 +0200141 self.request = None
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200142 self.logger = logger
143 m = definition
144 logger.debug("Parsing message definition `%s'" % m)
145 name = m[0]
146 self.name = name
147 logger.debug("Message name is `%s'" % name)
148 ignore = True
149 self.header = None
Klement Sekera2108c0c2018-08-24 11:43:20 +0200150 self.is_reply = json_parser.is_reply(self.name)
151 self.is_event = json_parser.is_event(self.name)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200152 fields = []
153 for header in get_msg_header_defs(struct_type_class, field_class,
Klement Sekera2108c0c2018-08-24 11:43:20 +0200154 json_parser, logger):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200155 logger.debug("Probing header `%s'" % header.name)
156 if header.is_part_of_def(m[1:]):
157 self.header = header
158 logger.debug("Found header `%s'" % header.name)
159 fields.append(field_class(field_name='header',
160 field_type=self.header))
161 ignore = False
162 break
Klement Sekera2108c0c2018-08-24 11:43:20 +0200163 if ignore and not self.is_event and not self.is_reply:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200164 raise ParseError("While parsing message `%s': could not find all "
165 "common header fields" % name)
166 for field in m[1:]:
167 if len(field) == 1 and 'crc' in field:
168 self.crc = field['crc']
169 logger.debug("Found CRC `%s'" % self.crc)
170 continue
171 else:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200172 field_type = json_parser.lookup_type_like_id(field[0])
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200173 logger.debug("Parsing message field `%s'" % field)
Ole Troan7adaa222019-08-27 15:05:27 +0200174 l = len(field)
175 if any(type(n) is dict for n in field):
176 l -= 1
177 if l == 2:
178 if self.header is not None and\
179 self.header.has_field(field[1]):
180 continue
181 p = field_class(field_name=field[1],
182 field_type=field_type)
183 elif l == 3:
Ole Troane5ff5a32019-08-23 22:55:18 +0200184 if field[2] == 0 and field[0] != 'string':
Ole Troan7adaa222019-08-27 15:05:27 +0200185 raise ParseError(
186 "While parsing message `%s': variable length "
187 "array `%s' doesn't have reference to member "
188 "containing the actual length" % (
189 name, field[1]))
Ole Troane5ff5a32019-08-23 22:55:18 +0200190 if field[0] == 'string' and field[2] > 0:
191 field_type = json_parser.lookup_type_like_id('u8')
192
Ole Troan7adaa222019-08-27 15:05:27 +0200193 p = field_class(
194 field_name=field[1],
195 field_type=field_type,
196 array_len=field[2])
197 elif l == 4:
198 nelem_field = None
199 for f in fields:
200 if f.name == field[3]:
201 nelem_field = f
202 if nelem_field is None:
203 raise ParseError(
204 "While parsing message `%s': couldn't find "
205 "variable length array `%s' member containing "
206 "the actual length `%s'" % (
207 name, field[1], field[3]))
208 p = field_class(
209 field_name=field[1],
210 field_type=field_type,
211 array_len=field[2],
212 nelem_field=nelem_field)
213 else:
214 raise Exception("Don't know how to parse message "
215 "definition for message `%s': `%s'" %
216 (m, m[1:]))
217 logger.debug("Parsed field `%s'" % p)
218 fields.append(p)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200219 self.fields = fields
Klement Sekera2108c0c2018-08-24 11:43:20 +0200220 self.depends = [f.type for f in self.fields]
Klement Sekera34a962b2018-09-06 19:31:36 +0200221 logger.debug("Parsed message: %s" % self)
222
223 def __str__(self):
224 return "Message(%s, [%s], {crc: %s}" % \
225 (self.name,
226 "], [".join([str(f) for f in self.fields]),
227 self.crc)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200228
229
230class StructType (Type, Struct):
231
Klement Sekera2108c0c2018-08-24 11:43:20 +0200232 def __init__(self, definition, json_parser, field_class, logger):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200233 t = definition
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200234 logger.debug("Parsing struct definition `%s'" % t)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200235 name = t[0]
236 fields = []
237 for field in t[1:]:
238 if len(field) == 1 and 'crc' in field:
239 self.crc = field['crc']
240 continue
Klement Sekera2108c0c2018-08-24 11:43:20 +0200241 field_type = json_parser.lookup_type_like_id(field[0])
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200242 logger.debug("Parsing type field `%s'" % field)
Ole Troan7adaa222019-08-27 15:05:27 +0200243 if len(field) == 2:
244 p = field_class(field_name=field[1],
245 field_type=field_type)
246 elif len(field) == 3:
247 if field[2] == 0:
248 raise ParseError("While parsing type `%s': array `%s' has "
249 "variable length" % (name, field[1]))
250 p = field_class(field_name=field[1],
251 field_type=field_type,
252 array_len=field[2])
253 elif len(field) == 4:
254 nelem_field = None
255 for f in fields:
256 if f.name == field[3]:
257 nelem_field = f
258 if nelem_field is None:
259 raise ParseError(
260 "While parsing message `%s': couldn't find "
261 "variable length array `%s' member containing "
262 "the actual length `%s'" % (
263 name, field[1], field[3]))
264 p = field_class(field_name=field[1],
265 field_type=field_type,
266 array_len=field[2],
267 nelem_field=nelem_field)
268 else:
269 raise ParseError(
270 "Don't know how to parse field `%s' of type definition "
271 "for type `%s'" % (field, t))
272 fields.append(p)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200273 Type.__init__(self, name)
274 Struct.__init__(self, name, fields)
275
Klement Sekera32a9d7b2017-12-10 05:15:41 +0100276 def __str__(self):
277 return "StructType(%s, %s)" % (Type.__str__(self),
278 Struct.__str__(self))
279
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200280 def has_field(self, name):
281 return name in self.field_names
282
283 def is_part_of_def(self, definition):
284 for idx in range(len(self.fields)):
285 field = definition[idx]
286 p = self.fields[idx]
287 if field[1] != p.name:
288 return False
289 if field[0] != p.type.name:
290 raise ParseError(
291 "Unexpected field type `%s' (should be `%s'), "
292 "while parsing msg/def/field `%s/%s/%s'" %
293 (field[0], p.type, p.name, definition, field))
294 return True
295
296
Klement Sekera958b7502017-09-28 06:31:53 +0200297class JsonParser(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200298 def __init__(self, logger, files, simple_type_class=SimpleType,
Klement Sekera2108c0c2018-08-24 11:43:20 +0200299 enum_class=Enum, union_class=Union,
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200300 struct_type_class=StructType, field_class=Field,
Ole Troan53fffa12018-11-13 12:36:56 +0100301 message_class=Message, alias_class=Alias):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200302 self.services = {}
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200303 self.messages = {}
Klement Sekera2108c0c2018-08-24 11:43:20 +0200304 self.enums = {}
305 self.unions = {}
Ole Troan53fffa12018-11-13 12:36:56 +0100306 self.aliases = {}
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200307 self.types = {
308 x: simple_type_class(x) for x in [
309 'i8', 'i16', 'i32', 'i64',
310 'u8', 'u16', 'u32', 'u64',
Filip Vargadd1e3e72019-04-15 18:52:43 +0200311 'f64', 'bool'
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200312 ]
313 }
314
Ole Troan003d5da2018-12-18 12:23:13 +0100315 self.types['string'] = simple_type_class('vl_api_string_t')
Klement Sekera2108c0c2018-08-24 11:43:20 +0200316 self.replies = set()
317 self.events = set()
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200318 self.simple_type_class = simple_type_class
Klement Sekera2108c0c2018-08-24 11:43:20 +0200319 self.enum_class = enum_class
320 self.union_class = union_class
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200321 self.struct_type_class = struct_type_class
322 self.field_class = field_class
Ole Troan53fffa12018-11-13 12:36:56 +0100323 self.alias_class = alias_class
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200324 self.message_class = message_class
325
326 self.exceptions = []
327 self.json_files = []
328 self.types_by_json = {}
Klement Sekera2108c0c2018-08-24 11:43:20 +0200329 self.enums_by_json = {}
330 self.unions_by_json = {}
Ole Troan53fffa12018-11-13 12:36:56 +0100331 self.aliases_by_json = {}
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200332 self.messages_by_json = {}
333 self.logger = logger
334 for f in files:
335 self.parse_json_file(f)
336 self.finalize_parsing()
337
338 def parse_json_file(self, path):
339 self.logger.info("Parsing json api file: `%s'" % path)
340 self.json_files.append(path)
Ole Troan52ca7562018-03-06 17:45:32 +0100341 self.types_by_json[path] = []
Klement Sekera2108c0c2018-08-24 11:43:20 +0200342 self.enums_by_json[path] = []
343 self.unions_by_json[path] = []
Ole Troan53fffa12018-11-13 12:36:56 +0100344 self.aliases_by_json[path] = []
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200345 self.messages_by_json[path] = {}
346 with open(path) as f:
347 j = json.load(f)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200348 for k in j['services']:
349 if k in self.services:
350 raise ParseError("Duplicate service `%s'" % k)
351 self.services[k] = j['services'][k]
352 self.replies.add(self.services[k]["reply"])
353 if "events" in self.services[k]:
354 for x in self.services[k]["events"]:
355 self.events.add(x)
356 for e in j['enums']:
357 name = e[0]
358 value_pairs = e[1:-1]
359 enumtype = self.types[e[-1]["enumtype"]]
360 enum = self.enum_class(name, value_pairs, enumtype)
361 self.enums[enum.name] = enum
362 self.logger.debug("Parsed enum: %s" % enum)
363 self.enums_by_json[path].append(enum)
364 exceptions = []
365 progress = 0
366 last_progress = 0
367 while True:
368 for u in j['unions']:
369 name = u[0]
370 if name in self.unions:
371 progress = progress + 1
372 continue
373 try:
374 type_pairs = [[self.lookup_type_like_id(t), n]
Ole Troan8dbfb432019-04-24 14:31:18 +0200375 for t, n in u[1:]]
376 union = self.union_class(name, type_pairs, 0)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200377 progress = progress + 1
378 except ParseError as e:
379 exceptions.append(e)
380 continue
381 self.unions[union.name] = union
382 self.logger.debug("Parsed union: %s" % union)
383 self.unions_by_json[path].append(union)
384 for t in j['types']:
385 if t[0] in self.types:
386 progress = progress + 1
387 continue
388 try:
389 type_ = self.struct_type_class(t, self,
390 self.field_class,
391 self.logger)
392 if type_.name in self.types:
393 raise ParseError(
394 "Duplicate type `%s'" % type_.name)
395 progress = progress + 1
396 except ParseError as e:
397 exceptions.append(e)
398 continue
399 self.types[type_.name] = type_
400 self.types_by_json[path].append(type_)
401 self.logger.debug("Parsed type: %s" % type_)
Ole Troan1b1ccad2019-10-25 18:30:40 +0200402 for name, body in j['aliases'].items():
Ole Troan75761b92019-09-11 17:49:08 +0200403 if name in self.aliases:
404 progress = progress + 1
405 continue
406 if 'length' in body:
407 array_len = body['length']
408 else:
409 array_len = None
410 try:
411 t = self.lookup_type_like_id(body['type'])
412 except ParseError as e:
413 exceptions.append(e)
414 continue
415 alias = self.alias_class(name, t, array_len)
416 self.aliases[name] = alias
417 self.logger.debug("Parsed alias: %s" % alias)
418 self.aliases_by_json[path].append(alias)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200419 if not exceptions:
420 # finished parsing
421 break
422 if progress <= last_progress:
423 # cannot make forward progress
424 self.exceptions.extend(exceptions)
Klement Sekeraab11ec92018-11-26 15:37:28 +0100425 break
Klement Sekera2108c0c2018-08-24 11:43:20 +0200426 exceptions = []
427 last_progress = progress
428 progress = 0
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200429 prev_length = len(self.messages)
430 processed = []
431 while True:
432 exceptions = []
433 for m in j['messages']:
434 if m in processed:
435 continue
436 try:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200437 msg = self.message_class(self.logger, m, self)
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200438 if msg.name in self.messages:
439 raise ParseError(
440 "Duplicate message `%s'" % msg.name)
441 except ParseError as e:
442 exceptions.append(e)
443 continue
444 self.messages[msg.name] = msg
445 self.messages_by_json[path][msg.name] = msg
446 processed.append(m)
447 if prev_length == len(self.messages):
448 # cannot make forward progress ...
449 self.exceptions.extend(exceptions)
450 break
451 prev_length = len(self.messages)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200452
Klement Sekera2108c0c2018-08-24 11:43:20 +0200453 def lookup_type_like_id(self, name):
454 mundane_name = remove_magic(name)
455 if name in self.types:
456 return self.types[name]
457 elif name in self.enums:
458 return self.enums[name]
459 elif name in self.unions:
460 return self.unions[name]
Ole Troan53fffa12018-11-13 12:36:56 +0100461 elif name in self.aliases:
462 return self.aliases[name]
Klement Sekera2108c0c2018-08-24 11:43:20 +0200463 elif mundane_name in self.types:
464 return self.types[mundane_name]
465 elif mundane_name in self.enums:
466 return self.enums[mundane_name]
467 elif mundane_name in self.unions:
468 return self.unions[mundane_name]
Ole Troan53fffa12018-11-13 12:36:56 +0100469 elif mundane_name in self.aliases:
470 return self.aliases[mundane_name]
Klement Sekera2108c0c2018-08-24 11:43:20 +0200471 raise ParseError(
472 "Could not find type, enum or union by magic name `%s' nor by "
473 "mundane name `%s'" % (name, mundane_name))
474
475 def is_reply(self, message):
476 return message in self.replies
477
478 def is_event(self, message):
479 return message in self.events
480
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200481 def get_reply(self, message):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200482 return self.messages[self.services[message]['reply']]
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200483
484 def finalize_parsing(self):
485 if len(self.messages) == 0:
486 for e in self.exceptions:
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200487 self.logger.warning(e)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200488 for jn, j in self.messages_by_json.items():
489 remove = []
490 for n, m in j.items():
491 try:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200492 if not m.is_reply and not m.is_event:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200493 try:
494 m.reply = self.get_reply(n)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200495 if "stream" in self.services[m.name]:
496 m.reply_is_stream = \
497 self.services[m.name]["stream"]
498 else:
499 m.reply_is_stream = False
Klement Sekeradc15be22017-06-12 06:49:33 +0200500 m.reply.request = m
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200501 except:
502 raise ParseError(
503 "Cannot find reply to message `%s'" % n)
504 except ParseError as e:
505 self.exceptions.append(e)
506 remove.append(n)
507
508 self.messages_by_json[jn] = {
509 k: v for k, v in j.items() if k not in remove}