Skip to content

Commit

Permalink
Change to use rvtysh when calling the show commands (#1646)
Browse files Browse the repository at this point in the history
Change to use rvtysh when calling the show commands
Merge code from master and fix code conflict, see #1572
  • Loading branch information
xumia authored Jun 3, 2021
1 parent 88a823f commit 43fff88
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions show/bgp_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ def show_routes(args, namespace, display, verbose, ipver):
# If not MultiASIC, skip namespace argument
cmd = "show {} route {}".format(ipver, arg_strg)
if multi_asic.is_multi_asic():
output = bgp_util.run_bgp_command(cmd, ns)
output = bgp_util.run_bgp_show_command(cmd, ns)
else:
output = bgp_util.run_bgp_command(cmd)
output = bgp_util.run_bgp_show_command(cmd)
print("{}".format(output))
return

Expand Down
4 changes: 2 additions & 2 deletions show/bgp_frr_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def summary(namespace, display):
def neighbors(ipaddress, info_type):
"""Show IP (IPv4) BGP neighbors"""

command = 'sudo vtysh -c "show ip bgp neighbor'
command = 'sudo {} -c "show ip bgp neighbor'.format(constants.RVTYSH_COMMAND)

if ipaddress is not None:
command += ' {}'.format(ipaddress)
Expand All @@ -56,7 +56,7 @@ def neighbors(ipaddress, info_type):
def network(ipaddress, info_type):
"""Show IP (IPv4) BGP network"""

command = 'sudo vtysh -c "show ip bgp'
command = 'sudo {} -c "show ip bgp'.format(constants.RVTYSH_COMMAND)

if ipaddress is not None:
if '/' in ipaddress:
Expand Down
4 changes: 2 additions & 2 deletions show/bgp_frr_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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)
command = 'sudo {} -c "show bgp ipv6 neighbor {} {}"'.format(constants.RVTYSH_COMMAND, ipaddress, info_type)
run_command(command)

# 'network' subcommand ("show ipv6 bgp network")
Expand All @@ -47,7 +47,7 @@ def neighbors(ipaddress, info_type):
def network(ipaddress, info_type):
"""Show BGP ipv6 network"""

command = 'sudo vtysh -c "show bgp ipv6'
command = 'sudo {} -c "show bgp ipv6'.format(constants.RVTYSH_COMMAND)

if ipaddress is not None:
if '/' in ipaddress:
Expand Down
7 changes: 4 additions & 3 deletions show/bgp_quagga_v4.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
from show.main import AliasedGroup, ip, run_command
from utilities_common.bgp_util import get_bgp_summary_extended
import utilities_common.constants as constants


###############################################################################
Expand All @@ -21,10 +22,10 @@ def bgp():
def summary():
"""Show summarized information of IPv4 BGP state"""
try:
device_output = run_command('sudo vtysh -c "show ip bgp summary"', return_cmd=True)
device_output = run_command('sudo {} -c "show ip bgp summary"'.format(constants.RVTYSH_COMMAND), return_cmd=True)
get_bgp_summary_extended(device_output)
except Exception:
run_command('sudo vtysh -c "show ip bgp summary"')
run_command('sudo {} -c "show ip bgp summary"'.format(constants.RVTYSH_COMMAND))


# 'neighbors' subcommand ("show ip bgp neighbors")
Expand All @@ -34,7 +35,7 @@ def summary():
def neighbors(ipaddress, info_type):
"""Show IP (IPv4) BGP neighbors"""

command = 'sudo vtysh -c "show ip bgp neighbor'
command = 'sudo {} -c "show ip bgp neighbor'.format(constants.RVTYSH_COMMAND)

if ipaddress is not None:
command += ' {}'.format(ipaddress)
Expand Down
7 changes: 4 additions & 3 deletions show/bgp_quagga_v6.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
from show.main import AliasedGroup, ipv6, run_command
from utilities_common.bgp_util import get_bgp_summary_extended
import utilities_common.constants as constants


###############################################################################
Expand All @@ -21,10 +22,10 @@ def bgp():
def summary():
"""Show summarized information of IPv6 BGP state"""
try:
device_output = run_command('sudo vtysh -c "show ipv6 bgp summary"', return_cmd=True)
device_output = run_command('sudo {} -c "show ipv6 bgp summary"'.format(constants.RVTYSH_COMMAND), return_cmd=True)
get_bgp_summary_extended(device_output)
except Exception:
run_command('sudo vtysh -c "show ipv6 bgp summary"')
run_command('sudo {} -c "show ipv6 bgp summary"'.format(constants.RVTYSH_COMMAND))


# 'neighbors' subcommand ("show ipv6 bgp neighbors")
Expand All @@ -33,5 +34,5 @@ def summary():
@click.argument('info_type', type=click.Choice(['routes', 'advertised-routes', 'received-routes']), required=True)
def neighbors(ipaddress, info_type):
"""Show IPv6 BGP neighbors"""
command = 'sudo vtysh -c "show ipv6 bgp neighbor {} {}"'.format(ipaddress, info_type)
command = 'sudo {} -c "show ipv6 bgp neighbor {} {}"'.format(constants.RVTYSH_COMMAND, ipaddress, info_type)
run_command(command)
13 changes: 7 additions & 6 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from swsscommon.swsscommon import SonicV2Connector
from tabulate import tabulate
from utilities_common.db import Db
import utilities_common.constants as constants

