Пример #1
0
def _arista_ospf_converter(hostname: str(), cmd_outputs: list) -> OSPF:

    if cmd_outputs is None:
        return None

    ospf_vrf_lst = ListOSPFSessionsVRF(ospf_sessions_vrf_lst=list())
    router_id = ""
    inst = ""
    vrf = ""

    for cmd_output in cmd_outputs:

        if 'vrfs' in cmd_output.get('rid').keys():

            # Retrieve router ID from "show ip ospf | json"
            for vrf_name in cmd_output.get('rid').get('vrfs').keys():
                for instance, facts in cmd_output.get('rid').get('vrfs').get(
                        vrf_name).get('instList').items():
                    router_id = facts.get('routerId', NOT_SET)
                    inst = instance
                    vrf = vrf_name

            ospf_sessions_vrf = OSPFSessionsVRF(
                router_id=router_id,
                vrf_name=vrf,
                ospf_sessions_area_lst=ListOSPFSessionsArea(list()))

            session_by_area = dict()

            for adj in cmd_output.get('data').get('vrfs').get(vrf).get(
                    'instList').get(inst).get('ospfNeighborEntries'):

                ospf = OSPFSession(hostname=hostname,
                                   peer_rid=adj.get('routerId', NOT_SET),
                                   peer_hostname=NOT_SET,
                                   session_state=adj.get(
                                       'adjacencyState', NOT_SET),
                                   local_interface=_mapping_interface_name(
                                       adj.get('interfaceName', NOT_SET)),
                                   peer_ip=adj.get('interfaceAddress',
                                                   NOT_SET))

                if adj.get('details').get(
                        'areaId') not in session_by_area.keys():
                    session_by_area[adj.get('details').get('areaId')] = list()

                session_by_area[adj.get('details').get('areaId')].append(ospf)

            for area_id, sessions in session_by_area.items():
                ospf_sessions_vrf.ospf_sessions_area_lst.ospf_sessions_area_lst.append(
                    OSPFSessionsArea(area_number=area_id,
                                     ospf_sessions=ListOSPFSessions(
                                         ospf_sessions_lst=sessions)))

            ospf_vrf_lst.ospf_sessions_vrf_lst.append(ospf_sessions_vrf)

    return OSPF(hostname=hostname, ospf_sessions_vrf_lst=ospf_vrf_lst)
Пример #2
0
def _compare_ospf(host_keys, hostname, ospf_host_data:OSPF, ospf_yaml_data:json, level_test:int):

    ospf_sessions_vrf_lst = ListOSPFSessionsVRF(list())

    if OSPF_SESSIONS_HOST_KEY in host_keys and hostname in ospf_yaml_data.keys():

        for vrf_name, ospf_vrf_facts in ospf_yaml_data.get(hostname, NOT_SET).items():

            ospf_sessions_vrf = OSPFSessionsVRF(
                router_id=ospf_vrf_facts.get('router_id', NOT_SET),
                vrf_name=vrf_name,
                ospf_sessions_area_lst=ListOSPFSessionsArea(list())
            )

            for area_id, session_in_area in ospf_vrf_facts.get('area_id', NOT_SET).items():

                ospf_session_area = OSPFSessionsArea(
                    area_number=area_id,
                    ospf_sessions=ListOSPFSessions(list())
                )

                for neighbor in session_in_area:

                    if isinstance(neighbor,dict):

                        ospf = OSPFSession(
                            hostname=hostname,
                            peer_rid=neighbor.get('peer_rid', NOT_SET),
                            peer_hostname=neighbor.get('peer_name', NOT_SET),
                            session_state=neighbor.get('state', NOT_SET),
                            local_interface=_mapping_interface_name(neighbor.get('local_interface', NOT_SET)),
                            peer_ip=neighbor.get('peer_ip', NOT_SET),
                        )

                        ospf_session_area.ospf_sessions.ospf_sessions_lst.append(ospf)

                ospf_sessions_vrf.ospf_sessions_area_lst.ospf_sessions_area_lst.append(ospf_session_area)

            ospf_sessions_vrf_lst.ospf_sessions_vrf_lst.append(ospf_sessions_vrf)

        verity_ospf = OSPF(
            hostname=hostname,
            ospf_sessions_vrf_lst=ospf_sessions_vrf_lst
        )


        return verity_ospf == ospf_host_data


    else:
        print(f"{HEADER_GET} Key {OSPF_SESSIONS_HOST_KEY} is missing for {hostname} or verity file is empty for this host")
        return False
