forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduthost_utils.py
More file actions
584 lines (513 loc) · 22.9 KB
/
Copy pathduthost_utils.py
File metadata and controls
584 lines (513 loc) · 22.9 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import pytest
import logging
import itertools
import collections
import ipaddress
import time
import json
from tests.common.helpers.assertions import pytest_assert
from tests.common.utilities import wait_until
from jinja2 import Template
from netaddr import valid_ipv4
logger = logging.getLogger(__name__)
def _backup_and_restore_config_db(duts, scope='function'):
"""Back up the existing config_db.json file and restore it once the test ends.
Some cases will update the running config during the test and save the config
to be recovered aftet reboot. In such a case we need to backup config_db.json before
the test starts and then restore it after the test ends.
"""
CONFIG_DB = "/etc/sonic/config_db.json"
CONFIG_DB_BAK = "/etc/sonic/config_db.json.before_test_{}".format(scope)
if type(duts) is not list:
duthosts = [duts]
else:
duthosts = duts
for duthost in duthosts:
logger.info("Backup {} to {} on {}".format(CONFIG_DB, CONFIG_DB_BAK, duthost.hostname))
duthost.shell("cp {} {}".format(CONFIG_DB, CONFIG_DB_BAK))
yield
for duthost in duthosts:
logger.info("Restore {} with {} on {}".format(CONFIG_DB, CONFIG_DB_BAK, duthost.hostname))
duthost.shell("mv {} {}".format(CONFIG_DB_BAK, CONFIG_DB))
@pytest.fixture(scope="module")
def backup_and_restore_config_db_on_duts(duthosts):
"""
A module level fixture to backup and restore config_db.json on all duts
"""
for func in _backup_and_restore_config_db(duthosts, "module"):
yield func
@pytest.fixture
def backup_and_restore_config_db(duthosts, rand_one_dut_hostname):
"""Back up and restore config DB at the function level."""
duthost = duthosts[rand_one_dut_hostname]
# TODO: Use the neater "yield from _function" syntax when we move to python3
for func in _backup_and_restore_config_db(duthost, "function"):
yield func
@pytest.fixture(scope="module")
def backup_and_restore_config_db_module(duthosts, rand_one_dut_hostname):
"""Back up and restore config DB at the module level."""
duthost = duthosts[rand_one_dut_hostname]
# TODO: Use the neater "yield from _function" syntax when we move to python3
for func in _backup_and_restore_config_db(duthost, "module"):
yield func
@pytest.fixture(scope="session")
def backup_and_restore_config_db_session(duthosts):
for func in _backup_and_restore_config_db(duthosts, "session"):
yield func
def _disable_route_checker(duthost):
"""
Some test cases will add static routes for test, which may trigger route_checker
to report error. This function is to disable route_checker before test, and recover it
after test.
Args:
duthost: DUT fixture
"""
duthost.command('monit stop routeCheck', module_ignore_errors=True)
yield
duthost.command('monit start routeCheck', module_ignore_errors=True)
@pytest.fixture
def disable_route_checker(duthosts, rand_one_dut_hostname):
"""
Wrapper for _disable_route_checker, function level
"""
duthost = duthosts[rand_one_dut_hostname]
for func in _disable_route_checker(duthost):
yield func
@pytest.fixture(scope='module')
def disable_route_checker_module(duthosts, rand_one_dut_hostname):
"""
Wrapper for _disable_route_checker, module level
"""
duthost = duthosts[rand_one_dut_hostname]
for func in _disable_route_checker(duthost):
yield func
@pytest.fixture(scope='module')
def disable_fdb_aging(duthost):
"""
Disable fdb aging by swssconfig.
The original config will be recovered after running test.
"""
switch_config = """[
{
"SWITCH_TABLE:switch": {
"ecmp_hash_seed": "0",
"lag_hash_seed": "0",
"fdb_aging_time": "{{ aging_time }}"
},
"OP": "SET"
}
]"""
TMP_SWITCH_CONFIG_FILE = "/tmp/switch_config.json"
DST_SWITCH_CONFIG_FILE = "/switch_config.json"
switch_config_template = Template(switch_config)
duthost.copy(content=switch_config_template.render(aging_time=0),
dest=TMP_SWITCH_CONFIG_FILE)
if duthost.is_multi_asic:
ns = duthost.get_asic_namespace_list()[0]
asic_id = duthost.get_asic_id_from_namespace(ns)
else:
asic_id = ''
# Generate and load config with swssconfig
cmds = [
"docker cp {} swss{}:{}".format(TMP_SWITCH_CONFIG_FILE, asic_id, DST_SWITCH_CONFIG_FILE),
"docker exec -i swss{} swssconfig {}".format(asic_id, DST_SWITCH_CONFIG_FILE)
]
duthost.shell_cmds(cmds=cmds)
yield
# Recover default aging time
DEFAULT_SWITCH_CONFIG_FILE = "/etc/swss/config.d/switch.json"
cmds = [
"docker exec -i swss{} rm {}".format(asic_id, DST_SWITCH_CONFIG_FILE),
"docker exec -i swss{} swssconfig {}".format(asic_id, DEFAULT_SWITCH_CONFIG_FILE)
]
duthost.shell_cmds(cmds=cmds)
duthost.file(path=TMP_SWITCH_CONFIG_FILE, state="absent")
@pytest.fixture(scope="module")
def ports_list(duthosts, rand_one_dut_hostname, rand_selected_dut, tbinfo):
duthost = duthosts[rand_one_dut_hostname]
cfg_facts = duthost.config_facts(host=duthost.hostname, source="persistent")['ansible_facts']
mg_facts = rand_selected_dut.get_extended_minigraph_facts(tbinfo)
config_ports = {k: v for k,v in cfg_facts['PORT'].items() if v.get('admin_status', 'down') == 'up'}
config_port_indices = {k: v for k, v in mg_facts['minigraph_ptf_indices'].items() if k in config_ports}
ptf_ports_available_in_topo = {
port_index: 'eth{}'.format(port_index) for port_index in config_port_indices.values()
}
config_portchannels = cfg_facts.get('PORTCHANNEL_MEMBER', {})
config_port_channel_members = [port_channel.keys() for port_channel in config_portchannels.values()]
config_port_channel_member_ports = list(itertools.chain.from_iterable(config_port_channel_members))
ports = [port for port in config_ports
if config_port_indices[port] in ptf_ports_available_in_topo
and config_ports[port].get('admin_status', 'down') == 'up'
and port not in config_port_channel_member_ports]
return ports
def check_orch_cpu_utilization(dut, orch_cpu_threshold):
"""
Compare orchagent CPU utilization 5 times, with 1 second interval in between and make sure all 5 readings are
less than threshold
Args:
dut: DUT host object
orch_cpu_threshold: orch cpu threshold
"""
for i in range(5):
orch_cpu = dut.shell("COLUMNS=512 show processes cpu | grep orchagent | awk '{print $9}'")["stdout_lines"]
for line in orch_cpu:
if int(float(line)) > orch_cpu_threshold:
return False
time.sleep(1)
return True
def check_ebgp_routes(num_v4_routes, num_v6_routes, duthost):
MAX_DIFF = 5
sumv4, sumv6 = duthost.get_ip_route_summary()
rtn_val = True
if 'ebgp' in sumv4 and 'routes' in sumv4['ebgp'] and \
abs(int(float(sumv4['ebgp']['routes'])) - int(float(num_v4_routes))) >= MAX_DIFF:
logger.info("IPv4 ebgp routes: {}".format(float(sumv4['ebgp']['routes'])))
rtn_val = False
if 'ebgp' in sumv6 and 'routes' in sumv6['ebgp'] and \
abs(int(float(sumv6['ebgp']['routes'])) - int(float(num_v6_routes))) >= MAX_DIFF:
logger.info("IPv6 ebgp routes: {}".format(float(sumv6['ebgp']['routes'])))
rtn_val = False
return rtn_val
@pytest.fixture(scope="module")
def shutdown_ebgp(duthosts, rand_one_dut_hostname):
# To store the original number of eBGP v4 and v6 routes.
v4ebgps = {}
v6ebgps = {}
orch_cpu_threshold = 10
# increase timeout for check_orch_cpu_utilization to 120sec for chassis
# especially uplink cards need >60sec for orchagent cpu usage to come down to 10%
duthost = duthosts[rand_one_dut_hostname]
is_chassis = duthost.get_facts().get("modular_chassis")
orch_cpu_timeout = 120 if is_chassis else 60
for duthost in duthosts.frontend_nodes:
# Get the original number of eBGP v4 and v6 routes on the DUT.
sumv4, sumv6 = duthost.get_ip_route_summary()
v4ebgps[duthost.hostname] = sumv4.get('ebgp', {'routes': 0})['routes']
v6ebgps[duthost.hostname] = sumv6.get('ebgp', {'routes': 0})['routes']
# Shutdown all eBGP neighbors
duthost.command("sudo config bgp shutdown all")
# Verify that the total eBGP routes are 0.
pytest_assert(wait_until(60, 2, 5, check_ebgp_routes, 0, 0, duthost),
"eBGP routes are not 0 after shutting down all neighbors on {}".format(duthost))
pytest_assert(wait_until(orch_cpu_timeout, 2, 0, check_orch_cpu_utilization, duthost, orch_cpu_threshold),
"Orch CPU utilization {} > orch cpu threshold {} after shutdown all eBGP"
.format(duthost.shell("show processes cpu | grep orchagent | awk '{print $9}'")["stdout"],
orch_cpu_threshold))
yield
for duthost in duthosts.frontend_nodes:
# Startup all the eBGP neighbors
duthost.command("sudo config bgp startup all")
for duthost in duthosts.frontend_nodes:
# Verify that total eBGP routes are what they were before shutdown of all eBGP neighbors
orig_v4_ebgp = v4ebgps[duthost.hostname]
orig_v6_ebgp = v6ebgps[duthost.hostname]
pytest_assert(wait_until(120, 10, 10, check_ebgp_routes, orig_v4_ebgp, orig_v6_ebgp, duthost),
"eBGP v4 routes are {}, and v6 route are {}, and not what they were originally after enabling "
"all neighbors on {}".format(orig_v4_ebgp, orig_v6_ebgp, duthost))
pytest_assert(wait_until(orch_cpu_timeout, 2, 0, check_orch_cpu_utilization, duthost, orch_cpu_threshold),
"Orch CPU utilization {} > orch cpu threshold {} after startup all eBGP"
.format(duthost.shell("show processes cpu | grep orchagent | awk '{print $9}'")["stdout"],
orch_cpu_threshold))
@pytest.fixture(scope="module")
def utils_vlan_ports_list(duthosts, rand_one_dut_hostname, rand_selected_dut, tbinfo, ports_list):
"""
Get configured VLAN ports
"""
duthost = duthosts[rand_one_dut_hostname]
cfg_facts = duthost.config_facts(host=duthost.hostname, source="persistent")['ansible_facts']
mg_facts = rand_selected_dut.get_extended_minigraph_facts(tbinfo)
vlan_ports_list = []
config_ports = {k: v for k, v in cfg_facts['PORT'].items() if v.get('admin_status', 'down') == 'up'}
config_portchannels = cfg_facts.get('PORTCHANNEL_MEMBER', {})
config_port_indices = {k: v for k, v in mg_facts['minigraph_ptf_indices'].items() if k in config_ports}
config_ports_vlan = collections.defaultdict(list)
vlan_members = cfg_facts.get('VLAN_MEMBER', {})
# key is dev name, value is list for configured VLAN member.
for k, v in cfg_facts['VLAN'].items():
vlanid = v['vlanid']
for addr in cfg_facts['VLAN_INTERFACE']['Vlan'+vlanid]:
# address could be IPV6 and IPV4, only need IPV4 here
if addr and valid_ipv4(addr.split('/')[0]):
ip = addr
break
else:
continue
if k not in vlan_members:
continue
for port in vlan_members[k]:
if 'tagging_mode' not in vlan_members[k][port]:
continue
mode = vlan_members[k][port]['tagging_mode']
config_ports_vlan[port].append({'vlanid': int(vlanid), 'ip': ip, 'tagging_mode': mode})
if config_portchannels:
for po in config_portchannels:
vlan_port = {
'dev': po,
'port_index': [config_port_indices[member] for member in config_portchannels[po].keys()],
'permit_vlanid': []
}
if po in config_ports_vlan:
vlan_port['pvid'] = 0
for vlan in config_ports_vlan[po]:
if 'vlanid' not in vlan or 'ip' not in vlan or 'tagging_mode' not in vlan:
continue
if vlan['tagging_mode'] == 'untagged':
vlan_port['pvid'] = vlan['vlanid']
vlan_port['permit_vlanid'].append(vlan['vlanid'])
if 'pvid' in vlan_port:
vlan_ports_list.append(vlan_port)
for i, port in enumerate(ports_list):
vlan_port = {
'dev' : port,
'port_index' : [config_port_indices[port]],
'permit_vlanid' : []
}
if port in config_ports_vlan:
vlan_port['pvid'] = 0
for vlan in config_ports_vlan[port]:
if 'vlanid' not in vlan or 'ip' not in vlan or 'tagging_mode' not in vlan:
continue
if vlan['tagging_mode'] == 'untagged':
vlan_port['pvid'] = vlan['vlanid']
vlan_port['permit_vlanid'].append(vlan['vlanid'])
if 'pvid' in vlan_port:
vlan_ports_list.append(vlan_port)
return vlan_ports_list
def compare_network(src_ipprefix, dst_ipprefix):
src_network = ipaddress.IPv4Interface(src_ipprefix).network
dst_network = ipaddress.IPv4Interface(dst_ipprefix).network
return src_network.overlaps(dst_network)
@pytest.fixture(scope="module")
def utils_vlan_intfs_dict_orig(duthosts, rand_one_dut_hostname, tbinfo):
'''A module level fixture to record duthost's original vlan info
Args:
duthosts: All DUTs belong to the testbed.
rand_one_dut_hostname: hostname of a random chosen dut to run test.
tbinfo: A fixture to gather information about the testbed.
Returns:
VLAN info dict with original VLAN info
Example:
"Vlan1000": {
"fc02:1000::1/64": {},
"grat_arp": "enabled",
"proxy_arp": "enabled",
"192.168.0.1/21": {}
}
'''
duthost = duthosts[rand_one_dut_hostname]
cfg_facts = duthost.config_facts(host=duthost.hostname, source="persistent")['ansible_facts']
vlan_intfs_dict = {}
for k, v in cfg_facts['VLAN'].items():
vlanid = v['vlanid']
for addr in cfg_facts['VLAN_INTERFACE']['Vlan'+vlanid]:
# NOTE: here only returning IPv4.
if addr and valid_ipv4(addr.split('/')[0]):
ip = addr
break
else:
continue
logger.info("Original VLAN {}, ip {}".format(vlanid, ip))
vlan_intfs_dict[int(vlanid)] = {'ip': ip, 'orig': True}
return vlan_intfs_dict
def utils_vlan_intfs_dict_add(vlan_intfs_dict, add_cnt):
'''Utilities function to add add_cnt of new VLAN
Args:
vlan_intfs_dict: Original VLAN info dict
add_cnt: number of new vlan to add
Returns:
VLAN info dict combined with original and new added VLAN info
Example:
{
1000: {'ip':'192.168.0.1/21', 'orig': True},
108: {'ip':'192.168.8.1/24', 'orig': False},
109: {'ip':'192.168.9.1/24', 'orig': False}
}
'''
vlan_cnt = 0
for i in range(0, 255):
vid = 100 + i
if vid in vlan_intfs_dict:
continue
ip = u'192.168.{}.1/24'.format(i)
for v in vlan_intfs_dict.values():
if compare_network(ip, v['ip']):
break
else:
logger.info("Add VLAN {}, ip {}".format(vid, ip))
vlan_intfs_dict[vid] = {'ip': ip, 'orig': False}
vlan_cnt += 1
if vlan_cnt >= add_cnt:
break
assert vlan_cnt == add_cnt
return vlan_intfs_dict
def utils_create_test_vlans(duthost, cfg_facts, vlan_ports_list, vlan_intfs_dict, delete_untagged_vlan):
'''Utilities function to create vlans for test
Args:
duthost: Device Under Test (DUT)
cfg_facts: config facts fot the duthost
vlan_ports_list: vlan ports info
vlan_intfs_dict: VLAN info dict with VLAN info
delete_untagged_vlan: check to delete unttaged vlan
'''
cmds = []
logger.info("Add vlans, assign IPs")
for k, v in vlan_intfs_dict.items():
if v['orig']:
continue
cmds.append('config vlan add {}'.format(k))
cmds.append("config interface ip add Vlan{} {}".format(k, v['ip'].upper()))
# Delete untagged vlans from interfaces to avoid error message
# when adding untagged vlan to interface that already have one
if delete_untagged_vlan and '201911' not in duthost.os_version:
logger.info("Delete untagged vlans from interfaces")
for vlan_port in vlan_ports_list:
vlan_members = cfg_facts.get('VLAN_MEMBER', {})
vlan_name, vid = vlan_members.keys()[0], vlan_members.keys()[0].replace("Vlan", '')
try:
if vlan_members[vlan_name][vlan_port['dev']]['tagging_mode'] == 'untagged':
cmds.append("config vlan member del {} {}".format(vid, vlan_port['dev']))
except KeyError:
continue
logger.info("Add members to Vlans")
for vlan_port in vlan_ports_list:
for permit_vlanid in vlan_port['permit_vlanid']:
if vlan_intfs_dict[int(permit_vlanid)]['orig']:
continue
cmds.append('config vlan member add {tagged} {id} {port}'.format(
tagged=('--untagged' if vlan_port['pvid'] == permit_vlanid else ''),
id=permit_vlanid,
port=vlan_port['dev']
))
logger.info("Commands: {}".format(cmds))
duthost.shell_cmds(cmds=cmds)
def _dut_qos_map(dut):
"""
A helper function to get QoS map from DUT host.
Return a dict
{
"dscp_to_tc_map": {
"0":"1",
...
},
"tc_to_queue_map": {
"0":"0"
},
...
}
or an empty dict if failed to parse the output
"""
maps = {}
try:
if dut.is_multi_asic:
sonic_cfggen_cmd = "sonic-cfggen -n asic0 -d --var-json"
else:
sonic_cfggen_cmd = "sonic-cfggen -d --var-json"
# port_qos_map
port_qos_map = dut.shell("{} 'PORT_QOS_MAP'".format(sonic_cfggen_cmd))['stdout']
maps['port_qos_map'] = json.loads(port_qos_map) if port_qos_map else None
# dscp_to_tc_map
dscp_to_tc_map = dut.shell("{} 'DSCP_TO_TC_MAP'".format(sonic_cfggen_cmd))['stdout']
maps['dscp_to_tc_map'] = json.loads(dscp_to_tc_map) if dscp_to_tc_map else None
# tc_to_queue_map
tc_to_queue_map = dut.shell("{} 'TC_TO_QUEUE_MAP'".format(sonic_cfggen_cmd))['stdout']
maps['tc_to_queue_map'] = json.loads(tc_to_queue_map) if tc_to_queue_map else None
# tc_to_priority_group_map
tc_to_priority_group_map = dut.shell("{} 'TC_TO_PRIORITY_GROUP_MAP'".format(sonic_cfggen_cmd))['stdout']
maps['tc_to_priority_group_map'] = json.loads(tc_to_priority_group_map) if tc_to_priority_group_map else None
# tc_to_dscp_map
tc_to_dscp_map = dut.shell("{} 'TC_TO_DSCP_MAP'".format(sonic_cfggen_cmd))['stdout']
maps['tc_to_dscp_map'] = json.loads(tc_to_dscp_map) if tc_to_dscp_map else None
except Exception as e:
logger.error("Got exception: " + repr(e))
return maps
@pytest.fixture(scope='class')
def dut_qos_maps(get_src_dst_asic_and_duts):
"""
A class level fixture to get QoS map from DUT host.
Return a dict
"""
dut = get_src_dst_asic_and_duts['src_dut']
return _dut_qos_map(dut)
@pytest.fixture(scope='module')
def dut_qos_maps_module(rand_selected_front_end_dut):
"""
A module level fixture to get QoS map from DUT host.
return a dict
"""
dut = rand_selected_front_end_dut
return _dut_qos_map(dut)
def separated_dscp_to_tc_map_on_uplink(dut_qos_maps_module):
"""
A helper function to check if separated DSCP_TO_TC_MAP is applied to
downlink/unlink ports.
"""
dscp_to_tc_map_names = set()
for port_name, qos_map in dut_qos_maps_module['port_qos_map'].iteritems():
if port_name == "global":
continue
dscp_to_tc_map_names.add(qos_map.get("dscp_to_tc_map", ""))
if len(dscp_to_tc_map_names) > 1:
return True
return False
def load_dscp_to_pg_map(duthost, port, dut_qos_maps_module):
"""
Helper function to calculate DSCP to PG map for a port.
The map is derived from DSCP_TO_TC_MAP + TC_TO_PG_MAP
return a dict like {0:0, 1:1...}
"""
try:
port_qos_map = dut_qos_maps_module['port_qos_map']
dscp_to_tc_map_name = port_qos_map[port]['dscp_to_tc_map'].split('|')[-1].strip(']')
tc_to_pg_map_name = port_qos_map[port]['tc_to_pg_map'].split('|')[-1].strip(']')
# Load dscp_to_tc_map
dscp_to_tc_map = dut_qos_maps_module['dscp_to_tc_map'][dscp_to_tc_map_name]
# Load tc_to_pg_map
tc_to_pg_map = dut_qos_maps_module['tc_to_priority_group_map'][tc_to_pg_map_name]
# Calculate dscp to pg map
dscp_to_pg_map = {}
for dscp, tc in dscp_to_tc_map.items():
dscp_to_pg_map[dscp] = tc_to_pg_map[tc]
return dscp_to_pg_map
except:
logger.error("Failed to retrieve dscp to pg map for port {} on {}".format(port, duthost.hostname))
return {}
def load_dscp_to_queue_map(duthost, port, dut_qos_maps_module):
"""
Helper function to calculate DSCP to Queue map for a port.
The map is derived from DSCP_TO_TC_MAP + TC_TO_QUEUE_MAP
return a dict like {0:0, 1:1...}
"""
try:
port_qos_map = dut_qos_maps_module['port_qos_map']
dscp_to_tc_map_name = port_qos_map[port]['dscp_to_tc_map'].split('|')[-1].strip(']')
tc_to_queue_map_name = port_qos_map[port]['tc_to_queue_map'].split('|')[-1].strip(']')
# Load dscp_to_tc_map
dscp_to_tc_map = dut_qos_maps_module['dscp_to_tc_map'][dscp_to_tc_map_name][dscp_to_tc_map_name]
# Load tc_to_queue_map
tc_to_queue_map = dut_qos_maps_module['tc_to_queue_map'][tc_to_queue_map_name]
# Calculate dscp to queue map
dscp_to_queue_map = {}
for dscp, tc in dscp_to_tc_map.items():
dscp_to_queue_map[dscp] = tc_to_queue_map[tc]
return dscp_to_queue_map
except:
logger.error("Failed to retrieve dscp to queue map for port {} on {}".format(port, duthost.hostname))
return {}
def check_bgp_router_id(duthost, mgFacts):
"""
Check bgp router ID is same as Loopback0
"""
check_bgp_router_id_cmd = r'vtysh -c "show ip bgp summary json"'
bgp_summary = duthost.shell(check_bgp_router_id_cmd, module_ignore_errors=True)
try:
bgp_summary_json = json.loads(bgp_summary['stdout'])
router_id = str(bgp_summary_json['ipv4Unicast']['routerId'])
loopback0 = str(mgFacts['minigraph_lo_interfaces'][0]['addr'])
if router_id == loopback0:
logger.info("BGP router identifier: %s == Loopback0 address %s" % (router_id, loopback0))
return True
else:
logger.info("BGP router identifier %s != Loopback0 address %s" % (router_id, loopback0))
return False
except Exception as e:
logger.error("Error loading BGP routerID - {}".format(e))