Skip to content

Commit

Permalink
Fix bgp 'show' and 'clear' commands (sonic-net#692)
Browse files Browse the repository at this point in the history
* Fix bgp commands

* Make the function more pythonic

* Fix clear command for current frr
  • Loading branch information
pavel-shirshov authored Oct 4, 2019
1 parent 46f344e commit 02375e1
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 28 deletions.
115 changes: 115 additions & 0 deletions clear/bgp_frr_v6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import click
from clear.main import *


###############################################################################
#
# 'clear ipv6 bgp' cli stanza
#
###############################################################################


@ipv6.group(cls=AliasedGroup, default_if_no_args=True,
context_settings=CONTEXT_SETTINGS)
def bgp():
"""Clear IPv6 BGP (Border Gateway Protocol) information"""
pass


# Default 'bgp' command (called if no subcommands or their aliases were passed)
@bgp.command(default=True)
def default():
"""Clear all BGP peers"""
command = 'sudo vtysh -c "clear bgp ipv6 *"'
run_command(command)


@bgp.group(cls=AliasedGroup, default_if_no_args=True,
context_settings=CONTEXT_SETTINGS)
def neighbor():
"""Clear specific BGP peers"""
pass


@neighbor.command(default=True)
@click.argument('ipaddress', required=False)
def default(ipaddress):
"""Clear all BGP peers"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} "'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 *"'
run_command(command)


# 'in' subcommand
@neighbor.command('in')
@click.argument('ipaddress', required=False)
def neigh_in(ipaddress):
"""Send route-refresh"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} in"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 * in"'
run_command(command)


# 'out' subcommand
@neighbor.command('out')
@click.argument('ipaddress', required=False)
def neigh_out(ipaddress):
"""Resend all outbound updates"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} out"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 * out"'
run_command(command)


@neighbor.group(cls=AliasedGroup, default_if_no_args=True,
context_settings=CONTEXT_SETTINGS)
def soft():
"""Soft reconfig BGP's inbound/outbound updates"""
pass


@soft.command(default=True)
@click.argument('ipaddress', required=False)
def default(ipaddress):
"""Clear BGP neighbors soft configuration"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} soft "'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 * soft"'
run_command(command)


# 'soft in' subcommand
@soft.command('in')
@click.argument('ipaddress', required=False)
def soft_in(ipaddress):
"""Send route-refresh"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} soft in"'.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 * soft in"'
run_command(command)


# 'soft out' subcommand
@soft.command('out')
@click.argument('ipaddress', required=False)
def soft_out(ipaddress):
"""Resend all outbound updates"""

if ipaddress is not None:
command = 'sudo vtysh -c "clear bgp ipv6 {} soft out"' \
.format(ipaddress)
else:
command = 'sudo vtysh -c "clear bgp ipv6 * soft out"'
run_command(command)
32 changes: 9 additions & 23 deletions clear/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,15 @@ def get_routing_stack():

def run_command(command, pager=False, return_output=False):
# Provide option for caller function to Process the output.
if return_output == True:
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
if return_output:
return proc.communicate()

if pager is True:
elif pager:
#click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
click.echo_via_pager(p.stdout.read())
click.echo_via_pager(proc.stdout.read())
else:
#click.echo(click.style("Command: ", fg='cyan') + click.style(command, fg='green'))
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
click.echo(p.stdout.read())
click.echo(proc.stdout.read())


CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?'])
Expand Down Expand Up @@ -165,21 +162,10 @@ def ipv6():
from .bgp_quagga_v6 import bgp
ipv6.add_command(bgp)
elif routing_stack == "frr":
@cli.command()
@click.argument('bgp_args', nargs = -1, required = False)
def bgp(bgp_args):
"""BGP information"""
bgp_cmd = "clear bgp"
options = False
for arg in bgp_args:
bgp_cmd += " " + str(arg)
options = True
if options is True:
command = 'sudo vtysh -c "{}"'.format(bgp_cmd)
else:
command = 'sudo vtysh -c "clear bgp *"'
run_command(command)

from .bgp_quagga_v4 import bgp
ip.add_command(bgp)
from .bgp_frr_v6 import bgp
ipv6.add_command(bgp)

@cli.command()
def counters():
Expand Down
34 changes: 34 additions & 0 deletions show/bgp_frr_v6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import click
from show.main import *


###############################################################################
#
# 'show ipv6 bgp' cli stanza
#
###############################################################################


@ipv6.group(cls=AliasedGroup, default_if_no_args=False)
def bgp():
"""Show IPv6 BGP (Border Gateway Protocol) information"""
pass


# 'summary' subcommand ("show ipv6 bgp summary")
@bgp.command()
def summary():
"""Show summarized information of IPv6 BGP state"""
run_command('sudo vtysh -c "show bgp ipv6 summary"')


# 'neighbors' subcommand ("show ipv6 bgp neighbors")
@bgp.command()
@click.argument('ipaddress', required=False)
@click.argument('info_type', type=click.Choice(['routes', 'advertised-routes', 'received-routes']), required=False)
def neighbors(ipaddress, info_type):
"""Show IPv6 BGP neighbors"""
ipaddress = "" if ipaddress is None else ipaddress
info_type = "" if info_type is None else info_type
command = 'sudo vtysh -c "show bgp ipv6 neighbor {} {}"'.format(ipaddress, info_type)
run_command(command)
29 changes: 24 additions & 5 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,22 @@ def ipv6():
"""Show IPv6 commands"""
pass

#
# 'prefix-list' subcommand ("show ipv6 prefix-list")
#

@ipv6.command('prefix-list')
@click.argument('prefix_list_name', required=False)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def prefix_list(prefix_list_name, verbose):
"""show ip prefix-list"""
cmd = 'sudo vtysh -c "show ipv6 prefix-list'
if prefix_list_name is not None:
cmd += ' {}'.format(prefix_list_name)
cmd += '"'
run_command(cmd, display_cmd=verbose)



#
# 'show ipv6 interfaces' command
Expand Down Expand Up @@ -1158,12 +1174,15 @@ def protocol(verbose):
# Inserting BGP functionality into cli's show parse-chain.
# BGP commands are determined by the routing-stack being elected.
#
from .bgp_quagga_v4 import bgp
ip.add_command(bgp)

if routing_stack == "quagga":
from .bgp_quagga_v4 import bgp
ip.add_command(bgp)
from .bgp_quagga_v6 import bgp
ipv6.add_command(bgp)
elif routing_stack == "frr":
from .bgp_frr_v6 import bgp
ipv6.add_command(bgp)
@cli.command()
@click.argument('bgp_args', nargs = -1, required = False)
@click.option('--verbose', is_flag=True, help="Enable verbose output")
Expand Down Expand Up @@ -1215,15 +1234,15 @@ def table(verbose):

def get_hw_info_dict():
"""
This function is used to get the HW info helper function
This function is used to get the HW info helper function
"""
hw_info_dict = {}
machine_info = sonic_device_util.get_machine_info()
platform = sonic_device_util.get_platform_info(machine_info)
config_db = ConfigDBConnector()
config_db.connect()
data = config_db.get_table('DEVICE_METADATA')
try:
try:
hwsku = data['localhost']['hwsku']
except KeyError:
hwsku = "Unknown"
Expand Down Expand Up @@ -1327,7 +1346,7 @@ def version(verbose):
version_info = sonic_device_util.get_sonic_version_info()
hw_info_dict = get_hw_info_dict()
serial_number_cmd = "sudo decode-syseeprom -s"
serial_number = subprocess.Popen(serial_number_cmd, shell=True, stdout=subprocess.PIPE)
serial_number = subprocess.Popen(serial_number_cmd, shell=True, stdout=subprocess.PIPE)
sys_uptime_cmd = "uptime"
sys_uptime = subprocess.Popen(sys_uptime_cmd, shell=True, stdout=subprocess.PIPE)
click.echo("\nSONiC Software Version: SONiC.{}".format(version_info['build_version']))
Expand Down

0 comments on commit 02375e1

Please sign in to comment.