Пример #3
0
def _juniper_ospf_converter(hostname: str(), cmd_outputs: json) -> OSPF:

    if cmd_outputs is None:
        return None

    ospf_vrf_lst = ListOSPFSessionsVRF(list())

    for vrf in cmd_outputs:
        if OSPF_RIB_KEY in cmd_outputs.get(
                vrf).keys() and OSPF_NEI_KEY in cmd_outputs.get(vrf).keys():

            area_in_vrf_dict = dict()

            ospf_area_lst = ListOSPFSessionsArea(ospf_sessions_area_lst=list())

            for nei in cmd_outputs.get(vrf).get(OSPF_NEI_KEY).get(
                    "ospf-neighbor-information")[0].get("ospf-neighbor"):

                if nei.get("ospf-area")[0].get("data") not in area_in_vrf_dict:
                    area_in_vrf_dict[nei.get("ospf-area")[0].get(
                        "data")] = list()

                area_in_vrf_dict.get(
                    nei.get("ospf-area")[0].get("data")).append(
                        OSPFSession(
                            hostname=hostname,
                            peer_rid=nei.get("neighbor-id")[0].get("data"),
                            peer_hostname=NOT_SET,
                            session_state=nei.get(
                                "ospf-neighbor-state")[0].get("data"),
                            local_interface=nei.get("interface-name")[0].get(
                                "data"),
                            peer_ip=nei.get("neighbor-address")[0].get(
                                "data")))

            for area in area_in_vrf_dict:
                ospf_area_lst.ospf_sessions_area_lst.append(
                    OSPFSessionsArea(
                        area_number=area,
                        ospf_sessions=ListOSPFSessions(
                            ospf_sessions_lst=area_in_vrf_dict.get(area))))

        ospf_vrf_lst.ospf_sessions_vrf_lst.append(
            OSPFSessionsVRF(
                vrf_name=vrf,
                router_id=cmd_outputs.get(vrf).get(OSPF_RIB_KEY).get(
                    "ospf-overview-information")[0].get("ospf-overview")
                [0].get("ospf-router-id")[0].get("data", NOT_SET),
                ospf_sessions_area_lst=ospf_area_lst))

    return OSPF(hostname=hostname, ospf_sessions_vrf_lst=ospf_vrf_lst)
Пример #4
0
def _extreme_vsp_ospf_converter(hostname: str(), cmd_outputs: json) -> OSPF:

    if cmd_outputs is None:
        return None

    ospf_vrf_lst = ListOSPFSessionsVRF(list())

    formated_data = _extreme_vsp_format_data(cmd_outputs=cmd_outputs)

    for vrf in formated_data:

        ospf_area_lst = ListOSPFSessionsArea(ospf_sessions_area_lst=list())

        for area in formated_data.get(vrf):

            ospf_session_lst = ListOSPFSessions(ospf_sessions_lst=list())

            for interface in formated_data.get(vrf).get(area):

                ospf_session_lst.ospf_sessions_lst.append(
                    OSPFSession(hostname=hostname,
                                peer_rid=formated_data.get(vrf).get(area).get(
                                    interface).get('peer_rid', NOT_SET),
                                peer_hostname=NOT_SET,
                                session_state=formated_data.get(vrf).get(
                                    area).get(interface).get(
                                        'session_state', NOT_SET),
                                local_interface=_mapping_interface_name(
                                    _extreme_vsp_find_interface_name(
                                        interface_ip=interface,
                                        vrf_name=vrf,
                                        cmd_outputs=cmd_outputs)),
                                peer_ip=formated_data.get(vrf).get(area).get(
                                    interface).get('peer_ip', NOT_SET)))

            ospf_area_lst.ospf_sessions_area_lst.append(
                OSPFSessionsArea(area_number=area,
                                 ospf_sessions=ospf_session_lst))

        ospf_vrf_lst.ospf_sessions_vrf_lst.append(
            OSPFSessionsVRF(
                vrf_name=vrf,
                router_id=cmd_outputs.get(vrf).get(OSPF_RIB_KEY)[0][0],
                ospf_sessions_area_lst=ospf_area_lst))

    return OSPF(hostname=hostname, ospf_sessions_vrf_lst=ospf_vrf_lst)
