blob: f6c6acd41e997a8db8c2de2fe4997e14e3bb9061 [file] [log] [blame]
Matej Klotton0178d522016-11-04 11:11:44 +01001import socket
Klement Sekera7bb873a2016-11-18 07:38:42 +01002import sys
Klement Sekera0e3c0de2016-09-29 14:43:44 +02003from abc import abstractmethod, ABCMeta
Klement Sekera7bb873a2016-11-18 07:38:42 +01004from cStringIO import StringIO
5
6
7def 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 Marionf56b77a2016-10-03 19:44:57 +020017
Klement Sekera0e3c0de2016-09-29 14:43:44 +020018class 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 Klotton0178d522016-11-04 11:11:44 +010039class Host(object):
40 """ Generic test host "connected" to VPPs interface. """
Damjan Marionf56b77a2016-10-03 19:44:57 +020041
Klement Sekeraf62ae122016-10-11 11:47:09 +020042 @property
43 def mac(self):
44 """ MAC address """
45 return self._mac
Damjan Marionf56b77a2016-10-03 19:44:57 +020046
Klement Sekeraf62ae122016-10-11 11:47:09 +020047 @property
48 def ip4(self):
49 """ IPv4 address """
50 return self._ip4
Damjan Marionf56b77a2016-10-03 19:44:57 +020051
Klement Sekeraf62ae122016-10-11 11:47:09 +020052 @property
Matej Klotton0178d522016-11-04 11:11:44 +010053 def ip4n(self):
54 """ IPv4 address """
55 return socket.inet_pton(socket.AF_INET, self._ip4)
56
57 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020058 def ip6(self):
59 """ IPv6 address """
60 return self._ip6
Damjan Marionf56b77a2016-10-03 19:44:57 +020061
Klement Sekeraf62ae122016-10-11 11:47:09 +020062 def __init__(self, mac=None, ip4=None, ip6=None):
63 self._mac = mac
64 self._ip4 = ip4
65 self._ip6 = ip6