Skip to content

Commit 810320f

Browse files
committed
fixed pr tasks
1 parent 9c1bbcf commit 810320f

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

platform/mellanox/cmis_host_mgmt/cmis_host_mgmt.py

+54-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
#
3-
# Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES.
3+
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
44
# Apache-2.0
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -47,26 +47,31 @@ def change_param(param, path, action):
4747
file_path = '{}/{}'.format(path, CMISHostMgmtActivator.PARAMS[param]["file_name"])
4848
lines = None
4949

50-
with open(file_path, 'r') as param_file:
51-
lines = param_file.read()
50+
try:
51+
with open(file_path, 'r') as param_file:
52+
lines = param_file.read()
5253

53-
if lines:
54-
if action == "disable":
55-
lines = re.sub(CMISHostMgmtActivator.PARAMS[param]["enabled_param"],
56-
CMISHostMgmtActivator.PARAMS[param]["disabled_param"],
57-
lines)
58-
elif action == "enable":
59-
if param == "sai_profile" and not re.search(CMISHostMgmtActivator.PARAMS[param]["disabled_param"], lines):
60-
with open(file_path, 'a') as param_file:
61-
param_file.write(CMISHostMgmtActivator.PARAMS[param]["enabled_param"])
62-
return
54+
if lines:
55+
if action == "disable":
56+
lines = re.sub(CMISHostMgmtActivator.PARAMS[param]["enabled_param"],
57+
CMISHostMgmtActivator.PARAMS[param]["disabled_param"],
58+
lines)
59+
elif action == "enable":
60+
if param == "sai_profile" and not re.search(CMISHostMgmtActivator.PARAMS[param]["disabled_param"], lines):
61+
if not re.search(CMISHostMgmtActivator.PARAMS[param]["enabled_param"], lines):
62+
with open(file_path, 'a') as param_file:
63+
param_file.write(CMISHostMgmtActivator.PARAMS[param]["enabled_param"])
64+
return
6365

64-
lines = re.sub(CMISHostMgmtActivator.PARAMS[param]["disabled_param"],
65-
CMISHostMgmtActivator.PARAMS[param]["enabled_param"],
66-
lines)
66+
lines = re.sub(CMISHostMgmtActivator.PARAMS[param]["disabled_param"],
67+
CMISHostMgmtActivator.PARAMS[param]["enabled_param"],
68+
lines)
6769

68-
with open(file_path, 'w') as param_file:
69-
param_file.write(lines)
70+
with open(file_path, 'w') as param_file:
71+
param_file.write(lines)
72+
73+
except FileNotFoundError as e:
74+
print('Missing file: {}'.format(e.filename))
7075

7176

7277
@staticmethod
@@ -88,44 +93,56 @@ def parse_show_platform_summary():
8893

8994

9095
@staticmethod
91-
def remove_im_file(file_path):
96+
def remove_file(file_path):
9297
if os.path.isfile(file_path):
9398
os.remove(file_path)
9499

95100

96101
@staticmethod
97-
def copy_im_file(src_path, dest_path):
102+
def copy_file(src_path, dest_path):
98103
if os.path.isfile(src_path):
99104
shutil.copy(src_path, dest_path)
100105

101106

102107
@staticmethod
103-
def disable_im():
108+
def is_spc_supported(spc):
109+
return int(spc) >= 4000
110+
111+
@staticmethod
112+
def disable():
104113
platform, sku = CMISHostMgmtActivator.parse_show_platform_summary()
105114
sku_path = '/usr/share/sonic/device/{0}/{1}'.format(platform, sku)
106115
platform_path = '/usr/share/sonic/device/{0}'.format(platform)
107116
CMISHostMgmtActivator.change_param("sai_profile", sku_path, 'disable')
108-
CMISHostMgmtActivator.change_param("pmon_daemon_control", platform_path, 'disable')
109117

