コード例 #1
0
ファイル: dhcp_relay.py プロジェクト: zero804/sonic-mgmt
def _get_rest_dhcp_relay_statistics(dut, interface="", family='ipv4'):
    """
    To get the dhcp-relay statistics data
    Author Jagadish Chatrasi ([email protected])
    :param dut:
    :param interface:
    :param family:
    :return:
    """
    retval = list()
    rest_urls = st.get_datastore(dut, 'rest_urls')
    ip_string = '' if family == 'ipv4' else 'v6'
    if not interface:
        output = get_interface_ip_address(dut, family=family)
        interfaces = {entry['interface'] for entry in output}
        interfaces.discard('eth0')
    else:
        interfaces = make_list(interface)
    for intf in interfaces:
        url = rest_urls['get_dhcp{}_relay_counters'.format(ip_string)].format(id=intf)
        out = get_rest(dut, rest_url=url)
        if isinstance(out, dict) and out.get('output') and out['output'].get('openconfig-relay-agent:counters') and isinstance(out['output']['openconfig-relay-agent:counters'], dict):
            data = out['output']['openconfig-relay-agent:counters']
            temp = dict()
            if family == 'ipv4':
                temp['bootrequest_msgs_received_by_the_relay_agent'] = str(data['bootrequest-received']) if data.get('bootrequest-received') else '0'
                temp['bootrequest_msgs_forwarded_by_the_relay_agent'] = str(data['bootrequest-sent']) if data.get('bootrequest-sent') else '0'
                temp['bootreply_msgs_forwarded_by_the_relay_agent'] = str(data['bootreply-sent']) if data.get('bootreply-sent') else '0'
                temp['dhcp_ack_msgs_sent_by_the_relay_agent'] = str(data['dhcp-ack-sent']) if data.get('dhcp-ack-sent') else '0'
                temp['dhcp_decline_msgs_received_by_the_relay_agent'] = str(data['dhcp-decline-received']) if data.get('dhcp-decline-received') else '0'
                temp['dhcp_discover_msgs_received_by_the_relay_agent'] = str(data['dhcp-discover-received']) if data.get('dhcp-discover-received') else '0'
                temp['dhcp_inform_msgs_received_by_the_relay_agent'] = str(data['dhcp-inform-received']) if data.get('dhcp-inform-received') else '0'
                temp['dhcp_nack_msgs_sent_by_the_relay_agent'] = str(data['dhcp-nack-sent']) if data.get('dhcp-nack-sent') else '0'
                temp['dhcp_offer_msgs_sent_by_the_relay_agent'] = str(data['dhcp-offer-sent']) if data.get('dhcp-offer-sent') else '0'
                temp['dhcp_release_msgs_received_by_the_relay_agent'] = str(data['dhcp-release-received']) if data.get('dhcp-release-received') else '0'
                temp['dhcp_request_msgs_received_by_the_relay_agent'] = str(data['dhcp-request-received']) if data.get('dhcp-request-received') else '0'
                temp['number_of_dhcp_pkts_drpd_due_to_an_invd_opcode'] = str(data['invalid-opcode']) if data.get('invalid-opcode') else '0'
                temp['number_of_dhcp_pkts_drpd_due_to_an_invd_option'] = str(data['invalid-options']) if data.get('invalid-options') else '0'
                temp['total_nbr_of_dhcp_pkts_drpd_by_the_relay_agent'] = str(data['total-dropped']) if data.get('total-dropped') else '0'
            else:
                temp['dhcpv6_advt_msgs_sent_by_the_relay_agent'] = str(data['dhcpv6-adverstise-sent']) if data.get('dhcpv6-adverstise-sent') else '0'
                temp['dhcpv6_confirm_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-confirm-received']) if data.get('dhcpv6-confirm-received') else '0'
                temp['dhcpv6_decline_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-decline-received']) if data.get('dhcpv6-decline-received') else '0'
                temp['dhcpv6_info_rqst_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-info-request-received']) if data.get('dhcpv6-info-request-received') else '0'
                temp['dhcpv6_rebind_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-rebind-received']) if data.get('dhcpv6-rebind-received') else '0'
                temp['dhcpv6_reconfig_msgs_sent_by_the_relay_agent'] = str(data['dhcpv6-reconfigure-sent']) if data.get('dhcpv6-reconfigure-sent') else '0'
                temp['dhcpv6_relay_fwd_msgs_sent_by_the_relay_agent'] = str(data['dhcpv6-relay-forw-sent']) if data.get('dhcpv6-relay-forw-sent') else '0'
                temp['dhcpv6_relay_reply_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-relay-reply-received']) if data.get('dhcpv6-relay-reply-received') else '0'
                temp['dhcpv6_release_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-release-received']) if data.get('dhcpv6-release-received') else '0'
                temp['dhcpv6_reply_msgs_sent_by_the_relay_agent'] = str(data['dhcpv6-reply-sent']) if data.get('dhcpv6-reply-sent') else '0'
                temp['dhcpv6_rqst_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-request-received']) if data.get('dhcpv6-request-received') else '0'
                temp['dhcpv6_solic_msgs_rcvd_by_the_relay_agent'] = str(data['dhcpv6-solicit-received']) if data.get('dhcpv6-solicit-received') else '0'
                temp['number_of_dhcpv6_pkts_drpd_due_to_an_inv_opcode'] = str(data['invalid-opcode']) if data.get('invalid-opcode') else '0'
                temp['number_of_dhcpv6_pkts_drpd_due_to_an_inv_option'] = str(data['invalid-options']) if data.get('invalid-options') else '0'
                temp['total_nbr_of_dhcpv6_pkts_drpd_by_the_relay_agent'] = str(data['total-dropped']) if data.get('total-dropped') else '0'
            retval.append(temp)
    st.debug(retval)
    return retval
