Skip to content

Commit da5cc8c

Browse files
rvisnujleveque
authored andcommitted
[config] Add commands to remove BGP neighbor configuration (sonic-net#625)
1 parent a6a44e8 commit da5cc8c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

config/main.py

+40
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,31 @@ def _change_bgp_session_status(ipaddr_or_hostname, status, verbose):
238238
for ip_addr in ip_addrs:
239239
_change_bgp_session_status_by_addr(ip_addr, status, verbose)
240240

241+
def _validate_bgp_neighbor(neighbor_ip_or_hostname):
242+
"""validates whether the given ip or host name is a BGP neighbor
243+
"""
244+
ip_addrs = []
245+
if _is_neighbor_ipaddress(neighbor_ip_or_hostname.lower()):
246+
ip_addrs.append(neighbor_ip_or_hostname.lower())
247+
else:
248+
ip_addrs = _get_neighbor_ipaddress_list_by_hostname(neighbor_ip_or_hostname.upper())
249+
250+
if not ip_addrs:
251+
click.get_current_context().fail("Could not locate neighbor '{}'".format(neighbor_ip_or_hostname))
252+
253+
return ip_addrs
254+
255+
def _remove_bgp_neighbor_config(neighbor_ip_or_hostname):
256+
"""Removes BGP configuration of the given neighbor
257+
"""
258+
ip_addrs = _validate_bgp_neighbor(neighbor_ip_or_hostname)
259+
config_db = ConfigDBConnector()
260+
config_db.connect()
261+
262+
for ip_addr in ip_addrs:
263+
config_db.mod_entry('bgp_neighbor', ip_addr, None)
264+
click.echo("Removed configuration of BGP neighbor {}".format(ip_addr))
265+
241266
def _change_hostname(hostname):
242267
current_hostname = os.uname()[1]
243268
if current_hostname != hostname:
@@ -952,6 +977,21 @@ def neighbor(ipaddr_or_hostname, verbose):
952977
"""Start up BGP session by neighbor IP address or hostname"""
953978
_change_bgp_session_status(ipaddr_or_hostname, 'up', verbose)
954979

980+
#
981+
# 'remove' subgroup ('config bgp remove ...')
982+
#
983+
984+
@bgp.group()
985+
def remove():
986+
"Remove BGP neighbor configuration from the device"
987+
pass
988+
989+
@remove.command('neighbor')
990+
@click.argument('neighbor_ip_or_hostname', metavar='<neighbor_ip_or_hostname>', required=True)
991+
def remove_neighbor(neighbor_ip_or_hostname):
992+
"""Deletes BGP neighbor configuration of given hostname or ip from devices"""
993+
_remove_bgp_neighbor_config(neighbor_ip_or_hostname)
994+
955995
#
956996
# 'interface' group ('config interface ...')
957997
#

doc/Command-Reference.md

+22
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,8 @@ The list of possible BGP config commands are given below.
14661466
startup
14671467
all
14681468
neighbor
1469+
remove
1470+
neighbor
14691471

14701472
**config bgp shut down all**
14711473

@@ -1524,6 +1526,26 @@ This command is used to start up the particular IPv4 or IPv6 BGP neighbor using
15241526
admin@sonic:~$ sudo config bgp startup neighbor SONIC02SPINE
15251527
```
15261528

1529+
1530+
**config bgp remove neighbor <neighbor_ip_or_hostname>**
1531+
1532+
This command is used to remove particular IPv4 or IPv6 BGP neighbor configuration using either the IP address or hostname.
1533+
1534+
- Usage:
1535+
sudo config bgp remove neighbor <neighbor_ip_or_hostname>
1536+
1537+
- Examples:
1538+
```
1539+
admin@sonic:~$ sudo config bgp remove neighbor 192.168.1.124
1540+
```
1541+
```
1542+
admin@sonic:~$ sudo config bgp remove neighbor 2603:10b0:b0f:346::4a
1543+
```
1544+
```
1545+
admin@sonic:~$ sudo config bgp remove neighbor SONIC02SPINE
1546+
```
1547+
1548+
15271549
Go Back To [Beginning of the document](#SONiC-COMMAND-LINE-INTERFACE-GUIDE) or [Beginning of this section](#BGP-Configuration-And-Show-Commands)
15281550

15291551

0 commit comments

Comments
 (0)