Esempio n. 1
0
def _juniper_bgp_api_converter(hostname: str,
                               cmd_output: dict,
                               options={}) -> BGP:

    bgp_sessions_vrf_lst = ListBGPSessionsVRF(list())

    for k, v in cmd_output.items():
        if not isinstance(v.get('bgp'), dict):
            v['bgp'] = format_xml_output(v.get('bgp'))
        if not isinstance(v.get('rid'), dict):
            v['rid'] = format_xml_output(v.get('rid'))
        if verbose_mode(user_value=os.environ.get('NETESTS_VERBOSE', NOT_SET),
                        needed_value=LEVEL3):
            printline()
            PP.pprint(v)

        bgp_sessions_lst = ListBGPSessions(list())

        if ('bgp' in v.keys() and 'bgp-information' in v.get('bgp').keys()
                and v.get('bgp').get('bgp-information') is not None
                and 'bgp-peer' in v.get('bgp').get('bgp-information').keys()):
            for n in v.get('bgp').get('bgp-information').get('bgp-peer'):
                bgp_sessions_lst.bgp_sessions.append(
                    BGPSession(src_hostname=hostname,
                               peer_ip=n.get('peer-address', NOT_SET),
                               peer_hostname=NOT_SET,
                               remote_as=n.get('peer-as', NOT_SET),
                               state_brief=get_bgp_state_brief(
                                   n.get('peer-state', NOT_SET)),
                               session_state=n.get('peer-state', NOT_SET),
                               state_time=NOT_SET,
                               prefix_received=NOT_SET,
                               options=options))
                as_number = n.get('bgp-option-information').get('local-as')

            router_id = NOT_SET
            if ('rid' in v.keys()
                    and 'instance-information' in v.get('rid').keys()
                    and 'instance-core'
                    in v.get('rid').get('instance-information').keys()):
                router_id = v.get('rid') \
                             .get('instance-information') \
                             .get('instance-core') \
                             .get('router-id')

            bgp_sessions_vrf_lst.bgp_sessions_vrf.append(
                BGPSessionsVRF(vrf_name=k,
                               as_number=as_number,
                               router_id=router_id,
                               bgp_sessions=bgp_sessions_lst))

    bgp = BGP(hostname=hostname, bgp_sessions_vrf_lst=bgp_sessions_vrf_lst)

    if verbose_mode(user_value=os.environ.get('NETESTS_VERBOSE', NOT_SET),
                    needed_value=LEVEL1):
        printline()
        print(f'>>>>> {hostname}')
        PP.pprint(bgp.to_json())

    return bgp
Esempio n. 2
0
def _juniper_facts_api_converter(hostname: str(), cmd_output,
                                 options={}) -> Facts:

    interfaces_lst = list()
    if FACTS_INT_DICT_KEY in cmd_output.keys():
        for i in format_xml_output(cmd_output.get(FACTS_INT_DICT_KEY)).get(
                'interface-information').get('physical-interface'):
            interfaces_lst.append(i.get('name'))

    hostname = NOT_SET
    version = NOT_SET
    model = NOT_SET
    if FACTS_SYS_DICT_KEY in cmd_output.keys():
        i = format_xml_output(cmd_output.get(FACTS_SYS_DICT_KEY))
        hostname = i.get('software-information') \
                    .get('host-name', NOT_SET)
        version = i.get('software-information') \
                   .get('junos-version', NOT_SET)
        model = i.get('software-information') \
                 .get('product-name', NOT_SET).upper()

    serial = NOT_SET
    if FACTS_SERIAL_DICT_KEY in cmd_output.keys():
        i = format_xml_output(cmd_output.get(FACTS_SERIAL_DICT_KEY))
        serial = i.get('chassis-inventory') \
                  .get('chassis') \
                  .get('serial-number', NOT_SET)

    memory = NOT_SET
    if FACTS_MEMORY_DICT_KEY in cmd_output.keys():
        i = format_xml_output(cmd_output.get(FACTS_MEMORY_DICT_KEY))
        memory = i.get('system-memory-information') \
                  .get('system-memory-summary-information') \
                  .get('system-memory-total', NOT_SET)

    facts = Facts(
        hostname=hostname,
        domain=NOT_SET,
        version=version,
        build=NOT_SET,
        serial=serial,
        base_mac=NOT_SET,
        memory=memory,
        vendor="Juniper",
        model=model,
        interfaces_lst=interfaces_lst,
        options=options,
    )

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL1):
        printline()
        PP.pprint(facts.to_json())

    return facts
