|
| 1 | +############################################################################# |
| 2 | +# PDDF |
| 3 | +# |
| 4 | +# PDDF fan_drawer base class inherited from the common base class fan_drawer.py |
| 5 | +# |
| 6 | +############################################################################# |
| 7 | + |
| 8 | +try: |
| 9 | + from sonic_platform_base.fan_drawer_base import FanDrawerBase |
| 10 | + from sonic_platform.fan import Fan |
| 11 | +except ImportError as e: |
| 12 | + raise ImportError(str(e) + "- required module not found") |
| 13 | + |
| 14 | + |
| 15 | +class PddfFanDrawer(FanDrawerBase): |
| 16 | + """PDDF generic Fan Drawer class""" |
| 17 | + |
| 18 | + pddf_obj = {} |
| 19 | + plugin_data = {} |
| 20 | + |
| 21 | + def __init__(self, tray_idx, pddf_data=None, pddf_plugin_data=None): |
| 22 | + FanDrawerBase.__init__(self) |
| 23 | + if not pddf_data or not pddf_plugin_data: |
| 24 | + raise ValueError('PDDF JSON data error') |
| 25 | + |
| 26 | + self.pddf_obj = pddf_data |
| 27 | + self.plugin_data = pddf_plugin_data |
| 28 | + self.platform = self.pddf_obj.get_platform() |
| 29 | + |
| 30 | + if tray_idx < 0 or tray_idx >= self.platform['num_fantrays']: |
| 31 | + print("Invalid fantray index %d\n" % tray_idx) |
| 32 | + return |
| 33 | + |
| 34 | + self.fantray_index = tray_idx+1 |
| 35 | + for j in range(self.platform['num_fans_pertray']): |
| 36 | + # Fan index is 0-based for the init call |
| 37 | + self._fan_list.append(Fan(tray_idx, j, self.pddf_obj, self.plugin_data)) |
| 38 | + |
| 39 | + def get_name(self): |
| 40 | + """ |
| 41 | + Retrieves the fan drawer name |
| 42 | + Returns: String containing fan-drawer name |
| 43 | + """ |
| 44 | + return "Fantray{0}".format(self.fantray_index) |
| 45 | + |
| 46 | + def get_presence(self): |
| 47 | + status = False |
| 48 | + # Usually if a tray is removed, all the fans inside it are absent |
| 49 | + if self._fan_list: |
| 50 | + status = self._fan_list[0].get_presence() |
| 51 | + |
| 52 | + return status |
| 53 | + |
| 54 | + def get_status(self): |
| 55 | + status = False |
| 56 | + # if all the fans are working fine, then tray status should be okay |
| 57 | + if self._fan_list: |
| 58 | + status = all(fan.get_status() == True for fan in self._fan_list) |
| 59 | + |
| 60 | + return status |
| 61 | + |
| 62 | + def is_replaceable(self): |
| 63 | + """ |
| 64 | + Indicate whether this device is replaceable. |
| 65 | + Returns: |
| 66 | + bool: True if it is replaceable. |
| 67 | + """ |
| 68 | + # Usually Fantrays are replaceable |
| 69 | + return True |
| 70 | + |
| 71 | + def get_position_in_parent(self): |
| 72 | + """ |
| 73 | + Retrieves 1-based relative physical position in parent device. |
| 74 | + Returns: |
| 75 | + integer: The 1-based relative physical position in parent |
| 76 | + device or -1 if cannot determine the position |
| 77 | + """ |
| 78 | + return self.fantray_index |
| 79 | + |
| 80 | + def get_status_led(self): |
| 81 | + led_device_name = "FANTRAY{}".format(self.fantray_index) + "_LED" |
| 82 | + |
| 83 | + if led_device_name not in self.pddf_obj.data.keys(): |
| 84 | + # Implement a generic status_led color scheme |
| 85 | + if self.get_status(): |
| 86 | + return self.STATUS_LED_COLOR_GREEN |
| 87 | + else: |
| 88 | + return self.STATUS_LED_COLOR_OFF |
| 89 | + |
| 90 | + device_name = self.pddf_obj.data[led_device_name]['dev_info']['device_name'] |
| 91 | + self.pddf_obj.create_attr('device_name', device_name, self.pddf_obj.get_led_path()) |
| 92 | + self.pddf_obj.create_attr('index', str(self.fantray_index-1), self.pddf_obj.get_led_path()) |
| 93 | + self.pddf_obj.create_attr('dev_ops', 'get_status', self.pddf_obj.get_led_path()) |
| 94 | + color = self.pddf_obj.get_led_color() |
| 95 | + return (color) |
| 96 | + |
| 97 | + def set_status_led(self, color): |
| 98 | + result = False |
| 99 | + led_device_name = "FANTRAY{}".format(self.fantray_index) + "_LED" |
| 100 | + result, msg = self.pddf_obj.is_supported_sysled_state(led_device_name, color) |
| 101 | + if result == False: |
| 102 | + print(msg) |
| 103 | + return (False) |
| 104 | + |
| 105 | + device_name = self.pddf_obj.data[led_device_name]['dev_info']['device_name'] |
| 106 | + self.pddf_obj.create_attr('device_name', device_name, self.pddf_obj.get_led_path()) |
| 107 | + self.pddf_obj.create_attr('index', str(self.fantray_index-1), self.pddf_obj.get_led_path()) |
| 108 | + self.pddf_obj.create_attr('color', color, self.pddf_obj.get_led_cur_state_path()) |
| 109 | + self.pddf_obj.create_attr('dev_ops', 'set_status', self.pddf_obj.get_led_path()) |
| 110 | + return (True) |
0 commit comments