コード例 #2
0
ファイル: dhcp_relay.py プロジェクト: zero804/sonic-mgmt
def verify_dhcp_relay(dut, interface, dhcp_relay_addr, family="ipv4", cli_type=""):
    """
    API to verify DHCP RELAY configuration
    Author Chaitanya Vella ([email protected])
    :param dut:
    :param interface:
    :param dhcp_relay_addr:
    """
    cli_type = st.get_ui_type(dut, cli_type=cli_type)
    output = dhcp_relay_show(dut, family=family, interface=interface, cli_type=cli_type)
    dhcp_relay_address = make_list(dhcp_relay_addr)
    filter=list()
    for address in dhcp_relay_address:
        match = {"intf": interface, "dhcprelay_addr": address}
        filter.append(match)
    entries = filter_and_select(output, ["intf"], filter)
    return True if entries else False
コード例 #3
0
def clear_logging(dut, thread=True):
    """
    Clear all logging
    Author: Prudvi Mangadu ([email protected])

    :param dut: list
    :param thread: true
    :return:
    """
    def _clear_logging(dut):
        for each_log in log_files:
            command = "sudo truncate -s 0 {}".format(each_log)
            st.config(dut, command)
        return True

    dut_li = utils.make_list(dut)
    [out, _] = putils.exec_foreach(thread, dut_li, _clear_logging)
    return False if False in out else True
コード例 #4
0
ファイル: mirroring.py プロジェクト: zero804/sonic-mgmt
def config_max_sessions(dut, **kwargs):
    """
    API to configure max mirror sessions
    Author: Chaitanya Vella ([email protected])
    :param dut:
    :param kwargs: {"cli_ype":"rest","data":[{"name":"Mirror1","src_ip":"10.20.3.1","dst_ip":"10.23.3.5",
    "gre_type":"0x855","dscp":16,"ttl":5,"queue":6,"dst_port":"Ethernet28","src_port":"Ethernet20",
    "direction":"rx/tx"},{"name":"Mirror2","dst_port":"Ethernet20","src_port":"Ethernet22","direction":"rx"},
    {"name":"Mirror3","dst_port":"Ethernet26","src_port":"Ethernet22","direction":"tx"}],"action":"config"}
    :return: response/False
    """
    cli_type = kwargs.get("cli_type", "rest")
    if cli_type == "rest":
        status = 204
        data = kwargs.get("data")
        action = data.get("action", "config")
        rest_url = "/restconf/data/{}".format(YANG_MODEL)
        if action == "config":
            if data.get("data"):
                rest_data = dict()
                rest_data[YANG_MODEL] = dict()
                rest_data[YANG_MODEL]["MIRROR_SESSION"] = dict()
                rest_data[YANG_MODEL]["MIRROR_SESSION"][
                    "MIRROR_SESSION_LIST"] = make_list(data.get("data"))
                response = st.rest_modify(dut, rest_url, rest_data)
            else:
                st.log("Required data not found -- {}".format(data))
                return False
        elif action == "unconfig":
            response = st.rest_delete(dut, rest_url)
        elif action == "get":
            response = st.rest_read(dut, rest_url)
            status = 200
        else:
            st.log("Unsupporte ACTION -- {}".format(action))
            return False
        if response and response["status"] == status:
            return response
        else:
            st.log("RESPONSE -- {}".format(response))
            return False
    else:
        st.log("UNSUPPORTED CLI TYPE -- {}".format(cli_type))
        return False
