1
- import click
2
1
import ipaddress
3
- from tabulate import tabulate
2
+ from collections import OrderedDict
3
+
4
+ import click
5
+ import utilities_common .cli as clicommon
4
6
from swsssdk import ConfigDBConnector
5
7
from swsssdk import SonicV2Connector
6
- import utilities_common . cli as clicommon
7
- from collections import OrderedDict
8
+ from tabulate import tabulate
9
+
8
10
9
11
@click .group (cls = clicommon .AliasedGroup )
10
12
def fgnhg ():
11
13
"""Show FGNHG information"""
12
14
pass
13
15
16
+
14
17
@fgnhg .command ()
15
18
@click .argument ('nhg' , required = False )
16
19
def active_hops (nhg ):
17
20
config_db = ConfigDBConnector ()
18
21
config_db .connect ()
19
22
fg_nhg_prefix_table = {}
20
- fg_nhg_alias = {}
23
+ fg_nhg_alias = {}
21
24
fg_nhg_prefix_table = config_db .get_table ('FG_NHG_PREFIX' )
22
-
25
+
23
26
for key , value in fg_nhg_prefix_table .items ():
24
- fg_nhg_alias [key ] = value ['FG_NHG' ]
25
-
27
+ fg_nhg_alias [key ] = value ['FG_NHG' ]
28
+
26
29
state_db = SonicV2Connector (host = '127.0.0.1' )
27
30
state_db .connect (state_db .STATE_DB , False ) # Make one attempt only STATE_DB
28
-
31
+
29
32
TABLE_NAME_SEPARATOR = '|'
30
33
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
31
34
_hash = '{}{}' .format (prefix , '*' )
@@ -34,68 +37,68 @@ def active_hops(nhg):
34
37
t_dict = {}
35
38
table = []
36
39
output_dict = {}
37
-
40
+
38
41
if nhg is None :
39
- for nhg_prefix in table_keys :
40
- t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
42
+ for nhg_prefix in table_keys :
43
+ t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
41
44
vals = sorted (set ([val for val in t_dict .values ()]))
42
- for nh_ip in vals :
43
- if nhg_prefix in output_dict :
44
- output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
45
- else :
46
- output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
47
-
48
- nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
49
- header = ["FG_NHG_PREFIX" , "Active Next Hops" ]
45
+ for nh_ip in vals :
46
+ if nhg_prefix in output_dict :
47
+ output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
48
+ else :
49
+ output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
50
+
51
+ nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
52
+ header = ["FG_NHG_PREFIX" , "Active Next Hops" ]
50
53
formatted_nhps = ',' .replace (',' , '\n ' ).join (output_dict [nhg_prefix ])
51
54
table .append ([nhg_prefix_report , formatted_nhps ])
52
55
53
- click .echo (tabulate (table , header , tablefmt = "grid" ))
54
-
56
+ click .echo (tabulate (table , header , tablefmt = "grid" ))
57
+
55
58
else :
56
- for nhg_prefix , alias in fg_nhg_alias .items ():
57
- if nhg == alias :
58
- if ":" in nhg_prefix :
59
- for key in table_keys :
60
- mod_key = key .split ("|" )[1 ].split ("/" )[0 ]
61
- mod_nhg_prefix = nhg_prefix .split ("/" )[0 ]
62
- if ipaddress .ip_address (unicode (mod_key )).exploded == ipaddress .ip_address (unicode (mod_nhg_prefix )).exploded :
59
+ for nhg_prefix , alias in fg_nhg_alias .items ():
60
+ if nhg == alias :
61
+ if ":" in nhg_prefix :
62
+ for key in table_keys :
63
+ mod_key = key .split ("|" )[1 ].split ("/" )[0 ]
64
+ mod_nhg_prefix = nhg_prefix .split ("/" )[0 ]
65
+ if ipaddress .ip_address (unicode (mod_key )).exploded == ipaddress .ip_address (unicode (mod_nhg_prefix )).exploded :
63
66
t_dict = state_db .get_all (state_db .STATE_DB , key )
64
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
67
+ nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
65
68
else :
66
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
67
- t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
68
-
69
- vals = sorted (set ([val for val in t_dict .values ()]))
70
-
71
- for nh_ip in vals :
72
- if nhg_prefix in output_dict :
73
- output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
74
- else :
75
- output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
76
-
77
- nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
78
- formatted_nhps = ',' .replace (',' , '\n ' ).join (output_dict [nhg_prefix ])
79
- table .append ([nhg_prefix_report , formatted_nhps ])
80
- header = ["FG_NHG_PREFIX" , "Active Next Hops" ]
81
- click .echo (tabulate (table , header , tablefmt = "grid" ))
82
-
69
+ nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
70
+ t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
71
+
72
+ vals = sorted (set ([val for val in t_dict .values ()]))
73
+
74
+ for nh_ip in vals :
75
+ if nhg_prefix in output_dict :
76
+ output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
77
+ else :
78
+ output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
79
+
80
+ nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
81
+ formatted_nhps = ',' .replace (',' , '\n ' ).join (output_dict [nhg_prefix ])
82
+ table .append ([nhg_prefix_report , formatted_nhps ])
83
+ header = ["FG_NHG_PREFIX" , "Active Next Hops" ]
84
+ click .echo (tabulate (table , header , tablefmt = "grid" ))
85
+
83
86
84
87
@fgnhg .command ()
85
88
@click .argument ('nhg' , required = False )
86
89
def hash_view (nhg ):
87
90
config_db = ConfigDBConnector ()
88
91
config_db .connect ()
89
92
fg_nhg_prefix_table = {}
90
- fg_nhg_alias = {}
93
+ fg_nhg_alias = {}
91
94
fg_nhg_prefix_table = config_db .get_table ('FG_NHG_PREFIX' )
92
-
95
+
93
96
for key , value in fg_nhg_prefix_table .items ():
94
- fg_nhg_alias [key ] = value ['FG_NHG' ]
95
-
96
- state_db = SonicV2Connector (host = '127.0.0.1' )
97
+ fg_nhg_alias [key ] = value ['FG_NHG' ]
98
+
99
+ state_db = SonicV2Connector (host = '127.0.0.1' )
97
100
state_db .connect (state_db .STATE_DB , False ) # Make one attempt only STATE_DB
98
-
101
+
99
102
TABLE_NAME_SEPARATOR = '|'
100
103
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
101
104
_hash = '{}{}' .format (prefix , '*' )
@@ -107,63 +110,63 @@ def hash_view(nhg):
107
110
bank_dict = {}
108
111
109
112
if nhg is None :
110
- for nhg_prefix in table_keys :
111
- bank_dict = {}
112
- t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
113
+ for nhg_prefix in table_keys :
114
+ bank_dict = {}
115
+ t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
113
116
vals = sorted (set ([val for val in t_dict .values ()]))
114
117
115
- for nh_ip in vals :
116
- bank_ids = sorted ([int (k ) for k , v in t_dict .items () if v == nh_ip ])
117
-
118
- bank_ids = [str (x ) for x in bank_ids ]
119
-
120
- if nhg_prefix in output_dict :
121
- output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
122
- else :
123
- output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
124
- bank_dict [nh_ip .split ("@" )[0 ]] = bank_ids
125
-
126
- bank_dict = OrderedDict (sorted (bank_dict .items ()))
127
- nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
128
- header = ["FG_NHG_PREFIX" , "Next Hop" , "Hash buckets" ]
129
-
130
- for nhip ,val in bank_dict .items ():
131
- formatted_banks = ',' .replace (',' , '\n ' ).join (bank_dict [nhip ])
132
- table .append ([nhg_prefix_report , nhip , formatted_banks ])
133
-
134
- click .echo (tabulate (table , header , tablefmt = "grid" ))
135
-
118
+ for nh_ip in vals :
119
+ bank_ids = sorted ([int (k ) for k , v in t_dict .items () if v == nh_ip ])
120
+
121
+ bank_ids = [str (x ) for x in bank_ids ]
122
+
123
+ if nhg_prefix in output_dict :
124
+ output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
125
+ else :
126
+ output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
127
+ bank_dict [nh_ip .split ("@" )[0 ]] = bank_ids
128
+
129
+ bank_dict = OrderedDict (sorted (bank_dict .items ()))
130
+ nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
131
+ header = ["FG_NHG_PREFIX" , "Next Hop" , "Hash buckets" ]
132
+
133
+ for nhip , val in bank_dict .items ():
134
+ formatted_banks = ',' .replace (',' , '\n ' ).join (bank_dict [nhip ])
135
+ table .append ([nhg_prefix_report , nhip , formatted_banks ])
136
+
137
+ click .echo (tabulate (table , header , tablefmt = "grid" ))
138
+
136
139
else :
137
- for nhg_prefix , alias in fg_nhg_alias .items ():
138
- if nhg == alias :
139
- if ":" in nhg_prefix :
140
- for key in table_keys :
141
- mod_key = key .split ("|" )[1 ].split ("/" )[0 ]
142
- mod_nhg_prefix = nhg_prefix .split ("/" )[0 ]
143
- if ipaddress .ip_address (unicode (mod_key )).exploded == ipaddress .ip_address (unicode (mod_nhg_prefix )).exploded :
140
+ for nhg_prefix , alias in fg_nhg_alias .items ():
141
+ if nhg == alias :
142
+ if ":" in nhg_prefix :
143
+ for key in table_keys :
144
+ mod_key = key .split ("|" )[1 ].split ("/" )[0 ]
145
+ mod_nhg_prefix = nhg_prefix .split ("/" )[0 ]
146
+ if ipaddress .ip_address (unicode (mod_key )).exploded == ipaddress .ip_address (unicode (mod_nhg_prefix )).exploded :
144
147
t_dict = state_db .get_all (state_db .STATE_DB , key )
145
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
148
+ nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
146
149
else :
147
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
148
- t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
149
-
150
- vals = sorted (set ([val for val in t_dict .values ()]))
151
-
152
- for nh_ip in vals :
153
- bank_ids = sorted ([int (k ) for k , v in t_dict .items () if v == nh_ip ])
154
- bank_ids = [str (x ) for x in bank_ids ]
155
- if nhg_prefix in output_dict :
156
- output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
157
- else :
158
- output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
159
- bank_dict [nh_ip .split ("@" )[0 ]] = bank_ids
160
-
161
- nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
162
- bank_dict = OrderedDict (sorted (bank_dict .items ()))
163
- header = ["FG_NHG_PREFIX" , "Next Hop" , "Hash buckets" ]
164
-
165
- for nhip ,val in bank_dict .items ():
166
- formatted_banks = ',' .replace (',' , '\n ' ).join (bank_dict [nhip ])
167
- table .append ([nhg_prefix_report , nhip , formatted_banks ])
168
-
169
- click .echo (tabulate (table , header , tablefmt = "grid" ))
150
+ nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
151
+ t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
152
+
153
+ vals = sorted (set ([val for val in t_dict .values ()]))
154
+
155
+ for nh_ip in vals :
156
+ bank_ids = sorted ([int (k ) for k , v in t_dict .items () if v == nh_ip ])
157
+ bank_ids = [str (x ) for x in bank_ids ]
158
+ if nhg_prefix in output_dict :
159
+ output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
160
+ else :
161
+ output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
162
+ bank_dict [nh_ip .split ("@" )[0 ]] = bank_ids
163
+
164
+ nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
165
+ bank_dict = OrderedDict (sorted (bank_dict .items ()))
166
+ header = ["FG_NHG_PREFIX" , "Next Hop" , "Hash buckets" ]
167
+
168
+ for nhip , val in bank_dict .items ():
169
+ formatted_banks = ',' .replace (',' , '\n ' ).join (bank_dict [nhip ])
170
+ table .append ([nhg_prefix_report , nhip , formatted_banks ])
171
+
172
+ click .echo (tabulate (table , header , tablefmt = "grid" ))
0 commit comments