Define sulfur version
[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 re
10 import xmlrpc.client
11
12
13 class BgpRpcClient(object):
14     """The client for SimpleXMLRPCServer."""
15
16     def __init__(self, peer_addr, port=8000):
17         """Setup destination point of the rpc server"""
18         self.proxy = xmlrpc.client.ServerProxy("http://{}:{}".format(peer_addr, port))
19
20     def exa_announce(self, full_exabgp_cmd):
21         """The full command to be passed to exabgp."""
22         return self.proxy.execute(full_exabgp_cmd)
23
24     def _exa_get_counter(self, msg_type):
25         """Gets counter form the server of given message type."""
26         return self.proxy.get_counter(msg_type)
27
28     def exa_get_received_open_count(self):
29         """Gets open messages counter."""
30         return self._exa_get_counter("open")
31
32     def exa_get_received_keepalive_count(self):
33         """Gets keepalive messages counter."""
34         return self._exa_get_counter("keepalive")
35
36     def exa_get_received_update_count(self):
37         """Gets update messges counter."""
38         return self._exa_get_counter("update")
39
40     def exa_get_received_route_refresh_count(self):
41         """Gets route refresh message counter."""
42         return self._exa_get_counter("route_refresh")
43
44     def _exa_clean_counter(self, msg_type):
45         """Cleans counter on the server of given message type."""
46         return self.proxy.clean_counter(msg_type)
47
48     def exa_clean_received_open_count(self):
49         """Cleans open message counter."""
50         return self._exa_clean_counter("open")
51
52     def exa_clean_received_keepalive_count(self):
53         """Cleans keepalive message counter."""
54         return self._exa_clean_counter("keepalive")
55
56     def exa_clean_received_update_count(self):
57         """Cleans update message counter."""
58         return self._exa_clean_counter("update")
59
60     def exa_clean_received_route_refresh_count(self):
61         """Cleans route refresh message counter."""
62         return self._exa_clean_counter("route_refresh")
63
64     def _exa_clean_message(self, msg_type):
65         """Cleans stored message on the server of given message type."""
66         return self.proxy.clean_message(msg_type)
67
68     def exa_clean_update_message(self):
69         """Cleans update message."""
70         return self._exa_clean_message("update")
71
72     def _exa_get_message(self, msg_type):
73         """Gets stored message on the server of given message type."""
74         return self.proxy.get_message(msg_type)
75
76     def exa_get_update_message(self, msg_only=True):
77         """Cleans update message.
78
79         Exabgp provides more details than just message content (e.g. peer ip,
80         timestamp, ...). msg_only is a flag that we want just message content
81         and no details.
82         """
83         msg = self._exa_get_message("update")
84         if not msg_only:
85             return msg
86         return msg if "neighbor" not in msg else msg["neighbor"]["message"]
87
88     def play_send(self, hexstring):
89         """Sends given hex data, already encoded bgp update message is expected."""
90         return self.proxy.send(hexstring.rstrip())
91
92     def play_get(self, what="update"):
93         """Gets the last received (update) mesage as hex string."""
94         return self.proxy.get(what)
95
96     def play_clean(self, what="update"):
97         """Cleans the message (update) on the server."""
98         return self.proxy.clean(what)
99
100     def sum_hex_message(self, hex_string):
101         """Verifies two hex messages are equal even in case, their arguments are misplaced.
102         Converts hex message arguments to integers and sums them up and returns the sum."""
103         return sum(
104             [int(x, 16) for x in re.compile("[a-f\d]{2}").findall(hex_string[32:])]
105         )