コード例 #5
0
ファイル: dhcp_relay.py プロジェクト: zero804/sonic-mgmt
def _get_rest_detailed_dhcp_relay_data(dut, interface="", family='ipv4'):
    """
    To get the dhcp-relay detailed data
    Author Jagadish Chatrasi ([email protected])
    :param dut:
    :param interface:
    :param family:
    :return:
    """
    retval = list()
    rest_urls = st.get_datastore(dut, 'rest_urls')
    ip_string = '' if family == 'ipv4' else 'v6'
    if not interface:
        output = get_interface_ip_address(dut, family=family)
        interfaces = {entry['interface'] for entry in output}
        interfaces.discard('eth0')
    else:
        interfaces = make_list(interface)
    for intf in interfaces:
        url = rest_urls['dhcp{}_relay_config'.format(ip_string)].format(id=intf)
        out = get_rest(dut, rest_url=url)
        if isinstance(out, dict) and out.get('output') and out['output'].get('openconfig-relay-agent:config') and isinstance(out['output']['openconfig-relay-agent:config'], dict):
            data = out['output']['openconfig-relay-agent:config']
            temp = dict()
            temp['intf'] = intf
            temp['server_addr'] = ", ".join(data['helper-address']) if data.get('helper-address') and isinstance(data['helper-address'], list) else ''
            temp['vrf_name'] = data['openconfig-relay-agent-ext:vrf'] if data.get('openconfig-relay-agent-ext:vrf') else 'Not Configured'
            temp['src_interface'] = data['openconfig-relay-agent-ext:src-intf'] if data.get('openconfig-relay-agent-ext:src-intf') else 'Not Configured'
            temp['vrf_select'] = data['openconfig-relay-agent-ext:vrf-select'].lower() if data.get('openconfig-relay-agent-ext:vrf-select') else 'disable'
            temp['max_hop_count'] = str(data['openconfig-relay-agent-ext:max-hop-count']) if data.get('openconfig-relay-agent-ext:max-hop-count') else '10'
            if family == 'ipv4':
                temp['policy_action'] = data['openconfig-relay-agent-ext:policy-action'].lower() if data.get('openconfig-relay-agent-ext:policy-action') else 'discard'
                temp['link_select'] = data['openconfig-relay-agent-ext:link-select'].lower() if data.get('openconfig-relay-agent-ext:link-select') else 'disable'
            retval.append(temp)
    st.debug(retval)
    return retval
