def get_change_event(self, timeout=0): """ Returns a nested dictionary containing all devices which have experienced a change at chassis level Args: timeout: Timeout in milliseconds (optional). If timeout == 0, this method will block until a change is detected. Returns: (bool, dict): - True if call successful, False if not; - A nested dictionary where key is a device type, value is a dictionary with key:value pairs in the format of {'device_id':'device_event'}, where device_id is the device ID for this device and device_event, status='1' represents device inserted, status='0' represents device removed. Ex. {'fan':{'0':'0', '2':'1'}, 'sfp':{'11':'0'}} indicates that fan 0 has been removed, fan 2 has been inserted and sfp 11 has been removed. """ # Initialize SFP event first if not self.sfp_event_initialized: from sonic_platform.sfp_event import sfp_event self.sfp_event = sfp_event() self.sfp_event.initialize() self.MAX_SELECT_EVENT_RETURNED = self.PORT_END self.sfp_event_initialized = True wait_for_ever = (timeout == 0) port_dict = {} if wait_for_ever: timeout = MAX_SELECT_DELAY while True: status = self.sfp_event.check_sfp_status(port_dict, timeout) if not port_dict == {}: break else: status = self.sfp_event.check_sfp_status(port_dict, timeout) if status: # get_change_event has the meaning of retrieving all the notifications through a single call. # Typically this is implemented via a select framework which requires the underlay file-reading # interface able to retrieve all notifications without blocking once the fd has been selected. # However, sdk doesn't provide any interface satisfied the requirement. as a result, # check_sfp_status returns only one notification may indicate more notifications in its queue. # In this sense, we have to iterate in a loop to get all the notifications in case that # the first call returns at least one. i = 0 while i < self.MAX_SELECT_EVENT_RETURNED: status = self.sfp_event.check_sfp_status(port_dict, 0) if not status: break i = i + 1 return True, {'sfp':port_dict} else: return True, {'sfp':{}}
def get_change_event(self, timeout=0): """ Returns a nested dictionary containing all devices which have experienced a change at chassis level Args: timeout: Timeout in milliseconds (optional). If timeout == 0, this method will block until a change is detected. Returns: (bool, dict): - True if call successful, False if not; - A nested dictionary where key is a device type, value is a dictionary with key:value pairs in the format of {'device_id':'device_event'}, where device_id is the device ID for this device and device_event, status='1' represents device inserted, status='0' represents device removed. Ex. {'fan':{'0':'0', '2':'1'}, 'sfp':{'11':'0'}} indicates that fan 0 has been removed, fan 2 has been inserted and sfp 11 has been removed. """ # Initialize SFP event first if not self.sfp_event_initialized: from sonic_platform.sfp_event import sfp_event self.sfp_event = sfp_event() self.sfp_event.initialize() self.MAX_SELECT_EVENT_RETURNED = self.PORT_END self.sfp_event_initialized = True wait_for_ever = (timeout == 0) port_dict = {} error_dict = {} if wait_for_ever: timeout = MAX_SELECT_DELAY while True: status = self.sfp_event.check_sfp_status( port_dict, error_dict, timeout) if bool(port_dict): break else: status = self.sfp_event.check_sfp_status(port_dict, error_dict, timeout) if status: self.reinit_sfps(port_dict) result_dict = {'sfp': port_dict} if error_dict: result_dict['sfp_error'] = error_dict return True, result_dict else: return True, {'sfp': {}}
def executor(self, mock_module_state, mock_error_type, expect_status, description=None): from sonic_platform.sfp_event import sfp_event event = sfp_event() event.on_pmpe = MagicMock(return_value=(True, [0,1], mock_module_state, mock_error_type)) port_change = {} error_dict = {} found = event.check_sfp_status(port_change, error_dict, 0) assert found expect_status_str = str(expect_status) assert 1 in port_change and port_change[1] == expect_status_str assert 2 in port_change and port_change[2] == expect_status_str if description: assert 1 in error_dict and error_dict[1] == description assert 2 in error_dict and error_dict[2] == description
def get_change_event(self, timeout=0): # logger.log_error("Get-change-event with thread-{} start ".format( # str(os.getpid())+str(threading.current_thread().ident))) if not self.sfp_event_initialized: from sonic_platform.sfp_event import sfp_event # use the same stub as direct sfp ops does self.sfp_event_stub = self.sfp_stub logger.log_info("Initializing sfp_event with num {} and stub {} : sfp stub {}".format( NUM_SFP, self.sfp_event_stub, self.sfp_stub)) self.sfp_event_list = sfp_event(NUM_SFP, self.sfp_event_stub) self.sfp_event_list.initialize() self.sfp_event_initialized = True timeout = 0 port_dict = {} self.sfp_event_list.check_sfp_status(port_dict, timeout) return True, {'sfp': port_dict}