1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#!/usr/bin/env python
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright 2023, 6wind
import json
import os
from lib import topotest
from lib.topogen import get_topogen
from lib.topolog import logger
# remember the last sequence number of the logging messages
SEQ = 0
def bmp_reset_seq():
global SEQ
SEQ = 0
def get_bmp_messages(bmp_collector, bmp_log_file):
"""
Read the BMP logging messages.
"""
messages = []
text_output = bmp_collector.run(f"cat {bmp_log_file}")
for m in text_output.splitlines():
# some output in the bash can break the message decoding
try:
messages.append(json.loads(m))
except Exception as e:
logger.warning(str(e) + " message: {}".format(str(m)))
continue
if not messages:
logger.error("Bad BMP log format, check your BMP server")
return messages
def bmp_update_seq(bmp_collector, bmp_log_file):
global SEQ
messages = get_bmp_messages(bmp_collector, bmp_log_file)
if len(messages):
SEQ = messages[-1]["seq"]
def bmp_update_expected_files(
bmp_actual,
expected_prefixes,
bmp_log_type,
policy,
step,
bmp_client,
bmp_log_folder,
):
tgen = get_topogen()
with open(
f"{bmp_log_folder}/tmp/bmp-{bmp_log_type}-{policy}-step{step}.json", "w"
) as json_file:
json.dump(bmp_actual, json_file, indent=4)
out = bmp_client.vtysh_cmd("show bgp vrf vrf1 ipv4 json", isjson=True)
filtered_out = {
"routes": {
prefix: route_info
for prefix, route_info in out["routes"].items()
if prefix in expected_prefixes
}
}
if bmp_log_type == "withdraw":
for pfx in expected_prefixes:
if "::" in pfx:
continue
filtered_out["routes"][pfx] = None
# ls {bmp_log_folder}/tmp/show*json | while read file; do egrep -v 'prefix|network|metric|ocPrf|version|weight|peerId|vrf|Version|valid|Reason|fe80' $file >$(basename $file); echo >> $(basename $file); done
with open(
f"{bmp_log_folder}/tmp/show-bgp-ipv4-{bmp_log_type}-step{step}.json", "w"
) as json_file:
json.dump(filtered_out, json_file, indent=4)
out = tgen.gears["r1"].vtysh_cmd("show bgp vrf vrf1 ipv6 json", isjson=True)
filtered_out = {
"routes": {
prefix: route_info
for prefix, route_info in out["routes"].items()
if prefix in expected_prefixes
}
}
if bmp_log_type == "withdraw":
for pfx in expected_prefixes:
if "::" not in pfx:
continue
filtered_out["routes"][pfx] = None
with open(
f"{bmp_log_folder}/tmp/show-bgp-ipv6-{bmp_log_type}-step{step}.json", "w"
) as json_file:
json.dump(filtered_out, json_file, indent=4)
def bmp_check_for_prefixes(
expected_prefixes,
bmp_log_type,
policy,
step,
bmp_collector,
bmp_log_folder,
bmp_client,
expected_json_path,
update_expected_json,
loc_rib,
):
"""
Check for the presence of the given prefixes in the BMP server logs with
the given message type and the set policy.
"""
global SEQ
bmp_log_file = f"{bmp_log_folder}/bmp.log"
# we care only about the new messages
messages = [
m
for m in sorted(
get_bmp_messages(bmp_collector, bmp_log_file), key=lambda d: d["seq"]
)
if m["seq"] > SEQ
]
# create empty initial files
# for step in $(seq 1); do
# for i in "update" "withdraw"; do
# for j in "pre-policy" "post-policy" "loc-rib"; do
# echo '{"null": {}}'> bmp-$i-$j-step$step.json
# done
# done
# done
ref_file = f"{expected_json_path}/bmp-{bmp_log_type}-{policy}-step{step}.json"
expected = json.loads(open(ref_file).read())
# Build actual json from logs
actual = {}
for m in messages:
if (
"bmp_log_type" in m.keys()
and "ip_prefix" in m.keys()
and m["ip_prefix"] in expected_prefixes
and m["bmp_log_type"] == bmp_log_type
and m["policy"] == policy
):
policy_dict = actual.setdefault(m["policy"], {})
bmp_log_type_dict = policy_dict.setdefault(m["bmp_log_type"], {})
# Add or update the ip_prefix dictionary with filtered key-value pairs
bmp_log_type_dict[m["ip_prefix"]] = {
k: v
for k, v in sorted(m.items())
# filter out variable keys
if k not in ["timestamp", "seq", "nxhp_link-local"]
}
# build expected JSON files
if (
update_expected_json
and actual
and set(actual.get(policy, {}).get(bmp_log_type, {}).keys())
== set(expected_prefixes)
):
bmp_update_expected_files(
actual,
expected_prefixes,
bmp_log_type,
policy,
step,
bmp_client,
bmp_log_folder,
)
return topotest.json_cmp(actual, expected, exact=True)
def bmp_check_for_peer_message(
expected_peers,
bmp_log_type,
bmp_collector,
bmp_log_file,
is_rd_instance=False,
peer_bgp_id=None,
peer_distinguisher=None,
):
"""
Check for the presence of a peer up message for the peer
"""
global SEQ
last_seq = SEQ
# we care only about the new messages
messages = [
m
for m in sorted(
get_bmp_messages(bmp_collector, bmp_log_file), key=lambda d: d["seq"]
)
if m["seq"] > SEQ
]
# get the list of pairs (prefix, policy, seq) for the given message type
peers = []
for m in messages:
if is_rd_instance and m["peer_distinguisher"] == "0:0":
continue
if peer_distinguisher and m["peer_distinguisher"] != peer_distinguisher:
continue
if peer_bgp_id and m["peer_bgp_id"] != peer_bgp_id:
continue
if (
"peer_ip" in m.keys()
and m["peer_ip"] != "0.0.0.0"
and m["bmp_log_type"] == bmp_log_type
):
if is_rd_instance and m["peer_type"] != "route distinguisher instance":
continue
peers.append((m["peer_ip"], m["seq"]))
elif m["policy"] == "loc-rib" and m["bmp_log_type"] == bmp_log_type:
peers.append(("0.0.0.0", m["seq"]))
# check for prefixes
for ep in expected_peers:
for _ip, _seq in peers:
if ep == _ip:
msg = "The peer {} is present in the {} log messages."
logger.debug(msg.format(ep, bmp_log_type))
if _seq > last_seq:
last_seq = _seq
break
else:
msg = "The peer {} is not present in the {} log messages."
logger.debug(msg.format(ep, bmp_log_type))
return False
SEQ = last_seq
return True
|