コード例 #6
0
ファイル: dhcp_relay.py プロジェクト: zero804/sonic-mgmt
def dhcp_relay_config(dut, **kwargs):
    """
    API for DHCP relay configuration
    Author Chaitanya Vella ([email protected])
    :param dut:
    :param kwargs:
    :return:
    """
    cli_type = st.get_ui_type(dut, **kwargs)
    interface = kwargs.get("vlan", kwargs.get("interface", None))
    ip_address = make_list(kwargs.get('IP', []))
    ip_addr_lst = " ".join(ip_address)
    ip_family = kwargs.get("family", "ipv4")
    skip_error_check = kwargs.get("skip_error_check", False)
    action = kwargs.get("action","add")
    if not interface:
        st.error("Required key 'interface' is not passed")
        return False
    command = ""
    if cli_type == "click":
        if ip_family == "ipv4":
            command = "config interface ip dhcp-relay {} {} {}".format(action, interface, ip_addr_lst)
        else:
            command = "config interface ipv6 dhcp-relay {} {} {}".format(action, interface, ip_addr_lst)
        if 'link_select' in kwargs:
            link_select = 'enable'
            command += " -link-select={}".format(link_select)
        if 'src_interface' in kwargs:
            src_interface = kwargs['src_interface']
            command += " -src-intf={}".format(src_interface)
        if 'max_hop_count' in kwargs:
            max_hop_count = kwargs['max_hop_count']
            command += " -max-hop-count={}".format(max_hop_count)
        if 'vrf_name' in kwargs and action == 'add':
            vrf_name = kwargs['vrf_name']
            command += " -vrf-name={}".format(vrf_name)
        if 'vrf_select' in kwargs:
            vrf_select = kwargs['vrf_select']
            command += " -vrf-select={}".format(vrf_select)
    elif cli_type == "klish":
        if ip_family not in ["ipv4", "ipv6"]:
            st.error("INVALID IP FAMILY -- {}".format(ip_family))
            return False
        command = list()
        interface_data = get_interface_number_from_name(interface)
        command.append("interface {} {}".format(interface_data.get("type"), interface_data.get("number")))
        no_form = "" if action == "add" else "no"
        ip_string = "ip" if ip_family == "ipv4" else "ipv6"
        if kwargs.get("link_select") and not kwargs.get("src_interface"):
            st.log("SRC INTF needed for LINK SELECT operation")
            return False
        if ip_addr_lst:
            cmd = "{} {} dhcp-relay {}".format(no_form, ip_string, ip_addr_lst)
            if 'vrf_name' in kwargs and action == 'add':
                cmd += " vrf-name {}".format(kwargs['vrf_name'])
            command.append(cmd)
        if 'src_interface' in kwargs:
            src_interface = kwargs['src_interface']
            command.append("{} {} dhcp-relay source-interface {}".format(no_form, ip_string, src_interface))
        if 'link_select' in kwargs:
            command.append("{} {} dhcp-relay link-select".format(no_form, ip_string))
        if 'max_hop_count' in kwargs:
            max_hop_count = kwargs['max_hop_count']
            command.append("{} {} dhcp-relay max-hop-count {}".format(no_form, ip_string, max_hop_count))
        if 'vrf_select' in kwargs:
            vrf_select = kwargs['vrf_select']
            command.append("{} {} dhcp-relay vrf-select {}".format(no_form, ip_string, vrf_select))
    elif cli_type in ["rest-patch", "rest-put"]:
        if ip_family not in ["ipv4", "ipv6"]:
            st.error("INVALID IP FAMILY -- {}".format(ip_family))
            return False
        ip_string = "" if ip_family == "ipv4" else "v6"
        if kwargs.get("link_select") and not kwargs.get("src_interface"):
            st.log("SRC INTF needed for LINK SELECT operation")
            return False
        config_data = {"openconfig-relay-agent:config": {"id": interface}}
        rest_urls = st.get_datastore(dut, 'rest_urls')
        if ip_address:
            if action == 'add':
                config_data["openconfig-relay-agent:config"].update({"helper-address": ip_address})
                if kwargs.get('vrf_name'):
                    config_data["openconfig-relay-agent:config"].update({"openconfig-relay-agent-ext:vrf": kwargs['vrf_name']})
            else:
                for ip in ip_address:
                    if not delete_rest(dut, rest_url=rest_urls['dhcp{}_relay_address_config'.format(ip_string)].format(id=interface, helper_address=ip)):
                        st.error("Failed to delete DHCP-Relay Helper-Address: {}".format(ip))
                        return False
        if 'src_interface' in kwargs:
            if action == 'add':
                config_data["openconfig-relay-agent:config"].update({"openconfig-relay-agent-ext:src-intf": kwargs['src_interface']})
            else:
                if not delete_rest(dut, rest_url=rest_urls['dhcp{}_relay_src_intf_config'.format(ip_string)].format(id=interface)):
                    st.error("Failed to delete DHCP-Relay source-interface on interface: {}".format(interface))
                    return False
        if 'link_select' in kwargs:
            if action == 'add':
                config_data["openconfig-relay-agent:config"].update({"openconfig-relay-agent-ext:link-select": "ENABLE"})
            else:
                if not delete_rest(dut, rest_url=rest_urls['dhcp{}_relay_link_select_config'.format(ip_string)].format(id=interface)):
                    st.error("Failed to delete DHCP-Relay link-select")
                    return False
        if 'max_hop_count' in kwargs:
            if action == 'add':
                config_data["openconfig-relay-agent:config"].update({"openconfig-relay-agent-ext:max-hop-count": int(kwargs['max_hop_count'])})
            else:
                if not delete_rest(dut, rest_url=rest_urls['dhcp{}_relay_max_hop_count_config'.format(ip_string)].format(id=interface)):
                    st.error("Failed to delete DHCP-Relay max-hop-count on interface: {}".format(interface))
                    return False
        if 'vrf_select' in kwargs:
            if action == 'add':
                config_data["openconfig-relay-agent:config"].update({"openconfig-relay-agent-ext:vrf-select": "ENABLE"})
            else:
                if not delete_rest(dut, rest_url=rest_urls['dhcp{}_relay_vrf_select_config'.format(ip_string)].format(id=interface)):
                    st.error("Failed to delete DHCP-Relay vrf-select on interface: {}".format(interface))
                    return False
        if 'policy_action' in kwargs:
            if action == 'add':
                config_data["openconfig-relay-agent:config"].update({"openconfig-relay-agent-ext:policy-action": kwargs['policy_action'].upper()})
            else:
                if not delete_rest(dut, rest_url=rest_urls['dhcp{}_relay_policy_action_config'.format(ip_string)].format(id=interface)):
                    st.error("Failed to delete DHCP-Relay policy_action on interface: {}".format(interface))
                    return False
        if len(config_data["openconfig-relay-agent:config"]) > 1:
            if not config_rest(dut, rest_url=rest_urls['dhcp{}_relay_config'.format(ip_string)].format(id=interface), http_method=cli_type, json_data=config_data):
                st.error("Failed to configure DHCP-Relay parameters")
                return False
    else:
        st.error("Unsupported CLI_TYPE: {}".format(cli_type))
        return False
    if command:
        st.debug("command is {}".format(command))
        output = st.config(dut, command, skip_error_check=skip_error_check, type=cli_type)
        if "Error" in output:
            if skip_error_check:
                return True
            else:
                return False
    return True