Skip to content

Commit 2e305c9

Browse files
[multi-asic][sonic-config-engine]: Get PORT table from namespace config db (#7632)
Why I did it portconfig.py gets PORT table from config_db if it is present. If not, port_config.ini files are parsed. For multi-asic platform, if namespace is passed to get_port_config(), config_db connection was done to host namespace always and not to asic specific namespace. Provides fix for: #7161 How I did it Modify db connection function to connect to namespace config_db.
1 parent b2a2cf0 commit 2e305c9

18 files changed

+896
-15
lines changed

src/sonic-config-engine/minigraph.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313

1414
from portconfig import get_port_config
15-
from sonic_py_common.multi_asic import get_asic_id_from_name
1615
from sonic_py_common.interface import backplane_prefix
1716

1817
# TODO: Remove this once we no longer support Python 2
@@ -1183,12 +1182,6 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
11831182
local_devices = []
11841183
kube_data = {}
11851184

1186-
# hostname is the asic_name, get the asic_id from the asic_name
1187-
if asic_name is not None:
1188-
asic_id = get_asic_id_from_name(asic_name)
1189-
else:
1190-
asic_id = None
1191-
11921185
hwsku_qn = QName(ns, "HwSku")
11931186
hostname_qn = QName(ns, "Hostname")
11941187
docker_routing_config_mode_qn = QName(ns, "DockerRoutingConfigMode")
@@ -1200,7 +1193,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
12001193
if child.tag == str(docker_routing_config_mode_qn):
12011194
docker_routing_config_mode = child.text
12021195

1203-
(ports, alias_map, alias_asic_map) = get_port_config(hwsku=hwsku, platform=platform, port_config_file=port_config_file, asic=asic_id, hwsku_config_file=hwsku_config_file)
1196+
(ports, alias_map, alias_asic_map) = get_port_config(hwsku=hwsku, platform=platform, port_config_file=port_config_file, asic_name=asic_name, hwsku_config_file=hwsku_config_file)
12041197
port_alias_map.update(alias_map)
12051198
port_alias_asic_map.update(alias_asic_map)
12061199

src/sonic-config-engine/portconfig.py

+32-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@
55
import re
66
import sys
77

8-
from swsscommon.swsscommon import ConfigDBConnector
8+
from swsscommon import swsscommon
99
from sonic_py_common import device_info
10+
from sonic_py_common.multi_asic import get_asic_id_from_name
1011
except ImportError as e:
1112
raise ImportError("%s - required module not found" % str(e))
1213

14+
try:
15+
if os.environ["CFGGEN_UNIT_TESTING"] == "2":
16+
modules_path = os.path.join(os.path.dirname(__file__), ".")
17+
tests_path = os.path.join(modules_path, "tests")
18+
sys.path.insert(0, modules_path)
19+
sys.path.insert(0, tests_path)
20+
import mock_tables.dbconnector
21+
mock_tables.dbconnector.load_namespace_config()
22+
23+
except KeyError:
24+
pass
25+
1326
# Global Variable
1427
PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
1528
PLATFORM_ROOT_PATH_DOCKER = '/usr/share/sonic/platform'
@@ -45,11 +58,16 @@ def readJson(filename):
4558
print("error occurred while parsing json: {}".format(sys.exc_info()[1]))
4659
return None
4760

48-
def db_connect_configdb():
61+
def db_connect_configdb(namespace=None):
4962
"""
5063
Connect to configdb
5164
"""
52-
config_db = ConfigDBConnector()
65+
try:
66+
if namespace is not None:
67+
swsscommon.SonicDBConfig.load_sonic_global_db_config(namespace=namespace)
68+
config_db = swsscommon.ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
69+
except Exception as e:
70+
return None
5371
if config_db is None:
5472
return None
5573
try:
@@ -77,8 +95,8 @@ def get_hwsku_file_name(hwsku=None, platform=None):
7795
return candidate
7896
return None
7997

80-
def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_config_file=None, asic=None):
81-
config_db = db_connect_configdb()
98+
def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_config_file=None, asic_name=None):
99+
config_db = db_connect_configdb(asic_name)
82100
# If available, Read from CONFIG DB first
83101
if config_db is not None and port_config_file is None:
84102

@@ -88,11 +106,18 @@ def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_conf
88106
port_alias_map = {}
89107
port_alias_asic_map = {}
90108
for intf_name in ports.keys():
91-
port_alias_map[ports[intf_name]["alias"]] = intf_name
109+
if "alias" in ports[intf_name]:
110+
port_alias_map[ports[intf_name]["alias"]] = intf_name
92111
return (ports, port_alias_map, port_alias_asic_map)
93112

