@@ -32,14 +32,18 @@ import sys
32
32
import os
33
33
import re
34
34
35
+ from utilities_common .general import load_db_config
36
+
35
37
# mock the redis for unit test purposes #
36
38
try : # pragma: no cover
37
- if os .environ ["UTILITIES_UNIT_TESTING" ] == "1" :
39
+ if os .environ ["UTILITIES_UNIT_TESTING" ] == "1" :
38
40
modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
39
41
test_path = os .path .join (modules_path , "tests" )
40
42
sys .path .insert (0 , modules_path )
41
43
sys .path .insert (0 , test_path )
42
- import mock_tables .dbconnector
44
+ from tests .mock_tables import dbconnector
45
+
46
+ if os .environ ["FDBSHOW_UNIT_TESTING" ] == "1" :
43
47
mock_variants = { "1" : 'asic_db' ,
44
48
"2" : 'asic_db_def_vlan' ,
45
49
"3" : 'asic_db_no_fdb' ,
@@ -50,23 +54,34 @@ try: # pragma: no cover
50
54
mock_db_path = os .path .join (test_path , "fdbshow_input" )
51
55
file_name = mock_variants [os .environ ["FDBSHOW_MOCK" ]]
52
56
jsonfile_asic = os .path .join (mock_db_path , file_name )
53
- mock_tables . dbconnector .dedicated_dbs ['ASIC_DB' ] = jsonfile_asic
57
+ dbconnector .dedicated_dbs ['ASIC_DB' ] = jsonfile_asic
54
58
jsonfile_counters = os .path .join (mock_db_path , 'counters_db' )
55
- mock_tables .dbconnector .dedicated_dbs ['COUNTERS_DB' ] = jsonfile_counters
59
+ dbconnector .dedicated_dbs ['COUNTERS_DB' ] = jsonfile_counters
60
+
61
+ if os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] == "multi_asic" :
62
+ import tests .mock_tables .mock_multi_asic
63
+ dbconnector .load_namespace_config ()
56
64
except KeyError : # pragma: no cover
57
65
pass
58
66
59
- from sonic_py_common import port_util
60
- from swsscommon .swsscommon import SonicV2Connector
67
+ from sonic_py_common import port_util , multi_asic
68
+ from swsscommon .swsscommon import SonicV2Connector , SonicDBConfig
61
69
from tabulate import tabulate
62
70
63
71
class FdbShow (object ):
64
72
65
73
HEADER = ['No.' , 'Vlan' , 'MacAddress' , 'Port' , 'Type' ]
66
74
67
- def __init__ (self ):
75
+ def __init__ (self , namespace = None ):
68
76
super (FdbShow ,self ).__init__ ()
69
- self .db = SonicV2Connector (host = "127.0.0.1" )
77
+ if namespace is not None :
78
+ if not SonicDBConfig .isGlobalInit ():
79
+ SonicDBConfig .load_sonic_global_db_config ()
80
+
81
+ self .db = SonicV2Connector (use_unix_socket_path = True , namespace = namespace )
82
+ else :
83
+ self .db = SonicV2Connector (host = "127.0.0.1" )
84
+
70
85
self .if_name_map , \
71
86
self .if_oid_map = port_util .get_interface_oid_map (self .db )
72
87
self .if_br_oid_map = port_util .get_bridge_port_map (self .db )
@@ -169,7 +184,7 @@ class FdbShow(object):
169
184
170
185
print ("Total number of entries {0}" .format (len (self .bridge_mac_list )))
171
186
172
- def validate_params (self , vlan , port , address , entry_type ):
187
+ def validate_params (self , vlan , port , address , entry_type , namespace ):
173
188
if vlan is not None :
174
189
if not vlan .isnumeric ():
175
190
print ("Error: Invalid vlan id {0}" .format (vlan ))
@@ -194,6 +209,15 @@ class FdbShow(object):
194
209
print ("Error: Invalid type {0}" . format (entry_type ))
195
210
return False
196
211
212
+ if namespace is not None :
213
+ if not multi_asic .is_multi_asic ():
214
+ print ("Error: Namespace is not supported in single asic" )
215
+ return False
216
+
217
+ if namespace not in multi_asic .get_namespace_list ():
218
+ print ("Error: Invalid namespace {0}" .format (namespace ))
219
+ return False
220
+
197
221
return True
198
222
199
223
def main ():
@@ -205,11 +229,12 @@ def main():
205
229
parser .add_argument ('-a' , '--address' , type = str , help = 'FDB display based on specific mac address' , default = None )
206
230
parser .add_argument ('-t' , '--type' , type = str , help = 'FDB display of specific type of mac address' , default = None )
207
231
parser .add_argument ('-c' , '--count' , action = 'store_true' , help = 'FDB display count of mac address' )
232
+ parser .add_argument ('-n' , '--namespace' , type = str , help = 'Namespace name or all' , default = None )
208
233
args = parser .parse_args ()
209
234
210
235
try :
211
- fdb = FdbShow ()
212
- if not fdb .validate_params (args .vlan , args .port , args .address , args .type ):
236
+ fdb = FdbShow (namespace = args . namespace )
237
+ if not fdb .validate_params (args .vlan , args .port , args .address , args .type , args . namespace ):
213
238
sys .exit (1 )
214
239
215
240
fdb .display (args .vlan , args .port , args .address , args .type , args .count )
0 commit comments