1
- import ipaddress
2
1
from collections import OrderedDict
3
-
4
2
import click
5
3
import utilities_common .cli as clicommon
6
4
from swsscommon .swsscommon import SonicV2Connector , ConfigDBConnector
@@ -12,32 +10,28 @@ def fgnhg():
12
10
"""Show FGNHG information"""
13
11
pass
14
12
15
-
16
13
@fgnhg .command ()
17
14
@click .argument ('nhg' , required = False )
18
15
def active_hops (nhg ):
19
16
config_db = ConfigDBConnector ()
20
17
config_db .connect ()
21
- fg_nhg_prefix_table = {}
22
- fg_nhg_alias = {}
23
- fg_nhg_prefix_table = config_db .get_table ('FG_NHG_PREFIX' )
24
-
25
- for key , value in fg_nhg_prefix_table .items ():
26
- fg_nhg_alias [key ] = value ['FG_NHG' ]
27
-
28
18
state_db = SonicV2Connector (host = '127.0.0.1' )
29
19
state_db .connect (state_db .STATE_DB , False ) # Make one attempt only STATE_DB
30
-
31
20
TABLE_NAME_SEPARATOR = '|'
32
21
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
33
22
_hash = '{}{}' .format (prefix , '*' )
34
23
table_keys = []
35
- table_keys = state_db .keys (state_db .STATE_DB , _hash )
36
24
t_dict = {}
37
- header = ["FG_NHG_PREFIX " , "Active Next Hops" ]
25
+ header = ["FG NHG Prefix " , "Active Next Hops" ]
38
26
table = []
39
27
output_dict = {}
40
-
28
+ ctx = click .get_current_context ()
29
+ try :
30
+ table_keys = sorted (state_db .keys (state_db .STATE_DB , _hash ))
31
+ except Exception as e :
32
+ ctx .fail ("FG_ROUTE_TABLE does not exist!" )
33
+ if table_keys is None :
34
+ ctx .fail ("FG_ROUTE_TABLE does not exist!" )
41
35
if nhg is None :
42
36
for nhg_prefix in table_keys :
43
37
t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
@@ -47,123 +41,154 @@ def active_hops(nhg):
47
41
output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
48
42
else :
49
43
output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
50
-
51
44
nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
52
45
formatted_nhps = ',' .replace (',' , '\n ' ).join (output_dict [nhg_prefix ])
53
46
table .append ([nhg_prefix_report , formatted_nhps ])
54
47
55
- click .echo (tabulate (table , header , tablefmt = "grid" ))
56
-
48
+ click .echo (tabulate (table , header , tablefmt = "simple" ))
57
49
else :
58
- for nhg_prefix , alias in fg_nhg_alias .items ():
59
- if nhg == alias :
60
- if ":" in nhg_prefix :
61
- for key in table_keys :
62
- mod_key = key .split ("|" )[1 ].split ("/" )[0 ]
63
- mod_nhg_prefix = nhg_prefix .split ("/" )[0 ]
64
- if ipaddress .ip_address (mod_key ).exploded == ipaddress .ip_address (mod_nhg_prefix ).exploded :
65
- t_dict = state_db .get_all (state_db .STATE_DB , key )
66
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
67
- else :
68
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
69
- t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
70
-
50
+ nhip_prefix_map = {}
51
+ header = ["FG NHG Prefix" , "Active Next Hops" ]
52
+ try :
53
+ fg_nhg_member_table = config_db .get_table ('FG_NHG_MEMBER' )
54
+ except Exception as e :
55
+ ctx .fail ("FG_NHG_MEMBER entries not present in config_db" )
56
+ alias_list = []
57
+ nexthop_alias = {}
58
+ output_list = []
59
+ for nexthop , nexthop_metadata in fg_nhg_member_table .items ():
60
+ alias_list .append (nexthop_metadata ['FG_NHG' ])
61
+ nexthop_alias [nexthop ] = nexthop_metadata ['FG_NHG' ]
62
+ if nhg not in alias_list :
63
+ ctx .fail ("Please provide a valid NHG alias" )
64
+ else :
65
+ for nhg_prefix in table_keys :
66
+ t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
71
67
vals = sorted (set ([val for val in t_dict .values ()]))
72
-
73
68
for nh_ip in vals :
74
- if nhg_prefix in output_dict :
75
- output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
69
+ nhip_prefix_map [nh_ip .split ("@" )[0 ]] = nhg_prefix
70
+
71
+ if nh_ip .split ("@" )[0 ] in nexthop_alias :
72
+ if nexthop_alias [nh_ip .split ("@" )[0 ]] == nhg :
73
+ output_list .append (nh_ip .split ("@" )[0 ])
76
74
else :
77
- output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
78
-
79
- nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
80
- formatted_nhps = ',' .replace (',' , '\n ' ).join (output_dict [nhg_prefix ])
81
- table .append ([nhg_prefix_report , formatted_nhps ])
82
- click .echo (tabulate (table , header , tablefmt = "grid" ))
83
-
75
+ ctx .fail ("state_db and config_db have FGNHG prefix config mismatch. Check device config!" );
76
+ output_list = sorted (output_list )
77
+ if not output_list :
78
+ ctx .fail ("FG_ROUTE table likely does not contain the required entries" )
79
+ nhg_prefix_report = nhip_prefix_map [output_list [0 ]].split ("|" )[1 ]
80
+ formatted_output_list = ',' .replace (',' , '\n ' ).join (output_list )
81
+ table .append ([nhg_prefix_report , formatted_output_list ])
82
+ click .echo (tabulate (table , header , tablefmt = "simple" ))
84
83
85
84
@fgnhg .command ()
86
85
@click .argument ('nhg' , required = False )
87
86
def hash_view (nhg ):
88
87
config_db = ConfigDBConnector ()
89
88
config_db .connect ()
90
- fg_nhg_prefix_table = {}
91
- fg_nhg_alias = {}
92
- fg_nhg_prefix_table = config_db .get_table ('FG_NHG_PREFIX' )
93
-
94
- for key , value in fg_nhg_prefix_table .items ():
95
- fg_nhg_alias [key ] = value ['FG_NHG' ]
96
-
97
89
state_db = SonicV2Connector (host = '127.0.0.1' )
98
90
state_db .connect (state_db .STATE_DB , False ) # Make one attempt only STATE_DB
99
-
100
91
TABLE_NAME_SEPARATOR = '|'
101
92
prefix = 'FG_ROUTE_TABLE' + TABLE_NAME_SEPARATOR
102
93
_hash = '{}{}' .format (prefix , '*' )
103
94
table_keys = []
104
- table_keys = state_db .keys (state_db .STATE_DB , _hash )
105
95
t_dict = {}
106
- header = ["FG_NHG_PREFIX " , "Next Hop" , "Hash buckets" ]
96
+ header = ["FG NHG Prefix " , "Next Hop" , "Hash buckets" ]
107
97
table = []
108
98
output_dict = {}
109
99
bank_dict = {}
110
-
100
+ ctx = click .get_current_context ()
101
+ try :
102
+ table_keys = sorted (state_db .keys (state_db .STATE_DB , _hash ))
103
+ except Exception as e :
104
+ ctx .fail ("FG_ROUTE_TABLE does not exist!" )
105
+ if table_keys is None :
106
+ ctx .fail ("FG_ROUTE_TABLE does not exist!" )
111
107
if nhg is None :
112
108
for nhg_prefix in table_keys :
113
109
bank_dict = {}
114
110
t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
115
111
vals = sorted (set ([val for val in t_dict .values ()]))
116
-
117
112
for nh_ip in vals :
118
113
bank_ids = sorted ([int (k ) for k , v in t_dict .items () if v == nh_ip ])
119
-
120
114
bank_ids = [str (x ) for x in bank_ids ]
121
-
122
115
if nhg_prefix in output_dict :
123
116
output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
124
117
else :
125
118
output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
126
119
bank_dict [nh_ip .split ("@" )[0 ]] = bank_ids
127
-
128
120
bank_dict = OrderedDict (sorted (bank_dict .items ()))
129
121
nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
130
-
131
122
for nhip , val in bank_dict .items ():
132
- formatted_banks = ',' .replace (',' , '\n ' ).join (bank_dict [nhip ])
133
- table .append ([nhg_prefix_report , nhip , formatted_banks ])
134
-
135
- click .echo (tabulate (table , header , tablefmt = "grid" ))
136
-
123
+ displayed_banks = []
124
+ bank_output = ""
125
+ formatted_banks = (bank_dict [nhip ])
126
+ for bankid in formatted_banks :
127
+ if (len (str (bankid )) == 1 ):
128
+ displayed_banks .append (str (bankid ) + " " )
129
+
130
+ if (len (str (bankid )) == 2 ):
131
+ displayed_banks .append (str (bankid ) + " " )
132
+
133
+ if (len (str (bankid )) == 3 ):
134
+ displayed_banks .append (str (bankid ))
135
+ for i in range (0 , len (displayed_banks ), 8 ):
136
+ bank_output = bank_output + " " .join (displayed_banks [i :i + 8 ]) + "\n "
137
+ bank_output = bank_output + "\n "
138
+ table .append ([nhg_prefix_report , nhip , bank_output ])
139
+ click .echo (tabulate (table , header , tablefmt = "simple" ))
137
140
else :
138
- for nhg_prefix , alias in fg_nhg_alias .items ():
139
- if nhg == alias :
140
- if ":" in nhg_prefix :
141
- for key in table_keys :
142
- mod_key = key .split ("|" )[1 ].split ("/" )[0 ]
143
- mod_nhg_prefix = nhg_prefix .split ("/" )[0 ]
144
- if ipaddress .ip_address (mod_key ).exploded == ipaddress .ip_address (mod_nhg_prefix ).exploded :
145
- t_dict = state_db .get_all (state_db .STATE_DB , key )
146
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
147
- else :
148
- nhg_prefix = "FG_ROUTE_TABLE|" + nhg_prefix
149
- t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
150
-
141
+ header = ["FG NHG Prefix" , "Next Hop" , "Hash buckets" ]
142
+ try :
143
+ fg_nhg_member_table = config_db .get_table ('FG_NHG_MEMBER' )
144
+ except Exception as e :
145
+ ctx .fail ("FG_NHG_MEMBER entries not present in config_db" )
146
+ alias_list = []
147
+ nexthop_alias = {}
148
+ for nexthop , nexthop_metadata in fg_nhg_member_table .items ():
149
+ alias_list .append (nexthop_metadata ['FG_NHG' ])
150
+ nexthop_alias [nexthop ] = nexthop_metadata ['FG_NHG' ]
151
+ if nhg not in alias_list :
152
+ ctx .fail ("Please provide a valid NHG alias" )
153
+ else :
154
+ nhip_prefix_map = {}
155
+ for nhg_prefix in table_keys :
156
+ bank_dict = {}
157
+ t_dict = state_db .get_all (state_db .STATE_DB , nhg_prefix )
151
158
vals = sorted (set ([val for val in t_dict .values ()]))
152
-
153
159
for nh_ip in vals :
154
160
bank_ids = sorted ([int (k ) for k , v in t_dict .items () if v == nh_ip ])
161
+ nhip_prefix_map [nh_ip .split ("@" )[0 ]] = nhg_prefix
155
162
bank_ids = [str (x ) for x in bank_ids ]
156
163
if nhg_prefix in output_dict :
157
164
output_dict [nhg_prefix ].append (nh_ip .split ("@" )[0 ])
158
165
else :
159
166
output_dict [nhg_prefix ] = [nh_ip .split ("@" )[0 ]]
160
167
bank_dict [nh_ip .split ("@" )[0 ]] = bank_ids
161
-
162
- nhg_prefix_report = (nhg_prefix .split ("|" )[1 ])
163
168
bank_dict = OrderedDict (sorted (bank_dict .items ()))
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" ))
169
+ output_bank_dict = {}
170
+ for nexthop , banks in bank_dict .items ():
171
+ if nexthop in nexthop_alias :
172
+ if nexthop_alias [nexthop ] == nhg :
173
+ output_bank_dict [nexthop ] = banks
174
+ else :
175
+ ctx .fail ("state_db and config_db have FGNHG prefix config mismatch. Check device config!" );
176
+ nhg_prefix_report = nhip_prefix_map [list (bank_dict .keys ())[0 ]].split ("|" )[1 ]
177
+ output_bank_dict = OrderedDict (sorted (output_bank_dict .items ()))
178
+ for nhip , val in output_bank_dict .items ():
179
+ bank_output = ""
180
+ displayed_banks = []
181
+ formatted_banks = (bank_dict [nhip ])
182
+ for bankid in formatted_banks :
183
+ if (len (str (bankid )) == 1 ):
184
+ displayed_banks .append (str (bankid ) + " " )
185
+
186
+ if (len (str (bankid )) == 2 ):
187
+ displayed_banks .append (str (bankid ) + " " )
188
+
189
+ if (len (str (bankid )) == 3 ):
190
+ displayed_banks .append (str (bankid ))
191
+ for i in range (0 , len (displayed_banks ), 8 ):
192
+ bank_output = bank_output + " " .join (displayed_banks [i :i + 8 ]) + "\n "
193
+ table .append ([nhg_prefix_report , nhip , bank_output ])
194
+ click .echo (tabulate (table , header , tablefmt = "simple" ))
0 commit comments