Esempio n. 3
0
def _juniper_facts_netconf_converter(hostname: str(), cmd_output,
                                     options={}) -> Facts:

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL5):
        printline()
        PP.pprint(cmd_output.get(FACTS_SYS_DICT_KEY))
        print(ElementTree.tostring(cmd_output.get(FACTS_INT_DICT_KEY)))

    interfaces_lst = list()
    for i in format_xml_output(cmd_output.get(FACTS_INT_DICT_KEY)).get(
            'interface-information').get('physical-interface'):
        interfaces_lst.append(i.get('name'))

    facts = Facts(
        hostname=cmd_output.get(FACTS_SYS_DICT_KEY).get('hostname', NOT_SET),
        domain=cmd_output.get(FACTS_SYS_DICT_KEY).get('domain', NOT_SET),
        build=NOT_SET,
        version=cmd_output.get(FACTS_SYS_DICT_KEY).get('version', NOT_SET),
        serial=cmd_output.get(FACTS_SYS_DICT_KEY).get('serialnumber', NOT_SET),
        base_mac=NOT_SET,
        memory=NOT_SET,
        vendor="Juniper",
        model=cmd_output.get(FACTS_SYS_DICT_KEY).get('model', NOT_SET).upper(),
        interfaces_lst=interfaces_lst,
        options=options)

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL1):
        printline()
        PP.pprint(facts.to_json())

    return facts
Esempio n. 4
0
def _juniper_vrf_api_converter(hostname: str,
                               cmd_output: list,
                               options={}) -> ListVRF:
    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL5):
        printline()
        print(type(cmd_output))
        print(cmd_output)

    cmd_output = format_xml_output(cmd_output)

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL4):
        printline()
        print(type(cmd_output))
        PP.pprint(cmd_output)

    vrf_list = ListVRF(vrf_lst=list())

    for vrf in cmd_output.get('instance-information') \
            .get('instance-core'):
        if _juniper_vrf_filter(vrf.get('instance-name')):
            rd = NOT_SET,
            rt_imp = NOT_SET
            rt_exp = NOT_SET
            imp_targ = NOT_SET
            exp_targ = NOT_SET
            if "instance-vrf" in vrf.keys():
                rd = vrf.get('instance-vrf') \
                        .get('route-distinguisher', NOT_SET)
                rt_imp = vrf.get('instance-vrf') \
                            .get('vrf-import', NOT_SET)
                rt_exp = vrf.get('instance-vrf') \
                            .get('vrf-export', NOT_SET)
                imp_targ = vrf.get('instance-vrf') \
                              .get('vrf-import-target', NOT_SET)
                exp_targ = vrf.get('instance-vrf') \
                              .get('vrf-export-target', NOT_SET)

            vrf_list.vrf_lst.append(
                VRF(vrf_name=_juniper_vrf_default_mapping(
                    vrf.get('instance-name')),
                    vrf_id=vrf.get('router-id', NOT_SET),
                    vrf_type=vrf.get('instance-type', NOT_SET),
                    l3_vni=NOT_SET,
                    rd=rd,
                    rt_imp=rt_imp,
                    rt_exp=rt_exp,
                    imp_targ=imp_targ,
                    exp_targ=exp_targ,
                    options=options))

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL1):
        printline()
        print(f">>>>> {hostname}")
        PP.pprint(vrf_list.to_json())

    return vrf_list
