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

[asic_sensors] Generate the asic_sensors polling configuration based on the platform.json #20826

Merged
merged 10 commits into from
Dec 24, 2024
40 changes: 40 additions & 0 deletions src/sonic-config-engine/asic_sensors_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import sys
import portconfig
from sonic_py_common import device_info

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

ASIC_SENSORS_KEY = "asic_sensors"


def get_asic_sensors_config():
config = {}
if os.environ.get("CFGGEN_UNIT_TESTING") == "2":
json_file = os.path.join(tests_path, "data", "asic_sensors", "platform.json")
else:
platform_path = device_info.get_path_to_platform_dir()
json_file = os.path.join(platform_path, device_info.PLATFORM_JSON_FILE)

if not os.path.exists(json_file):
return config

platform_json = portconfig.readJson(json_file)
if not platform_json:
return config

if ASIC_SENSORS_KEY in platform_json:
config["ASIC_SENSORS"] = {"ASIC_SENSORS_POLLER_INTERVAL": {"interval": platform_json[ASIC_SENSORS_KEY]["poll_interval"]},
"ASIC_SENSORS_POLLER_STATUS": {"admin_status": platform_json[ASIC_SENSORS_KEY]["poll_admin_status"]}
}
return config
3 changes: 2 additions & 1 deletion src/sonic-config-engine/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
'minigraph',
'openconfig_acl',
'portconfig',
'smartswitch_config'
'smartswitch_config',
'asic_sensors_config'
]
if sys.version_info.major == 3:
# Python 3-only modules
Expand Down
11 changes: 10 additions & 1 deletion src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ from portconfig import get_port_config, get_breakout_mode
from sonic_py_common.multi_asic import get_asic_id_from_name, get_asic_device_id, is_multi_asic
from sonic_py_common import device_info
from swsscommon.swsscommon import ConfigDBConnector, SonicDBConfig, ConfigDBPipeConnector

from asic_sensors_config import get_asic_sensors_config

PY3x = sys.version_info >= (3, 0)

Expand Down Expand Up @@ -459,6 +459,15 @@ def main():

deep_update(data, hardware_data)

asic_sensors = {}
if is_multi_asic():
if asic_name is not None:
asic_sensors = get_asic_sensors_config()
else:
asic_sensors = get_asic_sensors_config()
if asic_sensors:
deep_update(data, asic_sensors)

paths = ['/', '/usr/share/sonic/templates']
if args.template_dir:
paths.append(os.path.abspath(args.template_dir))
Expand Down
6 changes: 6 additions & 0 deletions src/sonic-config-engine/tests/data/asic_sensors/platform.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"asic_sensors": {
"poll_interval": "10",
"poll_admin_status": "enable"
}
}
10 changes: 10 additions & 0 deletions src/sonic-config-engine/tests/test_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,3 +1155,13 @@ def testsnmp_agent_address_config(self):
self.assertEqual(
utils.liststr_to_dict(output.strip()),
utils.liststr_to_dict("['192.168.200.15|161|', '100.0.0.6|161|', '100.0.0.7|161|', 'fe80::1%Management0|161|']"))

def test_platform_asic_sensors_config(self):
os.environ["PLATFORM"] = "x86_64-kvm_x86_64-r0"
argument = ["-H","--print-data"]
output = self.run_script(argument, check_stderr=False)
config_json = utils.to_dict(output.strip())
os.environ["PLATFORM"] = ""
self.assertEqual(config_json['ASIC_SENSORS'], utils.to_dict("{'ASIC_SENSORS_POLLER_INTERVAL': {'interval': '10'}, 'ASIC_SENSORS_POLLER_STATUS': {'admin_status': 'enable'}}"))


Loading