Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 1 | """ |
| 2 | MAC Types |
| 3 | |
| 4 | """ |
| 5 | |
| 6 | from util import mactobinary |
| 7 | |
| 8 | |
| 9 | class VppMacAddress(): |
| 10 | def __init__(self, addr): |
| 11 | self.address = addr |
| 12 | |
| 13 | def encode(self): |
| 14 | return { |
| 15 | 'bytes': self.bytes |
| 16 | } |
| 17 | |
| 18 | @property |
| 19 | def bytes(self): |
| 20 | return mactobinary(self.address) |
| 21 | |
| 22 | @property |
| 23 | def address(self): |
Paul Vinciguerra | dd89173 | 2018-11-30 12:03:04 -0800 | [diff] [blame] | 24 | return self.address |
| 25 | |
| 26 | @address.setter |
| 27 | def address(self, value): |
| 28 | self.address = value |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 29 | |
| 30 | def __str__(self): |
| 31 | return self.address |
| 32 | |
| 33 | def __eq__(self, other): |
| 34 | if isinstance(other, self.__class__): |
Paul Vinciguerra | dd89173 | 2018-11-30 12:03:04 -0800 | [diff] [blame] | 35 | return self.address == other.address |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 36 | elif hasattr(other, "bytes"): |
| 37 | # vl_api_mac_addres_t |
| 38 | return self.bytes == other.bytes |
| 39 | else: |
Paul Vinciguerra | dd89173 | 2018-11-30 12:03:04 -0800 | [diff] [blame] | 40 | raise TypeError("Comparing VppMacAddress:%s" |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 41 | "with unknown type: %s" % |
| 42 | (self, other)) |
| 43 | return False |