MAP: Remove dual loop in MAP-T in preparation for refactor.
[vpp.git] / test / vpp_mac.py
1 """
2   MAC Types
3
4 """
5 import binascii
6
7
8 def mactobinary(mac):
9     """ Convert the : separated format into binary packet data for the API """
10     return binascii.unhexlify(mac.replace(':', ''))
11
12
13 def binarytomac(binary):
14     """ Convert binary packed data in a : separated string """
15     x = b':'.join(binascii.hexlify(binary)[i:i + 2]
16                   for i in range(0, 12, 2))
17     return str(x.decode('ascii'))
18
19
20 class VppMacAddress():
21     def __init__(self, addr):
22         self._address = addr
23
24     def encode(self):
25         return {
26             'bytes': self.bytes
27         }
28
29     @property
30     def bytes(self):
31         return mactobinary(self.address)
32
33     @property
34     def address(self):
35         return self._address
36
37     def __str__(self):
38         return self.address
39
40     def __eq__(self, other):
41         if isinstance(other, self.__class__):
42             return self.address == other.address
43         elif hasattr(other, "bytes"):
44             # vl_api_mac_addres_t
45             return self.bytes == other.bytes
46         else:
47             raise TypeError("Comparing VppMacAddress:%s"
48                             "with unknown type: %s" %
49                             (self, other))
50         return False