Skip to content

Commit

Permalink
[sai-gen] Remove all complicated logic when parsing p4runtime object …
Browse files Browse the repository at this point in the history
…names. (#482)
  • Loading branch information
r12f authored Dec 13, 2023
1 parent e720803 commit 276b52a
Showing 1 changed file with 12 additions and 49 deletions.
61 changes: 12 additions & 49 deletions dash-pipeline/SAI/sai_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def parse(self, p4rt_value, *args, **kwargs):
self.name = kwargs['name']
kwargs.pop('name')

self.parse_preamble_if_exists(p4rt_value)
self.parse_basic_info_if_exists(p4rt_value)
self.parse_p4rt(p4rt_value, *args, **kwargs)

return
Expand Down Expand Up @@ -231,9 +231,9 @@ def __init__(self):
self.field = None
self.default = None

def parse_preamble_if_exists(self, p4rt_object):
def parse_basic_info_if_exists(self, p4rt_object):
'''
This method parses the P4Runtime preamble object and populates the SAI object.
This method parses basic info, such as id and name, from either the object itself or the P4Runtime preamble object and populates the SAI object.
Example P4Runtime preamble object:
Expand All @@ -248,46 +248,16 @@ def parse_preamble_if_exists(self, p4rt_object):
self.id = preamble['id']
self.name = preamble['name']
self.alias = preamble['alias']

@staticmethod
def parse_sai_annotated_name(name, full_name_part_start = 0, full_name_part_end = None):
'''
This method parses the annotated name and returns the parsed parts.
Annotated name format in ABNF:
- annotated_name = full_name [":" preferred_name] ["|" api_name]
- full_name = control_block *child_control_block "." object_name
- child_control_block = "." control_block
- control_block = object_name = preferred_name = name
- name = 1*(ALPHA / DIGIT / "_")
Also if preferred_name is not specified, it will be the same as object_name.
Annotated name examples:
- dash_ingress.inbound_routing|dash_inbound_routing
- dash_ingress.outbound.acl.stage3:dash_acl_rule|dash_acl
- meta.dash_acl_group_id:dash_acl_group_id
'''
full_name = ""
preferred_name = None
api_name = None

if '|' in name:
full_name, api_name = name.split('|')
else:
full_name = name
self.id = p4rt_object['id'] if 'id' in p4rt_object else self.id
self.name = p4rt_object['name'] if 'name' in p4rt_object else self.name

if ':' in full_name:
full_name, preferred_name = full_name.split(':')

full_name_parts = full_name.split('.')
if preferred_name == None:
preferred_name = full_name_parts[-1]
# We only care about the last piece of the name, which is the actual object name.
if '.' in self.name:
name_parts = self.name.split('.')
self.name = name_parts[-1]

full_name_parts = full_name_parts[full_name_part_start:full_name_part_end]
full_name = '.'.join(full_name_parts)

return full_name, preferred_name, api_name
return

def _parse_sai_object_annotation(self, p4rt_anno_list):
'''
Expand Down Expand Up @@ -356,7 +326,7 @@ def parse_p4rt(self, p4rt_member):
{ "name": "INVALID", "value": "AAA=" }
'''
self.p4rt_value = p4rt_member
self.p4rt_value = p4rt_member["value"]


@sai_parser_from_p4rt
Expand Down Expand Up @@ -388,7 +358,7 @@ def parse_p4rt(self, p4rt_enum):

self.name = self.name[:-2]
self.bitwidth = p4rt_enum['underlyingType'][BITWIDTH_TAG]
self.members = [SAIEnumMember.from_p4rt(enum_member['value'], name = enum_member['name']) for enum_member in p4rt_enum[MEMBERS_TAG]]
self.members = [SAIEnumMember.from_p4rt(enum_member) for enum_member in p4rt_enum[MEMBERS_TAG]]

# Register enum type info.
SAITypeSolver.register_sai_type(
Expand Down Expand Up @@ -429,13 +399,9 @@ def parse_p4rt(self, p4rt_table_key):
}
'''

self.id = p4rt_table_key['id']
self.name = p4rt_table_key[NAME_TAG]
self.bitwidth = p4rt_table_key[BITWIDTH_TAG]
# print("Parsing table key: " + self.name)

_, self.name, _ = self.parse_sai_annotated_name(self.name, full_name_part_start = -2)

if OTHER_MATCH_TYPE_TAG in p4rt_table_key:
self.match_type = p4rt_table_key[OTHER_MATCH_TYPE_TAG].lower()
elif MATCH_TYPE_TAG in p4rt_table_key:
Expand Down Expand Up @@ -486,7 +452,6 @@ def parse_p4rt(self, p4rt_table_action, sai_enums):
}
'''
# print("Parsing table action: " + self.name)
_, self.name, _ = self.parse_sai_annotated_name(self.name)
self.parse_action_params(p4rt_table_action, sai_enums)

def parse_action_params(self, p4rt_table_action, sai_enums):
Expand Down Expand Up @@ -519,8 +484,6 @@ def parse_p4rt(self, p4rt_table_action_param):
{ "id": 1, "name": "dst_vnet_id", "bitwidth": 16 }
'''
self.id = p4rt_table_action_param['id']
self.name = p4rt_table_action_param[NAME_TAG]
self.bitwidth = p4rt_table_action_param[BITWIDTH_TAG]
# print("Parsing table action param: " + self.name)

Expand Down

0 comments on commit 276b52a

Please sign in to comment.