Skip to content

Commit

Permalink
Merge pull request #143 from e-sonic/snc-15980_AS_path_list_seqnum
Browse files Browse the repository at this point in the history
SNC-15980 Add sequence number ordering for as-path-list
  • Loading branch information
bhavini-gada authored and GitHub Enterprise committed Sep 21, 2022
2 parents 64ab93d + 50089fa commit 521dc4c
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 43 deletions.
44 changes: 35 additions & 9 deletions CLI/actioner/show_config_bgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,22 +487,48 @@ def show_bgpaspath_lists(render_tables):

get_aspath_name = (render_tables["aspath-list-name"]
if "aspath-list-name" in render_tables else None)
if ("sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST"

aspath_seqnlist = {}
if ("sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH/AS_PATH_LIST"
in render_tables):
aspath_list = render_tables[
"sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST"]
cmd_prfx = "bgp as-path-list "
"sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH/AS_PATH_LIST"]
for aspath in aspath_list:
if "name" in aspath:
if "name" in aspath and "sequence_number" in aspath:
if get_aspath_name and aspath["name"] != get_aspath_name:
continue
if "action" in aspath:
if "as_path_set_member" in aspath:
for member in aspath["as_path_set_member"]:
if aspath["name"] in aspath_seqnlist:
aspath_seqnlist[aspath["name"]].append(aspath)
else:
aspath_seqnlist[aspath["name"]] = [ aspath ]

if ("sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST"
in render_tables):
aspathset_list = render_tables[
"sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST"]
cmd_prfx = "bgp as-path-list "
for aspathset in aspathset_list:
if "name" in aspathset:
if get_aspath_name and aspathset["name"] != get_aspath_name:
continue
if "action" in aspathset:
if "as_path_set_member" in aspathset:
for member in aspathset["as_path_set_member"]:
member = member.replace('\\', '\\\\')
cmd_str += (cmd_prfx + aspath["name"] + " " +
aspath["action"].lower() + " " +
cmd_str += (cmd_prfx + aspathset["name"] + " " +
aspathset["action"].lower() + " " +
'"' + member + '"' + ";")
else:
if "as_path_set_member" in aspathset:
continue
if aspathset["name"] not in aspath_seqnlist:
continue
for seqn in aspath_seqnlist[aspathset["name"]]:
if "action" in seqn and "as_path_member" in seqn:
cmd_str += (cmd_prfx + aspathset["name"] + " seq " +
str(seqn["sequence_number"]) + " " +
seqn["action"].lower() + " " +
'"' + ','.join(seqn["as_path_member"]) + '"' + ";")

return "CB_SUCCESS", cmd_str

Expand Down
100 changes: 72 additions & 28 deletions CLI/actioner/sonic_cli_bgp_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,17 @@ def aspath_delete(aa, args):
paths_exits = 0
paths = ""
as_action = ""
for arg in args[1:]:
if args[1] == 'seq=':
dbpath = '/restconf/data/sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST={name}'
dbtblst = 'sonic-routing-policy-sets:AS_PATH_SET_LIST'
dbmembr = 'as_path_set_member'
ocextpath = ''
else:
dbpath = '/restconf/data/sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH/AS_PATH_LIST={name},{seqno}'
dbtblst = 'sonic-routing-policy-sets:AS_PATH_LIST'
ocextpath = '/openconfig-bgp-policy-ext:extended-as-path-sets/extended-as-path-set={sequence_number}'
dbmembr = 'as_path_member'
for arg in args[2:]:
paths_exits = 1
if "permit" == arg:
as_action = "PERMIT"
Expand All @@ -309,20 +319,19 @@ def aspath_delete(aa, args):
if paths_exits:

full_delete = True
keypath = cc.Path(
"/restconf/data/sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST={name}", name=args[0],)
keypath = cc.Path(dbpath, name=args[0], seqno=args[1][4:])
response = aa.get(keypath)
if response.ok() and response.content is not None:
api_response = response.content
if api_response and "action" in api_response['sonic-routing-policy-sets:AS_PATH_SET_LIST'][0]:
current_action = api_response['sonic-routing-policy-sets:AS_PATH_SET_LIST'][0]['action']
if api_response and "action" in api_response[dbtblst][0]:
current_action = api_response[dbtblst][0]['action']
if current_action != as_action.lower():
response.status_code = 0
response.set_error_message("Action mismatch.")
return response

if api_response and "as_path_set_member" in api_response['sonic-routing-policy-sets:AS_PATH_SET_LIST'][0]:
current_mem = api_response['sonic-routing-policy-sets:AS_PATH_SET_LIST'][0]['as_path_set_member']
if api_response and dbmembr in api_response[dbtblst][0]:
current_mem = api_response[dbtblst][0][dbmembr]
del_list = paths.split(",")
for mem in current_mem:
if mem not in del_list:
Expand All @@ -333,18 +342,23 @@ def aspath_delete(aa, args):

if full_delete:
keypath = cc.Path(
"/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets/as-path-set={as_path_set_name}", as_path_set_name=args[0],)
"/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets/as-path-set={as_path_set_name}" + ocextpath,
as_path_set_name=args[0],
sequence_number=args[1][4:]
)
else:
paths = paths[:-1]
keypath = cc.Path(
"/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets/as-path-set={as_path_set_name}/config/as-path-set-member={path}",
"/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets/as-path-set={as_path_set_name}" + ocextpath + "/config/as-path-set-member={path}",
as_path_set_name=args[0],
sequence_number=args[1][4:],
path=paths,
)
else:
keypath = cc.Path(
"/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets/as-path-set={as_path_set_name}",
"/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets/as-path-set={as_path_set_name}" + ocextpath,
as_path_set_name=args[0],
sequence_number=args[1][4:]
)
return aa.delete(keypath)

