BFD: basic asynchronous session up/down
[vpp.git] / test / util.py
1 import socket
2 import sys
3 from abc import abstractmethod, ABCMeta
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
17
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
39 class Host(object):
40     """ Generic test host "connected" to VPPs interface. """
41
42     @property
43     def mac(self):
44         """ MAC address """
45         return self._mac
46
47     @property
48     def ip4(self):
49         """ IPv4 address """
50         return self._ip4
51
52     @property
53     def ip4n(self):
54         """ IPv4 address """
55         return socket.inet_pton(socket.AF_INET, self._ip4)
56
57     @property
58     def ip6(self):
59         """ IPv6 address """
60         return self._ip6
61
62     def __init__(self, mac=None, ip4=None, ip6=None):
63         self._mac = mac
64         self._ip4 = ip4
65         self._ip6 = ip6