Пример #5
0
def _nexus_ospf_converter(hostname: str(), cmd_outputs: list) -> OSPF:

    if cmd_outputs is None:
        return None

    ospf_vrf_lst = ListOSPFSessionsVRF(ospf_sessions_vrf_lst=list())

    for cmd_output in cmd_outputs:

        ospf_sessions_vrf = OSPFSessionsVRF(
            router_id=cmd_output.get('rid').get('TABLE_ctx').get(
                'ROW_ctx')[0].get('rid', NOT_SET),
            vrf_name=cmd_output.get('rid').get('TABLE_ctx').get(
                'ROW_ctx')[0].get('cname', NOT_SET),
            ospf_sessions_area_lst=ListOSPFSessionsArea(
                ospf_sessions_area_lst=list()))

        session_by_area = dict()

        for session in cmd_output.get('data').get('TABLE_ctx').get(
                'ROW_ctx').get('TABLE_nbr').get('ROW_nbr'):

            ospf = OSPFSession(hostname=hostname,
                               peer_rid=session.get('rid', NOT_SET),
                               peer_hostname=NOT_SET,
                               session_state=session.get('state', NOT_SET),
                               local_interface=_mapping_interface_name(
                                   session.get('intf', NOT_SET)),
                               peer_ip=session.get('addr', NOT_SET))

            if session.get('area') not in session_by_area.keys():
                session_by_area[session.get('area')] = list()

            session_by_area[session.get('area')].append(ospf)

        for area_id, sessions in session_by_area.items():

            ospf_sessions_vrf.ospf_sessions_area_lst.ospf_sessions_area_lst.append(
                OSPFSessionsArea(area_number=area_id,
                                 ospf_sessions=ListOSPFSessions(
                                     ospf_sessions_lst=sessions)))

        ospf_vrf_lst.ospf_sessions_vrf_lst.append(ospf_sessions_vrf)

    return OSPF(hostname=hostname, ospf_sessions_vrf_lst=ospf_vrf_lst)
Пример #6
0
def _ios_ospf_converter(hostname: str(), cmd_outputs: json) -> OSPF:

    if cmd_outputs is None:
        return None

    formated_data, mapping_router_id = _ios_format_data(
        cmd_outputs=cmd_outputs)

    ospf_vrf_lst = ListOSPFSessionsVRF(list())

    for vrf in formated_data:

        ospf_area_lst = ListOSPFSessionsArea(ospf_sessions_area_lst=list())

        for area in formated_data.get(vrf):

            ospf_session_lst = ListOSPFSessions(ospf_sessions_lst=list())

            for interface in formated_data.get(vrf).get(area):
                for peer in formated_data.get(vrf).get(area).get(interface):

                    ospf_session_lst.ospf_sessions_lst.append(
                        OSPFSession(hostname=hostname,
                                    peer_rid=peer.get('peer_rid'),
                                    peer_hostname=NOT_SET,
                                    session_state=peer.get(
                                        'session_state', NOT_SET),
                                    local_interface=interface,
                                    peer_ip=peer.get('peer_ip')))

            ospf_area_lst.ospf_sessions_area_lst.append(
                OSPFSessionsArea(area_number=area,
                                 ospf_sessions=ospf_session_lst))

        ospf_vrf_lst.ospf_sessions_vrf_lst.append(
            OSPFSessionsVRF(vrf_name=vrf,
                            router_id=mapping_router_id[vrf],
                            ospf_sessions_area_lst=ospf_area_lst))

    return OSPF(hostname=hostname, ospf_sessions_vrf_lst=ospf_vrf_lst)
