@@ -12,44 +12,46 @@ from natsort import natsorted
12
12
13
13
14
14
PORT_STATUS_TABLE_PREFIX = "PORT_TABLE:"
15
+ PORT_TRANSCEIVER_TABLE_PREFIX = "TRANSCEIVER_INFO|"
15
16
PORT_LANES_STATUS = "lanes"
16
17
PORT_ALIAS = "alias"
17
18
PORT_OPER_STATUS = "oper_status"
18
19
PORT_ADMIN_STATUS = "admin_status"
19
20
PORT_SPEED = "speed"
20
21
PORT_MTU_STATUS = "mtu"
21
22
PORT_DESCRIPTION = "description"
23
+ PORT_OPTICS_TYPE = "type"
22
24
23
25
24
- def db_connect ():
25
- db = swsssdk .SonicV2Connector (host = '127.0.0.1' )
26
- if db == None :
26
+ def db_connect_appl ():
27
+ appl_db = swsssdk .SonicV2Connector (host = '127.0.0.1' )
28
+ if appl_db is None :
27
29
return None
28
30
29
- db .connect (db .APPL_DB )
31
+ appl_db .connect (appl_db .APPL_DB )
30
32
31
- return db
33
+ return appl_db
32
34
33
35
34
- def db_keys_get ( db , intf_name ):
36
+ def appl_db_keys_get ( appl_db , intf_name ):
35
37
36
- if intf_name == None :
37
- db_keys = db .keys (db .APPL_DB , "PORT_TABLE:*" )
38
+ if intf_name is None :
39
+ appl_db_keys = appl_db .keys (appl_db .APPL_DB , "PORT_TABLE:*" )
38
40
elif intf_name .startswith ('Ethernet' ):
39
- db_keys = db .keys (db .APPL_DB , "PORT_TABLE:%s" % intf_name )
41
+ appl_db_keys = db .keys (appl_db .APPL_DB , "PORT_TABLE:%s" % intf_name )
40
42
else :
41
43
return None
42
44
43
- return db_keys
45
+ return appl_db_keys
44
46
45
47
46
- def db_port_status_get ( db , intf_name , status_type ):
48
+ def appl_db_port_status_get ( appl_db , intf_name , status_type ):
47
49
"""
48
50
Get the port status
49
51
"""
50
52
51
53
full_table_id = PORT_STATUS_TABLE_PREFIX + intf_name
52
- status = db .get (db .APPL_DB , full_table_id , status_type )
54
+ status = appl_db .get (appl_db .APPL_DB , full_table_id , status_type )
53
55
if status is None :
54
56
return "N/A"
55
57
@@ -59,13 +61,34 @@ def db_port_status_get(db, intf_name, status_type):
59
61
return status
60
62
61
63
64
+ def db_connect_state ():
65
+ """
66
+ Connect to REDIS STATE DB and get optics info
67
+ """
68
+ state_db = swsssdk .SonicV2Connector (host = '127.0.0.1' )
69
+ if state_db is None :
70
+ return None
71
+ state_db .connect (state_db .STATE_DB , False ) # Make one attempt only
72
+ return state_db
73
+
74
+
75
+ def state_db_port_optics_get (state_db , intf_name , type ):
76
+ """
77
+ Get optic type info for port
78
+ """
79
+ full_table_id = PORT_TRANSCEIVER_TABLE_PREFIX + intf_name
80
+ optics_type = state_db .get (state_db .STATE_DB , full_table_id , type )
81
+ if optics_type is None :
82
+ return "N/A"
83
+ return optics_type
84
+
62
85
# ========================== interface-status logic ==========================
63
86
64
- header_stat = ['Interface' , 'Lanes' , 'Speed' , 'MTU' , 'Alias' , 'Oper' , 'Admin' ]
87
+ header_stat = ['Interface' , 'Lanes' , 'Speed' , 'MTU' , 'Alias' , 'Oper' , 'Admin' , 'Type' ]
65
88
66
89
class IntfStatus (object ):
67
90
68
- def display_intf_status (self , db_keys ):
91
+ def display_intf_status (self , appl_db_keys ):
69
92
"""
70
93
Generate interface-status output
71
94
"""
@@ -78,16 +101,17 @@ class IntfStatus(object):
78
101
# Iterate through all the keys and append port's associated state to
79
102
# the result table.
80
103
#
81
- for i in db_keys :
104
+ for i in appl_db_keys :
82
105
key = re .split (':' , i , maxsplit = 1 )[- 1 ].strip ()
83
106
if key and key .startswith ('Ethernet' ):
84
107
table .append ((key ,
85
- db_port_status_get (self .db , key , PORT_LANES_STATUS ),
86
- db_port_status_get (self .db , key , PORT_SPEED ),
87
- db_port_status_get (self .db , key , PORT_MTU_STATUS ),
88
- db_port_status_get (self .db , key , PORT_ALIAS ),
89
- db_port_status_get (self .db , key , PORT_OPER_STATUS ),
90
- db_port_status_get (self .db , key , PORT_ADMIN_STATUS )))
108
+ appl_db_port_status_get (self .appl_db , key , PORT_LANES_STATUS ),
109
+ appl_db_port_status_get (self .appl_db , key , PORT_SPEED ),
110
+ appl_db_port_status_get (self .appl_db , key , PORT_MTU_STATUS ),
111
+ appl_db_port_status_get (self .appl_db , key , PORT_ALIAS ),
112
+ appl_db_port_status_get (self .appl_db , key , PORT_OPER_STATUS ),
113
+ appl_db_port_status_get (self .appl_db , key , PORT_ADMIN_STATUS ),
114
+ state_db_port_optics_get (self .state_db , key , PORT_OPTICS_TYPE )))
91
115
92
116
# Sorting and tabulating the result table.
93
117
sorted_table = natsorted (table )
@@ -96,15 +120,17 @@ class IntfStatus(object):
96
120
97
121
def __init__ (self , intf_name ):
98
122
99
- self .db = db_connect ()
100
- if self .db == None :
123
+ self .appl_db = db_connect_appl ()
124
+ self .state_db = db_connect_state ()
125
+ if self .appl_db is None :
101
126
return
102
-
103
- db_keys = db_keys_get (self .db , intf_name )
104
- if db_keys == None :
127
+ if self .state_db is None :
128
+ return
129
+ appl_db_keys = appl_db_keys_get (self .appl_db , intf_name )
130
+ if appl_db_keys is None :
105
131
return
132
+ self .display_intf_status (appl_db_keys )
106
133
107
- self .display_intf_status (db_keys )
108
134
109
135
110
136
# ========================== interface-description logic ==========================
@@ -115,7 +141,7 @@ header_desc = ['Interface', 'Oper', 'Admin', 'Alias', 'Description']
115
141
116
142
class IntfDescription (object ):
117
143
118
- def display_intf_description (self , db_keys ):
144
+ def display_intf_description (self , appl_db_keys ):
119
145
"""
120
146
Generate interface-description output
121
147
"""
@@ -128,30 +154,30 @@ class IntfDescription(object):
128
154
# Iterate through all the keys and append port's associated state to
129
155
# the result table.
130
156
#
131
- for i in db_keys :
157
+ for i in appl_db_keys :
132
158
key = re .split (':' , i , maxsplit = 1 )[- 1 ].strip ()
133
159
if key and key .startswith ('Ethernet' ):
134
160
table .append ((key ,
135
- db_port_status_get (self .db , key , PORT_OPER_STATUS ),
136
- db_port_status_get (self .db , key , PORT_ADMIN_STATUS ),
137
- db_port_status_get (self .db , key , PORT_ALIAS ),
138
- db_port_status_get (self .db , key , PORT_DESCRIPTION )))
161
+ appl_db_port_status_get (self .appl_db , key , PORT_OPER_STATUS ),
162
+ appl_db_port_status_get (self .appl_db , key , PORT_ADMIN_STATUS ),
163
+ appl_db_port_status_get (self .appl_db , key , PORT_ALIAS ),
164
+ appl_db_port_status_get (self .appl_db , key , PORT_DESCRIPTION )))
139
165
140
166
# Sorting and tabulating the result table.
141
167
sorted_table = natsorted (table )
142
168
print tabulate (sorted_table , header_desc , tablefmt = "simple" , stralign = 'right' )
143
169
144
170
def __init__ (self , intf_name ):
145
171
146
- self .db = db_connect ()
147
- if self .db == None :
172
+ self .appl_db = db_connect_appl ()
173
+ if self .appl_db is None :
148
174
return
149
175
150
- db_keys = db_keys_get (self .db , intf_name )
151
- if db_keys == None :
176
+ appl_db_keys = appl_db_keys_get (self .appl_db , intf_name )
177
+ if appl_db_keys is None :
152
178
return
153
179
154
- self .display_intf_description (db_keys )
180
+ self .display_intf_description (appl_db_keys )
155
181
156
182
157
183
0 commit comments