Skip to content

Commit ed0bc8e

Browse files
authored
Implemented get_stats and clear_stats for Thrift RPC. (#249)
Signed-off-by: xuyinhao <xuyinhao@asterfusion.com>
1 parent 9eb69fd commit ed0bc8e

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

common/sai_client/sai_thrift_client/sai_thrift_client.py

+46
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ def _operate_attributes(self, operation, attrs=(), oid=None, obj_type=None, key=
208208

209209
return status, result
210210

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+
211235
def cleanup(self):
212236
if self.thrift_transport:
213237
self.thrift_transport.close()
@@ -296,5 +320,27 @@ def bulk_set(self, obj_type, keys, attrs, do_assert=True):
296320
self.set(obj_type + ":" + json.dumps(keys[i]), attr, do_assert)
297321
return "SAI_STATUS_SUCCESS", statuses
298322

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+
299345
def get_object_key(self, obj_type=None):
300346
return dict()

common/sai_client/sai_thrift_client/sai_thrift_utils.py

+28
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from sai_thrift.ttypes import *
77
from sai_thrift import ttypes
88
from sai_thrift.sai_headers import *
9+
from sai_thrift.sai_adapter import *
10+
import sai_thrift.sai_adapter as adapter
911
from saichallenger.common.sai_data import SaiObjType, SaiStatus
1012

1113

@@ -19,6 +21,14 @@ def convert_attributes_to_thrift(attributes):
1921
continue
2022
yield ThriftConverter.convert_attribute_name_to_thrift(name), ThriftConverter.convert_value_to_thrift(value, attr_name=name)
2123

24+
def convert_counter_ids_to_thrift(counter_ids, obj_type_name):
25+
"""
26+
[ 'SAI_PORT_STAT_IF_IN_OCTETS', '', 'SAI_PORT_STAT_IF_IN_UCAST_PKTS', '' ] => { "counter_ids": [SAI_PORT_STAT_IF_IN_OCTETS, SAI_PORT_STAT_IF_IN_UCAST_PKTS]) }
27+
[ 'SAI_QUEUE_STAT_PACKETS' ] => { "counter_ids": [SAI_QUEUE_STAT_PACKETS]) }
28+
"""
29+
cnt_ids = ThriftConverter.convert_counter_ids_name_to_thrift(counter_ids, obj_type_name)
30+
return {"counter_ids": cnt_ids}
31+
2232
def convert_key_to_thrift(object_type, key = None):
2333
"""
2434
Converts dictionary 'key' to the thrift key entry according to 'object_type':
@@ -59,6 +69,24 @@ def convert_attribute_name_to_thrift(attr):
5969
"""
6070
return re.search('SAI_.*_ATTR_(.*)', attr).group(1).lower()
6171

72+
@staticmethod
73+
def convert_counter_ids_name_to_thrift(counter_ids, obj_type_name):
74+
"""
75+
[ 'SAI_PORT_STAT_IF_IN_OCTETS', '', 'SAI_PORT_STAT_IF_IN_UCAST_PKTS', '' ] => [SAI_PORT_STAT_IF_IN_OCTETS,SAI_PORT_STAT_IF_IN_UCAST_PKTS]
76+
"""
77+
cnts_list = []
78+
id_dict_name = "sai_get_{}_stats_counter_ids_dict".format(obj_type_name)
79+
id_dict = getattr(adapter, id_dict_name)
80+
reverse_id_dict = {v: k for k, v in id_dict.items()}
81+
for counter_id in counter_ids:
82+
if counter_id == '':
83+
continue
84+
if counter_id in reverse_id_dict:
85+
cnts_list.append(reverse_id_dict[counter_id])
86+
else:
87+
raise ValueError("Counter id {} is not supported for {}".format(counter_id, obj_type_name))
88+
return cnts_list
89+
6290
@staticmethod
6391
def convert_u8_to_thrift(u8_str):
6492
# Thrift does not support unsigned int notation.

0 commit comments

Comments
 (0)