blob: 799542491f681cd7b43077fb04c890ef44e0ec6b [file] [log] [blame]
Klement Sekera958b7502017-09-28 06:31:53 +02001#!/usr/bin/env python2
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:
31 return "name: %s, type: %s" % (self.name, self.type)
32 elif self.len > 0:
33 return "name: %s, type: %s, length: %s" % (self.name, self.type,
34 self.len)
35 else:
36 return ("name: %s, type: %s, variable length stored in: %s" %
37 (self.name, self.type, self.nelem_field))
38
39
Klement Sekera958b7502017-09-28 06:31:53 +020040class Type(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020041 def __init__(self, name):
42 self.name = name
43
Klement Sekera32a9d7b2017-12-10 05:15:41 +010044 def __str__(self):
45 return self.name
46
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020047
48class SimpleType (Type):
49
50 def __init__(self, name):
Klement Sekera958b7502017-09-28 06:31:53 +020051 super(SimpleType, self).__init__(name)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020052
53 def __str__(self):
54 return self.name
55
56
Klement Sekera2108c0c2018-08-24 11:43:20 +020057def get_msg_header_defs(struct_type_class, field_class, json_parser, logger):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020058 return [
59 struct_type_class(['msg_header1_t',
60 ['u16', '_vl_msg_id'],
61 ['u32', 'context'],
62 ],
Klement Sekera2108c0c2018-08-24 11:43:20 +020063 json_parser, field_class, logger
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020064 ),
65 struct_type_class(['msg_header2_t',
66 ['u16', '_vl_msg_id'],
67 ['u32', 'client_index'],
68 ['u32', 'context'],
69 ],
Klement Sekera2108c0c2018-08-24 11:43:20 +020070 json_parser, field_class, logger
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020071 ),
72 ]
73
74
Klement Sekera958b7502017-09-28 06:31:53 +020075class Struct(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020076
77 def __init__(self, name, fields):
78 self.name = name
79 self.fields = fields
80 self.field_names = [n.name for n in self.fields]
Klement Sekera2108c0c2018-08-24 11:43:20 +020081 self.depends = [f.type for f in self.fields]
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020082
Klement Sekera32a9d7b2017-12-10 05:15:41 +010083 def __str__(self):
84 return "[%s]" % "], [".join([str(f) for f in self.fields])
85
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020086
Klement Sekera2108c0c2018-08-24 11:43:20 +020087class Enum(SimpleType):
88 def __init__(self, name, value_pairs, enumtype):
89 super(Enum, self).__init__(name)
90 self.type = enumtype
91 self.value_pairs = value_pairs
92
93 def __str__(self):
94 return "Enum(%s, [%s])" % (
95 self.name,
96 "], [" .join(["%s => %s" % (i, j) for i, j in self.value_pairs])
97 )
98
99
100class Union(Type):
101 def __init__(self, name, type_pairs, crc):
102 Type.__init__(self, name)
103 self.crc = crc
104 self.type_pairs = type_pairs
105 self.depends = [t for t, _ in self.type_pairs]
106
107 def __str__(self):
108 return "Union(%s, [%s])" % (
109 self.name,
110 "], [" .join(["%s %s" % (i, j) for i, j in self.type_pairs])
111 )
112
113
Klement Sekera958b7502017-09-28 06:31:53 +0200114class Message(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200115
Klement Sekera2108c0c2018-08-24 11:43:20 +0200116 def __init__(self, logger, definition, json_parser):
117 struct_type_class = json_parser.struct_type_class
118 field_class = json_parser.field_class
Klement Sekeradc15be22017-06-12 06:49:33 +0200119 self.request = None
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200120 self.logger = logger
121 m = definition
122 logger.debug("Parsing message definition `%s'" % m)
123 name = m[0]
124 self.name = name
125 logger.debug("Message name is `%s'" % name)
126 ignore = True
127 self.header = None
Klement Sekera2108c0c2018-08-24 11:43:20 +0200128 self.is_reply = json_parser.is_reply(self.name)
129 self.is_event = json_parser.is_event(self.name)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200130 fields = []
131 for header in get_msg_header_defs(struct_type_class, field_class,
Klement Sekera2108c0c2018-08-24 11:43:20 +0200132 json_parser, logger):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200133 logger.debug("Probing header `%s'" % header.name)
134 if header.is_part_of_def(m[1:]):
135 self.header = header
136 logger.debug("Found header `%s'" % header.name)
137 fields.append(field_class(field_name='header',
138 field_type=self.header))
139 ignore = False
140 break
Klement Sekera2108c0c2018-08-24 11:43:20 +0200141 if ignore and not self.is_event and not self.is_reply:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200142 raise ParseError("While parsing message `%s': could not find all "
143 "common header fields" % name)
144 for field in m[1:]:
145 if len(field) == 1 and 'crc' in field:
146 self.crc = field['crc']
147 logger.debug("Found CRC `%s'" % self.crc)
148 continue
149 else:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200150 field_type = json_parser.lookup_type_like_id(field[0])
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200151 logger.debug("Parsing message field `%s'" % field)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200152 if len(field) == 2:
153 if self.header is not None and\
154 self.header.has_field(field[1]):
155 continue
156 p = field_class(field_name=field[1],
157 field_type=field_type)
158 elif len(field) == 3:
159 if field[2] == 0:
160 raise ParseError(
161 "While parsing message `%s': variable length "
162 "array `%s' doesn't have reference to member "
163 "containing the actual length" % (
164 name, field[1]))
165 p = field_class(
166 field_name=field[1],
167 field_type=field_type,
168 array_len=field[2])
169 elif len(field) == 4:
170 nelem_field = None
171 for f in fields:
172 if f.name == field[3]:
173 nelem_field = f
174 if nelem_field is None:
175 raise ParseError(
176 "While parsing message `%s': couldn't find "
177 "variable length array `%s' member containing "
178 "the actual length `%s'" % (
179 name, field[1], field[3]))
180 p = field_class(
181 field_name=field[1],
182 field_type=field_type,
183 array_len=field[2],
184 nelem_field=nelem_field)
185 else:
186 raise Exception("Don't know how to parse message "
187 "definition for message `%s': `%s'" %
188 (m, m[1:]))
189 logger.debug("Parsed field `%s'" % p)
190 fields.append(p)
191 self.fields = fields
Klement Sekera2108c0c2018-08-24 11:43:20 +0200192 self.depends = [f.type for f in self.fields]
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200193
194
195class StructType (Type, Struct):
196
Klement Sekera2108c0c2018-08-24 11:43:20 +0200197 def __init__(self, definition, json_parser, field_class, logger):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200198 t = definition
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200199 logger.debug("Parsing struct definition `%s'" % t)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200200 name = t[0]
201 fields = []
202 for field in t[1:]:
203 if len(field) == 1 and 'crc' in field:
204 self.crc = field['crc']
205 continue
Klement Sekera2108c0c2018-08-24 11:43:20 +0200206 field_type = json_parser.lookup_type_like_id(field[0])
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200207 logger.debug("Parsing type field `%s'" % field)
Klement Sekera329a1c12018-02-25 18:48:04 +0100208 if len(field) == 2:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200209 p = field_class(field_name=field[1],
Klement Sekera329a1c12018-02-25 18:48:04 +0100210 field_type=field_type)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200211 elif len(field) == 3:
212 if field[2] == 0:
213 raise ParseError("While parsing type `%s': array `%s' has "
214 "variable length" % (name, field[1]))
215 p = field_class(field_name=field[1],
Klement Sekera329a1c12018-02-25 18:48:04 +0100216 field_type=field_type,
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200217 array_len=field[2])
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200218 elif len(field) == 4:
219 nelem_field = None
220 for f in fields:
221 if f.name == field[3]:
222 nelem_field = f
223 if nelem_field is None:
224 raise ParseError(
225 "While parsing message `%s': couldn't find "
226 "variable length array `%s' member containing "
227 "the actual length `%s'" % (
228 name, field[1], field[3]))
229 p = field_class(field_name=field[1],
230 field_type=field_type,
231 array_len=field[2],
232 nelem_field=nelem_field)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200233 else:
234 raise ParseError(
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200235 "Don't know how to parse field `%s' of type definition "
236 "for type `%s'" % (field, t))
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200237 fields.append(p)
238 Type.__init__(self, name)
239 Struct.__init__(self, name, fields)
240
Klement Sekera32a9d7b2017-12-10 05:15:41 +0100241 def __str__(self):
242 return "StructType(%s, %s)" % (Type.__str__(self),
243 Struct.__str__(self))
244
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200245 def has_field(self, name):
246 return name in self.field_names
247
248 def is_part_of_def(self, definition):
249 for idx in range(len(self.fields)):
250 field = definition[idx]
251 p = self.fields[idx]
252 if field[1] != p.name:
253 return False
254 if field[0] != p.type.name:
255 raise ParseError(
256 "Unexpected field type `%s' (should be `%s'), "
257 "while parsing msg/def/field `%s/%s/%s'" %
258 (field[0], p.type, p.name, definition, field))
259 return True
260
261
Klement Sekera958b7502017-09-28 06:31:53 +0200262class JsonParser(object):
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200263 def __init__(self, logger, files, simple_type_class=SimpleType,
Klement Sekera2108c0c2018-08-24 11:43:20 +0200264 enum_class=Enum, union_class=Union,
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200265 struct_type_class=StructType, field_class=Field,
266 message_class=Message):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200267 self.services = {}
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200268 self.messages = {}
Klement Sekera2108c0c2018-08-24 11:43:20 +0200269 self.enums = {}
270 self.unions = {}
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200271 self.types = {
272 x: simple_type_class(x) for x in [
273 'i8', 'i16', 'i32', 'i64',
274 'u8', 'u16', 'u32', 'u64',
275 'f64'
276 ]
277 }
278
Klement Sekera2108c0c2018-08-24 11:43:20 +0200279 self.replies = set()
280 self.events = set()
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200281 self.simple_type_class = simple_type_class
Klement Sekera2108c0c2018-08-24 11:43:20 +0200282 self.enum_class = enum_class
283 self.union_class = union_class
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200284 self.struct_type_class = struct_type_class
285 self.field_class = field_class
286 self.message_class = message_class
287
288 self.exceptions = []
289 self.json_files = []
290 self.types_by_json = {}
Klement Sekera2108c0c2018-08-24 11:43:20 +0200291 self.enums_by_json = {}
292 self.unions_by_json = {}
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200293 self.messages_by_json = {}
294 self.logger = logger
295 for f in files:
296 self.parse_json_file(f)
297 self.finalize_parsing()
298
299 def parse_json_file(self, path):
300 self.logger.info("Parsing json api file: `%s'" % path)
301 self.json_files.append(path)
Ole Troan52ca7562018-03-06 17:45:32 +0100302 self.types_by_json[path] = []
Klement Sekera2108c0c2018-08-24 11:43:20 +0200303 self.enums_by_json[path] = []
304 self.unions_by_json[path] = []
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200305 self.messages_by_json[path] = {}
306 with open(path) as f:
307 j = json.load(f)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200308 for k in j['services']:
309 if k in self.services:
310 raise ParseError("Duplicate service `%s'" % k)
311 self.services[k] = j['services'][k]
312 self.replies.add(self.services[k]["reply"])
313 if "events" in self.services[k]:
314 for x in self.services[k]["events"]:
315 self.events.add(x)
316 for e in j['enums']:
317 name = e[0]
318 value_pairs = e[1:-1]
319 enumtype = self.types[e[-1]["enumtype"]]
320 enum = self.enum_class(name, value_pairs, enumtype)
321 self.enums[enum.name] = enum
322 self.logger.debug("Parsed enum: %s" % enum)
323 self.enums_by_json[path].append(enum)
324 exceptions = []
325 progress = 0
326 last_progress = 0
327 while True:
328 for u in j['unions']:
329 name = u[0]
330 if name in self.unions:
331 progress = progress + 1
332 continue
333 try:
334 type_pairs = [[self.lookup_type_like_id(t), n]
335 for t, n in u[1:-1]]
336 crc = u[-1]["crc"]
337 union = self.union_class(name, type_pairs, crc)
338 progress = progress + 1
339 except ParseError as e:
340 exceptions.append(e)
341 continue
342 self.unions[union.name] = union
343 self.logger.debug("Parsed union: %s" % union)
344 self.unions_by_json[path].append(union)
345 for t in j['types']:
346 if t[0] in self.types:
347 progress = progress + 1
348 continue
349 try:
350 type_ = self.struct_type_class(t, self,
351 self.field_class,
352 self.logger)
353 if type_.name in self.types:
354 raise ParseError(
355 "Duplicate type `%s'" % type_.name)
356 progress = progress + 1
357 except ParseError as e:
358 exceptions.append(e)
359 continue
360 self.types[type_.name] = type_
361 self.types_by_json[path].append(type_)
362 self.logger.debug("Parsed type: %s" % type_)
363 if not exceptions:
364 # finished parsing
365 break
366 if progress <= last_progress:
367 # cannot make forward progress
368 self.exceptions.extend(exceptions)
369 exceptions = []
370 last_progress = progress
371 progress = 0
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200372 prev_length = len(self.messages)
373 processed = []
374 while True:
375 exceptions = []
376 for m in j['messages']:
377 if m in processed:
378 continue
379 try:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200380 msg = self.message_class(self.logger, m, self)
Klement Sekera8b6b5ab2018-05-03 14:27:42 +0200381 if msg.name in self.messages:
382 raise ParseError(
383 "Duplicate message `%s'" % msg.name)
384 except ParseError as e:
385 exceptions.append(e)
386 continue
387 self.messages[msg.name] = msg
388 self.messages_by_json[path][msg.name] = msg
389 processed.append(m)
390 if prev_length == len(self.messages):
391 # cannot make forward progress ...
392 self.exceptions.extend(exceptions)
393 break
394 prev_length = len(self.messages)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200395
Klement Sekera2108c0c2018-08-24 11:43:20 +0200396 def lookup_type_like_id(self, name):
397 mundane_name = remove_magic(name)
398 if name in self.types:
399 return self.types[name]
400 elif name in self.enums:
401 return self.enums[name]
402 elif name in self.unions:
403 return self.unions[name]
404 elif mundane_name in self.types:
405 return self.types[mundane_name]
406 elif mundane_name in self.enums:
407 return self.enums[mundane_name]
408 elif mundane_name in self.unions:
409 return self.unions[mundane_name]
410 raise ParseError(
411 "Could not find type, enum or union by magic name `%s' nor by "
412 "mundane name `%s'" % (name, mundane_name))
413
414 def is_reply(self, message):
415 return message in self.replies
416
417 def is_event(self, message):
418 return message in self.events
419
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200420 def get_reply(self, message):
Klement Sekera2108c0c2018-08-24 11:43:20 +0200421 return self.messages[self.services[message]['reply']]
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200422
423 def finalize_parsing(self):
424 if len(self.messages) == 0:
425 for e in self.exceptions:
Damjan Marion4c64b6e2018-08-26 18:14:46 +0200426 self.logger.warning(e)
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200427 for jn, j in self.messages_by_json.items():
428 remove = []
429 for n, m in j.items():
430 try:
Klement Sekera2108c0c2018-08-24 11:43:20 +0200431 if not m.is_reply and not m.is_event:
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200432 try:
433 m.reply = self.get_reply(n)
Klement Sekera2108c0c2018-08-24 11:43:20 +0200434 if "stream" in self.services[m.name]:
435 m.reply_is_stream = \
436 self.services[m.name]["stream"]
437 else:
438 m.reply_is_stream = False
Klement Sekeradc15be22017-06-12 06:49:33 +0200439 m.reply.request = m
Klement Sekera8f2a4ea2017-05-04 06:15:18 +0200440 except:
441 raise ParseError(
442 "Cannot find reply to message `%s'" % n)
443 except ParseError as e:
444 self.exceptions.append(e)
445 remove.append(n)
446
447 self.messages_by_json[jn] = {
448 k: v for k, v in j.items() if k not in remove}