Expand Down Expand Up @@ -408,40 +422,67 @@ def invoke(func, args):

# bgp-as-path-list command
elif func == "bgp_as_path_list":
if args[1] == "permit":
if args[2] == "permit":
action = "PERMIT"
else:
action = "DENY"

keypath = cc.Path(
"/restconf/data/sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST={name}", name=args
[0]
)
seqnum = args[1][4:]
if args[1] == 'seq=':
dbpath = '/restconf/data/sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST={name}'
dbtblst = 'sonic-routing-policy-sets:AS_PATH_SET_LIST'
else:
dbpath = '/restconf/data/sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH/AS_PATH_LIST={name},{seqno}'
dbtblst = 'sonic-routing-policy-sets:AS_PATH_LIST'

keypath = cc.Path(dbpath, name=args[0], seqno=seqnum)
response = aa.get(keypath)
if response.ok() and response.content is not None:
api_response = response.content
if api_response and "action" in api_response['sonic-routing-policy-sets:AS_PATH_SET_LIST'][0]:
current_action = api_response['sonic-routing-policy-sets:AS_PATH_SET_LIST'][0]['action']
if current_action != args[1]:
if api_response and "action" in api_response[dbtblst][0]:
current_action = api_response[dbtblst][0]['action']
if current_action != args[2]:
response.status_code = 0
response.set_error_message("Cannot override already configured action")
return response

