2 # Copyright (c) 2018 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
21 if sys.version_info <= (3, 4):
22 from aenum import IntEnum # noqa: F401
24 from enum import IntEnum # noqa: F401
26 if sys.version_info <= (3, 6):
27 from aenum import IntFlag # noqa: F401
30 from enum import IntFlag # noqa: F401
32 from . import vpp_format # noqa: E402
35 # Set log-level in application by doing e.g.:
36 # logger = logging.getLogger('vpp_serializer')
37 # logger.setLevel(logging.DEBUG)
39 logger = logging.getLogger(__name__)
41 if sys.version[0] == '2':
42 def check(d): type(d) is dict
44 def check(d): type(d) is dict or type(d) is bytes
47 def conversion_required(data, field_type):
51 if type(data).__name__ in vpp_format.conversion_table[field_type]:
57 def conversion_packer(data, field_type):
58 t = type(data).__name__
59 return types[field_type].pack(vpp_format.
60 conversion_table[field_type][t](data))
63 def conversion_unpacker(data, field_type):
64 if field_type not in vpp_format.conversion_unpacker_table:
66 return vpp_format.conversion_unpacker_table[field_type](data)
69 class BaseTypes(object):
70 def __init__(self, type, elements=0, options=None):
71 base_types = {'u8': '>B',
81 if elements > 0 and (type == 'u8' or type == 'string'):
82 self.packer = struct.Struct('>%ss' % elements)
84 self.packer = struct.Struct(base_types[type])
85 self.size = self.packer.size
86 self.options = options
88 def __call__(self, args):
92 def pack(self, data, kwargs=None):
93 if not data: # Default to zero if not specified
94 if self.options and 'default' in self.options:
95 data = self.options['default']
98 return self.packer.pack(data)
100 def unpack(self, data, offset, result=None, ntc=False):
101 return self.packer.unpack_from(data, offset)[0], self.packer.size
104 class String(object):
105 def __init__(self, options):
108 self.length_field_packer = BaseTypes('u32')
109 self.limit = options['limit'] if 'limit' in options else None
111 def pack(self, list, kwargs=None):
113 return self.length_field_packer.pack(0) + b""
114 if self.limit and len(list) > self.limit:
115 raise VPPSerializerValueError(
116 "Invalid argument length for: {}, {} maximum {}".
117 format(list, len(list), self.limit))
119 return self.length_field_packer.pack(len(list)) + list.encode('utf8')
121 def unpack(self, data, offset=0, result=None, ntc=False):
122 length, length_field_size = self.length_field_packer.unpack(data,
126 p = BaseTypes('u8', length)
127 x, size = p.unpack(data, offset + length_field_size)
128 x2 = x.split(b'\0', 1)[0]
129 return (x2.decode('utf8'), size + length_field_size)
132 types = {'u8': BaseTypes('u8'), 'u16': BaseTypes('u16'),
133 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'),
134 'u64': BaseTypes('u64'), 'f64': BaseTypes('f64'),
135 'bool': BaseTypes('bool'), 'string': String}
138 def vpp_get_type(name):
145 class VPPSerializerValueError(ValueError):
149 class FixedList_u8(object):
150 def __init__(self, name, field_type, num):
153 self.packer = BaseTypes(field_type, num)
154 self.size = self.packer.size
155 self.field_type = field_type
157 def __call__(self, args):
161 def pack(self, data, kwargs=None):
162 """Packs a fixed length bytestring. Left-pads with zeros
163 if input data is too short."""
165 return b'\x00' * self.size
167 if len(data) > self.num:
168 raise VPPSerializerValueError(
169 'Fixed list length error for "{}", got: {}'
171 .format(self.name, len(data), self.num))
173 return self.packer.pack(data)
175 def unpack(self, data, offset=0, result=None, ntc=False):
176 if len(data[offset:]) < self.num:
177 raise VPPSerializerValueError(
178 'Invalid array length for "{}" got {}'
180 .format(self.name, len(data[offset:]), self.num))
181 if self.field_type == 'string':
182 s = self.packer.unpack(data, offset)
183 s2 = s[0].split(b'\0', 1)[0]
184 return (s2.decode('utf-8'), self.num)
185 return self.packer.unpack(data, offset)
188 class FixedList(object):
189 def __init__(self, name, field_type, num):
191 self.packer = types[field_type]
192 self.size = self.packer.size * num
194 self.field_type = field_type
196 def __call__(self, args):
200 def pack(self, list, kwargs):
201 if len(list) != self.num:
202 raise VPPSerializerValueError(
203 'Fixed list length error, got: {} expected: {}'
204 .format(len(list), self.num))
207 b += self.packer.pack(e)
210 def unpack(self, data, offset=0, result=None, ntc=False):
211 # Return a list of arguments
214 for e in range(self.num):
215 x, size = self.packer.unpack(data, offset, ntc=ntc)
222 class VLAList(object):
223 def __init__(self, name, field_type, len_field_name, index):
225 self.field_type = field_type
227 self.packer = types[field_type]
228 self.size = self.packer.size
229 self.length_field = len_field_name
231 def __call__(self, args):
235 def pack(self, list, kwargs=None):
238 if len(list) != kwargs[self.length_field]:
239 raise VPPSerializerValueError(
240 'Variable length error, got: {} expected: {}'
241 .format(len(list), kwargs[self.length_field]))
246 if self.packer.size == 1:
247 return bytearray(list)
250 b += self.packer.pack(e)
253 def unpack(self, data, offset=0, result=None, ntc=False):
254 # Return a list of arguments
258 if self.packer.size == 1:
259 if result[self.index] == 0:
261 p = BaseTypes('u8', result[self.index])
262 return p.unpack(data, offset, ntc=ntc)
265 for e in range(result[self.index]):
266 x, size = self.packer.unpack(data, offset, ntc=ntc)
273 class VLAList_legacy():
274 def __init__(self, name, field_type):
275 self.packer = types[field_type]
276 self.size = self.packer.size
278 def __call__(self, args):
282 def pack(self, list, kwargs=None):
283 if self.packer.size == 1:
288 b += self.packer.pack(e)
291 def unpack(self, data, offset=0, result=None, ntc=False):
293 # Return a list of arguments
294 if (len(data) - offset) % self.packer.size:
295 raise VPPSerializerValueError(
296 'Legacy Variable Length Array length mismatch.')
297 elements = int((len(data) - offset) / self.packer.size)
299 for e in range(elements):
300 x, size = self.packer.unpack(data, offset, ntc=ntc)
302 offset += self.packer.size
307 class VPPEnumType(object):
308 def __init__(self, name, msgdef):
309 self.size = types['u32'].size
310 self.enumtype = 'u32'
313 if type(f) is dict and 'enumtype' in f:
314 if f['enumtype'] != 'u32':
315 self.size = types[f['enumtype']].size
316 self.enumtype = f['enumtype']
319 e_hash[ename] = evalue
320 self.enum = IntFlag(name, e_hash)
323 def __call__(self, args):
327 def __getattr__(self, name):
328 return self.enum[name]
330 def __nonzero__(self):
333 def pack(self, data, kwargs=None):
334 return types[self.enumtype].pack(data)
336 def unpack(self, data, offset=0, result=None, ntc=False):
337 x, size = types[self.enumtype].unpack(data, offset)
338 return self.enum(x), size
341 class VPPUnionType(object):
342 def __init__(self, name, msgdef):
347 self.packers = collections.OrderedDict()
348 for i, f in enumerate(msgdef):
349 if type(f) is dict and 'crc' in f:
353 if f_type not in types:
354 logger.debug('Unknown union type {}'.format(f_type))
355 raise VPPSerializerValueError(
356 'Unknown message type {}'.format(f_type))
357 fields.append(f_name)
358 size = types[f_type].size
359 self.packers[f_name] = types[f_type]
365 self.tuple = collections.namedtuple(name, fields, rename=True)
367 def __call__(self, args):
371 # Union of variable length?
372 def pack(self, data, kwargs=None):
374 return b'\x00' * self.size
376 for k, v in data.items():
377 logger.debug("Key: {} Value: {}".format(k, v))
378 b = self.packers[k].pack(v, kwargs)
380 r = bytearray(self.size)
384 def unpack(self, data, offset=0, result=None, ntc=False):
387 for k, p in self.packers.items():
388 x, size = p.unpack(data, offset, ntc=ntc)
392 return self.tuple._make(r), maxsize
395 class VPPTypeAlias(object):
396 def __init__(self, name, msgdef):
398 t = vpp_get_type(msgdef['type'])
401 if 'length' in msgdef:
402 if msgdef['length'] == 0:
404 if msgdef['type'] == 'u8':
405 self.packer = FixedList_u8(name, msgdef['type'],
407 self.size = self.packer.size
409 self.packer = FixedList(name, msgdef['type'], msgdef['length'])
416 def __call__(self, args):
420 def pack(self, data, kwargs=None):
421 if data and conversion_required(data, self.name):
423 return conversion_packer(data, self.name)
424 # Python 2 and 3 raises different exceptions from inet_pton
425 except(OSError, socket.error, TypeError):
428 return self.packer.pack(data, kwargs)
430 def unpack(self, data, offset=0, result=None, ntc=False):
431 t, size = self.packer.unpack(data, offset, result, ntc=ntc)
433 return conversion_unpacker(t, self.name), size
437 class VPPType(object):
438 # Set everything up to be able to pack / unpack
439 def __init__(self, name, msgdef):
445 self.field_by_name = {}
447 for i, f in enumerate(msgdef):
448 if type(f) is dict and 'crc' in f:
451 f_type, f_name = f[:2]
452 self.fields.append(f_name)
453 self.field_by_name[f_name] = None
454 self.fieldtypes.append(f_type)
455 if f_type not in types:
456 logger.debug('Unknown type {}'.format(f_type))
457 raise VPPSerializerValueError(
458 'Unknown message type {}'.format(f_type))
461 options = [x for x in f if type(x) is dict]
463 self.options = options[0]
467 if fieldlen == 3: # list
469 if list_elements == 0:
470 p = VLAList_legacy(f_name, f_type)
471 self.packers.append(p)
472 elif f_type == 'u8' or f_type == 'string':
473 p = FixedList_u8(f_name, f_type, list_elements)
474 self.packers.append(p)
477 p = FixedList(f_name, f_type, list_elements)
478 self.packers.append(p)
480 elif fieldlen == 4: # Variable length list
481 length_index = self.fields.index(f[3])
482 p = VLAList(f_name, f_type, f[3], length_index)
483 self.packers.append(p)
485 p = types[f_type](self.options)
486 self.packers.append(p)
490 self.tuple = collections.namedtuple(name, self.fields, rename=True)
493 def __call__(self, args):
497 def pack(self, data, kwargs=None):
502 # Try one of the format functions
503 if data and conversion_required(data, self.name):
504 return conversion_packer(data, self.name)
506 for i, a in enumerate(self.fields):
507 if data and type(data) is not dict and a not in data:
508 raise VPPSerializerValueError(
509 "Invalid argument: {} expected {}.{}".
510 format(data, self.name, a))
512 # Defaulting to zero.
513 if not data or a not in data: # Default to 0
515 kwarg = None # No default for VLA
518 kwarg = kwargs[a] if a in kwargs else None
519 if isinstance(self.packers[i], VPPType):
520 b += self.packers[i].pack(arg, kwarg)
522 b += self.packers[i].pack(arg, kwargs)
526 def unpack(self, data, offset=0, result=None, ntc=False):
527 # Return a list of arguments
530 for p in self.packers:
531 x, size = p.unpack(data, offset, result, ntc)
532 if type(x) is tuple and len(x) == 1:
537 t = self.tuple._make(result)
539 t = conversion_unpacker(t, self.name)
543 class VPPMessage(VPPType):