def reinit_data(self): """ Subclass update data routine. """ self.man_addr_list = [] # establish connection to application database. self.db_conn.connect(mibs.APPL_DB) mgmt_ip_bytes = self.db_conn.get(mibs.APPL_DB, mibs.LOC_CHASSIS_TABLE, b'lldp_loc_man_addr') if not mgmt_ip_bytes: self.mgmt_ip_str = '' else: self.mgmt_ip_str = mgmt_ip_bytes.decode() logger.debug("Got mgmt ip from db : {}".format(self.mgmt_ip_str)) try: addr_subtype_sub_oid = 4 mgmt_ip_sub_oid = None for mgmt_ip in self.mgmt_ip_str.split(','): if '.' in mgmt_ip: mgmt_ip_sub_oid = (addr_subtype_sub_oid, *[int(i) for i in mgmt_ip.split('.')]) break except ValueError: logger.error("Invalid local mgmt IP {}".format(self.mgmt_ip_str)) return if mgmt_ip_sub_oid == None: return sub_oid = (ManAddrConst.man_addr_subtype_ipv4, *mgmt_ip_sub_oid) self.man_addr_list.append(sub_oid)
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) 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)) # { SONiC name -> optional rename } if_alias_map = _if_alias_map logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2)) if if_alias_map is None or len(if_alias_map) == 0: logger.warning("No alias map found--port names will use SONiC names.") if_alias_map = dict(zip(if_name_map.keys(), if_name_map.keys())) return if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_map
def poll_lldp_entry_updates(pubsub): ret = None, None, None msg = pubsub.get_message() if not msg: return ret try: interface = msg["channel"].split(b":")[-1].decode() data = msg['data'] except (KeyError, AttributeError) as e: logger.error("Invalid msg when polling for lldp updates: {}\n" "The error seems to be: {}".format(msg, e)) return ret if interface == 'eth0': return ret # get interface from interface name if_index = port_util.get_index_from_str(interface) if if_index is None: # interface name invalid, skip this entry logger.warning( "Invalid interface name in {} in APP_DB, skipping".format( interface)) return ret return data, interface, if_index
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) """ # { 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()))} # As sai_id is not unique in multi-asic platform, concatenate it with # namespace to get a unique key. Assuming that ':' is not present in namespace # string or in sai id. # sai_id_key = namespace : sai_id if_id_map = {get_sai_id_key(db_conn.namespace, 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)) 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_queue_tables(db_conn): """ Initializes queue maps for SyncD-connected MIB(s). :return: tuple(port_queues_map, queue_stat_map) """ # { Port name : Queue index (SONiC) -> sai_id } # ex: { "Ethernet0: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 -> {"port_index : queue_index" : sai_oid} # queue_stat_map -> {"port_index : queue stat table name" : {counter name : value}} # port_queue_list_map -> {port_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.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_key = queue_key(port_index, queue_stat_name) queue_stat_map[queue_stat_key] = 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 init_sync_d_interface_tables(): """ DRY helper method. Connects to and initializes interface maps for SyncD-connected MIB(s). :return: tuple(db_conn, if_name_map, if_id_map, oid_map, if_alias_map) """ # 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) db_conn.connect(COUNTERS_DB) # { if_name (SONiC) -> sai_id } # ex: { "Ethernet76" : "1000000000023" } if_name_map = db_conn.get_all(COUNTERS_DB, COUNTERS_PORT_NAME_MAP, blocking=True) logger.debug("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) # { sai_id -> if_name (SONiC) } if_id_map = {sai_id: if_name 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("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(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(SONIC_ETHERNET_RE_PATTERN)) logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) # { SONiC name -> optional rename } if_alias_map = _if_alias_map logger.debug("Chassis name map:\n" + pprint.pformat(if_alias_map, indent=2)) if if_alias_map is None or len(if_alias_map) == 0: logger.warning("No alias map found--port names will use SONiC names.") if_alias_map = dict(zip(if_name_map.keys(), if_name_map.keys())) return db_conn, if_name_map, if_alias_map, if_id_map, oid_sai_map, oid_name_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 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) 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 poll_lldp_entry_updates(pubsub): ret = None, None, None msg = pubsub.get_message() if not msg: return ret try: interface = msg["channel"].split(b":")[-1].decode() data = msg['data'] except (KeyError, AttributeError) as e: logger.error("Invalid msg when polling for lldp updates: {}\n" "The error seems to be: {}".format(msg, e)) return ret # get interface from interface name if_index = port_util.get_index_from_str(interface) if if_index is None: # interface name invalid, skip this entry logger.warning("Invalid interface name in {} in APP_DB, skipping" .format(interface)) return ret return data, interface, if_index
def reinit_data(self): """ Subclass update data routine. """ self.man_addr_list = [] self.mgmt_ip_str = None # establish connection to application database. self.db_conn.connect(mibs.APPL_DB) mgmt_ip_bytes = self.db_conn.get(mibs.APPL_DB, mibs.LOC_CHASSIS_TABLE, 'lldp_loc_man_addr') if not mgmt_ip_bytes: logger.warning("Missing lldp_loc_man_addr from APPL DB") return self.mgmt_ip_str = mgmt_ip_bytes logger.debug("Got mgmt ip from db : {}".format(self.mgmt_ip_str)) try: addr_subtype_sub_oid = 4 mgmt_ip_sub_oid = None for mgmt_ip in self.mgmt_ip_str.split(','): if '.' in mgmt_ip: mgmt_ip_tuple = ip2byte_tuple(mgmt_ip) mgmt_ip_sub_oid = (addr_subtype_sub_oid, *mgmt_ip_tuple) break else: logger.error( "Could not find IPv4 address in lldp_loc_man_addr") return except ValueError: logger.error("Invalid local mgmt IP {}".format(self.mgmt_ip_str)) return sub_oid = (ManAddrConst.man_addr_subtype_ipv4, *mgmt_ip_sub_oid) self.man_addr_list.append(sub_oid)