Skip to content

Replace py-swsssdk with swsscommon #5926

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

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ansible/library/acl_capabilities_facts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python

import swsssdk
from swsscommon import swsscommon
from sonic_py_common import multi_asic
DOCUMENTATION = '''
module: acl_capabilities_facts
Expand Down Expand Up @@ -32,8 +32,8 @@ def run(self):
"""
self.facts['acl_capabilities'] = {}
namespace_list = multi_asic.get_namespace_list()
swsssdk.SonicDBConfig.load_sonic_global_db_config()
conn = swsssdk.SonicV2Connector(namespace=namespace_list[0])
swsscommon.SonicDBConfig.load_sonic_global_db_config()
conn = swsscommon.SonicV2Connector(namespace=namespace_list[0])
conn.connect(conn.STATE_DB)
keys = conn.keys(conn.STATE_DB, 'ACL_STAGE_CAPABILITY_TABLE|*') or []

Expand Down
6 changes: 3 additions & 3 deletions ansible/library/switch_capabilities_facts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python

import swsssdk
from swsscommon import swsscommon
Copy link
Contributor

Choose a reason for hiding this comment

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

swsscommon

Is it compatible to old images?

Copy link
Contributor Author

@liuh-80 liuh-80 Jul 12, 2022

Choose a reason for hiding this comment

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

The code change may break on old device, I will test with 2018 image on by devbox.

Copy link
Contributor Author

@liuh-80 liuh-80 Jul 12, 2022

Choose a reason for hiding this comment

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

Fixed.
Manually test on 201911 and latest master branch. this script and related UT does not run on 201811 version.

Copy link
Contributor

Choose a reason for hiding this comment

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

The sonic-mgmt master branch will not be used to test 201811 and older branch images. If this is the truth, then it is good enough, and no need to improve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fix is necessary because SonicDBConfig.load_sonic_global_db_config() added on 2021, so on 2019 swsscommon exist but we still need use swsssdk.

from sonic_py_common import multi_asic
DOCUMENTATION = '''
module: switch_capability_facts
Expand Down Expand Up @@ -33,8 +33,8 @@ def run(self):
"""
self.facts['switch_capabilities'] = {}
namespace_list = multi_asic.get_namespace_list()
swsssdk.SonicDBConfig.load_sonic_global_db_config()
conn = swsssdk.SonicV2Connector(namespace=namespace_list[0])
swsscommon.SonicDBConfig.load_sonic_global_db_config()
conn = swsscommon.SonicV2Connector(namespace=namespace_list[0])
conn.connect(conn.STATE_DB)
keys = conn.keys(conn.STATE_DB, 'SWITCH_CAPABILITY|*')

Expand Down
12 changes: 10 additions & 2 deletions tests/sflow/test_sflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,16 @@ def get_ifindex(duthost, port):

def get_port_index(duthost, port):
py_version = 'python' if '201911' in duthost.os_version else 'python3'
Copy link
Contributor

Choose a reason for hiding this comment

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

201911

If the code is testing an old image (such as 201911 branch image), will it break?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.
Add code to check if the sonic_py_common.port_util class exist.

cmd = "{} -c \"from swsssdk import port_util; print(port_util.get_index_from_str(\'{}\'))\""
index = duthost.shell(cmd.format(py_version, port))['stdout']

# if sonic_py_common.port_util exist, use port_util from sonic_py_common.
util_lib = "swsssdk"
cmd = "{} -c \"import pkgutil; print(pkgutil.find_loader(\'sonic_py_common.port_util\'))\"".format(py_version)
class_exist = duthost.shell(cmd)['stdout']
if class_exist != "None":
util_lib = "sonic_py_common"

cmd = "{} -c \"from {} import port_util; print(port_util.get_index_from_str(\'{}\'))\""
index = duthost.shell(cmd.format(py_version, util_lib, port))['stdout']
return index

# ----------------------------------------------------------------------------------
Expand Down