import re


def parse_show_sflow(raw_result):
    """
    Parse the 'show sflow' command raw output.

    :param str raw_result: vtysh raw result string.
    :rtype: dict
    :return: The parsed result of the show sflow command in a \
       dictionary of the form:

     ::

            {
                'sflow': 'enabled',
                'collector':[
                    {
                        'ip': '10.10.11.2',
                        'port': '6343',
                        'vrf': 'vrf_default'
                    },
                    {
                        'ip': '10.10.12.2',
                        'port': '6344',
                        'vrf': 'vrf_default'
                    }
                ],
                'agent_interface': '3',
                'agent_address_family': 'ipv4',
                'sampling_rate': 20,
                'polling_interval': 30,
                'header_size': 128,
                'max_datagram_size': 1400,
                'number_of_samples': 10
            }
    """

    sflow_info_re = (
         r'\s*sFlow\s*Configuration\s*'
         r'\s*-----------------------------------------\s*'
         r'\s*sFlow\s*(?P<sflow>\S+)\s*'
         r'Collector\sIP/Port/Vrf\s*(?P<collector>.+)'
         r'Agent\sInterface\s*(?P<agent_interface>.+)'
         r'Agent\sAddress\sFamily\s*(?P<agent_address_family>Not set|ipv4|ipv6)\s*'  # noqa
         r'Sampling\sRate\s*(?P<sampling_rate>\d+)\s*'
         r'Polling\sInterval\s*(?P<polling_interval>\d+)\s*'
         r'Header\sSize\s*(?P<header_size>\d+)\s*'
         r'Max\sDatagram\sSize\s*(?P<max_datagram_size>\d+)\s*'
         r'Number\sof\sSamples\s*(?P<number_of_samples>\d+)\s*'
    )

    re_result = re.match(sflow_info_re, raw_result, re.DOTALL)
    assert re_result

    result = re_result.groupdict()
    for key, value in result.items():
        if value and value.isdigit():
            result[key] = int(value)
    result['agent_interface'] = result['agent_interface'].strip()
    if str(result['collector']) != 'Not set':
        count = result['collector'].count('\n')
        result['collector'] = \
            result['collector'].split('\n', count - 1)
        result['collector'] = \
            [x.strip(' \n') for x in result['collector']]
        for i in range(0, count):
            result['collector'][i] = \
                result['collector'][i].split('/', 2)
            result['collector'][i] = \
                dict(zip(['ip', 'port', 'vrf'], result['collector'][i]))

    return result