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 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_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 breakOutPort(self, delPorts=list(), portJson=dict(), force=False, \ loadDefConfig=True): ''' This is the main function for port breakout. Exposed to caller. Parameters: delPorts (list): ports to be deleted. portJson (dict): Config DB json Part of all Ports, generated from platform.json. force (bool): if false return dependecies, else delete dependencies. loadDefConfig: If loadDefConfig, add default config for ports as well. Returns: (deps, ret) (tuple)[list, bool]: dependecies and success/failure. ''' MAX_WAIT = 60 try: # delete Port and get the Config diff, deps and True/False delConfigToLoad, deps, ret = self._deletePorts(ports=delPorts, \ force=force) # return dependencies if delete port fails if ret == False: return deps, ret # add Ports and get the config diff and True/False addConfigtoLoad, ret = self._addPorts(portJson=portJson, \ loadDefConfig=loadDefConfig) # return if ret is False, Great thing, no change is done in Config if ret == False: return None, ret # Save Port OIDs Mapping Before Deleting Port dataBase = SonicV2Connector(host="127.0.0.1") if_name_map, if_oid_map = port_util.get_interface_oid_map(dataBase) self.sysLog(syslog.LOG_DEBUG, 'if_name_map {}'.format(if_name_map)) # If we are here, then get ready to update the Config DB as below: # -- shutdown the ports, # -- Update deletion of ports in Config DB, # -- verify Asic DB for port deletion, # -- then update addition of ports in config DB. self._shutdownIntf(delPorts) self.writeConfigDB(delConfigToLoad) # Verify in Asic DB, self._verifyAsicDB(db=dataBase, ports=delPorts, portMap=if_name_map, \ timeout=MAX_WAIT) self.writeConfigDB(addConfigtoLoad) except Exception as e: self.sysLog(doPrint=True, logLevel=syslog.LOG_ERR, msg=str(e)) return None, False return None, True
def nwi_get_fdb_info(oc_nwis, fill_info_bmp, key_ar, disp_args): # refer to /usr/bin/fdbshow key_mac = None key_vlan = None oc_nwi = None len_key_ar = len(key_ar) // 2 if len_key_ar > 3: return False for i in range(len_key_ar): if key_ar[i + len_key_ar] == 'name': if key_ar[i] in oc_nwis.network_instance: oc_nwi = oc_nwis.network_instance[key_ar[i]] else: return False elif key_ar[i + len_key_ar] == 'mac-address': key_mac = key_ar[i] elif key_ar[i + len_key_ar] == 'vlan': key_vlan = key_ar[i] else: return False # instance name is omitted if not oc_nwi: oc_nwi = oc_nwis.network_instance[DEFAULT_NWI_NAME] if_name_map, \ if_oid_map = port_util.get_interface_oid_map(disp_args.appdb) if_br_oid_map = port_util.get_bridge_port_map(disp_args.appdb) NEW_MAC_LST = [] global OLD_MAC_LST fdb_str = disp_args.appdb.keys(disp_args.appdb.ASIC_DB, "ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:*") if fdb_str and if_br_oid_map: oid_pfx = len("oid:0x") for s in fdb_str: fdb_entry = s.decode() fdb = json.loads(fdb_entry.split(":", 2)[-1]) if not fdb: continue ent = disp_args.appdb.get_all('ASIC_DB', s, blocking=True) br_port_id = ent[b"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID"][oid_pfx:] if br_port_id not in if_br_oid_map: continue port_id = if_br_oid_map[br_port_id] if_name = if_oid_map[port_id] if "vlan" not in fdb: if "bvid" in fdb: fdb["vlan"] = port_util.get_vlan_id_from_bvid( disp_args.appdb, fdb["bvid"]) else: continue #pdb.set_trace() # ex: # fdb["vlan"] : u'10' # fdb["mac"] : u'00:00:00:00:00:01' # if_name : 'Ethernet4' # oc_nwi.fdb.mac_table.entries.entry['00:00:00:00:00:01 10'] if key_mac and key_mac != fdb["mac"]: continue if key_vlan and key_vlan != fdb["vlan"]: continue mac_key = "%s %s" % (fdb["mac"], fdb["vlan"]) if mac_key in OLD_MAC_LST: OLD_MAC_LST.remove(mac_key) mac_entry = oc_nwi.fdb.mac_table.entries.entry[mac_key] else: mac_entry = oc_nwi.fdb.mac_table.entries.entry.add(mac_key) mac_entry.interface.interface_ref.config.interface = if_name mac_entry.state._set_entry_type('DYNAMIC') NEW_MAC_LST.append(mac_key) # remove old mac entries not used for mac_key in OLD_MAC_LST: oc_nwi.fdb.mac_table.entries.entry.delete(mac_key) OLD_MAC_LST = NEW_MAC_LST return True