1
1
import click
2
+
3
+ from sonic_py_common import multi_asic
4
+ from show .main import ip
2
5
import utilities_common .bgp_util as bgp_util
3
6
import utilities_common .cli as clicommon
4
7
import utilities_common .constants as constants
5
8
import utilities_common .multi_asic as multi_asic_util
6
9
7
- from show .main import ip , run_command
8
-
9
10
###############################################################################
10
11
#
11
12
# 'show ip bgp' cli stanza
12
13
#
13
14
###############################################################################
14
15
15
16
16
-
17
17
@ip .group (cls = clicommon .AliasedGroup )
18
18
def bgp ():
19
19
"""Show IPv4 BGP (Border Gateway Protocol) information"""
@@ -24,43 +24,93 @@ def bgp():
24
24
@bgp .command ()
25
25
@multi_asic_util .multi_asic_click_options
26
26
def summary (namespace , display ):
27
- bgp_summary = bgp_util .get_bgp_summary_from_all_bgp_instances (constants .IPV4 , namespace ,display )
27
+ bgp_summary = bgp_util .get_bgp_summary_from_all_bgp_instances (
28
+ constants .IPV4 , namespace , display )
28
29
bgp_util .display_bgp_summary (bgp_summary = bgp_summary , af = constants .IPV4 )
29
-
30
+
30
31
31
32
# 'neighbors' subcommand ("show ip bgp neighbors")
32
33
@bgp .command ()
33
34
@click .argument ('ipaddress' , required = False )
34
- @click .argument ('info_type' , type = click .Choice (['routes' , 'advertised-routes' , 'received-routes' ]), required = False )
35
- def neighbors (ipaddress , info_type ):
35
+ @click .argument ('info_type' ,
36
+ type = click .Choice (
37
+ ['routes' , 'advertised-routes' , 'received-routes' ]),
38
+ required = False )
39
+ @click .option ('--namespace' ,
40
+ '-n' ,
41
+ 'namespace' ,
42
+ default = None ,
43
+ type = str ,
44
+ show_default = True ,
45
+ help = 'Namespace name or all' ,
46
+ callback = multi_asic_util .multi_asic_namespace_validation_callback )
47
+ def neighbors (ipaddress , info_type , namespace ):
36
48
"""Show IP (IPv4) BGP neighbors"""
37
49
38
- command = 'sudo vtysh -c "show ip bgp neighbor'
39
-
50
+ command = 'show ip bgp neighbor'
40
51
if ipaddress is not None :
41
- command += ' {}' .format (ipaddress )
52
+ if not bgp_util .is_ipv4_address (ipaddress ):
53
+ ctx = click .get_current_context ()
54
+ ctx .fail ("{} is not valid ipv4 address\n " .format (ipaddress ))
55
+ try :
56
+ actual_namespace = bgp_util .get_namespace_for_bgp_neighbor (
57
+ ipaddress )
58
+ if namespace is not None and namespace != actual_namespace :
59
+ click .echo (
60
+ "[WARNING]: bgp neighbor {} is present in namespace {} not in {}"
61
+ .format (ipaddress , actual_namespace , namespace ))
42
62
43
- # info_type is only valid if ipaddress is specified
44
- if info_type is not None :
45
- command += ' {}' .format (info_type )
63
+ # save the namespace in which the bgp neighbor is configured
64
+ namespace = actual_namespace
65
+
66
+ command += ' {}' .format (ipaddress )
46
67
47
- command += '"'
68
+ # info_type is only valid if ipaddress is specified
69
+ if info_type is not None :
70
+ command += ' {}' .format (info_type )
71
+ except ValueError as err :
72
+ ctx = click .get_current_context ()
73
+ ctx .fail ("{}\n " .format (err ))
74
+
75
+ ns_list = multi_asic .get_namespace_list (namespace )
76
+ output = ""
77
+ for ns in ns_list :
78
+ output += bgp_util .run_bgp_command (command , ns )
79
+
80
+ click .echo (output .rstrip ('\n ' ))
48
81
49
- run_command (command )
50
82
51
83
# 'network' subcommand ("show ip bgp network")
52
84
@bgp .command ()
53
- @click .argument ('ipaddress' , metavar = '[<ipv4-address>|<ipv4-prefix>]' , required = False )
54
- @click .argument ('info_type' , metavar = '[bestpath|json|longer-prefixes|multipath]' ,
55
- type = click .Choice (['bestpath' , 'json' , 'longer-prefixes' , 'multipath' ]), required = False )
56
- def network (ipaddress , info_type ):
85
+ @click .argument ('ipaddress' ,
86
+ metavar = '[<ipv4-address>|<ipv4-prefix>]' ,
87
+ required = False )
88
+ @click .argument ('info_type' ,
89
+ metavar = '[bestpath|json|longer-prefixes|multipath]' ,
90
+ type = click .Choice (
91
+ ['bestpath' , 'json' , 'longer-prefixes' , 'multipath' ]),
92
+ required = False )
93
+ @click .option ('--namespace' ,
94
+ '-n' ,
95
+ 'namespace' ,
96
+ type = str ,
97
+ show_default = True ,
98
+ required = True if multi_asic .is_multi_asic is True else False ,
99
+ help = 'Namespace name or all' ,
100
+ default = None ,
101
+ callback = multi_asic_util .multi_asic_namespace_validation_callback )
102
+ def network (ipaddress , info_type , namespace ):
57
103
"""Show IP (IPv4) BGP network"""
58
104
59
- command = 'sudo vtysh -c "show ip bgp'
105
+ if multi_asic .is_multi_asic () and namespace not in multi_asic .get_namespace_list ():
106
+ ctx = click .get_current_context ()
107
+ ctx .fail ('-n/--namespace option required. provide namespace from list {}' \
108
+ .format (multi_asic .get_namespace_list ()))
60
109
110
+ command = 'show ip bgp'
61
111
if ipaddress is not None :
62
112
if '/' in ipaddress :
63
- # For network prefixes then this all info_type(s) are available
113
+ # For network prefixes then this all info_type(s) are available
64
114
pass
65
115
else :
66
116
# For an ipaddress then check info_type, exit if specified option doesn't work.
@@ -75,6 +125,5 @@ def network(ipaddress, info_type):
75
125
if info_type is not None :
76
126
command += ' {}' .format (info_type )
77
127
78
- command += '"'
79
-
80
- run_command (command )
128
+ output = bgp_util .run_bgp_command (command , namespace )
129
+ click .echo (output .rstrip ('\n ' ))
0 commit comments