Add ping test in aclrecovery suite
[integration/test.git] / csit / libraries / BgpRpcClient.py
1 """
2 The purpose of this library is communicate with tools which run xmlrpc server.
3 At the moment it is going to be the with the exarpc.py (used with exabgp) and
4 with play.py for bgp functional testing.
5 exa_ methods apply to test/tools/exabgp_files/exarpc.py
6 play_ methods apply to test/tool/fastbgp/play.py  (only with --evpn used)
7 """
8
9 import xmlrpclib
10
11
12 class BgpRpcClient(object):
13     """The client for SimpleXMLRPCServer."""
14
15     def __init__(self, peer_addr, port=8000):
16         """Setup destination point of the rpc server"""
17         self.proxy = xmlrpclib.ServerProxy("http://{}:{}".format(peer_addr, port))
18
19     def exa_announce(self, full_exabgp_cmd):
20         """The full command to be passed to exabgp."""
21         return self.proxy.execute(full_exabgp_cmd)
22
23     def _exa_get_counter(self, msg_type):
24         """Gets counter form the server of given message type."""
25         return self.proxy.get_counter(msg_type)
26
27     def exa_get_received_open_count(self):
28         """Gets open messages counter."""
29         return self._exa_get_counter('open')
30
31     def exa_get_received_keepalive_count(self):
32         """Gets keepalive messages counter."""
33         return self._exa_get_counter('keepalive')
34
35     def exa_get_received_update_count(self):
36         """Gets update messges counter."""
37         return self._exa_get_counter('update')
38
39     def exa_get_received_route_refresh_count(self):
40         """Gets route refresh message counter."""
41         return self._exa_get_counter('route_refresh')
42
43     def _exa_clean_counter(self, msg_type):
44         """Cleans counter on the server of given message type."""
45         return self.proxy.clean_counter(msg_type)
46
47     def exa_clean_received_open_count(self):
48         """Cleans open message counter."""
49         return self._exa_clean_counter('open')
50
51     def exa_clean_received_keepalive_count(self):
52         """Cleans keepalive message counter."""
53         return self._exa_clean_counter('keepalive')
54
55     def exa_clean_received_update_count(self):
56         """Cleans update message counter."""
57         return self._exa_clean_counter('update')
58
59     def exa_clean_received_route_refresh_count(self):
60         """Cleans route refresh message counter."""
61         return self._exa_clean_counter('route_refresh')
62
63     def _exa_clean_message(self, msg_type):
64         """Cleans stored message on the server of given message type."""
65         return self.proxy.clean_message(msg_type)
66
67     def exa_clean_update_message(self):
68         """Cleans update message."""
69         return self._exa_clean_message('update')
70
71     def _exa_get_message(self, msg_type):
72         """Gets stored message on the server of given message type."""
73         return self.proxy.get_message(msg_type)
74
75     def exa_get_update_message(self, msg_only=True):
76         """Cleans update message.
77
78         Exabgp provides more details than just message content (e.g. peer ip,
79         timestamp, ...). msg_only is a flag that we want just message content
80         and no details.
81         """
82         msg = self._exa_get_message('update')
83         if not msg_only:
84             return msg
85         return msg if 'neighbor' not in msg else msg['neighbor']['message']
86
87     def play_send(self, hexstring):
88         """Sends given hex data, already encoded bgp update message is expected."""
89         return self.proxy.send(hexstring.rstrip())
90
91     def play_get(self, what='update'):
92         """Gets the last received (update) mesage as hex string."""
93         return self.proxy.get(what)
94
95     def play_clean(self, what='update'):
96         """Cleans the message (update) on the server."""
97         return self.proxy.clean(what)