Esempio n. 5
0
def _ios_bgp_netconf_converter(hostname: str,
                               cmd_output: dict,
                               options={}) -> BGP:

    bgp_sessions_vrf_lst = ListBGPSessionsVRF(list())
    cmd_output = format_xml_output(cmd_output)

    if verbose_mode(user_value=os.environ.get('NETESTS_VERBOSE', NOT_SET),
                    needed_value=LEVEL3):
        printline()
        print(cmd_output)

    if ('data' in cmd_output.keys()
            and 'bgp-state-data' in cmd_output.get('data').keys()
            and 'address-families'
            in cmd_output.get('data').get('bgp-state-data').keys()
            and 'address-family' in cmd_output.get('data').get(
                'bgp-state-data').get('address-families').keys()):
        for v in cmd_output.get('data') \
                           .get('bgp-state-data') \
                           .get('address-families') \
                           .get('address-family'):

            bgp_sessions_lst = ListBGPSessions(list())

            if ('bgp-neighbor-summaries' in v.keys() and 'bgp-neighbor-summary'
                    in v.get('bgp-neighbor-summaries').keys()):
                if isinstance(
                        v.get('bgp-neighbor-summaries').get(
                            'bgp-neighbor-summary'), list):
                    for n in v.get('bgp-neighbor-summaries') \
                            .get('bgp-neighbor-summary'):
                        bgp_sessions_lst.bgp_sessions.append(
                            BGPSession(src_hostname=hostname,
                                       peer_ip=n.get('id', NOT_SET),
                                       peer_hostname=NOT_SET,
                                       remote_as=n.get('as', NOT_SET),
                                       state_brief=get_bgp_state_brief(
                                           state=n.get('state', NOT_SET)),
                                       session_state=n.get('state', NOT_SET),
                                       state_time=n.get('up-time', NOT_SET),
                                       prefix_received=n.get(
                                           'prefixes-received', 0),
                                       options=options))

                elif isinstance(
                        v.get('bgp-neighbor-summaries').get(
                            'bgp-neighbor-summary'), dict):
                    bgp_sessions_lst.bgp_sessions.append(
                        BGPSession(
                            src_hostname=hostname,
                            peer_ip=v.get('bgp-neighbor-summaries').get(
                                'bgp-neighbor-summary').get('id', NOT_SET),
                            peer_hostname=NOT_SET,
                            remote_as=v.get('bgp-neighbor-summaries').get(
                                'bgp-neighbor-summary').get('as', NOT_SET),
                            state_brief=get_bgp_state_brief(
                                state=v.get('bgp-neighbor-summaries').get(
                                    'bgp-neighbor-summary').get(
                                        'state', NOT_SET)),
                            session_state=v.get('bgp-neighbor-summaries').get(
                                'bgp-neighbor-summary').get('state', NOT_SET),
                            state_time=v.get('bgp-neighbor-summaries').get(
                                'bgp-neighbor-summary').get(
                                    'up-time', NOT_SET),
                            prefix_received=v.get('bgp-neighbor-summaries').
                            get('bgp-neighbor-summary').get(
                                'prefixes-received', 0),
                            options=options))

            bgp_sessions_vrf_lst.bgp_sessions_vrf.append(
                BGPSessionsVRF(
                    vrf_name=v.get('vrf-name'),
                    as_number=v.get('local-as'),
                    router_id=v.get('router-id'),
                    bgp_sessions=bgp_sessions_lst,
                ))

    bgp = BGP(hostname=hostname, bgp_sessions_vrf_lst=bgp_sessions_vrf_lst)

    if verbose_mode(user_value=os.environ.get('NETESTS_VERBOSE', NOT_SET),
                    needed_value=LEVEL1):
        printline()
        print(f'>>>>> {hostname}')
        PP.pprint(bgp.to_json())

    return bgp
Esempio n. 6
0
def _ios_vrf_api_converter(
    hostname: str(),
    cmd_output,
    options={}
) -> ListVRF:
    if verbose_mode(
        user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
        needed_value=LEVEL5
    ):
        printline()
        print(type(cmd_output))
        print(cmd_output)

    cmd_output = format_xml_output(cmd_output)

    vrf_list = ListVRF(vrf_lst=list())
    vrf_list.vrf_lst.append(
        VRF(
            vrf_name='default',
            vrf_id=NOT_SET,
            vrf_type=NOT_SET,
            l3_vni=NOT_SET,
            rd=NOT_SET,
            rt_imp=NOT_SET,
            rt_exp=NOT_SET,
            imp_targ=NOT_SET,
            exp_targ=NOT_SET,
            options=options
        )
    )

    if (
        'native' in cmd_output.keys() and
        'vrf' in cmd_output.get('native').keys() and
        'definition' in cmd_output.get('native').get('vrf').keys()
    ):
        if isinstance(cmd_output.get('native')
                                .get('vrf')
                                .get('definition'), dict):

            rt_imp = NOT_SET
            rt_exp = NOT_SET
            if 'route-target' in cmd_output.get('native') \
                                           .get('vrf') \
                                           .get('definition').keys():
                if (
                    'export' in cmd_output.get('native')
                                          .get('vrf')
                                          .get('definition')
                                          .get('route-target').keys() and
                    'asn-ip' in cmd_output.get('native')
                                          .get('vrf')
                                          .get('definition')
                                          .get('route-target')
                                          .get('export').keys()
                ):
                    rt_exp = cmd_output.get('native') \
                                       .get('vrf') \
                                       .get('definition') \
                                       .get('route-target') \
                                       .get('export') \
                                       .get('asn-ip')
                if (
                    'import' in cmd_output.get('native')
                                          .get('vrf')
                                          .get('definition')
                                          .get('route-target').keys() and
                    'asn-ip' in cmd_output.get('native')
                                          .get('vrf')
                                          .get('definition')
                                          .get('route-target')
                                          .get('import').keys()
                ):
                    rt_exp = cmd_output.get('native') \
                                       .get('vrf') \
                                       .get('definition') \
                                       .get('route-target') \
                                       .get('import') \
                                       .get('asn-ip')

            vrf_list.vrf_lst.append(
                VRF(
                    vrf_name=cmd_output.get('native')
                                       .get('vrf')
                                       .get('definition')
                                       .get('name'),
                    vrf_id=NOT_SET,
                    vrf_type=NOT_SET,
                    l3_vni=NOT_SET,
                    rd=cmd_output.get('native')
                                 .get('vrf')
                                 .get('definition')
                                 .get('rd', NOT_SET),
                    rt_imp=rt_imp,
                    rt_exp=rt_exp,
                    imp_targ=NOT_SET,
                    exp_targ=NOT_SET
                )
            )

        elif isinstance(cmd_output.get('native')
                                  .get('vrf')
                                  .get('definition'), list):
            for v in cmd_output.get('native') \
                               .get('vrf') \
                               .get('definition'):
                rt_imp = NOT_SET
                rt_exp = NOT_SET
                if 'route-target' in v.keys():
                    if (
                        'export' in v.get('route-target').keys() and
                        'asn-ip' in v.get('route-target').get('export').keys()
                    ):
                        rt_exp = v.get(
                            'route-target').get('export').get('asn-ip')
                    if (
                        'import' in v.get('route-target').keys() and
                        'asn-ip' in v.get('route-target').get('export').keys()
                    ):
                        rt_imp = v.get(
                            'route-target').get('import').get('asn-ip')

                vrf_list.vrf_lst.append(
                    VRF(
                        vrf_name=v.get('name', NOT_SET),
                        vrf_id=NOT_SET,
                        vrf_type=NOT_SET,
                        l3_vni=NOT_SET,
                        rd=v.get('rd', NOT_SET),
                        rt_imp=rt_imp,
                        rt_exp=rt_exp,
                        imp_targ=NOT_SET,
                        exp_targ=NOT_SET
                    )
                )

    if verbose_mode(
        user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
        needed_value=LEVEL1
    ):
        printline()
        print(f">>>>> {hostname}")
        PP.pprint(vrf_list.to_json())

    return vrf_list
