X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvpp-api%2Fpython%2Fvpp_papi%2Fvpp_serializer.py;h=13721ff88d4c8c1ad9c1d34b85561dfadb53ee62;hb=8c8acc027;hp=8635ce0070c64f042231067887b350764edcdd3c;hpb=31555a3475a37195938378217a635b3451e449de;p=vpp.git diff --git a/src/vpp-api/python/vpp_papi/vpp_serializer.py b/src/vpp-api/python/vpp_papi/vpp_serializer.py index 8635ce0070c..13721ff88d4 100644 --- a/src/vpp-api/python/vpp_papi/vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/vpp_serializer.py @@ -27,7 +27,7 @@ from .vpp_format import VPPFormat logger = logging.getLogger(__name__) -class BaseTypes(): +class BaseTypes(object): def __init__(self, type, elements=0): base_types = {'u8': '>B', 'u16': '>H', @@ -55,14 +55,10 @@ class BaseTypes(): return self.packer.unpack_from(data, offset)[0], self.packer.size -types = {} -types['u8'] = BaseTypes('u8') -types['u16'] = BaseTypes('u16') -types['u32'] = BaseTypes('u32') -types['i32'] = BaseTypes('i32') -types['u64'] = BaseTypes('u64') -types['f64'] = BaseTypes('f64') -types['bool'] = BaseTypes('bool') +types = {'u8': BaseTypes('u8'), 'u16': BaseTypes('u16'), + 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'), + 'u64': BaseTypes('u64'), 'f64': BaseTypes('f64'), + 'bool': BaseTypes('bool')} def vpp_get_type(name): @@ -72,33 +68,39 @@ def vpp_get_type(name): return None -class FixedList_u8(): +class VPPSerializerValueError(ValueError): + pass + + +class FixedList_u8(object): def __init__(self, name, field_type, num): self.name = name self.num = num self.packer = BaseTypes(field_type, num) self.size = self.packer.size - def pack(self, list, kwargs): + def pack(self, list, kwargs=None): """Packs a fixed length bytestring. Left-pads with zeros if input data is too short.""" if not list: return b'\x00' * self.size if len(list) > self.num: - raise ValueError('Fixed list length error for "{}", got: {}' - ' expected: {}' - .format(self.name, len(list), self.num)) + raise VPPSerializerValueError( + 'Fixed list length error for "{}", got: {}' + ' expected: {}' + .format(self.name, len(list), self.num)) return self.packer.pack(list) def unpack(self, data, offset=0, result=None): if len(data[offset:]) < self.num: - raise ValueError('Invalid array length for "{}" got {}' - ' expected {}' - .format(self.name, len(data[offset:]), self.num)) + raise VPPSerializerValueError( + 'Invalid array length for "{}" got {}' + ' expected {}' + .format(self.name, len(data[offset:]), self.num)) return self.packer.unpack(data, offset) -class FixedList(): +class FixedList(object): def __init__(self, name, field_type, num): self.num = num self.packer = types[field_type] @@ -106,8 +108,9 @@ class FixedList(): def pack(self, list, kwargs): if len(list) != self.num: - raise ValueError('Fixed list length error, got: {} expected: {}' - .format(len(list), self.num)) + raise VPPSerializerValueError( + 'Fixed list length error, got: {} expected: {}' + .format(len(list), self.num)) b = bytes() for e in list: b += self.packer.pack(e) @@ -125,7 +128,7 @@ class FixedList(): return result, total -class VLAList(): +class VLAList(object): def __init__(self, name, field_type, len_field_name, index): self.name = name self.index = index @@ -137,8 +140,9 @@ class VLAList(): if not list: return b"" if len(list) != kwargs[self.length_field]: - raise ValueError('Variable length error, got: {} expected: {}' - .format(len(list), kwargs[self.length_field])) + raise VPPSerializerValueError( + 'Variable length error, got: {} expected: {}' + .format(len(list), kwargs[self.length_field])) b = bytes() # u8 array @@ -187,7 +191,8 @@ class VLAList_legacy(): total = 0 # Return a list of arguments if (len(data) - offset) % self.packer.size: - raise ValueError('Legacy Variable Length Array length mismatch.') + raise VPPSerializerValueError( + 'Legacy Variable Length Array length mismatch.') elements = int((len(data) - offset) / self.packer.size) r = [] for e in range(elements): @@ -198,7 +203,7 @@ class VLAList_legacy(): return r, total -class VPPEnumType(): +class VPPEnumType(object): def __init__(self, name, msgdef): self.size = types['u32'].size e_hash = {} @@ -227,7 +232,7 @@ class VPPEnumType(): return self.enum(x), size -class VPPUnionType(): +class VPPUnionType(object): def __init__(self, name, msgdef): self.name = name self.size = 0 @@ -241,7 +246,8 @@ class VPPUnionType(): f_type, f_name = f if f_type not in types: logger.debug('Unknown union type {}'.format(f_type)) - raise ValueError('Unknown message type {}'.format(f_type)) + raise VPPSerializerValueError( + 'Unknown message type {}'.format(f_type)) fields.append(f_name) size = types[f_type].size self.packers[f_name] = types[f_type] @@ -277,7 +283,23 @@ class VPPUnionType(): return self.tuple._make(r), maxsize -class VPPType(): +def VPPTypeAlias(name, msgdef): + t = vpp_get_type(msgdef['type']) + if not t: + raise ValueError() + if 'length' in msgdef: + if msgdef['length'] == 0: + raise ValueError() + if msgdef['type'] == 'u8': + types[name] = FixedList_u8(name, msgdef['type'], + msgdef['length']) + else: + types[name] = FixedList(name, msgdef['type'], msgdef['length']) + else: + types[name] = t + + +class VPPType(object): # Set everything up to be able to pack / unpack def __init__(self, name, msgdef): self.name = name @@ -297,7 +319,8 @@ class VPPType(): self.fieldtypes.append(f_type) if f_type not in types: logger.debug('Unknown type {}'.format(f_type)) - raise ValueError('Unknown message type {}'.format(f_type)) + raise VPPSerializerValueError( + 'Unknown message type {}'.format(f_type)) if len(f) == 3: # list list_elements = f[2] if list_elements == 0: @@ -333,8 +356,9 @@ class VPPType(): # Try one of the format functions if data and type(data) is not dict and a not in data: - raise ValueError("Invalid argument: {} expected {}.{}". - format(data, self.name, a)) + raise VPPSerializerValueError( + "Invalid argument: {} expected {}.{}". + format(data, self.name, a)) # Defaulting to zero. if not data or a not in data: # Default to 0