keypath = cc.Path(
"/restconf/data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets"
)
body = {
"openconfig-bgp-policy:as-path-sets": {
"as-path-set": [{
"as-path-set-name": args[0],
"config": {
if args[1] == 'seq=':
body = {
"openconfig-bgp-policy:as-path-sets": {
"as-path-set": [{
"as-path-set-name": args[0],
"as-path-set-member": [args[2]],
"openconfig-bgp-policy-ext:action": action,
},
}]
"config": {
"as-path-set-name": args[0],
"as-path-set-member": [args[3]],
"openconfig-bgp-policy-ext:action": action,
},
}]
}
}
else:
body = {
"openconfig-bgp-policy:as-path-sets": {
"as-path-set": [{
"as-path-set-name": args[0],
"config": {
"as-path-set-name": args[0],
},
"openconfig-bgp-policy-ext:extended-as-path-sets": {
"extended-as-path-set": [{
"sequence-number": int(seqnum),
"config": {
"sequence-number": int(seqnum),
"as-path-set-member": [args[3]],
"openconfig-bgp-policy-ext:action": action,
},
}],
},
}]
}
}
}
return aa.patch(keypath, body)

# Remove the bgp-as-path-list set.
Expand Down Expand Up @@ -496,6 +537,9 @@ def run(func, args):
try:
response = invoke(func, args)

if response == None:
return

if response.ok():
if response.content is not None:
# Get Command Output
Expand Down
40 changes: 36 additions & 4 deletions CLI/clitree/cli-xml/bgp_community_sets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ limitations under the License.
<PARAM name="extcommunity-list-name" help="Name of extended community-list" ptype="STRING_63" optional="true"> </PARAM>
<ACTION builtin="clish_pyobj">sonic_cli_show_config show_view views=renderCfg_bgp_extcom_list view_keys="extcommunity-list-name=${extcommunity-list-name}"</ACTION>
</COMMAND>
<COMMAND name="show running-configuration bgp as-path-acess-list" help="Display current BGP AS path acess list configuration">
<COMMAND name="show running-configuration bgp as-path-acess-list" help="Display current BGP AS path acess list configuration" hidden="true">
<PARAM name="aspath-list-name" help="Name of as-path-list" ptype="STRING_63" optional="true"> </PARAM>
<ACTION builtin="clish_pyobj">sonic_cli_show_config show_view views=renderCfg_bgp_aspath_list view_keys="aspath-list-name=${aspath-list-name}"</ACTION>
</COMMAND>
<COMMAND name="show running-configuration bgp as-path-access-list" help="Display current BGP AS path access list configuration">
<PARAM name="aspath-list-name" help="Name of as-path-list" ptype="STRING_63" optional="true"> </PARAM>
<ACTION builtin="clish_pyobj">sonic_cli_show_config show_view views=renderCfg_bgp_aspath_list view_keys="aspath-list-name=${aspath-list-name}"</ACTION>
</COMMAND>
Expand Down Expand Up @@ -576,7 +580,7 @@ limitations under the License.
help="BGP autonomous system path"
escape_chars=""
command_keys="name=*"
command_tables="sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST/name={name}"
command_tables="sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH_SET/AS_PATH_SET_LIST/name={name};sonic-routing-policy-sets:sonic-routing-policy-sets/AS_PATH/AS_PATH_LIST/name={name}"
render_command_cb="bgp_aspath_list"
render_command_viewid="renderCfg_bgp_aspath_list"
>
Expand All @@ -585,6 +589,20 @@ limitations under the License.
help="Name of as-path-list (Max: 63 characters)"
ptype="STRING_63"
>
<PARAM
name="seq"
help="Sequence number"
mode="subcommand"
ptype="SUBCOMMAND"
optional="true"
>
<PARAM
name="seq-no"
help="Sequence number"
ptype="RANGE_1_4294967295"
>
</PARAM>
</PARAM>
<PARAM
name="as-path-action"
help="BGP autonomous system sub commands"
Expand Down Expand Up @@ -619,7 +637,7 @@ limitations under the License.
</PARAM>
</PARAM>
</PARAM>
<ACTION builtin="clish_pyobj">sonic_cli_bgp_community bgp_as_path_list ${as-path-list-name} ${as-path-action} ${#regx-id}</ACTION>
<ACTION builtin="clish_pyobj">sonic_cli_bgp_community bgp_as_path_list ${as-path-list-name} seq=${seq-no} ${as-path-action} ${#regx-id}</ACTION>
<DOCGEN>
<DESCRIPTION>
This command creates BGP AS Path list
Expand Down Expand Up @@ -752,6 +770,20 @@ limitations under the License.
help="Name of as-path-list"
ptype="STRING_63"
>
<PARAM
name="seq"
help="Sequence number"
mode="subcommand"
ptype="SUBCOMMAND"
optional="true"
>
<PARAM
name="seq-no"
help="Sequence number"
ptype="RANGE_1_4294967295"
>
</PARAM>
</PARAM>
<PARAM
name="as-path-action"
help="BGP autonomous system sub commands"
Expand Down Expand Up @@ -787,7 +819,7 @@ limitations under the License.
</PARAM>
</PARAM>
</PARAM>
<ACTION builtin="clish_pyobj">sonic_cli_bgp_community bgp_as_path_list_delete ${as-path-list-name} ${as-path-action} ${#regx-id}</ACTION>
<ACTION builtin="clish_pyobj">sonic_cli_bgp_community bgp_as_path_list_delete ${as-path-list-name} seq=${seq-no} ${as-path-action} ${#regx-id}</ACTION>
</COMMAND>
</VIEW>
</CLISH_MODULE>
15 changes: 13 additions & 2 deletions CLI/renderer/templates/show_bgp_aspath.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@
{% endif %}
{% for list in comm_list %}
{% set vars = {'list_name': ""} %}
{% set mems = "" %}
{% set temp = [ ] %}
{% if vars.update({'list_name':list ['as-path-set-name']}) %} {% endif %}
AS path list {{'%s'| format(vars.list_name)}}:
{% if "openconfig-bgp-policy-ext:extended-as-path-sets" is in list %}
{% set xlist = list["openconfig-bgp-policy-ext:extended-as-path-sets"]["extended-as-path-set"] %}
{% for xset in xlist %}
{% if "sequence-number" is in xset and "action" is in xset['config'] and "as-path-set-member" is in xset['config'] %}
{% set mems = xset['config']['as-path-set-member'] %}
seq: {{xset['sequence-number']}}
action: {{xset['config']['action']|lower}}
members: {{'%s'| format(mems|join(","))}}
{% endif %}
{% endfor %}
{% else %}
{% set temp = [ ] %}
action: {{list['config']['openconfig-bgp-policy-ext:action']|lower}}
{% set mems = list['config']['as-path-set-member'] %}
{% for mem in mems %}
{% do temp.append(mem) %}
{% endfor %}
members: {{'%s'| format(temp|join(","))}}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}

0 comments on commit 521dc4c

Please sign in to comment.