-
Notifications
You must be signed in to change notification settings - Fork 116
/
rfc2737.py
1134 lines (927 loc) · 42.6 KB
/
rfc2737.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
MIB implementation defined in RFC 2737
"""
from enum import Enum, unique
from bisect import bisect_right, insort_right
from sonic_py_common import port_util
from ax_interface import MIBMeta, MIBUpdater, ValueType, SubtreeMIBEntry
from sonic_ax_impl import mibs
from sonic_ax_impl.mibs import Namespace
from .physical_entity_sub_oid_generator import CHASSIS_SUB_ID
from .physical_entity_sub_oid_generator import CHASSIS_MGMT_SUB_ID
from .physical_entity_sub_oid_generator import get_chassis_thermal_sub_id
from .physical_entity_sub_oid_generator import get_fan_sub_id
from .physical_entity_sub_oid_generator import get_fan_drawer_sub_id
from .physical_entity_sub_oid_generator import get_fan_tachometers_sub_id
from .physical_entity_sub_oid_generator import get_psu_sub_id
from .physical_entity_sub_oid_generator import get_psu_sensor_sub_id
from .physical_entity_sub_oid_generator import get_transceiver_sub_id
from .physical_entity_sub_oid_generator import get_transceiver_sensor_sub_id
from .sensor_data import TransceiverSensorData
@unique
class PhysicalClass(int, Enum):
"""
Physical classes defined in RFC 2737.
"""
OTHER = 1
UNKNOWN = 2
CHASSIS = 3
BACKPLANE = 4
CONTAINER = 5
POWERSUPPLY = 6
FAN = 7
SENSOR = 8
MODULE = 9
PORT = 10
STACK = 11
CPU = 12 # Added in RFC 6933
@unique
class FanInfoDB(str, Enum):
"""
FAN info keys
"""
MODEL = 'model'
PRESENCE = 'presence'
SERIAL = 'serial'
SPEED = 'speed'
REPLACEABLE = 'is_replaceable'
@unique
class FanDrawerInfoDB(str, Enum):
"""
FAN drawer info keys
"""
MODEL = 'model'
PRESENCE = 'presence'
SERIAL = 'serial'
REPLACEABLE = 'is_replaceable'
@unique
class PhysicalRelationInfoDB(str, Enum):
"""
Physical relation info keys
"""
POSITION_IN_PARENT = 'position_in_parent'
PARENT_NAME = 'parent_name'
@unique
class PsuInfoDB(str, Enum):
"""
PSU info keys
"""
MODEL = 'model'
SERIAL = 'serial'
CURRENT = 'current'
POWER = 'power'
PRESENCE = 'presence'
VOLTAGE = 'voltage'
TEMPERATURE = 'temp'
REPLACEABLE = 'is_replaceable'
@unique
class XcvrInfoDB(str, Enum):
"""
Transceiver info keys
"""
TYPE = "type"
VENDOR_REVISION = "vendor_rev"
SERIAL_NUMBER = "serial"
MANUFACTURE_NAME = "manufacturer"
MODEL_NAME = "model"
REPLACEABLE = 'is_replaceable'
@unique
class ThermalInfoDB(str, Enum):
"""
FAN drawer info keys
"""
TEMPERATURE = 'temperature'
REPLACEABLE = 'is_replaceable'
# Map used to generate PSU sensor description
PSU_SENSOR_NAME_MAP = {
'temperature': 'Temperature',
'power' : 'Power',
'current' : 'Current',
'voltage' : 'Voltage'
}
PSU_SENSOR_POSITION_MAP = {
'temperature': 1,
'power' : 2,
'current' : 3,
'voltage' : 4
}
NOT_AVAILABLE = 'N/A'
RJ45_PORT_TYPE = 'RJ45'
def is_null_str(value):
"""
Indicate if a string value is null
:param value: input string value
:return: True is string value is empty or equal to 'N/A' or 'None'
"""
if not isinstance(value, str) or value == NOT_AVAILABLE or value == 'None':
return True
return False
def get_db_data(info_dict, enum_type):
"""
:param info_dict: db info dict
:param enum_type: db field enum
:return: tuple of fields values defined in enum_type;
Empty string if field not in info_dict
"""
return (info_dict.get(field.value, "")
for field in enum_type)
def get_transceiver_description(sfp_type, if_alias):
"""
:param sfp_type: SFP type of transceiver
:param if_alias: Port alias name
:return: Transceiver decsription
"""
if not if_alias:
description = "{}".format(sfp_type)
else:
description = "{} for {}".format(sfp_type, if_alias)
return description
def get_transceiver_sensor_description(name, lane_number, if_alias):
"""
:param name: sensor name
:param lane_number: lane number of this sensor
:param if_alias: interface alias
:return: description string about sensor
"""
if lane_number == 0:
port_name = if_alias
else:
port_name = "{}/{}".format(if_alias, lane_number)
return "DOM {} Sensor for {}".format(name, port_name)
class Callback(object):
"""
Utility class to store a callable and its arguments for future invoke
"""
def __init__(self, function, args):
# A callable
self.function = function
# Arguments for the given callable
self.args = args
def invoke(self):
"""
Invoke the callable
"""
self.function(*self.args)
class PhysicalTableMIBUpdater(MIBUpdater):
"""
Updater class for physical table MIB
"""
# Chassis key name in CHASSIS_INFO table
CHASSIS_NAME = 'chassis 1'
# Indicate that an entity is replaceable, according to RFC2737
REPLACEABLE = 1
# Indicate that an entity is not replaceable, according to RFC2737
NOT_REPLACEABLE = 2
# A list of physical entity updater, @decorator physical_entity_updater can register physical entity updater types
# to this list, and these types will be used for create instance for each type
physical_entity_updater_types = []
def __init__(self):
super().__init__()
self.statedb = Namespace.init_namespace_dbs()
Namespace.connect_all_dbs(self.statedb, mibs.STATE_DB)
# List of available sub OIDs.
self.physical_entities = []
# Map sub ID to its data.
self.physical_classes_map = {}
self.physical_description_map = {}
self.physical_name_map = {}
self.physical_hw_version_map = {}
self.physical_serial_number_map = {}
self.physical_mfg_name_map = {}
self.physical_model_name_map = {}
self.physical_contained_in_map = {}
self.physical_parent_relative_pos_map = {}
self.physical_fru_map = {}
# Map physical entity name and oid. According to RFC2737, entPhysicalContainedIn is indicates the value of
# entPhysicalIndex for the physical entity which 'contains' this physical entity. However, there is
# only parent entity name in database, so need a way to get physical entity oid by name.
self.physical_name_to_oid_map = {}
# Map physical entity name that need resolve. The key is the entity name, value is a list of Callback objects
# that will be called when the entity name is added to self.physical_name_to_oid_map.
# It's possible the parent name and parent oid are still not in self.physical_name_to_oid_map when child entity
# update cache. In that case, the child entity might not be able to calculate its sub id and cannot update its
# cache or do future operation. So this dictionary provides a way to store such operations for future executes.
self.pending_resolve_parent_name_map = {}
# physical entity updaters
self.physical_entity_updaters = self.create_physical_entity_updaters()
@classmethod
def register_entity_updater_type(cls, object_type):
"""
Register physical entity updater
:param object_type: entity updater type
"""
cls.physical_entity_updater_types.append(object_type)
def create_physical_entity_updaters(self):
"""
Create all physical entity updater instances
:return: a list of physical entity updater instance
"""
return [creator(self) for creator in PhysicalTableMIBUpdater.physical_entity_updater_types]
def reinit_connection(self):
Namespace.connect_all_dbs(self.statedb, mibs.STATE_DB)
def reinit_data(self):
"""
Re-initialize all data.
"""
# reinit cache
self.physical_classes_map = {}
self.physical_description_map = {}
self.physical_name_map = {}
self.physical_hw_version_map = {}
self.physical_serial_number_map = {}
self.physical_mfg_name_map = {}
self.physical_model_name_map = {}
self.physical_contained_in_map = {}
self.physical_parent_relative_pos_map = {}
self.physical_fru_map = {}
self.physical_name_to_oid_map = {}
self.pending_resolve_parent_name_map = {}
device_metadata = mibs.get_device_metadata(self.statedb[0])
chassis_sub_id = (CHASSIS_SUB_ID, )
self.physical_entities = [chassis_sub_id]
self.physical_name_to_oid_map[self.CHASSIS_NAME] = chassis_sub_id
if not device_metadata or not device_metadata.get("chassis_serial_number"):
chassis_serial_number = ""
else:
chassis_serial_number = device_metadata["chassis_serial_number"]
self.physical_classes_map[chassis_sub_id] = PhysicalClass.CHASSIS
self.physical_serial_number_map[chassis_sub_id] = chassis_serial_number
self.physical_name_map[chassis_sub_id] = self.CHASSIS_NAME
self.physical_description_map[chassis_sub_id] = self.CHASSIS_NAME
self.physical_contained_in_map[chassis_sub_id] = 0
self.physical_fru_map[chassis_sub_id] = self.NOT_REPLACEABLE
# Add a chassis mgmt node
chassis_mgmt_sub_id = (CHASSIS_MGMT_SUB_ID,)
self.add_sub_id(chassis_mgmt_sub_id)
self.physical_classes_map[chassis_mgmt_sub_id] = PhysicalClass.CPU
self.physical_contained_in_map[chassis_mgmt_sub_id] = CHASSIS_SUB_ID
self.physical_parent_relative_pos_map[chassis_mgmt_sub_id] = 1
name = 'MGMT'
self.physical_description_map[chassis_mgmt_sub_id] = name
self.physical_name_map[chassis_mgmt_sub_id] = name
self.physical_fru_map[chassis_mgmt_sub_id] = self.NOT_REPLACEABLE
exceptions = []
has_runtime_err = False
# Catch exception in the iteration
# This makes sure if any exception is raised in the mid of loop
# every updater's reinit_data function will be always called
# So that the redis subscriptions always get chance to be cleaned,
# Otherwise if the exception never recover,
# the redis subscription keeps increasing but never got consumed,
# this causes redis memory leak.
for updater in self.physical_entity_updaters:
try:
updater.reinit_data()
except BaseException as e:
if isinstance(e, RuntimeError):
has_runtime_err = True
# Log traceback so that we know the original error details
mibs.logger.error(e, exc_info=True)
exceptions.append(e)
# The RuntimeError will be considered as Redis connection error
# And will trigger re-init connection, if the exceptions contain any RuntimeError
# We raise runtime error
if exceptions:
if has_runtime_err:
raise RuntimeError(exceptions)
else:
raise Exception(exceptions)
def update_data(self):
# This code is not executed in unit test, since mockredis
# does not support pubsub
for i in range(len(self.statedb)):
for updater in self.physical_entity_updaters:
updater.update_data(i, self.statedb[i])
def add_sub_id(self, sub_id):
insort_right(self.physical_entities, sub_id)
def remove_sub_ids(self, remove_sub_ids):
"""
Remove all data related to given sub id list
:param remove_sub_ids: a list of sub ids that will be removed
"""
for sub_id in remove_sub_ids:
if not sub_id:
continue
if sub_id in self.physical_entities:
self.physical_entities.remove(sub_id)
if sub_id in self.physical_classes_map:
self.physical_classes_map.pop(sub_id)
if sub_id in self.physical_name_map:
name = self.physical_name_map[sub_id]
if name in self.physical_name_to_oid_map:
self.physical_name_to_oid_map.pop(name)
if name in self.pending_resolve_parent_name_map:
self.pending_resolve_parent_name_map.pop(name)
self.physical_name_map.pop(sub_id)
if sub_id in self.physical_hw_version_map:
self.physical_hw_version_map.pop(sub_id)
if sub_id in self.physical_serial_number_map:
self.physical_serial_number_map.pop(sub_id)
if sub_id in self.physical_mfg_name_map:
self.physical_mfg_name_map.pop(sub_id)
if sub_id in self.physical_model_name_map:
self.physical_model_name_map.pop(sub_id)
if sub_id in self.physical_contained_in_map:
self.physical_contained_in_map.pop(sub_id)
if sub_id in self.physical_parent_relative_pos_map:
self.physical_parent_relative_pos_map.pop(sub_id)
if sub_id in self.physical_fru_map:
self.physical_fru_map.pop(sub_id)
def add_pending_entity_name_callback(self, name, function, args):
"""
Store a callback for those entity whose parent entity name has not been resolved yet
:param name: parent entity name
:param function: a callable
:param args: arguments for the callable
"""
if name in self.pending_resolve_parent_name_map:
self.pending_resolve_parent_name_map[name].append(Callback(function, args))
else:
self.pending_resolve_parent_name_map[name] = [Callback(function, args)]
def update_name_to_oid_map(self, name, oid):
"""
Update entity name to oid map. If the given name is in self.pending_resolve_parent_name_map, update physical
contained in information accordingly.
:param name: entity name
:param oid: entity oid
"""
self.physical_name_to_oid_map[name] = oid
if name in self.pending_resolve_parent_name_map:
for callback in self.pending_resolve_parent_name_map[name]:
callback.invoke()
self.pending_resolve_parent_name_map.pop(name)
def set_phy_class(self, sub_id, phy_class):
"""
:param sub_id: sub OID
:param phy_class: physical entity class
"""
self.physical_classes_map[sub_id] = phy_class
def set_phy_parent_relative_pos(self, sub_id, pos):
"""
:param sub_id: sub OID
:param pos: 1-based relative position
"""
self.physical_parent_relative_pos_map[sub_id] = pos
def set_phy_descr(self, sub_id, phy_desc):
"""
:param sub_id: sub OID
:param phy_desc: physical entity description
"""
self.physical_description_map[sub_id] = phy_desc
def set_phy_name(self, sub_id, name):
"""
:param sub_id: sub OID
:param name: physical entity name
"""
self.physical_name_map[sub_id] = name
def set_phy_contained_in(self, sub_id, parent):
"""
:param sub_id: sub OID
:param parent: parent entity name or parent oid
"""
if isinstance(parent, str):
if parent in self.physical_name_to_oid_map:
self.physical_contained_in_map[sub_id] = self.physical_name_to_oid_map[parent][0]
else:
self._add_pending_entity_name_callback(parent, self.set_phy_contained_in, [sub_id, parent])
elif isinstance(parent, int):
self.physical_contained_in_map[sub_id] = parent
elif isinstance(parent, tuple):
self.physical_contained_in_map[sub_id] = parent[0]
def set_phy_hw_ver(self, sub_id, phy_hw_ver):
"""
:param sub_id: sub OID
:param phy_hw_ver: physical entity hardware version
"""
self.physical_hw_version_map[sub_id] = phy_hw_ver
def set_phy_serial_num(self, sub_id, phy_serial_num):
"""
:param sub_id: sub OID
:param phy_serial_num: physical entity serial number
"""
self.physical_serial_number_map[sub_id] = phy_serial_num
def set_phy_mfg_name(self, sub_id, phy_mfg_name):
"""
:param sub_id: sub OID
:param phy_mfg_name: physical entity manufacturer name
"""
self.physical_mfg_name_map[sub_id] = phy_mfg_name
def set_phy_model_name(self, sub_id, phy_model_name):
"""
:param sub_id: sub OID
:param phy_model_name: physical entity model name
"""
self.physical_model_name_map[sub_id] = phy_model_name
def set_phy_fru(self, sub_id, replaceable):
"""
:param sub_id: sub OID
:param replaceable: physical entity FRU
"""
if isinstance(replaceable, str):
replaceable = True if replaceable.lower() == 'true' else False
self.physical_fru_map[sub_id] = self.REPLACEABLE if replaceable else self.NOT_REPLACEABLE
elif isinstance(replaceable, bool):
self.physical_fru_map[sub_id] = self.REPLACEABLE if replaceable else self.NOT_REPLACEABLE
def get_next(self, sub_id):
"""
:param sub_id: The 1-based sub-identifier query.
:return: the next sub id.
"""
right = bisect_right(self.physical_entities, sub_id)
if right == len(self.physical_entities):
return None
return self.physical_entities[right]
def get_phy_class(self, sub_id):
"""
:param sub_id: sub OID
:return: physical class for this OID
"""
if sub_id in self.physical_entities:
return self.physical_classes_map.get(sub_id, PhysicalClass.UNKNOWN)
return None
def get_phy_parent_relative_pos(self, sub_id):
"""
:param sub_id: sub OID
:return: relative position in parent device for this OID
"""
if sub_id in self.physical_entities:
return self.physical_parent_relative_pos_map.get(sub_id, PhysicalClass.UNKNOWN)
return None
def get_phy_descr(self, sub_id):
"""
:param sub_id: sub OID
:return: description string for this OID
"""
if sub_id in self.physical_entities:
return self.physical_description_map.get(sub_id, "")
return None
def get_phy_vendor_type(self, sub_id):
"""
:param sub_id: sub OID
:return: vendor type for this OID
"""
return "" if sub_id in self.physical_entities else None
def get_phy_contained_in(self, sub_id):
"""
:param sub_id: sub OID
:return: physical contained in device OID for this OID
"""
if sub_id in self.physical_entities:
return self.physical_contained_in_map.get(sub_id, -1)
#return sub_id_tuple[0] if isinstance(sub_id_tuple, tuple) else None
return None
def get_phy_name(self, sub_id):
"""
:param sub_id: sub OID
:return: name string for this OID
"""
if sub_id in self.physical_entities:
return self.physical_name_map.get(sub_id, "")
return None
def get_phy_hw_ver(self, sub_id):
"""
:param sub_id: sub OID
:return: hardware version for this OID
"""
if sub_id in self.physical_entities:
return self.physical_hw_version_map.get(sub_id, "")
return None
def get_phy_fw_ver(self, sub_id):
"""
:param sub_id: sub OID
:return: firmware version for this OID
"""
return "" if sub_id in self.physical_entities else None
def get_phy_sw_rev(self, sub_id):
"""
:param sub_id: sub OID
:return: software version for this OID
"""
return "" if sub_id in self.physical_entities else None
def get_phy_serial_num(self, sub_id):
"""
:param sub_id: sub OID
:return: serial number for this OID
"""
if sub_id in self.physical_entities:
return self.physical_serial_number_map.get(sub_id, "")
return None
def get_phy_mfg_name(self, sub_id):
"""
:param sub_id: sub OID
:return: manufacture name for this OID
"""
if sub_id in self.physical_entities:
return self.physical_mfg_name_map.get(sub_id, "")
return None
def get_phy_model_name(self, sub_id):
"""
:param sub_id: sub OID
:return: model name for this OID
"""
if sub_id in self.physical_entities:
return self.physical_model_name_map.get(sub_id, "")
return None
def get_phy_alias(self, sub_id):
"""
:param sub_id: sub OID
:return: alias for this OID
"""
return "" if sub_id in self.physical_entities else None
def get_phy_assert_id(self, sub_id):
"""
:param sub_id: sub OID
:return: assert ID for this OID
"""
return "" if sub_id in self.physical_entities else None
def is_fru(self, sub_id):
"""
:param sub_id: sub OID
:return: if it is FRU for this OID
"""
if sub_id in self.physical_entities:
return self.physical_fru_map.get(sub_id, self.NOT_REPLACEABLE)
return None
def physical_entity_updater():
"""
Decorator for auto registering physical entity types
"""
def wrapper(object_type):
PhysicalTableMIBUpdater.register_entity_updater_type(object_type)
return object_type
return wrapper
class PhysicalEntityCacheUpdater(object):
"""
Base class for physical entity cache updater
"""
def __init__(self, mib_updater):
self.mib_updater = mib_updater
self.pub_sub_dict = {}
# Map to store fan to its related oid. The key is the db key in FAN_INFO table, the value is a list of oid that
# relates to this fan entry. The map is used for removing fan mib objects when a fan removing from the system.
self.entity_to_oid_map = {}
def reinit_data(self):
# Redis subscriptions are established and consumed in update_data,
# but if there's stable exception during update logic,
# the reinit_data will be called, but the update_data is never called.
# The message is sent into subscription queue, but never got consumed,
# this causes Redis memory leaking.
# Hence clear the message in the subscription and cancel the subscription during reinit_data
for db_index in list(self.pub_sub_dict):
pubsub = self.pub_sub_dict[db_index]
db_conn = self.mib_updater.statedb[db_index]
# clear message in the subscription and cancel the subscription
mibs.clear_pubsub_msg(pubsub)
mibs.cancel_redis_pubsub(pubsub, db_conn, db_conn.STATE_DB, self.get_key_pattern())
del self.pub_sub_dict[db_index]
self.entity_to_oid_map.clear()
# retrieve the initial list of entity in db
key_info = Namespace.dbs_keys(self.mib_updater.statedb, mibs.STATE_DB, self.get_key_pattern())
if key_info:
keys = [entry for entry in key_info]
else:
keys = []
# update cache with initial data
for key in keys:
# extract entity name
name = key.split(mibs.TABLE_NAME_SEPARATOR_VBAR)[-1]
self._update_entity_cache(name)
def update_data(self, db_index, db):
if db_index not in self.pub_sub_dict:
self.pub_sub_dict[db_index] = mibs.get_redis_pubsub(db, db.STATE_DB, self.get_key_pattern())
self._update_per_namespace_data(self.pub_sub_dict[db_index])
def _update_per_namespace_data(self, pubsub):
"""
Update cache.
Here we listen to changes in STATE_DB table
and update data only when there is a change (SET, DELETE)
"""
while True:
msg = pubsub.get_message()
if not msg:
break
db_entry = msg["channel"].split(":")[-1]
data = msg['data'] # event data
if not isinstance(data, str):
continue
# extract interface name
name = db_entry.split(mibs.TABLE_NAME_SEPARATOR_VBAR)[-1]
if "set" in data:
self._update_entity_cache(name)
elif "del" in data:
self._remove_entity_cache(name)
def get_key_pattern(self):
pass
def _update_entity_cache(self, name):
pass
def get_physical_relation_info(self, name):
return Namespace.dbs_get_all(self.mib_updater.statedb, mibs.STATE_DB,
mibs.physical_entity_info_table(name))
def _add_entity_related_oid(self, entity_name, oid):
if entity_name not in self.entity_to_oid_map:
self.entity_to_oid_map[entity_name] = [oid]
else:
self.entity_to_oid_map[entity_name].append(oid)
def _remove_entity_cache(self, entity_name):
if entity_name in self.entity_to_oid_map:
self.mib_updater.remove_sub_ids(self.entity_to_oid_map[entity_name])
self.entity_to_oid_map.pop(entity_name)
@physical_entity_updater()
class XcvrCacheUpdater(PhysicalEntityCacheUpdater):
KEY_PATTERN = mibs.transceiver_info_table("*")
def __init__(self, mib_updater):
super(XcvrCacheUpdater, self).__init__(mib_updater)
self.if_alias_map = {}
def get_key_pattern(self):
return XcvrCacheUpdater.KEY_PATTERN
def reinit_data(self):
# update interface maps
_, self.if_alias_map, _, _ = \
Namespace.get_sync_d_from_all_namespace(mibs.init_sync_d_interface_tables, Namespace.init_namespace_dbs())
PhysicalEntityCacheUpdater.reinit_data(self)
def _update_entity_cache(self, interface):
"""
Update data for single transceiver
:param: interface: Interface name associated with transceiver
"""
# get interface from interface name
ifindex = port_util.get_index_from_str(interface)
if ifindex is None:
# interface name invalid, skip this entry
mibs.logger.warning(
"Invalid interface name in {} \
in STATE_DB, skipping".format(interface))
return
# get transceiver information from transceiver info entry in STATE DB
transceiver_info = Namespace.dbs_get_all(self.mib_updater.statedb, mibs.STATE_DB,
mibs.transceiver_info_table(interface))
if not transceiver_info or transceiver_info['type'] == RJ45_PORT_TYPE:
return
# update xcvr info from DB
# use port's name as key for transceiver info entries
sub_id = get_transceiver_sub_id(ifindex)
# add interface to available OID list
self.mib_updater.add_sub_id(sub_id)
self._add_entity_related_oid(interface, sub_id)
# physical class - network port
self.mib_updater.set_phy_class(sub_id, PhysicalClass.PORT)
# save values into cache
sfp_type, hw_version, serial_number, mfg_name, model_name, replaceable = get_db_data(transceiver_info, XcvrInfoDB)
self.mib_updater.set_phy_hw_ver(sub_id, hw_version)
self.mib_updater.set_phy_serial_num(sub_id, serial_number)
self.mib_updater.set_phy_mfg_name(sub_id, mfg_name)
self.mib_updater.set_phy_model_name(sub_id, model_name)
self.mib_updater.set_phy_contained_in(sub_id, CHASSIS_SUB_ID)
self.mib_updater.set_phy_fru(sub_id, replaceable)
# Relative position of SFP can be changed at run time. For example, plug out a normal cable SFP3 and plug in
# a 1 split 4 SFP, the relative position of SFPs after SPF3 will change. In this case, it is hard to determine
# the relative position for other SFP. According to RFC 2737, 'If the agent cannot determine the parent-relative position
# for some reason, or if the associated value of entPhysicalContainedIn is '0', then the value '-1' is returned'.
# See https://tools.ietf.org/html/rfc2737.
self.mib_updater.set_phy_parent_relative_pos(sub_id, -1)
ifalias = self.if_alias_map.get(interface, "")
# generate a description for this transceiver
self.mib_updater.set_phy_descr(sub_id, get_transceiver_description(sfp_type, ifalias))
self.mib_updater.set_phy_name(sub_id, interface)
# update transceiver sensor cache
self._update_transceiver_sensor_cache(interface, sub_id)
def _update_transceiver_sensor_cache(self, interface, sub_id):
"""
Update sensor data for single transceiver
:param: interface: Interface name associated with transceiver
:param: sub_id: OID of transceiver
"""
ifalias = self.if_alias_map.get(interface, "")
ifindex = port_util.get_index_from_str(interface)
# get transceiver sensors from transceiver dom entry in STATE DB
transceiver_dom_entry = Namespace.dbs_get_all(self.mib_updater.statedb, mibs.STATE_DB,
mibs.transceiver_dom_table(interface))
if not transceiver_dom_entry:
return
sensor_data_list = TransceiverSensorData.create_sensor_data(transceiver_dom_entry)
sensor_data_list = TransceiverSensorData.sort_sensor_data(sensor_data_list)
for index, sensor_data in enumerate(sensor_data_list):
sensor_sub_id = get_transceiver_sensor_sub_id(ifindex, sensor_data.get_oid_offset())
self._add_entity_related_oid(interface, sensor_sub_id)
self.mib_updater.set_phy_class(sensor_sub_id, PhysicalClass.SENSOR)
sensor_description = get_transceiver_sensor_description(sensor_data.get_name(), sensor_data.get_lane_number(), ifalias)
self.mib_updater.set_phy_descr(sensor_sub_id, sensor_description)
self.mib_updater.set_phy_name(sensor_sub_id, sensor_description)
self.mib_updater.set_phy_contained_in(sensor_sub_id, sub_id)
self.mib_updater.set_phy_parent_relative_pos(sensor_sub_id, index + 1)
self.mib_updater.set_phy_fru(sensor_sub_id, False)
# add to available OIDs list
self.mib_updater.add_sub_id(sensor_sub_id)
@physical_entity_updater()
class PsuCacheUpdater(PhysicalEntityCacheUpdater):
KEY_PATTERN = mibs.psu_info_table("*")
def __init__(self, mib_updater):
super(PsuCacheUpdater, self).__init__(mib_updater)
def get_key_pattern(self):
return PsuCacheUpdater.KEY_PATTERN
def _update_entity_cache(self, psu_name):
psu_info = Namespace.dbs_get_all(self.mib_updater.statedb, mibs.STATE_DB,
mibs.psu_info_table(psu_name))
if not psu_info:
return
model, serial, current, power, presence, voltage, temperature, replaceable = get_db_data(psu_info, PsuInfoDB)
if presence.lower() != 'true':
self._remove_entity_cache(psu_name)
return
psu_relation_info = self.get_physical_relation_info(psu_name)
psu_position, psu_parent_name = get_db_data(psu_relation_info, PhysicalRelationInfoDB)
psu_position = int(psu_position)
psu_sub_id = get_psu_sub_id(psu_position)
self._add_entity_related_oid(psu_name, psu_sub_id)
self.mib_updater.update_name_to_oid_map(psu_name, psu_sub_id)
# add PSU to available OID list
self.mib_updater.add_sub_id(psu_sub_id)
self.mib_updater.set_phy_class(psu_sub_id, PhysicalClass.POWERSUPPLY)
self.mib_updater.set_phy_descr(psu_sub_id, psu_name)
self.mib_updater.set_phy_name(psu_sub_id, psu_name)
if not is_null_str(model):
self.mib_updater.set_phy_model_name(psu_sub_id, model)
if not is_null_str(serial):
self.mib_updater.set_phy_serial_num(psu_sub_id, serial)
self.mib_updater.set_phy_parent_relative_pos(psu_sub_id, psu_position)
self.mib_updater.set_phy_contained_in(psu_sub_id, psu_parent_name)
self.mib_updater.set_phy_fru(psu_sub_id, replaceable)
# add psu current sensor as a physical entity
if current and not is_null_str(current):
self._update_psu_sensor_cache(psu_name, psu_sub_id, 'current')
if power and not is_null_str(power):
self._update_psu_sensor_cache(psu_name, psu_sub_id, 'power')
if temperature and not is_null_str(temperature):
self._update_psu_sensor_cache(psu_name, psu_sub_id, 'temperature')
if voltage and not is_null_str(voltage):
self._update_psu_sensor_cache(psu_name, psu_sub_id, 'voltage')
def _update_psu_sensor_cache(self, psu_name, psu_sub_id, sensor_name):
psu_current_sub_id = get_psu_sensor_sub_id(psu_sub_id, sensor_name)
self._add_entity_related_oid(psu_name, psu_current_sub_id)
self.mib_updater.add_sub_id(psu_current_sub_id)
self.mib_updater.set_phy_class(psu_current_sub_id, PhysicalClass.SENSOR)
desc = '{} for {}'.format(PSU_SENSOR_NAME_MAP[sensor_name], psu_name)
self.mib_updater.set_phy_descr(psu_current_sub_id, desc)
self.mib_updater.set_phy_name(psu_current_sub_id, desc)
self.mib_updater.set_phy_parent_relative_pos(psu_current_sub_id, PSU_SENSOR_POSITION_MAP[sensor_name])
self.mib_updater.set_phy_contained_in(psu_current_sub_id, psu_sub_id)
self.mib_updater.set_phy_fru(psu_current_sub_id, False)
@physical_entity_updater()
class FanDrawerCacheUpdater(PhysicalEntityCacheUpdater):
KEY_PATTERN = mibs.fan_drawer_info_table("*")
def __init__(self, mib_updater):
super(FanDrawerCacheUpdater, self).__init__(mib_updater)
def get_key_pattern(self):
return FanDrawerCacheUpdater.KEY_PATTERN
def _update_entity_cache(self, drawer_name):
drawer_info = Namespace.dbs_get_all(self.mib_updater.statedb, mibs.STATE_DB,
mibs.fan_drawer_info_table(drawer_name))
if not drawer_info:
return
model, presence, serial, replaceable = get_db_data(drawer_info, FanDrawerInfoDB)
if presence.lower() != 'true':
self._remove_entity_cache(drawer_name)
return
drawer_relation_info = self.get_physical_relation_info(drawer_name)
if drawer_relation_info:
drawer_position, drawer_parent_name = get_db_data(drawer_relation_info, PhysicalRelationInfoDB)
drawer_position = int(drawer_position)
drawer_sub_id = get_fan_drawer_sub_id(drawer_position)
self._add_entity_related_oid(drawer_name, drawer_sub_id)
self.mib_updater.update_name_to_oid_map(drawer_name, drawer_sub_id)
# add fan drawer to available OID list
self.mib_updater.add_sub_id(drawer_sub_id)
self.mib_updater.set_phy_class(drawer_sub_id, PhysicalClass.CONTAINER)
self.mib_updater.set_phy_descr(drawer_sub_id, drawer_name)
self.mib_updater.set_phy_name(drawer_sub_id, drawer_name)
self.mib_updater.set_phy_parent_relative_pos(drawer_sub_id, drawer_position)
self.mib_updater.set_phy_contained_in(drawer_sub_id, drawer_parent_name)
if model and not is_null_str(model):
self.mib_updater.set_phy_model_name(drawer_sub_id, model)
if serial and not is_null_str(serial):
self.mib_updater.set_phy_serial_num(drawer_sub_id, serial)
self.mib_updater.set_phy_fru(drawer_sub_id, replaceable)
@physical_entity_updater()
class FanCacheUpdater(PhysicalEntityCacheUpdater):
KEY_PATTERN = mibs.fan_info_table("*")
def __init__(self, mib_updater):
super(FanCacheUpdater, self).__init__(mib_updater)
def get_key_pattern(self):
return FanCacheUpdater.KEY_PATTERN
def _update_entity_cache(self, fan_name):
fan_info = Namespace.dbs_get_all(self.mib_updater.statedb, mibs.STATE_DB,
mibs.fan_info_table(fan_name))
if not fan_info:
return
model, presence, serial, speed, replaceable = get_db_data(fan_info, FanInfoDB)
if presence.lower() != 'true':
self._remove_entity_cache(fan_name)
return
fan_relation_info = self.get_physical_relation_info(fan_name)
fan_position, fan_parent_name = get_db_data(fan_relation_info, PhysicalRelationInfoDB)
fan_position = int(fan_position)