113+
if asic_name is not None:
114+
asic_id = str(get_asic_id_from_name(asic_name))
115+
else:
116+
asic_id = None
117+
94118
if not port_config_file:
95-
port_config_file = device_info.get_path_to_port_config_file(hwsku, asic)
119+
port_config_file = device_info.get_path_to_port_config_file(hwsku, asic_id)
120+
96121
if not port_config_file:
97122
return ({}, {}, {})
98123

src/sonic-config-engine/tests/mock_tables/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"PORT|Ethernet0": {
3+
"index": "0",
4+
"lanes": "33,34,35,36",
5+
"description": "01T2:Ethernet1:config_db",
6+
"pfc_asym": "off",
7+
"mtu": "9100",
8+
"alias": "Ethernet1/1",
9+
"admin_status": "up",
10+
"role": "Ext",
11+
"speed": "40000",
12+
"asic_port_name": "Eth0-ASIC0"
13+
},
14+
"PORT|Ethernet4": {
15+
"index": "1",
16+
"lanes": "29,30,31,32",
17+
"description": "01T2:Ethernet2:config_db",
18+
"pfc_asym": "off",
19+
"mtu": "9100",
20+
"alias": "Ethernet1/2",
21+
"admin_status": "up",
22+
"role": "Ext",
23+
"speed": "40000",
24+
"asic_port_name": "Eth1-ASIC0"
25+
},
26+
"PORT|Ethernet8": {
27+
"index": "2",
28+
"lanes": "41,42,43,44",
29+
"description": "Ethernet1/3:config_db",
30+
"pfc_asym": "off",
31+
"mtu": "9100",
32+
"alias": "Ethernet1/3",
33+
"admin_status": "up",
34+
"role": "Ext",
35+
"speed": "40000",
36+
"asic_port_name": "Eth2-ASIC0"
37+
},
38+
"PORT|Ethernet12": {
39+
"index": "3",
40+
"lanes": "37,38,39,40",
41+
"description": "Ethernet1/4:config_db",
42+
"pfc_asym": "off",
43+
"mtu": "9100",
44+
"alias": "Ethernet1/4",
45+
"admin_status": "up",
46+
"role": "Ext",
47+
"speed": "40000",
48+
"asic_port_name": "Eth3-ASIC0"
49+
},
50+
"PORT|Ethernet-BP0": {
51+
"index": "0",
52+
"lanes": "13,14,15,16",
53+
"description": "ASIC2:Eth0-ASIC2:config_db",
54+
"pfc_asym": "off",
55+
"mtu": "9100",
56+
"alias": "Eth4-ASIC0",
57+
"admin_status": "up",
58+
"role": "Int",
59+
"speed": "40000",
60+
"asic_port_name": "Eth4-ASIC0"
61+
},
62+
"PORT|Ethernet-BP4": {
63+
"index": "1",
64+
"lanes": "17,18,19,20",
65+
"description": "ASIC2:Eth1-ASIC2:config_db",
66+
"pfc_asym": "off",
67+
"mtu": "9100",
68+
"alias": "Eth5-ASIC0",
69+
"admin_status": "up",
70+
"role": "Int",
71+
"speed": "40000",
72+
"asic_port_name": "Eth5-ASIC0"
73+
},
74+
"PORT|Ethernet-BP8": {
75+
"index": "2",
76+
"lanes": "21,22,23,24",
77+
"description": "ASIC3:Eth0-ASIC3:config_db",
78+
"pfc_asym": "off",
79+
"mtu": "9100",
80+
"alias": "Eth6-ASIC0",
81+
"admin_status": "up",
82+
"role": "Int",
83+
"speed": "40000",
84+
"asic_port_name": "Eth6-ASIC0"
85+
},
86+
"PORT|Ethernet-BP12": {
87+
"index": "3",
88+
"lanes": "25,26,27,28",
89+
"description": "ASIC3:Eth1-ASIC3:config_db",
90+
"pfc_asym": "off",
91+
"mtu": "9100",
92+
"alias": "Eth7-ASIC0",
93+
"admin_status": "up",
94+
"role": "Int",
95+
"speed": "40000",
96+
"asic_port_name": "Eth7-ASIC0"
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"INSTANCES": {
3+
"redis": {
4+
"hostname" : "127.0.0.1",
5+
"port" : 6379,
6+
"unix_socket_path" : "/var/run/redis/redis.sock"
7+
}
8+
},
9+
"DATABASES" : {
10+
"APPL_DB" : {
11+
"id" : 0,
12+
"separator": ":",
13+
"instance" : "redis"
14+
},
15+
"ASIC_DB" : {
16+
"id" : 1,
17+
"separator": ":",
18+
"instance" : "redis"
19+
},
20+
"COUNTERS_DB" : {
21+
"id" : 2,
22+
"separator": ":",
23+
"instance" : "redis"
24+
},
25+
"LOGLEVEL_DB" : {
26+
"id" : 3,
27+
"separator": ":",
28+
"instance" : "redis"
29+
},
30+
"CONFIG_DB" : {
31+
"id" : 4,
32+
"separator": "|",
33+
"instance" : "redis"
34+
},
35+
"PFC_WD_DB" : {
36+
"id" : 5,
37+
"separator": ":",
38+
"instance" : "redis"
39+
},
40+
"FLEX_COUNTER_DB" : {
41+
"id" : 5,
42+
"separator": ":",
43+
"instance" : "redis"
44+
},
45+
"STATE_DB" : {
46+
"id" : 6,
47+
"separator": "|",
48+
"instance" : "redis"
49+
},
50+
"SNMP_OVERLAY_DB" : {
51+
"id" : 7,
52+
"separator": "|",
53+
"instance" : "redis"
54+
}
55+
},
56+
"VERSION" : "1.1"
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"INSTANCES": {
3+
"redis": {
4+
"hostname" : "127.0.0.1",
5+
"port" : 6379,
6+
"unix_socket_path" : "/var/run/redis/redis.sock"
7+
}
8+
},
9+
"DATABASES" : {
10+
"APPL_DB" : {
11+
"id" : 0,
12+
"separator": ":",
13+
"instance" : "redis"
14+
},
15+
"ASIC_DB" : {
16+
"id" : 1,
17+
"separator": ":",
18+
"instance" : "redis"
19+
},
20+
"COUNTERS_DB" : {
21+
"id" : 2,
22+
"separator": ":",
23+
"instance" : "redis"
24+
},
25+
"LOGLEVEL_DB" : {
26+
"id" : 3,
27+
"separator": ":",
28+
"instance" : "redis"
29+
},
30+
"CONFIG_DB" : {
31+
"id" : 4,
32+
"separator": "|",
33+
"instance" : "redis"
34+
},
35+
"PFC_WD_DB" : {
36+
"id" : 5,
37+
"separator": ":",
38+
"instance" : "redis"
39+
},
40+
"FLEX_COUNTER_DB" : {
41+
"id" : 5,
42+
"separator": ":",
43+
"instance" : "redis"
44+
},
45+
"STATE_DB" : {
46+
"id" : 6,
47+
"separator": "|",
48+
"instance" : "redis"
49+
},
50+
"SNMP_OVERLAY_DB" : {
51+
"id" : 7,
52+
"separator": "|",
53+
"instance" : "redis"
54+
}
55+
},
56+
"VERSION" : "1.1"
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"INSTANCES": {
3+
"redis": {
4+
"hostname" : "127.0.0.1",
5+
"port" : 6379,
6+
"unix_socket_path" : "/var/run/redis/redis.sock"
7+
}
8+
},
9+
"DATABASES" : {
10+
"APPL_DB" : {
11+
"id" : 0,
12+
"separator": ":",
13+
"instance" : "redis"
14+
},
15+
"ASIC_DB" : {
16+
"id" : 1,
17+
"separator": ":",
18+
"instance" : "redis"
19+
},
20+
"COUNTERS_DB" : {
21+
"id" : 2,
22+
"separator": ":",
23+
"instance" : "redis"
24+
},
25+
"LOGLEVEL_DB" : {
26+
"id" : 3,
27+
"separator": ":",
28+
"instance" : "redis"
29+
},
30+
"CONFIG_DB" : {
31+
"id" : 4,
32+
"separator": "|",
33+
"instance" : "redis"
34+
},
35+
"PFC_WD_DB" : {
36+
"id" : 5,
37+
"separator": ":",
38+
"instance" : "redis"
39+
},
40+
"FLEX_COUNTER_DB" : {
41+
"id" : 5,
42+
"separator": ":",
43+
"instance" : "redis"
44+
},
45+
"STATE_DB" : {
46+
"id" : 6,
47+
"separator": "|",
48+
"instance" : "redis"
49+
},
50+
"SNMP_OVERLAY_DB" : {
51+
"id" : 7,
52+
"separator": "|",
53+
"instance" : "redis"
54+
}
55+
},
56+
"VERSION" : "1.1"
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

0 commit comments

Comments
 (0)