diff --git a/config/main.py b/config/main.py index a2e0b374845c..95730cd9e365 100755 --- a/config/main.py +++ b/config/main.py @@ -238,6 +238,31 @@ def _change_bgp_session_status(ipaddr_or_hostname, status, verbose): for ip_addr in ip_addrs: _change_bgp_session_status_by_addr(ip_addr, status, verbose) +def _validate_bgp_neighbor(neighbor_ip_or_hostname): + """validates whether the given ip or host name is a BGP neighbor + """ + ip_addrs = [] + if _is_neighbor_ipaddress(neighbor_ip_or_hostname.lower()): + ip_addrs.append(neighbor_ip_or_hostname.lower()) + else: + ip_addrs = _get_neighbor_ipaddress_list_by_hostname(neighbor_ip_or_hostname.upper()) + + if not ip_addrs: + click.get_current_context().fail("Could not locate neighbor '{}'".format(neighbor_ip_or_hostname)) + + return ip_addrs + +def _remove_bgp_neighbor_config(neighbor_ip_or_hostname): + """Removes BGP configuration of the given neighbor + """ + ip_addrs = _validate_bgp_neighbor(neighbor_ip_or_hostname) + config_db = ConfigDBConnector() + config_db.connect() + + for ip_addr in ip_addrs: + config_db.mod_entry('bgp_neighbor', ip_addr, None) + click.echo("Removed configuration of BGP neighbor {}".format(ip_addr)) + def _change_hostname(hostname): current_hostname = os.uname()[1] if current_hostname != hostname: @@ -952,6 +977,21 @@ def neighbor(ipaddr_or_hostname, verbose): """Start up BGP session by neighbor IP address or hostname""" _change_bgp_session_status(ipaddr_or_hostname, 'up', verbose) +# +# 'remove' subgroup ('config bgp remove ...') +# + +@bgp.group() +def remove(): + "Remove BGP neighbor configuration from the device" + pass + +@remove.command('neighbor') +@click.argument('neighbor_ip_or_hostname', metavar='', required=True) +def remove_neighbor(neighbor_ip_or_hostname): + """Deletes BGP neighbor configuration of given hostname or ip from devices""" + _remove_bgp_neighbor_config(neighbor_ip_or_hostname) + # # 'interface' group ('config interface ...') # diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index a7ed0ec02159..dfdc25f6c8fe 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -1466,6 +1466,8 @@ The list of possible BGP config commands are given below. startup all neighbor + remove + neighbor **config bgp shut down all** @@ -1524,6 +1526,26 @@ This command is used to start up the particular IPv4 or IPv6 BGP neighbor using admin@sonic:~$ sudo config bgp startup neighbor SONIC02SPINE ``` + +**config bgp remove neighbor ** + +This command is used to remove particular IPv4 or IPv6 BGP neighbor configuration using either the IP address or hostname. + + - Usage: + sudo config bgp remove neighbor + +- Examples: + ``` + admin@sonic:~$ sudo config bgp remove neighbor 192.168.1.124 + ``` + ``` + admin@sonic:~$ sudo config bgp remove neighbor 2603:10b0:b0f:346::4a + ``` + ``` + admin@sonic:~$ sudo config bgp remove neighbor SONIC02SPINE + ``` + + Go Back To [Beginning of the document](#SONiC-COMMAND-LINE-INTERFACE-GUIDE) or [Beginning of this section](#BGP-Configuration-And-Show-Commands)