Esempio n. 7
0
def _nxos_vrf_api_converter(hostname: str(), cmd_output,
                            options={}) -> ListVRF:
    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL5):
        printline()
        print(type(cmd_output))
        print(cmd_output)

    cmd_output = format_xml_output(cmd_output)

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL4):
        printline()
        print(type(cmd_output))
        PP.pprint(cmd_output)

    vrf_list = ListVRF(vrf_lst=list())

    for v in cmd_output.get('inst-items').get('Inst-list'):

        rd = NOT_SET
        rt_exp = NOT_SET
        rt_imp = NOT_SET
        if ('dom-items' in v.keys() and 'Dom-list' in v.get('dom-items')
                and 'af-items' in v.get('dom-items').get('Dom-list').keys()
                and 'DomAf-list'
                in v.get('dom-items').get('Dom-list').get('af-items').keys()
                and 'ctrl-items' in v.get('dom-items').get('Dom-list').get(
                    'af-items').get('DomAf-list').keys()
                and 'AfCtrl-list' in v.get('dom-items').get('Dom-list').get(
                    'af-items').get('DomAf-list').get('ctrl-items').keys()
                and 'rttp-items'
                in v.get('dom-items').get('Dom-list').get('af-items').get(
                    'DomAf-list').get('ctrl-items').get('AfCtrl-list').keys()
                and 'RttP-list' in v.get('dom-items').get('Dom-list').get(
                    'af-items').get('DomAf-list').get('ctrl-items').get(
                        'AfCtrl-list').get('rttp-items').keys()):
            for i in v.get('dom-items') \
                      .get('Dom-list') \
                      .get('af-items') \
                      .get('DomAf-list') \
                      .get('ctrl-items') \
                      .get('AfCtrl-list') \
                      .get('rttp-items') \
                      .get('RttP-list'):
                if i.get('type', NOT_SET) == 'export':
                    if ('ent-items' in i.keys()
                            and 'RttEntry-list' in i.get('ent-items').keys()
                            and 'rtt'
                            in i.get('ent-items').get('RttEntry-list').keys()):
                        y = i.get('ent-items') \
                             .get('RttEntry-list') \
                             .get('rtt').split(':')
                        rt_exp = f"{y[-2]}:{y[-1]}"

                if i.get('type', NOT_SET) == 'import':
                    if ('ent-items' in i.keys()
                            and 'RttEntry-list' in i.get('ent-items').keys()
                            and 'rtt'
                            in i.get('ent-items').get('RttEntry-list').keys()):
                        z = i.get('ent-items') \
                             .get('RttEntry-list') \
                             .get('rtt').split(':')
                        rt_imp = f"{z[-2]}:{z[-1]}"

        if ('dom-items' in v.keys() and 'Dom-list' in v.get('dom-items')
                and 'rd' in v.get('dom-items').get('Dom-list').keys()):
            t = v.get('dom-items') \
                 .get('Dom-list') \
                 .get('rd').split(':')
            rd = f"{t[-2]}:{t[-1]}"

        vrf_list.vrf_lst.append(
            VRF(vrf_name=v.get('name', NOT_SET),
                vrf_id=v.get('id', NOT_SET),
                vrf_type=NOT_SET,
                l3_vni=v.get('encap', NOT_SET),
                rd=rd,
                rt_imp=rt_imp,
                rt_exp=rt_exp,
                imp_targ=NOT_SET,
                exp_targ=NOT_SET,
                options=options))

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL1):
        printline()
        print(f">>>>> {hostname}")
        PP.pprint(vrf_list.to_json())

    return vrf_list
Esempio n. 8
0
def _arista_vrf_netconf_converter(
    hostname: str(),
    cmd_output,
    options={}
) -> ListVRF:

    cmd_output = format_xml_output(cmd_output)

    if verbose_mode(
        user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
        needed_value=LEVEL3
    ):
        printline()
        PP.pprint(cmd_output)

    if (
        'data' in cmd_output.keys() and
        'network-instances' in cmd_output.get('data').keys() and
        'network-instance' in cmd_output.get('data')
                                        .get('network-instances')
                                        .keys()
    ):
        vrf_list = ListVRF(list())

        if isinstance(
            cmd_output.get('data')
                      .get('network-instances')
                      .get('network-instance'),
            dict
        ):

            vrf_list.vrf_lst.append(
                VRF(
                    vrf_name=cmd_output.get('data')
                                       .get('network-instances')
                                       .get('network-instance')
                                       .get('name', NOT_SET),
                    vrf_id=NOT_SET,
                    vrf_type=NOT_SET,
                    l3_vni=NOT_SET,
                    rd=NOT_SET,
                    rt_imp=NOT_SET,
                    rt_exp=NOT_SET,
                    imp_targ=NOT_SET,
                    exp_targ=NOT_SET,
                    options=options
                )
            )

        elif isinstance(
            cmd_output.get('data')
                      .get('network-instances')
                      .get('network-instance'),
            list
        ):
            for v in cmd_output.get('data') \
                               .get('network-instances') \
                               .get('network-instance'):
                vrf_list.vrf_lst.append(
                    VRF(
                        vrf_name=v.get('name', NOT_SET),
                        vrf_id=NOT_SET,
                        vrf_type=NOT_SET,
                        l3_vni=NOT_SET,
                        rd=NOT_SET,
                        rt_imp=NOT_SET,
                        rt_exp=NOT_SET,
                        imp_targ=NOT_SET,
                        exp_targ=NOT_SET,
                        options=options
                    )
                )

    if verbose_mode(
        user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
        needed_value=LEVEL1
    ):
        printline()
        print(f">>>>> {hostname}")
        PP.pprint(vrf_list.to_json())

    return vrf_list
