Skip to content

Commit 4ae8bfa

Browse files
authored
Read switch_id of fabric switch from config_db (#3102)
* Read switch_id of fabric switch from config_db. * Validate fabric switch_id before create fabric switch. * Add fabric switch_id for virtual chassis * Add unit test for handling fabric switch_id.
1 parent f737663 commit 4ae8bfa

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

orchagent/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,28 @@ int main(int argc, char **argv)
588588
attr.value.u32 = SAI_SWITCH_TYPE_FABRIC;
589589
attrs.push_back(attr);
590590

591+
//Read switch_id from config_db.
592+
Table cfgDeviceMetaDataTable(&config_db, CFG_DEVICE_METADATA_TABLE_NAME);
593+
string value;
594+
if (cfgDeviceMetaDataTable.hget("localhost", "switch_id", value))
595+
{
596+
if (value.size())
597+
{
598+
gVoqMySwitchId = stoi(value);
599+
}
600+
601+
if (gVoqMySwitchId < 0)
602+
{
603+
SWSS_LOG_ERROR("Invalid fabric switch id %d configured", gVoqMySwitchId);
604+
exit(EXIT_FAILURE);
605+
}
606+
}
607+
else
608+
{
609+
SWSS_LOG_ERROR("Fabric switch id is not configured");
610+
exit(EXIT_FAILURE);
611+
}
612+
591613
attr.id = SAI_SWITCH_ATTR_SWITCH_ID;
592614
attr.value.u32 = gVoqMySwitchId;
593615
attrs.push_back(attr);

tests/dvslib/dvs_database.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,19 @@ def delete_field(self, table_name: str, key: str, field: str) -> None:
109109
"""
110110
table = swsscommon.Table(self.db_connection, table_name)
111111
table.hdel(key, field)
112-
112+
113+
def set_field(self, table_name: str, key: str, field: str, value: str) -> None:
114+
"""Add/Update a field in an entry stored at `key` in the specified table.
115+
116+
Args:
117+
table_name: The name of the table where the entry is being removed.
118+
key: The key that maps to the entry being added/updated.
119+
field: The field that needs to be added/updated.
120+
value: The value that is set for the field.
121+
"""
122+
table = swsscommon.Table(self.db_connection, table_name)
123+
table.hset(key, field, value)
124+
113125
def get_keys(self, table_name: str) -> List[str]:
114126
"""Get all of the keys stored in the specified table.
115127

tests/test_fabric_switch_id.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from dvslib.dvs_common import wait_for_result, PollingConfig
2+
import pytest
3+
4+
class TestFabricSwitchId(object):
5+
def check_syslog(self, dvs, marker, log):
6+
def do_check_syslog():
7+
(ec, out) = dvs.runcmd(['sh', '-c', "awk \'/%s/,ENDFILE {print;}\' /var/log/syslog | grep \'%s\' | wc -l" %(marker, log)])
8+
return (int(out.strip()) >= 1, None)
9+
max_poll = PollingConfig(polling_interval=5, timeout=600, strict=True)
10+
wait_for_result(do_check_syslog, polling_config=max_poll)
11+
12+
def test_invalid_fabric_switch_id(self, vst):
13+
# Find supervisor dvs.
14+
dvs = None
15+
config_db = None
16+
for name in vst.dvss.keys():
17+
dvs = vst.dvss[name]
18+
config_db = dvs.get_config_db()
19+
metatbl = config_db.get_entry("DEVICE_METADATA", "localhost")
20+
cfg_switch_type = metatbl.get("switch_type")
21+
if cfg_switch_type == "fabric":
22+
break
23+
assert dvs and config_db
24+
25+
# Verify orchagent's handling of invalid fabric switch_id in following cases:
26+
# - Invalid fabric switch_id, e.g, -1, is set.
27+
# - fabric switch_id is missing in ConfigDb.
28+
for invalid_switch_id in (-1, None):
29+
print(f"Test invalid switch id {invalid_switch_id}")
30+
if invalid_switch_id is None:
31+
config_db.delete_field("DEVICE_METADATA", "localhost", "switch_id")
32+
expected_log = "Fabric switch id is not configured"
33+
else:
34+
config_db.set_field("DEVICE_METADATA", "localhost", "switch_id", str(invalid_switch_id))
35+
expected_log = f"Invalid fabric switch id {invalid_switch_id} configured"
36+
37+
# Restart orchagent and verify orchagent behavior by checking syslog.
38+
dvs.stop_swss()
39+
marker = dvs.add_log_marker()
40+
dvs.start_swss()
41+
self.check_syslog(dvs, marker, expected_log)
42+
43+
44+
# Add Dummy always-pass test at end as workaroud
45+
# for issue when Flaky fail on final test it invokes module tear-down before retrying
46+
def test_nonflaky_dummy():
47+
pass
48+

tests/virtual_chassis/8/default_config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"chassis_db_address" : "10.8.1.200",
66
"inband_address" : "10.8.1.200/24",
77
"switch_type": "fabric",
8+
"switch_id": "0",
89
"sub_role" : "BackEnd",
910
"start_chassis_db" : "1",
1011
"comment" : "default_config for a vs that runs chassis_db"

0 commit comments

Comments
 (0)