Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 1 | # |
| 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: |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 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. |
| 14 | # |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 15 | import collections |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 16 | import logging |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 17 | import socket |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 18 | import struct |
| 19 | import sys |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 20 | |
| 21 | if sys.version_info <= (3, 4): |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 22 | from aenum import IntEnum # noqa: F401 |
Ole Troan | 48ae19e | 2019-03-07 11:28:32 +0100 | [diff] [blame] | 23 | else: |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 24 | from enum import IntEnum # noqa: F401 |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 25 | |
| 26 | if sys.version_info <= (3, 6): |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 27 | from aenum import IntFlag # noqa: F401 |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 28 | else: |
Paul Vinciguerra | 42bdba8 | 2019-03-07 11:26:23 -0800 | [diff] [blame] | 29 | |
Paul Vinciguerra | ae8819f | 2019-06-07 13:35:37 -0400 | [diff] [blame] | 30 | from enum import IntFlag # noqa: F401 |
| 31 | |
| 32 | from . import vpp_format # noqa: E402 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 33 | |
| 34 | # |
| 35 | # Set log-level in application by doing e.g.: |
| 36 | # logger = logging.getLogger('vpp_serializer') |
| 37 | # logger.setLevel(logging.DEBUG) |
| 38 | # |
| 39 | logger = logging.getLogger(__name__) |
| 40 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 41 | if sys.version[0] == '2': |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 42 | def check(d): type(d) is dict |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 43 | else: |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 44 | def check(d): type(d) is dict or type(d) is bytes |
| 45 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 46 | |
| 47 | def conversion_required(data, field_type): |
| 48 | if check(data): |
| 49 | return False |
| 50 | try: |
| 51 | if type(data).__name__ in vpp_format.conversion_table[field_type]: |
| 52 | return True |
| 53 | except KeyError: |
| 54 | return False |
| 55 | |
| 56 | |
| 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)) |
| 61 | |
| 62 | |
| 63 | def conversion_unpacker(data, field_type): |
| 64 | if field_type not in vpp_format.conversion_unpacker_table: |
| 65 | return data |
| 66 | return vpp_format.conversion_unpacker_table[field_type](data) |
| 67 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 68 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 69 | # TODO: post 20.01, remove inherit from object. |
| 70 | class Packer(object): |
| 71 | options = {} |
| 72 | |
| 73 | def pack(self, data, kwargs): |
| 74 | raise NotImplementedError |
| 75 | |
| 76 | def unpack(self, data, offset, result=None, ntc=False): |
| 77 | raise NotImplementedError |
| 78 | |
| 79 | # override as appropriate in subclasses |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 80 | @staticmethod |
| 81 | def _get_packer_with_options(f_type, options): |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 82 | return types[f_type] |
| 83 | |
| 84 | def get_packer_with_options(self, f_type, options): |
| 85 | if options is not None: |
| 86 | try: |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 87 | c = types[f_type].__class__ |
| 88 | return c._get_packer_with_options(f_type, options) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 89 | except IndexError: |
| 90 | raise VPPSerializerValueError( |
| 91 | "Options not supported for {}{} ({})". |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 92 | format(f_type, types[f_type].__class__, |
| 93 | options)) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 94 | |
| 95 | |
| 96 | class BaseTypes(Packer): |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 97 | def __init__(self, type, elements=0, options=None): |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 98 | self._type = type |
| 99 | self._elements = elements |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 100 | base_types = {'u8': '>B', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 101 | 'i8': '>b', |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 102 | 'string': '>s', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 103 | 'u16': '>H', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 104 | 'i16': '>h', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 105 | 'u32': '>I', |
| 106 | 'i32': '>i', |
| 107 | 'u64': '>Q', |
Paul Vinciguerra | 6ce0f35 | 2019-07-02 13:06:21 -0400 | [diff] [blame] | 108 | 'i64': '>q', |
Paul Vinciguerra | 888640a | 2019-05-14 21:01:28 -0400 | [diff] [blame] | 109 | 'f64': '=d', |
Ole Troan | f47e9b6 | 2018-10-16 15:14:03 +0200 | [diff] [blame] | 110 | 'bool': '>?', |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 111 | 'header': '>HI'} |
| 112 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 113 | if elements > 0 and (type == 'u8' or type == 'string'): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 114 | self.packer = struct.Struct('>%ss' % elements) |
| 115 | else: |
| 116 | self.packer = struct.Struct(base_types[type]) |
| 117 | self.size = self.packer.size |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 118 | self.options = options |
| 119 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 120 | def pack(self, data, kwargs=None): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 121 | if data is None: # Default to zero if not specified |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 122 | if self.options and 'default' in self.options: |
| 123 | data = self.options['default'] |
| 124 | else: |
| 125 | data = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 126 | return self.packer.pack(data) |
| 127 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 128 | def unpack(self, data, offset, result=None, ntc=False): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 129 | return self.packer.unpack_from(data, offset)[0], self.packer.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 130 | |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 131 | @staticmethod |
| 132 | def _get_packer_with_options(f_type, options): |
| 133 | return BaseTypes(f_type, options=options) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 134 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 135 | def __repr__(self): |
| 136 | return "BaseTypes(type=%s, elements=%s, options=%s)" % (self._type, |
| 137 | self._elements, |
| 138 | self.options) |
| 139 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 140 | |
| 141 | class String(Packer): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 142 | def __init__(self, name, num, options): |
| 143 | self.name = name |
| 144 | self.num = num |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 145 | self.size = 1 |
| 146 | self.length_field_packer = BaseTypes('u32') |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 147 | self.limit = options['limit'] if 'limit' in options else num |
| 148 | self.fixed = True if num else False |
| 149 | if self.fixed and not self.limit: |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 150 | raise VPPSerializerValueError( |
| 151 | "Invalid argument length for: {}, {} maximum {}". |
| 152 | format(list, len(list), self.limit)) |
| 153 | |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 154 | def pack(self, list, kwargs=None): |
| 155 | if not list: |
| 156 | if self.fixed: |
| 157 | return b"\x00" * self.limit |
| 158 | return self.length_field_packer.pack(0) + b"" |
| 159 | if self.limit and len(list) > self.limit - 1: |
| 160 | raise VPPSerializerValueError( |
| 161 | "Invalid argument length for: {}, {} maximum {}". |
| 162 | format(list, len(list), self.limit - 1)) |
| 163 | if self.fixed: |
| 164 | return list.encode('ascii').ljust(self.limit, b'\x00') |
| 165 | return self.length_field_packer.pack(len(list)) + list.encode('ascii') |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 166 | |
| 167 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 168 | if self.fixed: |
| 169 | p = BaseTypes('u8', self.num) |
| 170 | s = p.unpack(data, offset) |
| 171 | s2 = s[0].split(b'\0', 1)[0] |
| 172 | return (s2.decode('ascii'), self.num) |
| 173 | |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 174 | length, length_field_size = self.length_field_packer.unpack(data, |
| 175 | offset) |
| 176 | if length == 0: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 177 | return '', 0 |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 178 | p = BaseTypes('u8', length) |
| 179 | x, size = p.unpack(data, offset + length_field_size) |
Ole Troan | 33a5817 | 2019-09-04 09:12:29 +0200 | [diff] [blame] | 180 | return (x.decode('ascii', errors='replace'), size + length_field_size) |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 181 | |
| 182 | |
Paul Vinciguerra | ba2c7fa | 2019-11-23 21:02:27 -0500 | [diff] [blame] | 183 | types = {'u8': BaseTypes('u8'), 'i8': BaseTypes('i8'), |
| 184 | 'u16': BaseTypes('u16'), 'i16': BaseTypes('i16'), |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 185 | 'u32': BaseTypes('u32'), 'i32': BaseTypes('i32'), |
Paul Vinciguerra | ba2c7fa | 2019-11-23 21:02:27 -0500 | [diff] [blame] | 186 | 'u64': BaseTypes('u64'), 'i64': BaseTypes('i64'), |
| 187 | 'f64': BaseTypes('f64'), |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 188 | 'bool': BaseTypes('bool'), 'string': String} |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 189 | |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 190 | class_types = {} |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 191 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 192 | |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 193 | def vpp_get_type(name): |
| 194 | try: |
| 195 | return types[name] |
| 196 | except KeyError: |
| 197 | return None |
| 198 | |
| 199 | |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 200 | class VPPSerializerValueError(ValueError): |
| 201 | pass |
| 202 | |
| 203 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 204 | class FixedList_u8(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 205 | def __init__(self, name, field_type, num): |
| 206 | self.name = name |
| 207 | self.num = num |
| 208 | self.packer = BaseTypes(field_type, num) |
| 209 | self.size = self.packer.size |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 210 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 211 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 212 | def pack(self, data, kwargs=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 213 | """Packs a fixed length bytestring. Left-pads with zeros |
| 214 | if input data is too short.""" |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 215 | if not data: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 216 | return b'\x00' * self.size |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 217 | |
| 218 | if len(data) > self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 219 | raise VPPSerializerValueError( |
| 220 | 'Fixed list length error for "{}", got: {}' |
| 221 | ' expected: {}' |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 222 | .format(self.name, len(data), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 223 | |
Ole Troan | 64e978b | 2019-10-17 21:40:36 +0200 | [diff] [blame] | 224 | try: |
| 225 | return self.packer.pack(data) |
| 226 | except struct.error: |
| 227 | raise VPPSerializerValueError( |
| 228 | 'Packing failed for "{}" {}' |
| 229 | .format(self.name, kwargs)) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 230 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 231 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 232 | if len(data[offset:]) < self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 233 | raise VPPSerializerValueError( |
| 234 | 'Invalid array length for "{}" got {}' |
| 235 | ' expected {}' |
| 236 | .format(self.name, len(data[offset:]), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 237 | return self.packer.unpack(data, offset) |
| 238 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 239 | def __repr__(self): |
| 240 | return "FixedList_u8(name=%s, field_type=%s, num=%s)" % ( |
| 241 | self.name, self.field_type, self.num |
| 242 | ) |
| 243 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 244 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 245 | class FixedList(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 246 | def __init__(self, name, field_type, num): |
| 247 | self.num = num |
| 248 | self.packer = types[field_type] |
| 249 | self.size = self.packer.size * num |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 250 | self.name = name |
| 251 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 252 | |
| 253 | def pack(self, list, kwargs): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 254 | if len(list) != self.num: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 255 | raise VPPSerializerValueError( |
| 256 | 'Fixed list length error, got: {} expected: {}' |
| 257 | .format(len(list), self.num)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 258 | b = bytes() |
| 259 | for e in list: |
| 260 | b += self.packer.pack(e) |
| 261 | return b |
| 262 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 263 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 264 | # Return a list of arguments |
| 265 | result = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 266 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 267 | for e in range(self.num): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 268 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 269 | result.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 270 | offset += size |
| 271 | total += size |
| 272 | return result, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 273 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 274 | def __repr__(self): |
Paul Vinciguerra | 99fbf05 | 2019-11-26 14:15:53 -0500 | [diff] [blame] | 275 | return "FixedList(name=%s, field_type=%s, num=%s)" % ( |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 276 | self.name, self.field_type, self.num) |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 277 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 278 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 279 | class VLAList(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 280 | def __init__(self, name, field_type, len_field_name, index): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 281 | self.name = name |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 282 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 283 | self.index = index |
| 284 | self.packer = types[field_type] |
| 285 | self.size = self.packer.size |
| 286 | self.length_field = len_field_name |
| 287 | |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 288 | def pack(self, lst, kwargs=None): |
| 289 | if not lst: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 290 | return b"" |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 291 | if len(lst) != kwargs[self.length_field]: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 292 | raise VPPSerializerValueError( |
| 293 | 'Variable length error, got: {} expected: {}' |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 294 | .format(len(lst), kwargs[self.length_field])) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 295 | |
| 296 | # u8 array |
| 297 | if self.packer.size == 1: |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 298 | if isinstance(lst, list): |
| 299 | return b''.join(lst) |
| 300 | return bytes(lst) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 301 | |
Ole Troan | 2267429 | 2019-10-21 18:59:11 +0200 | [diff] [blame] | 302 | b = bytes() |
| 303 | for e in lst: |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 304 | b += self.packer.pack(e) |
| 305 | return b |
| 306 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 307 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 308 | # Return a list of arguments |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 309 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 310 | |
| 311 | # u8 array |
| 312 | if self.packer.size == 1: |
| 313 | if result[self.index] == 0: |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 314 | return b'', 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 315 | p = BaseTypes('u8', result[self.index]) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 316 | return p.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 317 | |
| 318 | r = [] |
| 319 | for e in range(result[self.index]): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 320 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 321 | r.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 322 | offset += size |
| 323 | total += size |
| 324 | return r, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 325 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 326 | def __repr__(self): |
| 327 | return "VLAList(name=%s, field_type=%s, " \ |
| 328 | "len_field_name=%s, index=%s)" % ( |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 329 | self.name, self.field_type, self.length_field, self.index |
| 330 | ) |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 331 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 332 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 333 | class VLAList_legacy(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 334 | def __init__(self, name, field_type): |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 335 | self.name = name |
| 336 | self.field_type = field_type |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 337 | self.packer = types[field_type] |
| 338 | self.size = self.packer.size |
| 339 | |
| 340 | def pack(self, list, kwargs=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 341 | if self.packer.size == 1: |
| 342 | return bytes(list) |
| 343 | |
| 344 | b = bytes() |
| 345 | for e in list: |
| 346 | b += self.packer.pack(e) |
| 347 | return b |
| 348 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 349 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 350 | total = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 351 | # Return a list of arguments |
| 352 | if (len(data) - offset) % self.packer.size: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 353 | raise VPPSerializerValueError( |
| 354 | 'Legacy Variable Length Array length mismatch.') |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 355 | elements = int((len(data) - offset) / self.packer.size) |
| 356 | r = [] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 357 | for e in range(elements): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 358 | x, size = self.packer.unpack(data, offset, ntc=ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 359 | r.append(x) |
| 360 | offset += self.packer.size |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 361 | total += size |
| 362 | return r, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 363 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 364 | def __repr__(self): |
| 365 | return "VLAList_legacy(name=%s, field_type=%s)" % ( |
| 366 | self.name, self.field_type |
| 367 | ) |
| 368 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 369 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 370 | class VPPEnumType(Packer): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 371 | def __init__(self, name, msgdef, options=None): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 372 | self.size = types['u32'].size |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 373 | self.name = name |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 374 | self.enumtype = 'u32' |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 375 | self.msgdef = msgdef |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 376 | e_hash = {} |
| 377 | for f in msgdef: |
| 378 | if type(f) is dict and 'enumtype' in f: |
| 379 | if f['enumtype'] != 'u32': |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 380 | self.size = types[f['enumtype']].size |
| 381 | self.enumtype = f['enumtype'] |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 382 | continue |
| 383 | ename, evalue = f |
| 384 | e_hash[ename] = evalue |
Ole Troan | 48ae19e | 2019-03-07 11:28:32 +0100 | [diff] [blame] | 385 | self.enum = IntFlag(name, e_hash) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 386 | types[name] = self |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 387 | class_types[name] = VPPEnumType |
| 388 | self.options = options |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 389 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 390 | def __getattr__(self, name): |
| 391 | return self.enum[name] |
| 392 | |
Paul Vinciguerra | a089ae1 | 2019-07-08 15:41:07 -0400 | [diff] [blame] | 393 | def __bool__(self): |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 394 | return True |
| 395 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 396 | # TODO: Remove post 20.01. |
Paul Vinciguerra | a089ae1 | 2019-07-08 15:41:07 -0400 | [diff] [blame] | 397 | if sys.version[0] == '2': |
| 398 | __nonzero__ = __bool__ |
| 399 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 400 | def pack(self, data, kwargs=None): |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 401 | if data is None: # Default to zero if not specified |
| 402 | if self.options and 'default' in self.options: |
| 403 | data = self.options['default'] |
| 404 | else: |
| 405 | data = 0 |
| 406 | |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 407 | return types[self.enumtype].pack(data) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 408 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 409 | def unpack(self, data, offset=0, result=None, ntc=False): |
Andrew Yourtchenko | 5c4b9f1 | 2019-03-19 14:52:29 +0100 | [diff] [blame] | 410 | x, size = types[self.enumtype].unpack(data, offset) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 411 | return self.enum(x), size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 412 | |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 413 | @staticmethod |
| 414 | def _get_packer_with_options(f_type, options): |
| 415 | return VPPEnumType(f_type, types[f_type].msgdef, options=options) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 416 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 417 | def __repr__(self): |
| 418 | return "VPPEnumType(name=%s, msgdef=%s, options=%s)" % ( |
| 419 | self.name, self.msgdef, self.options |
| 420 | ) |
| 421 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 422 | |
| 423 | class VPPUnionType(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 424 | def __init__(self, name, msgdef): |
| 425 | self.name = name |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 426 | self.msgdef = msgdef |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 427 | self.size = 0 |
| 428 | self.maxindex = 0 |
| 429 | fields = [] |
| 430 | self.packers = collections.OrderedDict() |
| 431 | for i, f in enumerate(msgdef): |
| 432 | if type(f) is dict and 'crc' in f: |
| 433 | self.crc = f['crc'] |
| 434 | continue |
| 435 | f_type, f_name = f |
| 436 | if f_type not in types: |
| 437 | logger.debug('Unknown union type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 438 | raise VPPSerializerValueError( |
| 439 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 440 | fields.append(f_name) |
| 441 | size = types[f_type].size |
| 442 | self.packers[f_name] = types[f_type] |
| 443 | if size > self.size: |
| 444 | self.size = size |
| 445 | self.maxindex = i |
| 446 | |
| 447 | types[name] = self |
| 448 | self.tuple = collections.namedtuple(name, fields, rename=True) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 449 | |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 450 | # Union of variable length? |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 451 | def pack(self, data, kwargs=None): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 452 | if not data: |
| 453 | return b'\x00' * self.size |
| 454 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 455 | for k, v in data.items(): |
| 456 | logger.debug("Key: {} Value: {}".format(k, v)) |
| 457 | b = self.packers[k].pack(v, kwargs) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 458 | break |
| 459 | r = bytearray(self.size) |
Ole Troan | b199e98 | 2018-08-02 19:19:21 +0200 | [diff] [blame] | 460 | r[:len(b)] = b |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 461 | return r |
| 462 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 463 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 464 | r = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 465 | maxsize = 0 |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 466 | for k, p in self.packers.items(): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 467 | x, size = p.unpack(data, offset, ntc=ntc) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 468 | if size > maxsize: |
| 469 | maxsize = size |
| 470 | r.append(x) |
| 471 | return self.tuple._make(r), maxsize |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 472 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 473 | def __repr__(self): |
| 474 | return"VPPUnionType(name=%s, msgdef=%r)" % (self.name, self.msgdef) |
| 475 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 476 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 477 | class VPPTypeAlias(Packer): |
| 478 | def __init__(self, name, msgdef, options=None): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 479 | self.name = name |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 480 | self.msgdef = msgdef |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 481 | t = vpp_get_type(msgdef['type']) |
| 482 | if not t: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 483 | raise ValueError('No such type: {}'.format(msgdef['type'])) |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 484 | if 'length' in msgdef: |
| 485 | if msgdef['length'] == 0: |
| 486 | raise ValueError() |
| 487 | if msgdef['type'] == 'u8': |
| 488 | self.packer = FixedList_u8(name, msgdef['type'], |
| 489 | msgdef['length']) |
| 490 | self.size = self.packer.size |
| 491 | else: |
| 492 | self.packer = FixedList(name, msgdef['type'], msgdef['length']) |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 493 | else: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 494 | self.packer = t |
| 495 | self.size = t.size |
| 496 | |
| 497 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 498 | self.toplevelconversion = False |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 499 | self.options = options |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 500 | |
| 501 | def pack(self, data, kwargs=None): |
| 502 | if data and conversion_required(data, self.name): |
| 503 | try: |
| 504 | return conversion_packer(data, self.name) |
| 505 | # Python 2 and 3 raises different exceptions from inet_pton |
| 506 | except(OSError, socket.error, TypeError): |
| 507 | pass |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 508 | if data is None: # Default to zero if not specified |
| 509 | if self.options and 'default' in self.options: |
| 510 | data = self.options['default'] |
| 511 | else: |
| 512 | data = 0 |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 513 | |
| 514 | return self.packer.pack(data, kwargs) |
| 515 | |
Ole Troan | da47f67 | 2019-11-24 15:22:55 +0100 | [diff] [blame] | 516 | @staticmethod |
| 517 | def _get_packer_with_options(f_type, options): |
| 518 | return VPPTypeAlias(f_type, types[f_type].msgdef, options=options) |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 519 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 520 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 521 | if ntc is False and self.name in vpp_format.conversion_unpacker_table: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 522 | # Disable type conversion for dependent types |
| 523 | ntc = True |
| 524 | self.toplevelconversion = True |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 525 | t, size = self.packer.unpack(data, offset, result, ntc=ntc) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 526 | if self.toplevelconversion: |
| 527 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 528 | return conversion_unpacker(t, self.name), size |
| 529 | return t, size |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 530 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 531 | def __repr__(self): |
| 532 | return "VPPTypeAlias(name=%s, msgdef=%s, options=%s)" % ( |
| 533 | self.name, self.msgdef, self.options) |
| 534 | |
Ole Troan | 53fffa1 | 2018-11-13 12:36:56 +0100 | [diff] [blame] | 535 | |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 536 | class VPPType(Packer): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 537 | # Set everything up to be able to pack / unpack |
| 538 | def __init__(self, name, msgdef): |
| 539 | self.name = name |
| 540 | self.msgdef = msgdef |
| 541 | self.packers = [] |
| 542 | self.fields = [] |
| 543 | self.fieldtypes = [] |
| 544 | self.field_by_name = {} |
| 545 | size = 0 |
| 546 | for i, f in enumerate(msgdef): |
| 547 | if type(f) is dict and 'crc' in f: |
| 548 | self.crc = f['crc'] |
| 549 | continue |
| 550 | f_type, f_name = f[:2] |
| 551 | self.fields.append(f_name) |
| 552 | self.field_by_name[f_name] = None |
| 553 | self.fieldtypes.append(f_type) |
| 554 | if f_type not in types: |
| 555 | logger.debug('Unknown type {}'.format(f_type)) |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 556 | raise VPPSerializerValueError( |
| 557 | 'Unknown message type {}'.format(f_type)) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 558 | |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 559 | fieldlen = len(f) |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 560 | options = [x for x in f if type(x) is dict] |
| 561 | if len(options): |
| 562 | self.options = options[0] |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 563 | fieldlen -= 1 |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 564 | else: |
| 565 | self.options = {} |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 566 | if fieldlen == 3: # list |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 567 | list_elements = f[2] |
| 568 | if list_elements == 0: |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 569 | if f_type == 'string': |
| 570 | p = String(f_name, 0, self.options) |
| 571 | else: |
| 572 | p = VLAList_legacy(f_name, f_type) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 573 | self.packers.append(p) |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 574 | elif f_type == 'u8': |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 575 | p = FixedList_u8(f_name, f_type, list_elements) |
| 576 | self.packers.append(p) |
| 577 | size += p.size |
Ole Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 578 | elif f_type == 'string': |
| 579 | p = String(f_name, list_elements, self.options) |
| 580 | self.packers.append(p) |
| 581 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 582 | else: |
| 583 | p = FixedList(f_name, f_type, list_elements) |
| 584 | self.packers.append(p) |
| 585 | size += p.size |
Ole Troan | 8546558 | 2019-04-30 10:04:36 +0200 | [diff] [blame] | 586 | elif fieldlen == 4: # Variable length list |
Ole Troan | 413f4a5 | 2018-11-28 11:36:05 +0100 | [diff] [blame] | 587 | length_index = self.fields.index(f[3]) |
| 588 | p = VLAList(f_name, f_type, f[3], length_index) |
| 589 | self.packers.append(p) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 590 | else: |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 591 | # default support for types that decay to basetype |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 592 | if 'default' in self.options: |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 593 | p = self.get_packer_with_options(f_type, self.options) |
Ole Troan | 40dc4b3 | 2019-10-22 14:01:53 +0200 | [diff] [blame] | 594 | else: |
| 595 | p = types[f_type] |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 596 | |
Ole Troan | 9ac1138 | 2019-04-23 17:11:01 +0200 | [diff] [blame] | 597 | self.packers.append(p) |
| 598 | size += p.size |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 599 | |
| 600 | self.size = size |
| 601 | self.tuple = collections.namedtuple(name, self.fields, rename=True) |
| 602 | types[name] = self |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 603 | self.toplevelconversion = False |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 604 | |
| 605 | def pack(self, data, kwargs=None): |
| 606 | if not kwargs: |
| 607 | kwargs = data |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 608 | b = bytes() |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 609 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 610 | # Try one of the format functions |
| 611 | if data and conversion_required(data, self.name): |
| 612 | return conversion_packer(data, self.name) |
| 613 | |
| 614 | for i, a in enumerate(self.fields): |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 615 | if data and type(data) is not dict and a not in data: |
Paul Vinciguerra | 6ccc6e9 | 2018-11-27 08:15:22 -0800 | [diff] [blame] | 616 | raise VPPSerializerValueError( |
| 617 | "Invalid argument: {} expected {}.{}". |
| 618 | format(data, self.name, a)) |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 619 | |
| 620 | # Defaulting to zero. |
| 621 | if not data or a not in data: # Default to 0 |
| 622 | arg = None |
| 623 | kwarg = None # No default for VLA |
| 624 | else: |
| 625 | arg = data[a] |
| 626 | kwarg = kwargs[a] if a in kwargs else None |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 627 | if isinstance(self.packers[i], VPPType): |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 628 | b += self.packers[i].pack(arg, kwarg) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 629 | else: |
Ole Troan | 31555a3 | 2018-10-22 09:30:26 +0200 | [diff] [blame] | 630 | b += self.packers[i].pack(arg, kwargs) |
| 631 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 632 | return b |
| 633 | |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 634 | def unpack(self, data, offset=0, result=None, ntc=False): |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 635 | # Return a list of arguments |
| 636 | result = [] |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 637 | total = 0 |
Ole Troan | 418ebb7 | 2019-11-19 04:38:20 +0100 | [diff] [blame] | 638 | if ntc is False and self.name in vpp_format.conversion_unpacker_table: |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 639 | # Disable type conversion for dependent types |
| 640 | ntc = True |
| 641 | self.toplevelconversion = True |
| 642 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 643 | for p in self.packers: |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 644 | x, size = p.unpack(data, offset, result, ntc) |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 645 | if type(x) is tuple and len(x) == 1: |
| 646 | x = x[0] |
| 647 | result.append(x) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 648 | offset += size |
| 649 | total += size |
| 650 | t = self.tuple._make(result) |
Ole Troan | 75761b9 | 2019-09-11 17:49:08 +0200 | [diff] [blame] | 651 | |
| 652 | if self.toplevelconversion: |
| 653 | self.toplevelconversion = False |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 654 | t = conversion_unpacker(t, self.name) |
Ole Troan | c84cbad | 2018-09-06 22:58:05 +0200 | [diff] [blame] | 655 | return t, total |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 656 | |
Paul Vinciguerra | 14b0b47 | 2019-11-23 23:49:57 -0500 | [diff] [blame] | 657 | def __repr__(self): |
| 658 | return "%s(name=%s, msgdef=%s)" % ( |
| 659 | self.__class__.__name__, self.name, self.msgdef |
| 660 | ) |
| 661 | |
Ole Troan | a7564e8 | 2018-06-12 21:06:44 +0200 | [diff] [blame] | 662 | |
| 663 | class VPPMessage(VPPType): |
| 664 | pass |