Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[202012][multi-asic][sonic-config-engine]: Get PORT table from namespace config db #10475

Merged
merged 7 commits into from
May 27, 2022
9 changes: 1 addition & 8 deletions src/sonic-config-engine/minigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


from portconfig import get_port_config
from sonic_py_common.multi_asic import get_asic_id_from_name
from sonic_py_common.interface import backplane_prefix

# TODO: Remove this once we no longer support Python 2
Expand Down Expand Up @@ -1152,12 +1151,6 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
local_devices = []
kube_data = {}

# hostname is the asic_name, get the asic_id from the asic_name
if asic_name is not None:
asic_id = get_asic_id_from_name(asic_name)
else:
asic_id = None

hwsku_qn = QName(ns, "HwSku")
hostname_qn = QName(ns, "Hostname")
docker_routing_config_mode_qn = QName(ns, "DockerRoutingConfigMode")
Expand All @@ -1169,7 +1162,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
if child.tag == str(docker_routing_config_mode_qn):
docker_routing_config_mode = child.text

(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)
(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)
port_alias_map.update(alias_map)
port_alias_asic_map.update(alias_asic_map)

Expand Down
39 changes: 32 additions & 7 deletions src/sonic-config-engine/portconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@
import re
import sys

from swsscommon.swsscommon import ConfigDBConnector
from swsscommon import swsscommon
from sonic_py_common import device_info
from sonic_py_common.multi_asic import get_asic_id_from_name
except ImportError as e:
raise ImportError("%s - required module not found" % str(e))

try:
if os.environ["CFGGEN_UNIT_TESTING"] == "2":
modules_path = os.path.join(os.path.dirname(__file__), ".")
tests_path = os.path.join(modules_path, "tests")
sys.path.insert(0, modules_path)
sys.path.insert(0, tests_path)
import mock_tables.dbconnector
mock_tables.dbconnector.load_namespace_config()

except KeyError:
pass

# Global Variable
PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
PLATFORM_ROOT_PATH_DOCKER = '/usr/share/sonic/platform'
Expand Down Expand Up @@ -46,11 +59,16 @@ def readJson(filename):
print("error occurred while parsing json: {}".format(sys.exc_info()[1]))
return None

def db_connect_configdb():
def db_connect_configdb(namespace=None):
"""
Connect to configdb
"""
config_db = ConfigDBConnector()
try:
if namespace is not None:
swsscommon.SonicDBConfig.load_sonic_global_db_config(namespace=namespace)
config_db = swsscommon.ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
except Exception as e:
Copy link
Collaborator

@qiluo-msft qiluo-msft Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception

What is the type of exception you are expecting? The general exception type will hide bugs and should be limited. #Pending

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was added to handle exception if thrown by load_sonic_global_db_config().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like load_sonic_global_db_config() calls SonicDBConfig::initializeGlobalConfig() which already handled exceptions, and it should be good enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created a PR : #10581 to remove this, I am working on unit-test failure, will merge that PR in master branch first and then take it in to 202012

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qiluo-msft #10581 is merged in master branch to fix this comment. Can we merge this PR to 202012 branch and cherry-pick the other PR to 202012.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#10960 - PR raised to fix this comment.

return None
if config_db is None:
return None
try:
Expand Down Expand Up @@ -78,8 +96,8 @@ def get_hwsku_file_name(hwsku=None, platform=None):
return candidate
return None

def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_config_file=None, asic=None):
config_db = db_connect_configdb()
def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_config_file=None, asic_name=None):
config_db = db_connect_configdb(asic_name)
# If available, Read from CONFIG DB first
if config_db is not None and port_config_file is None:

Expand All @@ -89,11 +107,18 @@ def get_port_config(hwsku=None, platform=None, port_config_file=None, hwsku_conf
port_alias_map = {}
port_alias_asic_map = {}
for intf_name in ports.keys():
port_alias_map[ports[intf_name]["alias"]] = intf_name
if "alias" in ports[intf_name]:
port_alias_map[ports[intf_name]["alias"]] = intf_name
return (ports, port_alias_map, port_alias_asic_map)

if asic_name is not None:
asic_id = str(get_asic_id_from_name(asic_name))
else:
asic_id = None

if not port_config_file:
port_config_file = device_info.get_path_to_port_config_file(hwsku, asic)
port_config_file = device_info.get_path_to_port_config_file(hwsku, asic_id)

if not port_config_file:
return ({}, {}, {})

Expand Down
Empty file.
98 changes: 98 additions & 0 deletions src/sonic-config-engine/tests/mock_tables/asic0/config_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"PORT|Ethernet0": {
"index": "0",
"lanes": "33,34,35,36",
"description": "01T2:Ethernet1:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Ethernet1/1",
"admin_status": "up",
"role": "Ext",
"speed": "40000",
"asic_port_name": "Eth0-ASIC0"
},
"PORT|Ethernet4": {
"index": "1",
"lanes": "29,30,31,32",
"description": "01T2:Ethernet2:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Ethernet1/2",
"admin_status": "up",
"role": "Ext",
"speed": "40000",
"asic_port_name": "Eth1-ASIC0"
},
"PORT|Ethernet8": {
"index": "2",
"lanes": "41,42,43,44",
"description": "Ethernet1/3:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Ethernet1/3",
"admin_status": "up",
"role": "Ext",
"speed": "40000",
"asic_port_name": "Eth2-ASIC0"
},
"PORT|Ethernet12": {
"index": "3",
"lanes": "37,38,39,40",
"description": "Ethernet1/4:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Ethernet1/4",
"admin_status": "up",
"role": "Ext",
"speed": "40000",
"asic_port_name": "Eth3-ASIC0"
},
"PORT|Ethernet-BP0": {
"index": "0",
"lanes": "13,14,15,16",
"description": "ASIC2:Eth0-ASIC2:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Eth4-ASIC0",
"admin_status": "up",
"role": "Int",
"speed": "40000",
"asic_port_name": "Eth4-ASIC0"
},
"PORT|Ethernet-BP4": {
"index": "1",
"lanes": "17,18,19,20",
"description": "ASIC2:Eth1-ASIC2:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Eth5-ASIC0",
"admin_status": "up",
"role": "Int",
"speed": "40000",
"asic_port_name": "Eth5-ASIC0"
},
"PORT|Ethernet-BP8": {
"index": "2",
"lanes": "21,22,23,24",
"description": "ASIC3:Eth0-ASIC3:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Eth6-ASIC0",
"admin_status": "up",
"role": "Int",
"speed": "40000",
"asic_port_name": "Eth6-ASIC0"
},
"PORT|Ethernet-BP12": {
"index": "3",
"lanes": "25,26,27,28",
"description": "ASIC3:Eth1-ASIC3:config_db",
"pfc_asym": "off",
"mtu": "9100",
"alias": "Eth7-ASIC0",
"admin_status": "up",
"role": "Int",
"speed": "40000",
"asic_port_name": "Eth7-ASIC0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"INSTANCES": {
"redis": {
"hostname" : "127.0.0.1",
"port" : 6379,
"unix_socket_path" : "/var/run/redis/redis.sock"
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
}
},
"VERSION" : "1.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"INSTANCES": {
"redis": {
"hostname" : "127.0.0.1",
"port" : 6379,
"unix_socket_path" : "/var/run/redis/redis.sock"
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
}
},
"VERSION" : "1.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"INSTANCES": {
"redis": {
"hostname" : "127.0.0.1",
"port" : 6379,
"unix_socket_path" : "/var/run/redis/redis.sock"
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
}
},
"VERSION" : "1.1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading