예제 #1
0
def _get_rest_neighbor_entries(dut, interface, is_arp=True):
    """
    This API returns ARP/NDP entries
    Author: Jagadish Chatrasi (jagadish.chatrasi@broadcom)
    :param dut:
    :param interfaces:
    :return:
    """
    retval = list()
    non_physical_ports = ['vlan']
    rest_urls = st.get_datastore(dut, 'rest_urls')
    intf_index = get_subinterface_index(dut, interface)
    if is_arp:
        if any(port in interface.lower() for port in non_physical_ports):
            url = rest_urls['get_arp_per_vlan_port'].format(name=interface)
        else:
            url = rest_urls['get_arp_per_port'].format(name=interface,
                                                       index=intf_index)
    else:
        if any(port in interface.lower() for port in non_physical_ports):
            url = rest_urls['get_ndp_per_vlan_port'].format(name=interface)
        else:
            url = rest_urls['get_ndp_per_port'].format(name=interface,
                                                       index=intf_index)
    intf = 'iface' if is_arp else 'interface'
    out = get_rest(dut, rest_url=url)
    arp_entries = out['output']['openconfig-if-ip:neighbors'][
        'neighbor'] if isinstance(out, dict) and out.get(
            'output'
        ) and 'openconfig-if-ip:neighbors' in out['output'] and out['output'][
            'openconfig-if-ip:neighbors'].get('neighbor') and isinstance(
                out['output']['openconfig-if-ip:neighbors']['neighbor'],
                list) else ''
    if arp_entries:
        for arp_entry in arp_entries:
            temp = dict()
            if isinstance(arp_entry, dict) and arp_entry.get('state'):
                arp = arp_entry['state']
                temp['address'] = arp['ip'] if arp.get('ip') else ''
                temp['macaddress'] = arp['link-layer-address'].lower(
                ) if arp.get('link-layer-address') else ''
                temp['count'] = ''
                if interface == 'eth0':
                    temp[intf] = 'Management0'
                    temp['vlan'] = '-'
                elif any(port in interface.lower()
                         for port in non_physical_ports):
                    temp[intf] = interface
                    egr_port = get_vlan_member(dut,
                                               vlan_list=interface.replace(
                                                   'Vlan', ''))
                    temp['vlan'] = egr_port[interface.replace(
                        'Vlan',
                        '')][0] if isinstance(egr_port, dict) and egr_port.get(
                            interface.replace('Vlan', '')) else '-'
                else:
                    temp[intf] = interface
                    temp['vlan'] = '-'
                retval.append(temp)
    return retval
예제 #2
0
def add_static_arp(dut, ipaddress, macaddress, interface="", cli_type=""):
    """
    To add static arp
    Author: Prudvi Mangadu ([email protected])
    :param dut:
    :param ipaddress:
    :param macaddress:
    :param interface:
    :return:
    """
    cli_type = st.get_ui_type(dut, cli_type=cli_type)
    command = ''
    if cli_type == "click":
        command = "arp -s {} {}".format(ipaddress, macaddress)
        if interface:
            command += " -i {}".format(interface)
    elif cli_type == "klish":
        if interface:
            intf = get_interface_number_from_name(interface)
            command = "interface {} {}".format(intf['type'], intf['number'])
            command = command + "\n" + "ip arp {} {}".format(
                ipaddress, macaddress) + "\n" + "exit"
        else:
            st.error(
                "'interface' option is mandatory for adding static arp entry in KLISH"
            )
            return False
    elif cli_type in ['rest-patch', 'rest-put']:
        if not interface:
            st.error(
                "'interface' option is mandatory for adding static arp entry in REST"
            )
            return False
        port_index = get_subinterface_index(dut, interface)
        rest_urls = st.get_datastore(dut, 'rest_urls')
        url = rest_urls['config_static_arp'].format(name=interface,
                                                    index=port_index)
        config_data = {
            "openconfig-if-ip:neighbor": [{
                "ip": ipaddress,
                "config": {
                    "ip": ipaddress,
                    "link-layer-address": macaddress
                }
            }]
        }
        if not config_rest(
                dut, rest_url=url, http_method=cli_type,
                json_data=config_data):
            st.error(
                "Failed to configure static ARP with IP: {}, MAC: {}, INTF: {}"
                .format(ipaddress, macaddress, interface))
            return False
    else:
        st.error("Unsupported CLI_TYPE: {}".format(cli_type))
        return False
    if command:
        st.config(dut, command, type=cli_type)
    return True
예제 #3
0
def config_static_ndp(dut,
                      ip6_address,
                      mac_address,
                      interface,
                      operation="add",
                      **kwargs):
    """
    Config static ndp
    Author: Chaitanya Vella ([email protected])
    :param dut:
    :param ip6_address:
    :param mac_address:
    :param interface:
    :param operation:
    :return:
    """
    cli_type = st.get_ui_type(dut, **kwargs)
    command = ''
    if cli_type == 'click':
        interface = st.get_other_names(
            dut, [interface])[0] if '/' in interface else interface
        oper = "replace" if operation == "add" else "del"
        command = "ip -6 neighbor {} {} lladdr {} dev {}".format(
            oper, ip6_address, mac_address, interface)
    elif cli_type == 'klish':
        command = list()
        intf = get_interface_number_from_name(interface)
        command.append('interface {} {}'.format(intf["type"], intf["number"]))
        cmd = 'ipv6 neighbor {} {}'.format(
            ip6_address, mac_address
        ) if operation == 'add' else 'no ipv6 neighbor {} {}'.format(
            ip6_address, mac_address)
        command.extend([cmd, 'exit'])
    elif cli_type in ['rest-patch', 'rest-put']:
        rest_urls = st.get_datastore(dut, 'rest_urls')
        port_index = get_subinterface_index(dut, interface)
        if operation == 'add':
            url = rest_urls['config_static_ndp'].format(name=interface,
                                                        index=port_index)
            config_data = {
                "openconfig-if-ip:neighbor": [{
                    "ip": ip6_address,
                    "config": {
                        "ip": ip6_address,
                        "link-layer-address": mac_address
                    }
                }]
            }
            if not config_rest(
                    dut, rest_url=url, http_method=cli_type,
                    json_data=config_data):
                st.error(
                    "Failed to configure static neighbor with IP: {} MAC: {} on INTF: {}"
                    .format(ip6_address, mac_address, interface))
                return False
        else:
            url = rest_urls['delete_static_ndp'].format(name=interface,
                                                        index=port_index,
                                                        ip=ip6_address)
            if not delete_rest(dut, rest_url=url):
                st.error(
                    "Failed to delete static neighbor with IP: {} MAC: {} on INTF: {}"
                    .format(ip6_address, mac_address, interface))
                return False
    else:
        st.error("Unsupported CLI Type: {}".format(cli_type))
        return False
    if command:
        st.config(dut, command, type=cli_type)
    return True
예제 #4
0
def delete_static_arp(dut,
                      ipaddress,
                      interface="",
                      mac="",
                      cli_type="",
                      vrf=""):
    """
    To delete static arp
    Author: Prudvi Mangadu ([email protected])
    :param dut:
    :param ipaddress:
    :param interface:
    :return:
    """
    cli_type = st.get_ui_type(dut, cli_type=cli_type)
    command = ''
    if cli_type == "click":
        command = "arp -d {} ".format(ipaddress)
        if interface:
            command += " -i {}".format(interface)
    elif cli_type == "klish":
        if interface:
            if mac:
                macaddress = mac
            else:
                output = show_arp(dut,
                                  ipaddress=ipaddress,
                                  interface=interface,
                                  vrf=vrf)
                if len(output) == 0:
                    st.error(
                        "Did not find static arp entry with IP : {} and Interface : {}"
                        .format(ipaddress, interface))
                    return False
                else:
                    macaddress = output[0]["macaddress"]
            intf = get_interface_number_from_name(interface)
            command = "interface {} {}".format(intf['type'], intf['number'])
            command = command + "\n" + "no ip arp {} {}".format(
                ipaddress, macaddress) + "\n" + "exit"
        else:
            st.error(
                "'interface' option is mandatory for deleting static arp entry in KLISH"
            )
            return False
    elif cli_type in ['rest-patch', 'rest-put']:
        if not interface:
            st.error(
                "'interface' option is mandatory for deleting static arp entry in REST"
            )
            return False
        port_index = get_subinterface_index(dut, interface)
        rest_urls = st.get_datastore(dut, 'rest_urls')
        url = rest_urls['delete_static_arp'].format(name=interface,
                                                    index=port_index,
                                                    ip=ipaddress)
        if not delete_rest(dut, rest_url=url):
            st.error(
                "Failed to delete static ARP with INTF: {},  IP: {}".format(
                    interface, ipaddress))
            return False
    else:
        st.error("Unsupported CLI_TYPE: {}".format(cli_type))
        return False
    if command:
        st.config(dut, command, type=cli_type)
    return True