Esempio n. 9
0
def _iosxr_vrf_netconf_converter(
    hostname: str(),
    cmd_output,
    options={}
) -> ListVRF:
    if verbose_mode(
        user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
        needed_value=LEVEL5
    ):
        printline()
        print(type(cmd_output))
        PP.pprint(cmd_output)

    if cmd_output.get('VRF') is not None:
        cmd_output['VRF'] = format_xml_output(cmd_output.get('VRF'))
    if cmd_output.get('BGP') is not None:
        cmd_output['BGP'] = format_xml_output(cmd_output.get('BGP'))

    if verbose_mode(
        user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
        needed_value=LEVEL4
    ):
        printline()
        print(type(cmd_output))
        PP.pprint(cmd_output)

    vrf_list = ListVRF(vrf_lst=list())

    if (
        'VRF' in cmd_output.keys() and
        'data' in cmd_output.get('VRF').keys() and
        'vrfs' in cmd_output.get('VRF').get('data').keys() and
        'vrf' in cmd_output.get('VRF').get('data').get('vrfs').keys()
    ):
        if isinstance(
            cmd_output.get('VRF').get('data').get('vrfs').get('vrf'),
            dict
        ):
            rias = None
            riin = None
            reas = None
            rein = None
            v = cmd_output.get('VRF').get('data').get('vrfs').get('vrf')
            if (
                    'afs' in v.keys() and
                    'af' in v.get('afs').keys() and
                    'bgp' in v.get('afs').get('af').keys()
            ):
                rias = v.get('afs').get('af').get('bgp') \
                                             .get('import-route-targets') \
                                             .get('route-targets') \
                                             .get('route-target') \
                                             .get('as-or-four-byte-as') \
                                             .get('as')

                riin = v.get('afs').get('af').get('bgp') \
                                             .get('import-route-targets') \
                                             .get('route-targets') \
                                             .get('route-target') \
                                             .get('as-or-four-byte-as') \
                                             .get('as-index')

                reas = v.get('afs').get('af').get('bgp') \
                                             .get('export-route-targets') \
                                             .get('route-targets') \
                                             .get('route-target') \
                                             .get('as-or-four-byte-as') \
                                             .get('as')

                rein = v.get('afs').get('af').get('bgp') \
                                             .get('export-route-targets') \
                                             .get('route-targets') \
                                             .get('route-target') \
                                             .get('as-or-four-byte-as') \
                                             .get('as-index')

            rd = NOT_SET
            if (
                cmd_output.get('BGP') is not None and
                'data' in cmd_output.get('BGP').keys() and
                'bgp' in cmd_output.get('BGP')
                                .get('data').keys() and
                'instance' in cmd_output.get('BGP')
                                        .get('data')
                                        .get('bgp').keys() and
                'instance-as' in cmd_output.get('BGP')
                                        .get('data')
                                        .get('bgp')
                                        .get('instance').keys() and
                'four-byte-as' in cmd_output.get('BGP')
                                        .get('data')
                                        .get('bgp')
                                        .get('instance')
                                        .get('instance-as').keys() and
                'vrfs' in cmd_output.get('BGP')
                                    .get('data')
                                    .get('bgp')
                                    .get('instance')
                                    .get('instance-as')
                                    .get('four-byte-as').keys() and
                'vrf' in cmd_output.get('BGP')
                                    .get('data')
                                    .get('bgp')
                                    .get('instance')
                                    .get('instance-as')
                                    .get('four-byte-as')
                                    .get('vrfs').keys()
            ):
                if isinstance(cmd_output.get('BGP')
                                        .get('data')
                                        .get('bgp')
                                        .get('instance')
                                        .get('instance-as')
                                        .get('four-byte-as')
                                        .get('vrfs')
                                        .get('vrf'), dict):
                    vrf = cmd_output.get('BGP') \
                                    .get('data') \
                                    .get('bgp') \
                                    .get('instance') \
                                    .get('instance-as') \
                                    .get('four-byte-as') \
                                    .get('vrfs') \
                                    .get('vrf')
                    if vrf.get('vrf-name') == v.get('vrf-name'):
                        if (
                            'vrf-global' in vrf.keys() and
                            'route-distinguisher' in vrf.get('vrf-global')
                                                            .keys()
                        ):
                            rd = vrf.get('vrf-global') \
                                    .get('route-distinguisher') \
                                    .get('as') + ":" + \
                                vrf.get('vrf-global') \
                                    .get('route-distinguisher') \
                                    .get('as-index')

                elif isinstance(cmd_output.get('BGP')
                                          .get('data')
                                          .get('bgp')
                                          .get('instance')
                                          .get('instance-as')
                                          .get('four-byte-as')
                                          .get('vrfs')
                                          .get('vrf'), list):

                    for vrf in cmd_output.get('BGP') \
                                         .get('data') \
                                         .get('bgp') \
                                         .get('instance') \
                                         .get('instance-as') \
                                         .get('four-byte-as') \
                                         .get('vrfs') \
                                         .get('vrf'):
                        if vrf.get('vrf-name') == v.get('vrf-name'):
                            if (
                                'vrf-global' in vrf.keys() and
                                'route-distinguisher' in
                                vrf.get('vrf-global').keys()
                            ):
                                rd = vrf.get('vrf-global') \
                                        .get('route-distinguisher') \
                                        .get('as') + ":" + \
                                    vrf.get('vrf-global') \
                                        .get('route-distinguisher') \
                                        .get('as-index')
            vrf_list.vrf_lst.append(
                VRF(
                    vrf_name=v.get('vrf-name'),
                    vrf_id=NOT_SET,
                    vrf_type=NOT_SET,
                    l3_vni=NOT_SET,
                    rd=rd,
                    rt_imp=f"{rias}:{riin}" if rias is not None else NOT_SET,
                    rt_exp=f"{reas}:{rein}" if reas is not None else NOT_SET,
                    imp_targ=NOT_SET,
                    exp_targ=NOT_SET,
                    options=options
                )
            )

        elif isinstance(
            cmd_output.get('VRF').get('data').get('vrfs').get('vrf'),
            list
        ):
            for v in cmd_output.get('VRF').get('data').get('vrfs').get('vrf'):
                rias = None
                riin = None
                reas = None
                rein = None
                if (
                    'afs' in v.keys() and 'af' in v.get('afs').keys() and
                    'bgp' in v.get('afs').get('af').keys()
                ):
                    rias = v.get('afs').get('af').get('bgp') \
                                                 .get('import-route-targets') \
                                                 .get('route-targets') \
                                                 .get('route-target') \
                                                 .get('as-or-four-byte-as') \
                                                 .get('as')

                    riin = v.get('afs').get('af').get('bgp') \
                                                 .get('import-route-targets') \
                                                 .get('route-targets') \
                                                 .get('route-target') \
                                                 .get('as-or-four-byte-as') \
                                                 .get('as-index')

                    reas = v.get('afs').get('af').get('bgp') \
                                                 .get('export-route-targets') \
                                                 .get('route-targets') \
                                                 .get('route-target') \
                                                 .get('as-or-four-byte-as') \
                                                 .get('as')

                    rein = v.get('afs').get('af').get('bgp') \
                                                 .get('export-route-targets') \
                                                 .get('route-targets') \
                                                 .get('route-target') \
                                                 .get('as-or-four-byte-as') \
                                                 .get('as-index')

                rd = NOT_SET
                if (
                    cmd_output.get('BGP') is not None and
                    'data' in cmd_output.get('BGP').keys() and
                    'bgp' in cmd_output.get('BGP')
                                    .get('data').keys() and
                    'instance' in cmd_output.get('BGP')
                                            .get('data')
                                            .get('bgp').keys() and
                    'instance-as' in cmd_output.get('BGP')
                                            .get('data')
                                            .get('bgp')
                                            .get('instance').keys() and
                    'four-byte-as' in cmd_output.get('BGP')
                                            .get('data')
                                            .get('bgp')
                                            .get('instance')
                                            .get('instance-as').keys() and
                    'vrfs' in cmd_output.get('BGP')
                                        .get('data')
                                        .get('bgp')
                                        .get('instance')
                                        .get('instance-as')
                                        .get('four-byte-as').keys() and
                    'vrf' in cmd_output.get('BGP')
                                        .get('data')
                                        .get('bgp')
                                        .get('instance')
                                        .get('instance-as')
                                        .get('four-byte-as')
                                        .get('vrfs').keys()
                ):
                    if isinstance(cmd_output.get('BGP')
                                            .get('data')
                                            .get('bgp')
                                            .get('instance')
                                            .get('instance-as')
                                            .get('four-byte-as')
                                            .get('vrfs')
                                            .get('vrf'), dict):
                        vrf = cmd_output.get('BGP') \
                                        .get('data') \
                                        .get('bgp') \
                                        .get('instance') \
                                        .get('instance-as') \
                                        .get('four-byte-as') \
                                        .get('vrfs') \
                                        .get('vrf')
                        if vrf.get('vrf-name') == v.get('vrf-name'):
                            if (
                                'vrf-global' in vrf.keys() and
                                'route-distinguisher' in vrf.get('vrf-global')
                                                                .keys()
                            ):
                                rd = vrf.get('vrf-global') \
                                        .get('route-distinguisher') \
                                        .get('as') + ":" + \
                                    vrf.get('vrf-global') \
                                        .get('route-distinguisher') \
                                        .get('as-index')

                    elif isinstance(cmd_output.get('BGP')
                                              .get('data')
                                              .get('bgp')
                                              .get('instance')
                                              .get('instance-as')
                                              .get('four-byte-as')
                                              .get('vrfs')
                                              .get('vrf'), list):

                        for vrf in cmd_output.get('BGP') \
                                             .get('data') \
                                             .get('bgp') \
                                             .get('instance') \
                                             .get('instance-as') \
                                             .get('four-byte-as') \
                                             .get('vrfs') \
                                             .get('vrf'):
                            if vrf.get('vrf-name') == v.get('vrf-name'):
                                if (
                                    'vrf-global' in vrf.keys() and
                                    'route-distinguisher' in
                                    vrf.get('vrf-global').keys()
                                ):
                                    rd = vrf.get('vrf-global') \
                                            .get('route-distinguisher') \
                                            .get('as') + ":" + \
                                        vrf.get('vrf-global') \
                                            .get('route-distinguisher') \
                                            .get('as-index')

                vrf_list.vrf_lst.append(
                    VRF(
                        vrf_name=v.get('vrf-name'),
                        vrf_id=NOT_SET,
                        vrf_type=NOT_SET,
                        l3_vni=NOT_SET,
                        rd=rd,
                        rt_imp=f"{rias}:{riin}"
                                    if rias is not None else NOT_SET,
                        rt_exp=f"{reas}:{rein}"
                                    if reas is not None else NOT_SET,
                        imp_targ=NOT_SET,
                        exp_targ=NOT_SET,
                        options=options
                    )
                )

    if verbose_mode(
        user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
        needed_value=LEVEL1
    ):
        printline()
        print(f">>>>> {hostname}")
        PP.pprint(vrf_list.to_json())

    return vrf_list