Пример #7
0
def _cumulus_ospf_converter(hostname: str(), cmd_outputs: list) -> OSPF:

    if cmd_outputs is None:
        return None

    ospf_vrf_lst = ListOSPFSessionsVRF(ospf_sessions_vrf_lst=list())

    for cmd_output in cmd_outputs:

        if cmd_output.get('data', NOT_SET).get('neighbors',
                                               NOT_SET) is NOT_SET:
            ### VRF
            for vrf, ospf_facts in cmd_output.get('data', NOT_SET).items():
                vrf_name = vrf

            ospf_sessions_vrf = OSPFSessionsVRF(
                router_id=cmd_output.get('rid', NOT_SET).get(
                    vrf_name, NOT_SET).get('routerId', NOT_SET),
                vrf_name=vrf_name,
                ospf_sessions_area_lst=ListOSPFSessionsArea(
                    ospf_sessions_area_lst=list()))

            result_area = dict()

            for neighbor_rid, neighbors_facts in cmd_output.get(
                    'data', NOT_SET).get(vrf_name,
                                         NOT_SET).get('neighbors',
                                                      NOT_SET).items():

                ospf = OSPFSession(
                    hostname=hostname,
                    peer_rid=neighbor_rid,
                    peer_hostname=NOT_SET,
                    session_state=neighbors_facts[0].get('nbrState', NOT_SET),
                    local_interface=_mapping_interface_name(
                        neighbors_facts[0].get('ifaceName', NOT_SET)),
                    peer_ip=neighbors_facts[0].get('ifaceAddress', NOT_SET),
                )

                if neighbors_facts[0].get('areaId',
                                          NOT_SET) not in result_area.keys():
                    result_area[neighbors_facts[0].get(
                        'areaId', NOT_SET)] = OSPFSessionsArea(
                            area_number=neighbors_facts[0].get(
                                'areaId', NOT_SET),
                            ospf_sessions=ListOSPFSessions(list()))

                result_area.get(neighbors_facts[0].get(
                    'areaId',
                    NOT_SET)).ospf_sessions.ospf_sessions_lst.append(ospf)

            for area_id, ospf_sessions in result_area.items():
                ospf_sessions_vrf.ospf_sessions_area_lst.ospf_sessions_area_lst.append(
                    ospf_sessions)

        else:

            ospf_sessions_vrf = OSPFSessionsVRF(
                router_id=cmd_output.get('rid',
                                         NOT_SET).get('routerId', NOT_SET),
                vrf_name="default",
                ospf_sessions_area_lst=ListOSPFSessionsArea(
                    ospf_sessions_area_lst=list()))

            result_area = dict()

            for neighbor_rid, neighbors_facts in cmd_output.get(
                    'data', NOT_SET).get('neighbors', NOT_SET).items():

                ospf = OSPFSession(
                    hostname=hostname,
                    peer_rid=neighbor_rid,
                    peer_hostname=NOT_SET,
                    session_state=neighbors_facts[0].get('nbrState', NOT_SET),
                    local_interface=_mapping_interface_name(
                        neighbors_facts[0].get('ifaceName', NOT_SET)),
                    peer_ip=neighbors_facts[0].get('ifaceAddress', NOT_SET),
                )

                if neighbors_facts[0].get('areaId',
                                          NOT_SET) not in result_area.keys():
                    result_area[neighbors_facts[0].get(
                        'areaId', NOT_SET)] = OSPFSessionsArea(
                            area_number=neighbors_facts[0].get(
                                'areaId', NOT_SET),
                            ospf_sessions=ListOSPFSessions(list()))

                result_area.get(neighbors_facts[0].get(
                    'areaId',
                    NOT_SET)).ospf_sessions.ospf_sessions_lst.append(ospf)

            for area_id, ospf_sessions in result_area.items():
                ospf_sessions_vrf.ospf_sessions_area_lst.ospf_sessions_area_lst.append(
                    ospf_sessions)

        ospf_vrf_lst.ospf_sessions_vrf_lst.append(ospf_sessions_vrf)

    return OSPF(hostname=hostname, ospf_sessions_vrf_lst=ospf_vrf_lst)
Пример #8
0
def create_an_ospf_object_manually(context) -> None:
    """
    Create a OSPF object manually

    :param context:
    :return None:
    """

    ospf_vrf_lst = ListOSPFSessionsVRF(ospf_sessions_vrf_lst=list())

    ### VRF - Default
    ospf_area_lst = ListOSPFSessionsArea(ospf_sessions_area_lst=list())

    ###### Area 0.0.0.0
    ospf_session_lst = ListOSPFSessions(ospf_sessions_lst=list())

    ospf_session_lst.ospf_sessions_lst.append(
        OSPFSession(hostname="spine01",
                    peer_rid="10.255.255.203",
                    peer_ip="10.1.3.2",
                    local_interface="swp3",
                    session_state="Full"))

    ospf_session_lst.ospf_sessions_lst.append(
        OSPFSession(hostname="spine01",
                    peer_rid="10.255.255.204",
                    peer_ip="10.1.4.2",
                    local_interface="swp4",
                    session_state="Full"))

    ospf_area_lst.ospf_sessions_area_lst.append(
        OSPFSessionsArea(area_number="0.0.0.0",
                         ospf_sessions=ospf_session_lst))

    ospf_vrf_lst.ospf_sessions_vrf_lst.append(
        OSPFSessionsVRF(vrf_name="default",
                        router_id="10.255.255.101",
                        ospf_sessions_area_lst=ospf_area_lst))

    ### VRF - Default
    ospf_area_lst = ListOSPFSessionsArea(ospf_sessions_area_lst=list())

    ###### Area 0.0.0.100
    ospf_session_lst = ListOSPFSessions(ospf_sessions_lst=list())

    ospf_session_lst.ospf_sessions_lst.append(
        OSPFSession(hostname="spine01",
                    peer_rid="201.201.201.201",
                    peer_ip="10.0.5.201",
                    local_interface="eth0",
                    session_state="Full"))

    ospf_session_lst.ospf_sessions_lst.append(
        OSPFSession(hostname="spine01",
                    peer_rid="205.205.205.205",
                    peer_ip="10.0.5.205",
                    local_interface="eth0",
                    session_state="Full"))

    ospf_area_lst.ospf_sessions_area_lst.append(
        OSPFSessionsArea(area_number="0.0.0.100",
                         ospf_sessions=ospf_session_lst))

    ospf_vrf_lst.ospf_sessions_vrf_lst.append(
        OSPFSessionsVRF(vrf_name="mgmt",
                        router_id="10.0.5.101",
                        ospf_sessions_area_lst=ospf_area_lst))

    context.object_01 = OSPF(hostname="spine01",
                             ospf_sessions_vrf_lst=ospf_vrf_lst)