110-
CMISHostMgmtActivator.remove_im_file('{0}/{1}'.format(sku_path, 'media_settings.json'))
111-
CMISHostMgmtActivator.remove_im_file('{0}/{1}'.format(sku_path,'optics_si_settings.json'))
112-
CMISHostMgmtActivator.remove_im_file('{0}/{1}'.format(platform_path, 'media_settings.json'))
113-
CMISHostMgmtActivator.remove_im_file('{0}/{1}'.format(platform_path, 'optics_si_settings.json'))
114-
CMISHostMgmtActivator.remove_im_file('{0}/{1}'.format(sku_path, 'pmon_daemon_control.json'))
118+
if os.path.isfile('{0}/{1}'.format(platform_path, 'pmon_daemon_control.json')):
119+
CMISHostMgmtActivator.change_param("pmon_daemon_control", platform_path, 'disable')
120+
CMISHostMgmtActivator.remove_file('{0}/{1}'.format(sku_path, 'pmon_daemon_control.json'))
121+
else:
122+
CMISHostMgmtActivator.change_param("pmon_daemon_control", sku_path, 'disable')
123+
124+
CMISHostMgmtActivator.remove_file('{0}/{1}'.format(sku_path, 'media_settings.json'))
125+
CMISHostMgmtActivator.remove_file('{0}/{1}'.format(sku_path,'optics_si_settings.json'))
126+
CMISHostMgmtActivator.remove_file('{0}/{1}'.format(platform_path, 'media_settings.json'))
127+
CMISHostMgmtActivator.remove_file('{0}/{1}'.format(platform_path, 'optics_si_settings.json'))
115128

116129

117130
@staticmethod
118-
def enable_im(args):
131+
def enable(args):
119132
platform, sku = CMISHostMgmtActivator.parse_show_platform_summary()
120133
sku_path = '/usr/share/sonic/device/{0}/{1}'.format(platform, sku)
121134
platform_path = '/usr/share/sonic/device/{0}'.format(platform)
122135

123136
sku_num = re.search('[0-9]{4}', sku).group()
137+
138+
if not CMISHostMgmtActivator.is_spc_supported(sku_num):
139+
print("Error: unsupported platform - feature is supported on SPC3 and higher.")
140+
124141
CMISHostMgmtActivator.PARAMS["sai_xml"]["file_name"] = "sai_{0}.xml".format(sku_num)
125142

126-
CMISHostMgmtActivator.copy_im_file(args[0], sku_path)
127-
CMISHostMgmtActivator.copy_im_file(args[1], sku_path)
128-
CMISHostMgmtActivator.copy_im_file('{0}/{1}'.format(platform_path, 'pmon_daemon_control.json'), sku_path)
143+
CMISHostMgmtActivator.copy_file(args[0], sku_path)
144+
CMISHostMgmtActivator.copy_file(args[1], sku_path)
145+
CMISHostMgmtActivator.copy_file('{0}/{1}'.format(platform_path, 'pmon_daemon_control.json'), sku_path)
129146

130147
CMISHostMgmtActivator.change_param("sai_profile", sku_path, 'enable')
131148
CMISHostMgmtActivator.change_param("pmon_daemon_control", sku_path, 'enable')
@@ -137,15 +154,18 @@ def enable_im(args):
137154
@click.option('--enable', nargs=2, type=click.Path(), help='Enable CMIS Host Management, receives two arguments: media_settings.json path, and optics_si_settings.json path')
138155
def main(disable, enable):
139156

140-
import pdb; pdb.set_trace()
141157
if disable and enable:
142158
print("Error: can't use both options, please choose one.")
159+
return
143160

144161
if disable:
145-
CMISHostMgmtActivator.disable_im()
162+
CMISHostMgmtActivator.disable()
146163

147164
elif enable:
148-
CMISHostMgmtActivator.enable_im(enable)
165+
CMISHostMgmtActivator.enable(enable)
166+
167+
else:
168+
print("Error: no option was provided - nothing to execute.")
149169

150170
if __name__ == '__main__':
151171
main()

0 commit comments

Comments
 (0)