@@ -208,6 +208,30 @@ def _operate_attributes(self, operation, attrs=(), oid=None, obj_type=None, key=
208
208
209
209
return status , result
210
210
211
+ def _operate_stats (self , operation , attrs = (), oid = None , obj_type = None ):
212
+ """
213
+ Get/Clear stats for given object
214
+
215
+ operation (str): operation type, get or clear
216
+ """
217
+ if oid is not None :
218
+ oid = ThriftConverter .object_id (oid )
219
+
220
+ obj_type_name = self .get_object_type (oid , default = obj_type ).name .lower ()
221
+ object_oid = {f'{ obj_type_name } _oid' :oid }
222
+ sai_thrift_function = getattr (sai_adapter , f'sai_thrift_{ operation } _{ obj_type_name } _stats' )
223
+
224
+ result = {}
225
+
226
+ attr_kwargs = ThriftConverter .convert_counter_ids_to_thrift (attrs , obj_type_name )
227
+ result = sai_thrift_function (self .thrift_client , ** object_oid , ** attr_kwargs )
228
+ status = ThriftConverter .convert_to_sai_status_str (sai_adapter .status )
229
+
230
+ if status != 'SAI_STATUS_SUCCESS' :
231
+ result = None
232
+
233
+ return status , result
234
+
211
235
def cleanup (self ):
212
236
if self .thrift_transport :
213
237
self .thrift_transport .close ()
@@ -296,5 +320,27 @@ def bulk_set(self, obj_type, keys, attrs, do_assert=True):
296
320
self .set (obj_type + ":" + json .dumps (keys [i ]), attr , do_assert )
297
321
return "SAI_STATUS_SUCCESS" , statuses
298
322
323
+ def get_stats (self , obj , attrs , do_assert = True ):
324
+ obj_type , oid , _ = self .obj_to_items (obj )
325
+ status , result = self ._operate_stats ('get' , attrs = attrs , oid = oid , obj_type = obj_type )
326
+
327
+ if do_assert :
328
+ assert status == 'SAI_STATUS_SUCCESS' , f"get({ obj } , { attrs } ) --> { status } "
329
+
330
+ result = [key for pair in result .items () for key in pair ]
331
+ result = SaiData (json .dumps (result ))
332
+
333
+ if do_assert :
334
+ return result
335
+ return status , result
336
+
337
+ def clear_stats (self , obj , attrs , do_assert = True ):
338
+ obj_type , oid , _ = self .obj_to_items (obj )
339
+ status , _ = self ._operate_stats ('clear' , attrs = attrs , oid = oid , obj_type = obj_type )
340
+
341
+ if do_assert :
342
+ assert status == 'SAI_STATUS_SUCCESS' , f"clear({ obj } , { attrs } ) --> { status } "
343
+ return status
344
+
299
345
def get_object_key (self , obj_type = None ):
300
346
return dict ()
0 commit comments