Esempio n. 10
0
def _ios_facts_netconf_converter(hostname: str,
                                 cmd_output,
                                 options={}) -> Facts:

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL5):
        printline()
        print(type(cmd_output))
        print(cmd_output)

    cmd_output = format_xml_output(cmd_output)

    hostname = NOT_SET
    domain = NOT_SET
    version = NOT_SET
    model = NOT_SET
    serial = NOT_SET
    interfaces_lst = list()
    cmd_output = format_xml_output(cmd_output)
    if isinstance(cmd_output, dict) and 'data' in cmd_output.keys():
        hostname = cmd_output.get('data') \
                             .get('native') \
                             .get('hostname')
        domain = cmd_output.get('data') \
                           .get('native') \
                           .get('ip') \
                           .get('domain') \
                           .get('name')
        version = cmd_output.get('data') \
                            .get('native') \
                            .get('version')
        serial = cmd_output.get('data') \
                           .get('native') \
                           .get('license') \
                           .get('udi') \
                           .get('sn')
        model = cmd_output.get('data') \
                          .get('native') \
                          .get('license') \
                          .get('udi') \
                          .get('pid')
        for t in cmd_output.get('data') \
                           .get('native') \
                           .get('interface').keys():
            for i in cmd_output.get('data') \
                               .get('native') \
                               .get('interface') \
                               .get(t):
                interfaces_lst.append(f"{t}{i.get('name')}")

    facts = Facts(hostname=hostname,
                  domain=domain,
                  version=version,
                  build=NOT_SET,
                  serial=serial,
                  base_mac=NOT_SET,
                  memory=NOT_SET,
                  vendor="Cisco",
                  model=model,
                  interfaces_lst=interfaces_lst,
                  options=options)

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL1):
        printline()
        PP.pprint(facts.to_json())

    return facts