3
3
import imp
4
4
import yaml
5
5
import subprocess
6
- from shlex import split
7
6
from sonic_py_common import device_info
8
7
9
8
@@ -25,6 +24,7 @@ class Common:
25
24
26
25
SET_METHOD_IPMI = 'ipmitool'
27
26
NULL_VAL = 'N/A'
27
+ HOST_CHK_CMD = "docker"
28
28
REF_KEY = '$ref:'
29
29
30
30
def __init__ (self , conf = None ):
@@ -46,8 +46,7 @@ def run_command(self, command):
46
46
status = False
47
47
output = ""
48
48
try :
49
- p = subprocess .Popen (
50
- split (command ), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
49
+ p = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
51
50
raw_data , err = p .communicate ()
52
51
if p .returncode == 0 :
53
52
status , output = True , raw_data .strip ()
@@ -82,14 +81,7 @@ def _clean_output(self, index, output, config):
82
81
output = ast .literal_eval (output_translator [index ].format (output ))
83
82
84
83
return output
85
-
86
- def _ipmi_get (self , index , config ):
87
- argument = config .get ('argument' )
88
- cmd = config ['command' ].format (
89
- config ['argument' ][index ]) if argument else config ['command' ]
90
- status , output = self .run_command (cmd )
91
- return output if status else None
92
-
84
+
93
85
def _sysfs_read (self , index , config ):
94
86
sysfs_path = config .get ('sysfs_path' )
95
87
argument = config .get ('argument' , '' )
@@ -132,10 +124,6 @@ def _sysfs_write(self, index, config, input):
132
124
return False , output
133
125
return True , output
134
126
135
- def _ipmi_set (self , index , config , input ):
136
- arg = config ['argument' ][index ].format (input )
137
- return self .run_command (config ['command' ].format (arg ))
138
-
139
127
def _hex_ver_decode (self , hver , num_of_bits , num_of_points ):
140
128
ver_list = []
141
129
c_bit = 0
@@ -159,14 +147,16 @@ def _get_class(self, config):
159
147
return class_
160
148
161
149
def get_reg (self , path , reg_addr ):
162
- cmd = "echo {1} > {0}; cat {0}" .format (path , reg_addr )
163
- status , output = self .run_command (cmd )
164
- return output if status else None
150
+ with open (path , 'w' ) as file :
151
+ file .write (reg_addr )
152
+ with open (path , 'r' ) as file :
153
+ output = file .readline ().strip ()
154
+ return output
165
155
166
156
def set_reg (self , path , reg_addr , value ):
167
- cmd = "echo {0} {1} > {2}" . format ( reg_addr , value , path )
168
- status , output = self . run_command ( cmd )
169
- return output if status else None
157
+ with open ( path , 'w' ) as file :
158
+ file . write ( "{0} {1}" . format ( reg_addr , value ) )
159
+ return None
170
160
171
161
def read_txt_file (self , path ):
172
162
try :
@@ -195,7 +185,7 @@ def write_txt_file(self, file_path, value):
195
185
return True
196
186
197
187
def is_host (self ):
198
- return subprocess .run ('docker' , stdout = None , stderr = None ).returncode == 0
188
+ return subprocess .run (self . HOST_CHK_CMD , stdout = None , stderr = None ).returncode == 0
199
189
200
190
def load_json_file (self , path ):
201
191
"""
@@ -221,88 +211,7 @@ def get_config_path(self, config_name):
221
211
"""
222
212
return os .path .join (self .DEVICE_PATH , self .platform , self .CONFIG_DIR , config_name ) if self .is_host () else os .path .join (self .PMON_PLATFORM_PATH , self .CONFIG_DIR , config_name )
223
213
224
- def get_output (self , index , config , default ):
225
- """
226
- Retrieves the output for each function base on config
227
-
228
- Args:
229
- index: An integer containing the index of device.
230
- config: A dict object containing the configuration of specified function.
231
- default: A string containing the default output of specified function.
232
-
233
- Returns:
234
- A string containing the output of specified function in config
235
- """
236
- output_source = config .get ('output_source' )
237
-
238
- if output_source == self .OUTPUT_SOURCE_IPMI :
239
- output = self ._ipmi_get (index , config )
240
-
241
- elif output_source == self .OUTPUT_SOURCE_GIVEN_VALUE :
242
- output = config ["value" ]
243
-
244
- elif output_source == self .OUTPUT_SOURCE_GIVEN_CLASS :
245
- output = self ._get_class (config )
246
-
247
- elif output_source == self .OUTPUT_SOURCE_GIVEN_LIST :
248
- output = config ["value_list" ][index ]
249
-
250
- elif output_source == self .OUTPUT_SOURCE_SYSFS :
251
- output = self ._sysfs_read (index , config )
252
-
253
- elif output_source == self .OUTPUT_SOURCE_FUNC :
254
- func_conf = self ._main_conf [config ['function' ][index ]]
255
- output = self .get_output (index , func_conf , default )
256
-
257
- elif output_source == self .OUTPUT_SOURCE_GIVEN_TXT_FILE :
258
- path = config .get ('path' )
259
- output = self .read_txt_file (path )
260
-
261
- elif output_source == self .OUTPUT_SOURCE_GIVEN_VER_HEX_FILE :
262
- path = config .get ('path' )
263
- hex_ver = self .read_txt_file (path )
264
- output = self ._hex_ver_decode (
265
- hex_ver , config ['num_of_bits' ], config ['num_of_points' ])
266
-
267
- elif output_source == self .OUTPUT_SOURCE_GIVEN_VER_HEX_ADDR :
268
- path = config .get ('path' )
269
- addr = config .get ('reg_addr' )
270
- hex_ver = self .get_reg (path , addr )
271
- output = self ._hex_ver_decode (
272
- hex_ver , config ['num_of_bits' ], config ['num_of_points' ])
273
-
274
- else :
275
- output = default
276
-
277
- return self ._clean_output (index , output , config ) or default
278
-
279
- def set_output (self , index , input , config ):
280
- """
281
- Sets the output of specified function on config
282
-
283
- Args:
284
- config: A dict object containing the configuration of specified function.
285
- index: An integer containing the index of device.
286
- input: A string containing the input of specified function.
287
-
288
- Returns:
289
- bool: True if set function is successfully, False if not
290
- """
291
- cleaned_input = self ._clean_input (input , config )
292
- if not cleaned_input :
293
- return False
294
-
295
- set_method = config .get ('set_method' )
296
- if set_method == self .SET_METHOD_IPMI :
297
- output = self ._ipmi_set (index , config , cleaned_input )[0 ]
298
- elif set_method == self .OUTPUT_SOURCE_SYSFS :
299
- output = self ._sysfs_write (index , config , cleaned_input )[0 ]
300
- else :
301
- output = False
302
-
303
- return output
304
-
305
- def get_event (self , timeout , config , sfp_list ):
214
+ def get_event (self , timeout , config , sfp_list ):
306
215
"""
307
216
Returns a nested dictionary containing all devices which have
308
217
experienced a change at chassis level
0 commit comments