from . import acl
from . import bgp_common
Expand Down Expand Up @@ -708,7 +709,7 @@ def mac(vlan, port, verbose):
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def route_map(route_map_name, verbose):
"""show route-map"""
cmd = 'sudo vtysh -c "show route-map'
cmd = 'sudo {} -c "show route-map'.format(constants.RVTYSH_COMMAND)
if route_map_name is not None:
cmd += ' {}'.format(route_map_name)
cmd += '"'
Expand Down Expand Up @@ -885,7 +886,7 @@ def route(args, namespace, display, verbose):
@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 ip prefix-list'
cmd = 'sudo {} -c "show ip prefix-list'.format(constants.RVTYSH_COMMAND)
if prefix_list_name is not None:
cmd += ' {}'.format(prefix_list_name)
cmd += '"'
Expand All @@ -897,7 +898,7 @@ def prefix_list(prefix_list_name, verbose):
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def protocol(verbose):
"""Show IPv4 protocol information"""
cmd = 'sudo vtysh -c "show ip protocol"'
cmd = 'sudo {} -c "show ip protocol"'.format(constants.RVTYSH_COMMAND)
run_command(cmd, display_cmd=verbose)


Expand All @@ -920,7 +921,7 @@ def ipv6():
@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'
cmd = 'sudo {} -c "show ipv6 prefix-list'.format(constants.RVTYSH_COMMAND)
if prefix_list_name is not None:
cmd += ' {}'.format(prefix_list_name)
cmd += '"'
Expand Down Expand Up @@ -1001,7 +1002,7 @@ def route(args, namespace, display, verbose):
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def protocol(verbose):
"""Show IPv6 protocol information"""
cmd = 'sudo vtysh -c "show ipv6 protocol"'
cmd = 'sudo {} -c "show ipv6 protocol"'.format(constants.RVTYSH_COMMAND)
run_command(cmd, display_cmd=verbose)

#
Expand Down Expand Up @@ -1228,7 +1229,7 @@ def ports(portname, verbose):
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def bgp(verbose):
"""Show BGP running configuration"""
cmd = 'sudo vtysh -c "show running-config"'
cmd = 'sudo {} -c "show running-config"'.format(constants.RVTYSH_COMMAND)
run_command(cmd, display_cmd=verbose)


Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .mock_tables import dbconnector
from . import show_ip_route_common
import utilities_common.constants as constantsn

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
Expand Down Expand Up @@ -118,7 +119,7 @@ def setup_single_bgp_instance(request):
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', 'dummy.json')

def mock_run_bgp_command(vtysh_cmd, bgp_namespace):
def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
if os.path.isfile(bgp_mocked_json):
with open(bgp_mocked_json) as json_data:
mock_frr_data = json_data.read()
Expand Down Expand Up @@ -178,7 +179,7 @@ def setup_multi_asic_bgp_instance(request):
m_asic_json_file = os.path.join(
test_path, 'mock_tables', 'dummy.json')

def mock_run_bgp_command(vtysh_cmd, bgp_namespace):
def mock_run_bgp_command(vtysh_cmd, bgp_namespace, vtysh_shell_cmd=constants.RVTYSH_COMMAND):
bgp_mocked_json = os.path.join(
test_path, 'mock_tables', bgp_namespace, m_asic_json_file)
if os.path.isfile(bgp_mocked_json):
Expand Down
10 changes: 6 additions & 4 deletions utilities_common/bgp_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ def get_neighbor_dict_from_table(db, table_name):
return neighbor_dict


def run_bgp_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE):
def run_bgp_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE, vtysh_shell_cmd=constants.VTYSH_COMMAND):
bgp_instance_id = ' '
output = None
if bgp_namespace is not multi_asic.DEFAULT_NAMESPACE:
bgp_instance_id = " -n {} ".format(multi_asic.get_asic_id_from_name(bgp_namespace))

cmd = 'sudo vtysh {} -c "{}"'.format(
bgp_instance_id, vtysh_cmd)
cmd = 'sudo {} {} -c "{}"'.format(
vtysh_shell_cmd, bgp_instance_id, vtysh_cmd)
try:
output = clicommon.run_command(cmd, return_cmd=True)
except Exception:
Expand All @@ -163,6 +163,8 @@ def run_bgp_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE):

return output

def run_bgp_show_command(vtysh_cmd, bgp_namespace=multi_asic.DEFAULT_NAMESPACE):
return run_bgp_command(vtysh_cmd, bgp_namespace, constants.RVTYSH_COMMAND)

def get_bgp_summary_from_all_bgp_instances(af, namespace, display):

Expand All @@ -178,7 +180,7 @@ def get_bgp_summary_from_all_bgp_instances(af, namespace, display):
bgp_summary = {}
cmd_output_json = {}
for ns in device.get_ns_list_based_on_options():
cmd_output = run_bgp_command(vtysh_cmd, ns)
cmd_output = run_bgp_show_command(vtysh_cmd, ns)
try:
cmd_output_json = json.loads(cmd_output)
except ValueError:
Expand Down
3 changes: 2 additions & 1 deletion utilities_common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
PORT_OBJ = 'PORT'
IPV4 = 'v4'
IPV6 = 'v6'

VTYSH_COMMAND = 'vtysh'
RVTYSH_COMMAND = 'rvtysh'

0 comments on commit 43fff88

Please sign in to comment.