import re


def parse_show_ip_ospf(raw_result):
    """
    Parse the 'show ip ospf' command raw output.

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

     ::

        {
            'router_id': '2.2.2.2',
            'no_of_area': '1',
            '0.0.0.0':
                {
                    'network_checksum': '0x00000000',
                    'no_of_active_interfaces': 1,
                    'area_id': '0.0.0.0',
                    'router_checksum': '0x00008d77',
                    'opaque_link_checksum': '0x00000000',
                    'opaque_link': 0,
                    'opaque_area': 0,
                    'abr_summary_lsa': 0,
                    'no_of_lsa': 2,
                    'asbr_summary_lsa': 0,
                    'no_of_interfaces': 1,
                    'opaque_area_checksum': '0x00000000',
                    'authentication_type': 'no authentication',
                    'nssa_checksum': '0x00000000',
                    'router_lsa': 2,
                    'abr_checksum': '0x00000000',
                    'network_lsa': 0,
                    'asbr_checksum': '0x00000000',
                    'nssa_lsa': 0
                }
        }
    """

    router_re = (
        r'[\S ]+Router\s*ID:\s*(?P<router_id>[\S]+).*'
        r'[\S ]+router:\s*(?P<no_of_area>\d+).*'
    )

    area_re = (
        r':\s*(?P<area_id>[0-255.]+).*'
        r'[\S ]+area:\s*Total:\s*(?P<no_of_interfaces>\d+),\s*Active:'
        '\s*?(?P<no_of_active_interfaces>\d+).*'
        r'[\S ]+Area\s*has\s*(?P<authentication_type>[\S ]+).*'
        r'[\S ]+LSA\s*(?P<no_of_lsa>\d+).*'
        r'[\S ]+router\s*LSA\s*(?P<router_lsa>\d+)[\S ]+Sum\s*'
        '(?P<router_checksum>[\S]+).*'
        r'[\S ]+network\s*LSA\s*(?P<network_lsa>\d+)[\S ]+Sum\s*'
        '(?P<network_checksum>[\S]+).*'
        r'[\S ]+ABR\s*summary\s*LSA\s*(?P<abr_summary_lsa>\d+)'
        '[\S ]+Sum\s*(?P<abr_checksum>[\S]+).*'
        r'[\S ]+ASBR\s*summary\s*LSA\s*(?P<asbr_summary_lsa>\d+)'
        '[\S ]+Sum\s*(?P<asbr_checksum>[\S]+).*'
        r'[\S ]+NSSA\s*LSA\s*(?P<nssa_lsa>\d+)[\S ]+Sum\s*'
        '(?P<nssa_checksum>[\S]+).*'
        r'[\S ]+opaque\s*link\s*(?P<opaque_link>\d+)[\S ]+Sum\s*'
        '(?P<opaque_link_checksum>[\S]+).*'
        r'[\S ]+opaque\s*area\s*(?P<opaque_area>\d+)[\S ]+Sum\s*'
        '(?P<opaque_area_checksum>[\S]+).*'
    )

    result = {}
    result_area = {}
    lines = []
    output = raw_result.split('Area ID')
    if(output):
        no_of_instance = len(output)
        for i in range(0, no_of_instance):
            if i == 0:
                re_result = re.match(router_re, output[i], re.DOTALL)
                if(re_result):
                    result = re_result.groupdict()
            if i <= int(result['no_of_area']):
                re_result = re.match(area_re, output[i], re.DOTALL)
                if(re_result):
                    partial = re_result.groupdict()
                    areas = partial['area_id']
                    lines.append(areas)
                    for key, value in partial.items():
                        if value and value.isdigit():
                            partial[key] = int(value)
                            result_area[partial['area_id']] = partial
                    result.update(result_area)
        return result