Skip to content

Commit

Permalink
[SAI-PTF] Enhance the function for getting counters in sai_adapter.py
Browse files Browse the repository at this point in the history
    Resolve issue #1614

    For the auto-generated adapter, need to add a parameter for which counter_id/counter_ids we want, in order to compatiable with current code and reduce the modification, we can define a constant parameter as the list of counter_ids, and use this constant as the default.

    Before

    ```
    def sai_thrift_get_bfd_session_stats(client,
                                         bfd_session_oid):

        counter_ids = []
        counter_ids.append(SAI_BFD_SESSION_STAT_IN_PACKETS)
        counter_ids.append(SAI_BFD_SESSION_STAT_OUT_PACKETS)
        counter_ids.append(SAI_BFD_SESSION_STAT_DROP_PACKETS)

        counters = [0] * 3
        global status
        status = SAI_STATUS_SUCCESS
    ```

    After the refactor
    ```
    sai_get_bfd_session_stats_counter_ids = []
    sai_get_bfd_session_stats_counter_ids.append(SAI_BFD_SESSION_STAT_IN_PACKETS)
    sai_get_bfd_session_stats_counter_ids.append(SAI_BFD_SESSION_STAT_OUT_PACKETS)
    sai_get_bfd_session_stats_counter_ids.append(SAI_BFD_SESSION_STAT_DROP_PACKETS)

    def sai_thrift_get_bfd_session_stats(client,
                                         bfd_session_oid,
                                         counter_ids=sai_get_bfd_session_stats_counter_ids):

        counters = [0] * 3
        global status
        status = SAI_STATUS_SUCCESS
    ```

Signed-off-by: richardyu-ms <richard.yu@microsoft.com>
  • Loading branch information
richardyu authored and richardyu-ms committed Oct 24, 2022
1 parent 13312c6 commit 490ccba
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions meta/templates/sai_adapter.py.tt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ def [% function.thrift_name %](client
[%- FOREACH attr IN apis.$api.objects.${function.object}.attrs.${function.operation} -%]
,[% br %][% attr.simple_name %]=None
[%- END -%]
[%- IF function.operation == 'stats' OR function.operation == 'clear' -%]
[%- FOREACH arg IN function.adapter_preprocessed_args -%]
[%- IF arg.is_list and arg.name == 'counter_ids' -%]
,[% br %][% arg.name %]=[% function.name %]_[% arg.name %]
[%- ELSIF arg.is_attr %]
[% arg.name %] = None
[%- END -%]
[%- END -%]
[%- END -%]
):
[% END -%]

Expand All @@ -102,15 +111,31 @@ def [% function.thrift_name %](client
[%- # Declare variables that are not part of python interface -%]
[%- # but are required by thritft functions -%]
[%- FOREACH arg IN function.adapter_preprocessed_args -%]

[%- IF arg.is_list %]
[% arg.name %] = []
[%- IF arg.is_list AND arg.name!= 'counter_ids' %]
[% function.name %]_[% arg.name %] = []
[%- ELSIF arg.is_attr %]
[% arg.name %] = None
[%- END -%]
[%- END -%]
[%- END -%]

[%- BLOCK declare_stats_variables -%]
[%- IF function.operation == 'stats' OR function.operation == 'clear' %]
[%- FOREACH arg IN function.adapter_preprocessed_args -%]

[%- IF arg.is_list %]
[% function.name %]_[% arg.name %] = []
[%- ELSIF arg.is_attr %]
[% arg.name %] = None
[%- END -%]
[%- END -%]
[%- FOREACH stat IN apis.$api.objects.${function.object}.stats.all %]
[% function.name %]_[% arg.name %].append([% stat.name %])
[%- END -%]

[%- END -%]
[%- END -%]

[%- ######################################################################## -%]

[%- ######################################################################## -%]
Expand Down Expand Up @@ -279,8 +304,10 @@ client.[% function.thrift_name %](

[%- BLOCK preprocess_stats -%]

[%- FOREACH stat IN apis.$api.objects.${function.object}.stats.all %]
[% arg.name %].append([% stat.name %])
[%- IF arg.name != "counter_ids" -%]
[%- FOREACH stat IN apis.$api.objects.${function.object}.stats.all %]
[% function.name %]_[% arg.name %].append([% stat.name %])
[%- END -%]
[%- END -%]

[%- END -%]
Expand Down Expand Up @@ -308,13 +335,11 @@ client.[% function.thrift_name %](
[%- # Basically we do it for each list variable, we assume we have one -%]
[%- FOREACH arg IN function.adapter_preprocessed_args -%]
[%- IF function.operation == 'stats' OR function.operation == 'clear' %]
[%- # For 'get stats' function create stats list -%]
[%- # For 'get stats' function NOT create stats list in function body -%]
[%- PROCESS preprocess_stats %]

[%- ELSIF function.operation != 'remove' %]
[%- # For 'create' and 'get' functions create list of arguments -%]
[%- PROCESS preprocess_attributes %]

[%- END -%]
[%- END -%]

Expand Down Expand Up @@ -400,14 +425,16 @@ client.[% function.thrift_name %](
[%- ######################################################################## -%]

[%- BLOCK function_body -%]
[%- PROCESS declare_stats_variables -%]


[%- PROCESS decorate_method IF dev_utils -%]
[%- PROCESS decorate_skip_test_on_error IF skip_error -%]
[%- PROCESS function_header %]
[%- PROCESS function_docstring %]

[%- IF has_body -%]
[%- PROCESS declare_variables %]

[%- PROCESS preprocess_args -%]

[%- # Now, call the thrift function -%]
Expand Down

0 comments on commit 490ccba

Please sign in to comment.