forked from sonic-net/sonic-snmpagent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
713 lines (582 loc) · 24.6 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
import pprint
import re
import os
from swsssdk import SonicV2Connector
from swsssdk import SonicDBConfig
from swsssdk import port_util
from swsssdk.port_util import get_index, get_index_from_str
from ax_interface.mib import MIBUpdater
from ax_interface.util import oid2tuple
from sonic_ax_impl import logger
COUNTERS_PORT_NAME_MAP = b'COUNTERS_PORT_NAME_MAP'
COUNTERS_QUEUE_NAME_MAP = b'COUNTERS_QUEUE_NAME_MAP'
LAG_TABLE = b'LAG_TABLE'
LAG_MEMBER_TABLE = b'LAG_MEMBER_TABLE'
LOC_CHASSIS_TABLE = b'LLDP_LOC_CHASSIS'
APPL_DB = 'APPL_DB'
ASIC_DB = 'ASIC_DB'
COUNTERS_DB = 'COUNTERS_DB'
CONFIG_DB = 'CONFIG_DB'
STATE_DB = 'STATE_DB'
SNMP_OVERLAY_DB = 'SNMP_OVERLAY_DB'
TABLE_NAME_SEPARATOR_COLON = ':'
TABLE_NAME_SEPARATOR_VBAR = '|'
# This is used in both rfc2737 and rfc3433
SENSOR_PART_ID_MAP = {
"temperature": 1,
"voltage": 2,
"rx1power": 11,
"rx2power": 21,
"rx3power": 31,
"rx4power": 41,
"tx1bias": 12,
"tx2bias": 22,
"tx3bias": 32,
"tx4bias": 42,
"tx1power": 13,
"tx2power": 23,
"tx3power": 33,
"tx4power": 43,
}
RIF_COUNTERS_AGGR_MAP = {
b"SAI_PORT_STAT_IF_IN_OCTETS": b"SAI_ROUTER_INTERFACE_STAT_IN_OCTETS",
b"SAI_PORT_STAT_IF_IN_UCAST_PKTS": b"SAI_ROUTER_INTERFACE_STAT_IN_PACKETS",
b"SAI_PORT_STAT_IF_IN_ERRORS": b"SAI_ROUTER_INTERFACE_STAT_IN_ERROR_PACKETS",
b"SAI_PORT_STAT_IF_OUT_OCTETS": b"SAI_ROUTER_INTERFACE_STAT_OUT_OCTETS",
b"SAI_PORT_STAT_IF_OUT_UCAST_PKTS": b"SAI_ROUTER_INTERFACE_STAT_OUT_PACKETS",
b"SAI_PORT_STAT_IF_OUT_ERRORS": b"SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_PACKETS"
}
RIF_DROPS_AGGR_MAP = {
b"SAI_PORT_STAT_IF_IN_ERRORS": b"SAI_ROUTER_INTERFACE_STAT_IN_ERROR_PACKETS",
b"SAI_PORT_STAT_IF_OUT_ERRORS": b"SAI_ROUTER_INTERFACE_STAT_OUT_ERROR_PACKETS"
}
# IfIndex to OID multiplier for transceiver
IFINDEX_SUB_ID_MULTIPLIER = 1000
redis_kwargs = {'unix_socket_path': '/var/run/redis/redis.sock'}
def chassis_info_table(chassis_name):
"""
:param: chassis_name: chassis name
:return: chassis info entry for this chassis
"""
return "CHASSIS_INFO" + TABLE_NAME_SEPARATOR_VBAR + chassis_name
def psu_info_table(psu_name):
"""
:param: psu_name: psu name
:return: psu info entry for this psu
"""
return "PSU_INFO" + TABLE_NAME_SEPARATOR_VBAR + psu_name
def counter_table(sai_id):
"""
:param if_name: given sai_id to cast.
:return: COUNTERS table key.
"""
return b'COUNTERS:oid:0x' + sai_id
def queue_table(sai_id):
"""
:param sai_id: given sai_id to cast.
:return: COUNTERS table key.
"""
return b'COUNTERS:' + sai_id
def queue_key(port_index, queue_index):
return str(port_index) + ':' + str(queue_index)
def get_port_index_from_queue_key(queue_key):
return int(queue_key.split(':')[0])
def transceiver_info_table(port_name):
"""
:param: port_name: port name
:return: transceiver info entry for this port
"""
return "TRANSCEIVER_INFO" + TABLE_NAME_SEPARATOR_VBAR + port_name
def transceiver_dom_table(port_name):
"""
:param: port_name: port name
:return: transceiver dom entry for this port
"""
return "TRANSCEIVER_DOM_SENSOR" + TABLE_NAME_SEPARATOR_VBAR + port_name
def lldp_entry_table(if_name):
"""
:param if_name: given interface to cast.
:return: LLDP_ENTRY_TABLE key.
"""
return b'LLDP_ENTRY_TABLE:' + if_name
def if_entry_table(if_name):
"""
:param if_name: given interface to cast.
:return: PORT_TABLE key.
"""
return b'PORT_TABLE:' + if_name
def vlan_entry_table(if_name):
"""
:param if_name: given interface to cast.
:return: VLAN_TABLE key.
"""
return b'VLAN_TABLE:' + if_name
def lag_entry_table(lag_name):
"""
:param lag_name: given lag to cast.
:return: LAG_TABLE key.
"""
return b'LAG_TABLE:' + lag_name
def mgmt_if_entry_table(if_name):
"""
:param if_name: given interface to cast
:return: MGMT_PORT_TABLE key
"""
return b'MGMT_PORT|' + if_name
def mgmt_if_entry_table_state_db(if_name):
"""
:param if_name: given interface to cast
:return: MGMT_PORT_TABLE key
"""
return b'MGMT_PORT_TABLE|' + if_name
def config(**kwargs):
global redis_kwargs
redis_kwargs = {k:v for (k,v) in kwargs.items() if k in ['unix_socket_path', 'host', 'port']}
def init_db():
"""
Connects to DB
:return: db_conn
"""
# SyncD database connector. THIS MUST BE INITIALIZED ON A PER-THREAD BASIS.
# Redis PubSub objects (such as those within swsssdk) are NOT thread-safe.
db_conn = SonicV2Connector(**redis_kwargs)
return db_conn
def init_mgmt_interface_tables(db_conn):
"""
Initializes interface maps for mgmt ports
:param db_conn: db connector
:return: tuple of mgmt name to oid map and mgmt name to alias map
"""
db_conn.connect(CONFIG_DB)
db_conn.connect(STATE_DB)
mgmt_ports_keys = db_conn.keys(CONFIG_DB, mgmt_if_entry_table(b'*'))
if not mgmt_ports_keys:
logger.debug('No managment ports found in {}'.format(mgmt_if_entry_table(b'')))
return {}, {}
mgmt_ports = [key.split(mgmt_if_entry_table(b''))[-1] for key in mgmt_ports_keys]
oid_name_map = {get_index(mgmt_name): mgmt_name for mgmt_name in mgmt_ports}
logger.debug('Managment port map:\n' + pprint.pformat(oid_name_map, indent=2))
if_alias_map = dict()
for if_name in oid_name_map.values():
if_entry = db_conn.get_all(CONFIG_DB, mgmt_if_entry_table(if_name), blocking=True)
if_alias_map[if_name] = if_entry.get(b'alias', if_name)
logger.debug("Management alias map:\n" + pprint.pformat(if_alias_map, indent=2))
return oid_name_map, if_alias_map
def init_sync_d_interface_tables(db_conn):
"""
Initializes interface maps for SyncD-connected MIB(s).
:return: tuple(if_name_map, if_id_map, oid_map, if_alias_map)
"""
# Make sure we're connected to COUNTERS_DB
db_conn.connect(COUNTERS_DB)
# { if_name (SONiC) -> sai_id }
# ex: { "Ethernet76" : "1000000000023" }
if_name_map, if_id_map = port_util.get_interface_oid_map(db_conn)
if_name_map = {if_name: sai_id for if_name, sai_id in if_name_map.items() if \
(re.match(port_util.SONIC_ETHERNET_RE_PATTERN, if_name.decode()) or \
re.match(port_util.SONIC_ETHERNET_BP_RE_PATTERN, if_name.decode()))}
if_id_map = {sai_id: if_name for sai_id, if_name in if_id_map.items() if \
(re.match(port_util.SONIC_ETHERNET_RE_PATTERN, if_name.decode()) or \
re.match(port_util.SONIC_ETHERNET_BP_RE_PATTERN, if_name.decode()))}
logger.debug("Port name map:\n" + pprint.pformat(if_name_map, indent=2))
logger.debug("Interface name map:\n" + pprint.pformat(if_id_map, indent=2))
# { OID -> sai_id }
oid_sai_map = {get_index(if_name): sai_id for if_name, sai_id in if_name_map.items()
# only map the interface if it's a style understood to be a SONiC interface.
if get_index(if_name) is not None}
logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2))
# { OID -> if_name (SONiC) }
oid_name_map = {get_index(if_name): if_name for if_name in if_name_map
# only map the interface if it's a style understood to be a SONiC interface.
if get_index(if_name) is not None}
logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2))
# SyncD consistency checks.
if not oid_sai_map:
# In the event no interface exists that follows the SONiC pattern, no OIDs are able to be registered.
# A RuntimeError here will prevent the 'main' module from loading. (This is desirable.)
message = "No interfaces found matching pattern '{}'. SyncD database is incoherent." \
.format(port_util.SONIC_ETHERNET_RE_PATTERN)
logger.error(message)
raise RuntimeError(message)
elif len(if_id_map) < len(if_name_map) or len(oid_sai_map) < len(if_name_map):
# a length mismatch indicates a bad interface name
logger.warning("SyncD database contains incoherent interface names. Interfaces must match pattern '{}'"
.format(port_util.SONIC_ETHERNET_RE_PATTERN))
logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2))
db_conn.connect(APPL_DB)
if_alias_map = dict()
for if_name in if_name_map:
if_entry = db_conn.get_all(APPL_DB, if_entry_table(if_name), blocking=True)
if_alias_map[if_name] = if_entry.get(b'alias', if_name)
logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2))
return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map
def init_sync_d_rif_tables(db_conn):
"""
Initializes map of RIF SAI oids to port SAI oid.
:return: dict
"""
rif_port_map = port_util.get_rif_port_map(db_conn)
if not rif_port_map:
return {}, {}
port_rif_map = {port: rif for rif, port in rif_port_map.items()}
logger.debug("Rif port map:\n" + pprint.pformat(rif_port_map, indent=2))
return rif_port_map, port_rif_map
def init_sync_d_vlan_tables(db_conn):
"""
Initializes vlan interface maps for SyncD-connected MIB(s).
:return: tuple(vlan_name_map, oid_sai_map, oid_name_map)
"""
vlan_name_map = port_util.get_vlan_interface_oid_map(db_conn)
logger.debug("Vlan oid map:\n" + pprint.pformat(vlan_name_map, indent=2))
# { OID -> sai_id }
oid_sai_map = {get_index(if_name): sai_id for sai_id, if_name in vlan_name_map.items()
# only map the interface if it's a style understood to be a SONiC interface.
if get_index(if_name) is not None}
logger.debug("OID sai map:\n" + pprint.pformat(oid_sai_map, indent=2))
# { OID -> if_name (SONiC) }
oid_name_map = {get_index(if_name): if_name for sai_id, if_name in vlan_name_map.items()
# only map the interface if it's a style understood to be a SONiC interface.
if get_index(if_name) is not None}
logger.debug("OID name map:\n" + pprint.pformat(oid_name_map, indent=2))
return vlan_name_map, oid_sai_map, oid_name_map
def init_sync_d_lag_tables(db_conn):
"""
Helper method. Connects to and initializes LAG interface maps for SyncD-connected MIB(s).
:param db_conn: database connector
:return: tuple(lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map)
"""
# { lag_name (SONiC) -> [ lag_members (if_name) ] }
# ex: { "PortChannel0" : [ "Ethernet0", "Ethernet4" ] }
lag_name_if_name_map = {}
# { if_name (SONiC) -> lag_name }
# ex: { "Ethernet0" : "PortChannel0" }
if_name_lag_name_map = {}
# { OID -> lag_name (SONiC) }
oid_lag_name_map = {}
# { lag_name (SONiC) -> lag_oid (SAI) }
lag_sai_map = {}
db_conn.connect(APPL_DB)
lag_entries = db_conn.keys(APPL_DB, b"LAG_TABLE:*")
if not lag_entries:
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map, lag_sai_map
db_conn.connect(COUNTERS_DB)
lag_sai_map = db_conn.get_all(COUNTERS_DB, b"COUNTERS_LAG_NAME_MAP")
lag_sai_map = {name: sai_id.lstrip(b"oid:0x") for name, sai_id in lag_sai_map.items()}
for lag_entry in lag_entries:
lag_name = lag_entry[len(b"LAG_TABLE:"):]
lag_members = db_conn.keys(APPL_DB, b"LAG_MEMBER_TABLE:%s:*" % lag_name)
# TODO: db_conn.keys() should really return [] instead of None
if lag_members is None:
lag_members = []
def member_name_str(val, lag_name):
return val[len(b"LAG_MEMBER_TABLE:%s:" % lag_name):]
lag_member_names = [member_name_str(m, lag_name) for m in lag_members]
lag_name_if_name_map[lag_name] = lag_member_names
for lag_member_name in lag_member_names:
if_name_lag_name_map[lag_member_name] = lag_name
for if_name in lag_name_if_name_map.keys():
idx = get_index(if_name)
if idx:
oid_lag_name_map[idx] = if_name
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map, lag_sai_map
def init_sync_d_queue_tables(db_conn):
"""
Initializes queue maps for SyncD-connected MIB(s).
:return: tuple(port_queues_map, queue_stat_map)
"""
# Make sure we're connected to COUNTERS_DB
db_conn.connect(COUNTERS_DB)
# { Port index : Queue index (SONiC) -> sai_id }
# ex: { "1:2" : "1000000000023" }
queue_name_map = db_conn.get_all(COUNTERS_DB, COUNTERS_QUEUE_NAME_MAP, blocking=True)
logger.debug("Queue name map:\n" + pprint.pformat(queue_name_map, indent=2))
# Parse the queue_name_map and create the following maps:
# port_queues_map -> {"if_index : queue_index" : sai_oid}
# queue_stat_map -> {queue stat table name : {counter name : value}}
# port_queue_list_map -> {if_index: [sorted queue list]}
port_queues_map = {}
queue_stat_map = {}
port_queue_list_map = {}
for queue_name, sai_id in queue_name_map.items():
port_name, queue_index = queue_name.decode().split(':')
queue_index = ''.join(i for i in queue_index if i.isdigit())
port_index = get_index_from_str(port_name)
key = queue_key(port_index, queue_index)
port_queues_map[key] = sai_id
queue_stat_name = queue_table(sai_id)
queue_stat = db_conn.get_all(COUNTERS_DB, queue_stat_name, blocking=False)
if queue_stat is not None:
queue_stat_map[queue_stat_name] = queue_stat
if not port_queue_list_map.get(int(port_index)):
port_queue_list_map[int(port_index)] = [int(queue_index)]
else:
port_queue_list_map[int(port_index)].append(int(queue_index))
# SyncD consistency checks.
if not port_queues_map:
# In the event no queue exists that follows the SONiC pattern, no OIDs are able to be registered.
# A RuntimeError here will prevent the 'main' module from loading. (This is desirable.)
logger.error("No queues found in the Counter DB. SyncD database is incoherent.")
raise RuntimeError('The port_queues_map is not defined')
elif not queue_stat_map:
logger.error("No queue stat counters found in the Counter DB. SyncD database is incoherent.")
raise RuntimeError('The queue_stat_map is not defined')
for queues in port_queue_list_map.values():
queues.sort()
return port_queues_map, queue_stat_map, port_queue_list_map
def get_device_metadata(db_conn):
"""
:param db_conn: Sonic DB connector
:return: device metadata
"""
DEVICE_METADATA = "DEVICE_METADATA|localhost"
db_conn.connect(db_conn.STATE_DB)
device_metadata = db_conn.get_all(db_conn.STATE_DB, DEVICE_METADATA)
return device_metadata
def get_transceiver_sub_id(ifindex):
"""
Returns sub OID for transceiver. Sub OID is calculated as folows:
+------------+------------+
|Interface |Index |
+------------+------------+
|Ethernet[X] |X * 1000 |
+------------+------------+
()
:param ifindex: interface index
:return: sub OID of a port calculated as sub OID = {{index}} * 1000
"""
return (ifindex * IFINDEX_SUB_ID_MULTIPLIER, )
def get_transceiver_sensor_sub_id(ifindex, sensor):
"""
Returns sub OID for transceiver sensor. Sub OID is calculated as folows:
+-------------------------------------+------------------------------+
|Sensor |Index |
+-------------------------------------+------------------------------+
|RX Power for Ethernet[X]/[LANEID] |X * 1000 + LANEID * 10 + 1 |
|TX Bias for Ethernet[X]/[LANEID] |X * 1000 + LANEID * 10 + 2 |
|Temperature for Ethernet[X] |X * 1000 + 1 |
|Voltage for Ethernet[X]/[LANEID] |X * 1000 + 2 |
+-------------------------------------+------------------------------+
()
:param ifindex: interface index
:param sensor: sensor key
:return: sub OID = {{index}} * 1000 + {{lane}} * 10 + sensor id
"""
transceiver_oid, = get_transceiver_sub_id(ifindex)
return (transceiver_oid + SENSOR_PART_ID_MAP[sensor], )
def get_redis_pubsub(db_conn, db_name, pattern):
redis_client = db_conn.get_redis_client(db_name)
db = db_conn.get_dbid(db_name)
pubsub = redis_client.pubsub()
pubsub.psubscribe("__keyspace@{}__:{}".format(db, pattern))
return pubsub
class RedisOidTreeUpdater(MIBUpdater):
def __init__(self, prefix_str):
super().__init__()
self.db_conn = Namespace.init_namespace_dbs()
if prefix_str.startswith('.'):
prefix_str = prefix_str[1:]
self.prefix_str = prefix_str
def get_next(self, sub_id):
"""
:param sub_id: The 1-based sub-identifier query.
:return: the next sub id.
"""
raise NotImplementedError
def reinit_data(self):
"""
Subclass update loopback information
"""
pass
def update_data(self):
"""
Update redis (caches config)
Pulls the table references for each interface.
"""
self.oid_list = []
self.oid_map = {}
Namespace.connect_all_dbs(self.db_conn, SNMP_OVERLAY_DB)
keys = Namespace.dbs_keys_namespace(self.db_conn, SNMP_OVERLAY_DB, self.prefix_str + '*')
# TODO: fix db_conn.keys to return empty list instead of None if there is no match
if keys is None:
keys = {}
for key in keys:
db_index = keys[key]
key = key.decode()
oid = oid2tuple(key, dot_prefix=False)
self.oid_list.append(oid)
value = self.db_conn[db_index].get_all(SNMP_OVERLAY_DB, key)
if value[b'type'] in [b'COUNTER_32', b'COUNTER_64']:
self.oid_map[oid] = int(value[b'data'])
else:
raise ValueError("Invalid value type")
self.oid_list.sort()
def get_oidvalue(self, oid):
if oid not in self.oid_map:
return None
return self.oid_map[oid]
class Namespace:
@staticmethod
def init_namespace_dbs():
db_conn = []
SonicDBConfig.load_sonic_global_db_config()
for namespace in SonicDBConfig.get_ns_list():
db = SonicV2Connector(use_unix_socket_path=True, namespace=namespace)
db_conn.append(db)
return db_conn
@staticmethod
def connect_all_dbs(dbs, db_name):
for db_conn in dbs:
db_conn.connect(db_name)
@staticmethod
def dbs_keys(dbs, db_name, pattern='*'):
"""
db keys function execute on global and all namespace DBs.
"""
result_keys = []
for db_conn in dbs:
db_conn.connect(db_name)
keys = db_conn.keys(db_name, pattern)
if keys is not None:
result_keys.extend(keys)
return result_keys
@staticmethod
def dbs_keys_namespace(dbs, db_name, pattern='*'):
"""
dbs_keys_namespace function execute on global
and all namespace DBs. Provides a map of keys
and namespace(db index).
"""
result_keys = {}
for db_index in range(len(dbs)):
keys = dbs[db_index].keys(db_name, pattern)
if keys is not None:
keys_ns = dict.fromkeys(keys, db_index)
result_keys.update(keys_ns)
return result_keys
@staticmethod
def get_non_host_db_indexes(dbs):
"""
From the list of all dbs, return the list of dbs
which will have interface related tables.
For single namespace db, return the single db.
For multiple namespace dbs, return db index ofall dbs
except the host namespace db which is the first db
in the list.
"""
if len(dbs) == 1:
start_index = 0
else:
start_index = 1
return range(start_index, len(dbs))
@staticmethod
def init_namespace_sync_d_interface_tables(dbs):
if_name_map = {}
if_alias_map = {}
if_id_map = {}
oid_sai_map = {}
oid_name_map = {}
if_oid_namespace = {}
"""
all_ns_db - will have db_conn to all namespace DBs and
global db. First db in the list is global db.
Ignore first global db to get interface tables if there
are multiple namespaces.
"""
for db_index in Namespace.get_non_host_db_indexes(dbs):
if_name_map_ns, \
if_alias_map_ns, \
if_id_map_ns, \
oid_sai_map_ns, \
oid_name_map_ns = init_sync_d_interface_tables(dbs[db_index])
if_name_map.update(if_name_map_ns)
if_alias_map.update(if_alias_map_ns)
if_id_map.update(if_id_map_ns)
oid_sai_map.update(oid_sai_map_ns)
oid_name_map.update(oid_name_map_ns)
if_oid_namespace_ns = dict.fromkeys(oid_name_map_ns.keys(), db_index)
if_oid_namespace.update(if_oid_namespace_ns)
return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map, if_oid_namespace
@staticmethod
def init_namespace_sync_d_lag_tables(dbs):
lag_name_if_name_map = {}
if_name_lag_name_map = {}
oid_lag_name_map = {}
lag_sai_map = {}
oid_lag_namespace = {}
"""
all_ns_db - will have db_conn to all namespace DBs and
global db. First db in the list is global db.
Ignore first global db to get lag tables if
there are multiple namespaces.
"""
for db_index in Namespace.get_non_host_db_indexes(dbs):
lag_name_if_name_map_ns, \
if_name_lag_name_map_ns, \
oid_lag_name_map_ns, \
lag_sai_map_ns = init_sync_d_lag_tables(dbs[db_index])
lag_name_if_name_map.update(lag_name_if_name_map_ns)
if_name_lag_name_map.update(if_name_lag_name_map_ns)
oid_lag_name_map.update(oid_lag_name_map_ns)
oid_lag_namespace_ns = dict.fromkeys(oid_lag_name_map_ns.keys(), db_index)
oid_lag_namespace.update(oid_lag_namespace_ns)
lag_sai_map.update(lag_sai_map_ns)
return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map, lag_sai_map, oid_lag_namespace
@staticmethod
def init_namespace_sync_d_rif_tables(dbs):
rif_port_map = {}
port_rif_map = {}
for db_conn in Namespace.get_non_host_dbs(dbs):
rif_port_map_ns, \
port_rif_map_ns = init_sync_d_rif_tables(db_conn)
rif_port_map.update(rif_port_map_ns)
port_rif_map.update(port_rif_map_ns)
return rif_port_map, port_rif_map
@staticmethod
def init_namespace_sync_d_vlan_tables(dbs):
vlan_name_map = {}
oid_sai_map = {}
oid_name_map = {}
for db_conn in Namespace.get_non_host_dbs(dbs):
vlan_name_map_ns, \
oid_sai_map_ns, \
oid_name_map_ns = init_sync_d_vlan_tables(db_conn)
vlan_name_map.update(vlan_name_map_ns)
oid_sai_map.update(oid_sai_map_ns)
oid_name_map.update(oid_name_map_ns)
return vlan_name_map, oid_sai_map, oid_name_map
@staticmethod
def init_namespace_sync_d_queue_tables(dbs):
port_queues_map = {}
queue_stat_map = {}
port_queue_list_map = {}
"""
all_ns_db - will have db_conn to all namespace DBs and
global db. First db in the list is global db.
Ignore first global db to get queue tables if there
are multiple namespaces.
"""
for db_index in Namespace.get_non_host_db_indexes(dbs):
port_queues_map_ns, \
queue_stat_map_ns, \
port_queue_list_map_ns = init_sync_d_queue_tables(dbs[db_index])
port_queues_map.update(port_queues_map_ns)
queue_stat_map.update(queue_stat_map_ns)
port_queue_list_map.update(port_queue_list_map_ns)
return port_queues_map, queue_stat_map, port_queue_list_map
@staticmethod
def dbs_get_bridge_port_map(dbs, db_name):
"""
get_bridge_port_map from all namespace DBs
"""
if_br_oid_map = {}
for db_index in Namespace.get_non_host_db_indexes(dbs):
if_br_oid_map_ns = port_util.get_bridge_port_map(dbs[db_index])
if_br_oid_map.update(if_br_oid_map_ns)
return if_br_oid_map
@staticmethod
def dbs_get_vlan_id_from_bvid(dbs, bvid):
for db_index in Namespace.get_non_host_db_indexes(dbs):
dbs[db_index].connect('ASIC_DB')
vlan_obj = dbs[db_index].keys('ASIC_DB', "ASIC_STATE:SAI_OBJECT_TYPE_VLAN:" + bvid)
if vlan_obj is not None:
return port_util.get_vlan_id_from_bvid(dbs[db_index], bvid)