def generate_dut_feature_list(request): empty = [encode_dut_port_name('unknown', 'unknown')] tbname = request.config.getoption("--testbed") if not tbname: return empty folder = 'metadata' filepath = os.path.join(folder, tbname + '.json') try: with open(filepath, 'r') as yf: metadata = json.load(yf) except IOError as e: return empty if tbname not in metadata: return empty meta = metadata[tbname] ret = [] for dut, val in meta.items(): if 'features' not in val: continue for feature, _ in val['features'].items(): ret.append(encode_dut_port_name(dut, feature)) return ret if ret else empty
def generate_port_lists(request, port_scope): empty = [ encode_dut_port_name('unknown', 'unknown') ] if 'ports' in port_scope: scope = 'Ethernet' elif 'pcs' in port_scope: scope = 'PortChannel' else: return empty if 'all' in port_scope: state = None elif 'oper_up' in port_scope: state = 'oper_state' elif 'admin_up' in port_scope: state = 'admin_state' else: return empty dut_ports = get_testbed_metadata(request) if dut_ports is None: return empty ret = [] for dut, val in dut_ports.items(): if 'intf_status' not in val: continue for intf, status in val['intf_status'].items(): if scope in intf and (not state or status[state] == 'up'): ret.append(encode_dut_port_name(dut, intf)) return ret if ret else empty
def _shutdown_fanout_tor_intfs(tor_host, tor_fanouthosts, tbinfo, dut_intfs=None): """Helper function for shutting down fanout interfaces that are connected to specified DUT interfaces. Args: tor_host (object): Host object for the ToR DUT. tor_fanouthosts (dict): Key is fanout hostname, value is fanout host object. tbinfo (dict): Testbed info from the tbinfo fixture. dut_intfs (list, optional): List of DUT interface names, for example: ['Ethernet0', 'Ethernet4']. All the fanout interfaces that are connected to the specified DUT interfaces will be shutdown. If dut_intfs is not specified, the function will shutdown all the fanout interfaces that are connected to the tor_host DUT and in a VLAN. Defaults to None. Returns: dict (fanouthost: list): Each key is a fanout host, and the corresponding value is the interfaces that were shut down on that host device. """ if not dut_intfs: # If no interface is specified, shutdown all VLAN ports vlan_intfs = [] vlan_member_table = tor_host.get_running_config_facts()['VLAN_MEMBER'] for vlan_members in vlan_member_table.values(): vlan_intfs.extend(list(vlan_members.keys())) dut_intfs = vlan_intfs dut_intfs = natsorted(dut_intfs) full_dut_fanout_port_map = {} for fanout_host in tor_fanouthosts.values(): for encoded_dut_intf, fanout_intf in fanout_host.host_to_fanout_port_map.items( ): full_dut_fanout_port_map[encoded_dut_intf] = { 'fanout_host': fanout_host, 'fanout_intf': fanout_intf } logger.debug( 'full_dut_fanout_port_map: {}'.format(full_dut_fanout_port_map)) fanout_shut_intfs = defaultdict(list) for dut_intf in dut_intfs: encoded_dut_intf = encode_dut_port_name(tor_host.hostname, dut_intf) if encoded_dut_intf in full_dut_fanout_port_map: fanout_host = full_dut_fanout_port_map[encoded_dut_intf][ 'fanout_host'] fanout_intf = full_dut_fanout_port_map[encoded_dut_intf][ 'fanout_intf'] fanout_shut_intfs[fanout_host].append(fanout_intf) else: logger.error('No dut intf "{}" in full_dut_fanout_port_map'.format( encoded_dut_intf)) for fanout_host, intf_list in fanout_shut_intfs.items(): fanout_host.shutdown(intf_list) return fanout_shut_intfs
def generate_port_lists(request, port_scope): empty = [encode_dut_port_name('unknown', 'unknown')] if 'ports' in port_scope: scope = 'Ethernet' elif 'pcs' in port_scope: scope = 'PortChannel' else: return empty if 'all' in port_scope: state = None elif 'oper_up' in port_scope: state = 'oper_state' elif 'admin_up' in port_scope: state = 'admin_state' else: return empty tbname = request.config.getoption("--testbed") if not tbname: return empty folder = 'metadata' filepath = os.path.join(folder, tbname + '.json') try: with open(filepath, 'r') as yf: ports = json.load(yf) except IOError as e: return empty if tbname not in ports: return empty dut_ports = ports[tbname] ret = [] for dut, val in dut_ports.items(): if 'intf_status' not in val: continue for intf, status in val['intf_status'].items(): if scope in intf and (not state or status[state] == 'up'): ret.append(encode_dut_port_name(dut, intf)) return ret if ret else empty
def _shutdown_fanout_tor_intfs(tor_host, tor_fanouthosts, tbinfo, dut_intfs=None): """Helper function for shutting down fanout interfaces that are connected to specified DUT interfaces. Args: tor_host (object): Host object for the ToR DUT. tor_fanouthosts (dict): Key is fanout hostname, value is fanout host object. tbinfo (dict): Testbed info from the tbinfo fixture. dut_intfs (list, optional): List of DUT interface names, for example: ['Ethernet0', 'Ethernet4']. All the fanout interfaces that are connected to the specified DUT interfaces will be shutdown. If dut_intfs is not specified, the function will shutdown all the fanout interfaces that are connected to the tor_host DUT. Defaults to None. Returns: list of tuple: Return a list of tuple. Each tuple has two items. The first item is the host object for fanout. The second item is the fanout interface that has been shutdown. The returned list makes it easy to recover the interfaces. """ down_intfs = [] if not dut_intfs: # If no interface is specified, shutdown all ports mg_facts = tor_host.get_extended_minigraph_facts(tbinfo) dut_intfs = mg_facts['minigraph_ports'].keys() dut_intfs = natsorted(dut_intfs) full_dut_fanout_port_map = {} for fanout_host in tor_fanouthosts.values(): for encoded_dut_intf, fanout_intf in fanout_host.host_to_fanout_port_map.items( ): full_dut_fanout_port_map[encoded_dut_intf] = { 'fanout_host': fanout_host, 'fanout_intf': fanout_intf } logger.debug( 'full_dut_fanout_port_map: {}'.format(full_dut_fanout_port_map)) for dut_intf in dut_intfs: encoded_dut_intf = encode_dut_port_name(tor_host.hostname, dut_intf) if encoded_dut_intf in full_dut_fanout_port_map: fanout_host = full_dut_fanout_port_map[encoded_dut_intf][ 'fanout_host'] fanout_intf = full_dut_fanout_port_map[encoded_dut_intf][ 'fanout_intf'] fanout_host.shutdown(fanout_intf) down_intfs.append((fanout_host, fanout_intf)) else: logger.error('No dut intf "{}" in full_dut_fanout_port_map'.format( encoded_dut_intf)) return down_intfs
def fanouthosts(ansible_adhoc, conn_graph_facts, creds): """ Shortcut fixture for getting Fanout hosts """ dev_conn = conn_graph_facts.get('device_conn', {}) fanout_hosts = {} # WA for virtual testbed which has no fanout try: for dut_host, value in dev_conn.items(): for dut_port in value.keys(): fanout_rec = value[dut_port] fanout_host = str(fanout_rec['peerdevice']) fanout_port = str(fanout_rec['peerport']) if fanout_host in fanout_hosts.keys(): fanout = fanout_hosts[fanout_host] else: host_vars = ansible_adhoc( ).options['inventory_manager'].get_host(fanout_host).vars os_type = host_vars.get('os', 'eos') admin_user = creds['fanout_admin_user'] admin_password = creds['fanout_admin_password'] # `fanout_network_user` and `fanout_network_password` are for # accessing the non-shell CLI of fanout. # Ansible will use this set of credentail for establishing # `network_cli` connection with device when applicable. network_user = creds.get('fanout_network_user', admin_user) network_password = creds.get('fanout_network_password', admin_password) shell_user = creds.get('fanout_shell_user', admin_user) shell_password = creds.get('fanout_shell_pass', admin_password) if os_type == 'sonic': shell_user = creds['fanout_sonic_user'] shell_password = creds['fanout_sonic_password'] fanout = FanoutHost(ansible_adhoc, os_type, fanout_host, 'FanoutLeaf', network_user, network_password, shell_user=shell_user, shell_passwd=shell_password) fanout.dut_hostnames = [dut_host] fanout_hosts[fanout_host] = fanout fanout.add_port_map(encode_dut_port_name(dut_host, dut_port), fanout_port) if dut_host not in fanout.dut_hostnames: fanout.dut_hostnames.append(dut_host) except: pass return fanout_hosts
def fanout_switch_port_lookup(fanout_switches, dut_name, dut_port): """ look up the fanout switch instance and the fanout switch port connecting to the dut_port Args: fanout_switches (list FanoutHost): list of fanout switch instances. dut_name (str): the host name of the DUT dut_port (str): port name on the DUT Returns: None, None if fanout switch instance and port is not found FanoutHost, Portname(str) if found """ dut_host_port = encode_dut_port_name(dut_name, dut_port) for _, fanout in fanout_switches.items(): if dut_host_port in fanout.host_to_fanout_port_map: return fanout, fanout.host_to_fanout_port_map[dut_host_port] return None, None