1
1
#!/usr/bin/env python3
2
2
#
3
- # Copyright (c) 2021- 2024 NVIDIA CORPORATION & AFFILIATES.
3
+ # Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES.
4
4
# Apache-2.0
5
5
#
6
6
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -47,26 +47,31 @@ def change_param(param, path, action):
47
47
file_path = '{}/{}' .format (path , CMISHostMgmtActivator .PARAMS [param ]["file_name" ])
48
48
lines = None
49
49
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 ()
52
53
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
63
65
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 )
67
69
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 ))
70
75
71
76
72
77
@staticmethod
@@ -88,44 +93,56 @@ def parse_show_platform_summary():
88
93
89
94
90
95
@staticmethod
91
- def remove_im_file (file_path ):
96
+ def remove_file (file_path ):
92
97
if os .path .isfile (file_path ):
93
98
os .remove (file_path )
94
99
95
100
96
101
@staticmethod
97
- def copy_im_file (src_path , dest_path ):
102
+ def copy_file (src_path , dest_path ):
98
103
if os .path .isfile (src_path ):
99
104
shutil .copy (src_path , dest_path )
100
105
101
106
102
107
@staticmethod
103
- def disable_im ():
108
+ def is_spc_supported (spc ):
109
+ return int (spc ) >= 4000
110
+
111
+ @staticmethod
112
+ def disable ():
104
113
platform , sku = CMISHostMgmtActivator .parse_show_platform_summary ()
105
114
sku_path = '/usr/share/sonic/device/{0}/{1}' .format (platform , sku )
106
115
platform_path = '/usr/share/sonic/device/{0}' .format (platform )
107
116
CMISHostMgmtActivator .change_param ("sai_profile" , sku_path , 'disable' )
108
- CMISHostMgmtActivator .change_param ("pmon_daemon_control" , platform_path , 'disable' )
109
117
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' ))
115
128
116
129
117
130
@staticmethod
118
- def enable_im (args ):
131
+ def enable (args ):
119
132
platform , sku = CMISHostMgmtActivator .parse_show_platform_summary ()
120
133
sku_path = '/usr/share/sonic/device/{0}/{1}' .format (platform , sku )
121
134
platform_path = '/usr/share/sonic/device/{0}' .format (platform )
122
135
123
136
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
+
124
141
CMISHostMgmtActivator .PARAMS ["sai_xml" ]["file_name" ] = "sai_{0}.xml" .format (sku_num )
125
142
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 )
129
146
130
147
CMISHostMgmtActivator .change_param ("sai_profile" , sku_path , 'enable' )
131
148
CMISHostMgmtActivator .change_param ("pmon_daemon_control" , sku_path , 'enable' )
@@ -137,15 +154,18 @@ def enable_im(args):
137
154
@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' )
138
155
def main (disable , enable ):
139
156
140
- import pdb ; pdb .set_trace ()
141
157
if disable and enable :
142
158
print ("Error: can't use both options, please choose one." )
159
+ return
143
160
144
161
if disable :
145
- CMISHostMgmtActivator .disable_im ()
162
+ CMISHostMgmtActivator .disable ()
146
163
147
164
elif enable :
148
- CMISHostMgmtActivator .enable_im (enable )
165
+ CMISHostMgmtActivator .enable (enable )
166
+
167
+ else :
168
+ print ("Error: no option was provided - nothing to execute." )
149
169
150
170
if __name__ == '__main__' :
151
171
main ()
0 commit comments