Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 1 | import socket |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 2 | import sys |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame^] | 3 | from abc import abstractmethod, ABCMeta |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 4 | from cStringIO import StringIO |
| 5 | |
| 6 | |
| 7 | def ppp(headline, packet): |
| 8 | """ Return string containing the output of scapy packet.show() call. """ |
| 9 | o = StringIO() |
| 10 | old_stdout = sys.stdout |
| 11 | sys.stdout = o |
| 12 | print(headline) |
| 13 | packet.show() |
| 14 | sys.stdout = old_stdout |
| 15 | return o.getvalue() |
| 16 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 17 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame^] | 18 | class NumericConstant(object): |
| 19 | __metaclass__ = ABCMeta |
| 20 | |
| 21 | desc_dict = {} |
| 22 | |
| 23 | @abstractmethod |
| 24 | def __init__(self, value): |
| 25 | self._value = value |
| 26 | |
| 27 | def __int__(self): |
| 28 | return self._value |
| 29 | |
| 30 | def __long__(self): |
| 31 | return self._value |
| 32 | |
| 33 | def __str__(self): |
| 34 | if self._value in self.desc_dict: |
| 35 | return self.desc_dict[self._value] |
| 36 | return "" |
| 37 | |
| 38 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 39 | class Host(object): |
| 40 | """ Generic test host "connected" to VPPs interface. """ |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 41 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 42 | @property |
| 43 | def mac(self): |
| 44 | """ MAC address """ |
| 45 | return self._mac |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 46 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 47 | @property |
| 48 | def ip4(self): |
| 49 | """ IPv4 address """ |
| 50 | return self._ip4 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 51 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 52 | @property |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 53 | def ip4n(self): |
| 54 | """ IPv4 address """ |
| 55 | return socket.inet_pton(socket.AF_INET, self._ip4) |
| 56 | |
| 57 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 58 | def ip6(self): |
| 59 | """ IPv6 address """ |
| 60 | return self._ip6 |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 61 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 62 | def __init__(self, mac=None, ip4=None, ip6=None): |
| 63 | self._mac = mac |
| 64 | self._ip4 = ip4 |
| 65 | self._ip6 = ip6 |