def _arista_ipv6_converter(hostname: str(), plateform: str(), cmd_output: json, *, filters=dict()) -> ListIPV6Interface: if cmd_output is None or cmd_output == "": return None ipv6_addresses_lst = ListIPV6Interface(ipv6_addresses_lst=list()) if "interfaces" in cmd_output.keys(): for interface_name in cmd_output.get("interfaces"): if _generic_interface_filter( plateform=plateform, interface_name=_mapping_interface_name(interface_name), filters=filters): for address in cmd_output.get("interfaces").get( interface_name).get("addresses"): index_slash = str(address.get("subnet", NOT_SET)).find("/") ipv6_addresses_lst.ipv6_addresses_lst.append( IPV6Interface(interface_name=_mapping_interface_name( interface_name), ip_address_with_mask=address.get( "address", NOT_SET), netmask=str( address.get("subnet", NOT_SET))[index_slash + 1:])) return ipv6_addresses_lst
def _napalm_lldp_converter(hostname: str(), cmd_output: json) -> ListLLDP: if cmd_output is None or cmd_output == "": return None lldp_neighbors_lst = ListLLDP(lldp_neighbors_lst=list()) if "get_lldp_neighbors_detail" in cmd_output.keys(): for interface_name, facts in cmd_output.get( "get_lldp_neighbors_detail", NOT_SET).items(): for neighbors in facts: lldp_neighbors_lst.lldp_neighbors_lst.append( LLDP(local_name=hostname, local_port=_mapping_interface_name(interface_name), neighbor_mgmt_ip=NOT_SET, neighbor_name=neighbors.get('remote_system_name', NOT_SET), neighbor_port=_mapping_interface_name( neighbors.get('remote_port', NOT_SET)), neighbor_os=neighbors.get('remote_system_description', NOT_SET), neighbor_type=_mapping_sys_capabilities( neighbors.get('remote_system_capab', NOT_SET)))) return lldp_neighbors_lst
def _cumulus_ipv4_converter(hostname: str(), plateform: str(), cmd_output: json, *, filters=dict()) -> ListIPV4Interface: ipv4_addresses_lst = ListIPV4Interface(hostname=hostname, ipv4_addresses_lst=list()) for interface_name, facts in cmd_output.items(): # v0 = vrrp / vrr / hsrp / HA ip address # bridge is a Linux bridge if _generic_interface_filter( interface_name=_mapping_interface_name(interface_name), plateform=plateform, filters=filters): for ip_address_in_interface in facts.get("iface_obj").get( 'ip_address').get('allentries'): if "/128" not in ip_address_in_interface and "/64" not in ip_address_in_interface and \ "/48" not in ip_address_in_interface and "::" not in ip_address_in_interface and \ "127.0.0.1" not in ip_address_in_interface and "::1/128" not in ip_address_in_interface: ipv4_obj = IPV4Interface( interface_name=_mapping_interface_name(interface_name), ip_address_with_mask=ip_address_in_interface) ipv4_addresses_lst.ipv4_addresses_lst.append(ipv4_obj) return ipv4_addresses_lst
def _nexus_cdp_converter(hostname:str(), cmd_output:json) -> ListCDP: cdp_neighbors_lst = ListCDP(list()) if "TABLE_cdp_neighbor_detail_info" in cmd_output.keys(): for cdp_neighbor in cmd_output.get('TABLE_cdp_neighbor_detail_info', NOT_SET).get( "ROW_cdp_neighbor_detail_info", NOT_SET): neighbor_type_lst = list() for sys_capability in cdp_neighbor.get("capability", NOT_SET): neighbor_type_lst.append(_mapping_sys_capabilities(sys_capability)) lldp_obj = CDP( local_name=hostname, local_port=_mapping_interface_name(cdp_neighbor.get("intf_id", NOT_SET)), neighbor_mgmt_ip=cdp_neighbor.get("v4addr", NOT_SET), neighbor_name=cdp_neighbor.get("device_id", NOT_SET), neighbor_port=_mapping_interface_name(cdp_neighbor.get("port_id", NOT_SET)), neighbor_os=cdp_neighbor.get("version", NOT_SET), neighbor_type=neighbor_type_lst ) cdp_neighbors_lst.cdp_neighbors_lst.append(lldp_obj) return cdp_neighbors_lst
def _juniper_lldp_converter(hostname: str(), cmd_output: dict) -> ListLLDP: if cmd_output is None or cmd_output == "": return None lldp_neighbors_lst = ListLLDP(lldp_neighbors_lst=list()) if "lldp-neighbors-information" in cmd_output.keys(): if "lldp-neighbor-information" in cmd_output.get( "lldp-neighbors-information")[0].keys(): for neighbors in cmd_output.get("lldp-neighbors-information")[ 0].get("lldp-neighbor-information"): lldp_neighbors_lst.lldp_neighbors_lst.append( LLDP(local_name=hostname, local_port=_mapping_interface_name( neighbors.get("lldp-local-port-id")[0].get( "data")), neighbor_mgmt_ip=NOT_SET, neighbor_name=neighbors.get("lldp-remote-system-name") [0].get("data"), neighbor_port=_mapping_interface_name( neighbors.get("lldp-remote-port-id")[0].get( "data")), neighbor_os=NOT_SET, neighbor_type=NOT_SET)) return lldp_neighbors_lst
def _nexus_lldp_converter(hostname: str(), cmd_output: json) -> ListLLDP: if cmd_output is None or cmd_output == "": return None lldp_neighbors_lst = ListLLDP(lldp_neighbors_lst=list()) if "TABLE_nbor_detail" in cmd_output.keys(): for lldp_neighbor in cmd_output.get('TABLE_nbor_detail', NOT_SET).get( "ROW_nbor_detail", NOT_SET): neighbor_type_lst = list() for sys_capability in lldp_neighbor.get("system_capability", NOT_SET): neighbor_type_lst.append( _mapping_sys_capabilities(sys_capability)) lldp_neighbors_lst.lldp_neighbors_lst.append( LLDP(local_name=hostname, local_port=_mapping_interface_name( lldp_neighbor.get("l_port_id", NOT_SET)), neighbor_mgmt_ip=lldp_neighbor.get("mgmt_addr", NOT_SET), neighbor_name=lldp_neighbor.get("sys_name", NOT_SET), neighbor_port=_mapping_interface_name( lldp_neighbor.get("port_id", NOT_SET)), neighbor_os=lldp_neighbor.get("sys_desc", NOT_SET), neighbor_type=neighbor_type_lst)) return lldp_neighbors_lst
def _ios_cdp_converter(hostname:str(), cmd_output:json) -> ListCDP: cdp_neighbors_lst = ListCDP(list()) for cdp_neighbor in cmd_output: neighbor_type_lst = list() for sys_capability in cdp_neighbor[6].split(","): neighbor_type_lst.append( _mapping_sys_capabilities( str(sys_capability).capitalize() ) ) cdp_neighbors_lst.cdp_neighbors_lst.append( CDP( local_name=hostname, local_port=_mapping_interface_name( cdp_neighbor[4] ), neighbor_mgmt_ip=NOT_SET, neighbor_name=cdp_neighbor[0], neighbor_port=_mapping_interface_name( cdp_neighbor[3] ), neighbor_os=cdp_neighbor[5], neighbor_type=neighbor_type_lst ) ) return cdp_neighbors_lst
def _napalm_ipv4_converter(hostname: str(), plateform: str(), cmd_output: json, *, filters=dict()) -> ListIPV4Interface: ipv4_addresses_lst = ListIPV4Interface(hostname=hostname, ipv4_addresses_lst=list()) for interface_name in cmd_output.get('get_interfaces_ip'): if _generic_interface_filter( interface_name=_mapping_interface_name(interface_name), plateform=plateform, filters=filters): for ip_addr in cmd_output.get('get_interfaces_ip').get( interface_name).get('ipv4'): ipv4_addresses_lst.ipv4_addresses_lst.append( IPV4Interface( interface_name=_mapping_interface_name(interface_name), ip_address_with_mask=ip_addr, netmask=cmd_output.get('get_interfaces_ip').get( interface_name).get('ipv4').get(ip_addr).get( 'prefix_length'))) print(ipv4_addresses_lst) return ipv4_addresses_lst
def _ios_ipv4_converter(hostname: str(), plateform: str(), cmd_output: list, *, filters=dict()) -> ListIPV4Interface: ipv4_addresses_lst = ListIPV4Interface(hostname=hostname, ipv4_addresses_lst=list()) for interface in cmd_output: # In textfsm -> IPADDR & MASK are list # Value List IPADDR (\S+?) # Value List MASK (\d*) for index, ip_addr in enumerate(interface[3]): if _generic_interface_filter( interface_name=_mapping_interface_name(interface[0]), plateform=plateform, filters=filters): ipv4_addresses_lst.ipv4_addresses_lst.append( IPV4Interface(interface_name=_mapping_interface_name( interface[0]), ip_address_with_mask=ip_addr, netmask=interface[4][index])) return ipv4_addresses_lst
def _ios_format_data(cmd_outputs: json) -> json: """ This function will retrieve data from differents commands outputs and gegroup them in a structured data format. Three command are executed on devices -> show ip ospf neighbor detail -> show ip ospf interface -> show ip ospf :param cmd_outputs: :return json: Structured data """ return_val = dict() result = dict() mapping_instance_vrf = dict() router_id = dict() if OSPF_RIB_KEY in cmd_outputs.keys(): for vrf in cmd_outputs.get(OSPF_RIB_KEY): result[vrf[0]] = dict() if vrf[2] == '': return_val['default'] = dict() mapping_instance_vrf[vrf[0]] = 'default' router_id['default'] = vrf[1] else: return_val[vrf[2]] = dict() mapping_instance_vrf[vrf[0]] = vrf[2] router_id[vrf[2]] = vrf[1] if OSPF_INT_KEY in cmd_outputs.keys(): for interface in cmd_outputs.get(OSPF_INT_KEY): if interface[1] in result.keys(): if interface[2] not in result.get(interface[1]).keys(): result[interface[1]][interface[2]] = dict() result[interface[1]][interface[2]][_mapping_interface_name( interface[0])] = list() if OSPF_NEI_KEY in cmd_outputs.keys() and OSPF_RIB_KEY in cmd_outputs.keys( ): for neighbor in cmd_outputs.get(OSPF_NEI_KEY): for instance_id in result: for area in result.get(instance_id): if _mapping_interface_name(neighbor[3]) in result.get( instance_id).get(area).keys(): result[instance_id][area][_mapping_interface_name( neighbor[3])].append({ 'peer_rid': neighbor[0], 'peer_ip': neighbor[1], 'session_state': neighbor[4] }) for mapping in mapping_instance_vrf.keys(): return_val[mapping_instance_vrf.get(mapping)] = result[mapping] return return_val, router_id
def create_an_vlan_object_manually(context) -> None: """ Create a VLAN object manually :param context: :return None: """ vlan_lst = ListVLAN( vlans_lst=list() ) ports_members = list() for port_member in ["Ethernet4", "Ethernet5", "Ethernet6", "Ethernet7"]: ports_members.append( _mapping_interface_name( port_member ) ) vlan_lst.vlans_lst.append( VLAN( vlan_id="1", ports_members=ports_members ) ) ports_members = list() for port_member in ["Ethernet6"]: ports_members.append( _mapping_interface_name( port_member ) ) vlan_lst.vlans_lst.append( VLAN( vlan_id="1000", ports_members=ports_members ) ) vlan_lst.vlans_lst.append( VLAN( vlan_id="2000", ports_members=["eth1/6"] ) ) context.object_01 = vlan_lst
def _cumulus_lldp_converter(hostname: str(), cmd_output: json) -> ListLLDP: if cmd_output is None or cmd_output == "": return None lldp_neighbors_lst = ListLLDP(lldp_neighbors_lst=list()) if "lldp" in cmd_output.keys(): if "interface" in cmd_output.get('lldp')[0].keys(): for lldp_neighbor in cmd_output.get('lldp')[0].get("interface"): if lldp_neighbor.get("via", NOT_SET) == "LLDP": neighbor_type_lst = list() if lldp_neighbor.get("chassis", NOT_SET)[0].get("descr", NOT_SET) == NOT_SET: neighbor_os = NOT_SET else: neighbor_os = lldp_neighbor.get( "chassis", NOT_SET)[0].get("descr", NOT_SET)[0].get("value", NOT_SET) for capability in lldp_neighbor.get( "chassis", NOT_SET)[0].get("capability", NOT_SET): neighbor_type_lst.append( capability.get("type", NOT_SET)) lldp_neighbors_lst.lldp_neighbors_lst.append( LLDP(local_name=hostname, local_port=_mapping_interface_name( lldp_neighbor.get("name", NOT_SET)), neighbor_mgmt_ip=lldp_neighbor.get( "chassis", NOT_SET)[0].get("mgmt-ip", NOT_SET)[0].get( "value", NOT_SET), neighbor_name=lldp_neighbor.get( "chassis", NOT_SET)[0].get("name", NOT_SET)[0].get( "value", NOT_SET), neighbor_port=_mapping_interface_name( lldp_neighbor.get("port", NOT_SET)[0].get( "id", NOT_SET)[0].get("value", NOT_SET)), neighbor_os=neighbor_os, neighbor_type=neighbor_type_lst)) return lldp_neighbors_lst
def _arista_ipv4_converter(hostname: str(), plateform: str(), cmd_output: json, *, filters=dict()) -> ListIPV4Interface: ipv4_addresses_lst = ListIPV4Interface(hostname=hostname, ipv4_addresses_lst=list()) for interface_name, facts in cmd_output.get('interfaces').items(): if _generic_interface_filter( interface_name=_mapping_interface_name(interface_name), plateform=plateform, filters=filters): ip_address = facts.get('interfaceAddress').get('primaryIp').get( 'address', NOT_SET) if ip_address != NOT_SET and "/128" not in ip_address and "/64" not in ip_address and \ "/48" not in ip_address and "::" not in ip_address and "127.0.0.1" not in ip_address and \ "::1/128" not in ip_address: ipv4_addresses_lst.ipv4_addresses_lst.append( IPV4Interface( interface_name=_mapping_interface_name(interface_name), ip_address_with_mask=ip_address, netmask=facts.get('interfaceAddress').get( 'primaryIp').get('maskLen', NOT_SET))) if len( facts.get('interfaceAddress').get( 'secondaryIpsOrderedList')) > 0: for ip_addr in facts.get('interfaceAddress').get( 'secondaryIpsOrderedList'): secondary_ip = ip_addr.get('address', NOT_SET) if secondary_ip != NOT_SET and "/128" not in secondary_ip and "/64" not in secondary_ip and \ "/48" not in secondary_ip and "::" not in secondary_ip and "127.0.0.1" not in secondary_ip and \ "::1/128" not in secondary_ip: ipv4_addresses_lst.ipv4_addresses_lst.append( IPV4Interface( interface_name=_mapping_interface_name( interface_name), ip_address_with_mask=secondary_ip, netmask=ip_addr.get('maskLen', NOT_SET))) return ipv4_addresses_lst
def _compare_bond(host_keys, hostname, bond_host_data:None, bond_yaml_data:json) -> bool: if bond_yaml_data is None: return False verity_bonds_lst = ListBOND( bonds_lst=list() ) if BOND_DATA_HOST_KEY in host_keys and hostname in bond_yaml_data.keys(): for bond in bond_yaml_data.get(hostname): ports_members = list() for port in bond.get("ports_members", NOT_SET): ports_members.append( _mapping_interface_name( port ) ) verity_bonds_lst.bonds_lst.append( BOND( bond_name=bond.get("bond_name", NOT_SET), ports_members=ports_members, vlans_members=bond.get("vlans_members", list()), native_vlan=bond.get("native_vlan", NOT_SET), mode=bond.get("mode", NOT_SET), ) ) return verity_bonds_lst == bond_host_data else: print(f"{HEADER_GET} {hostname} is not present in {PATH_TO_VERITY_FILES}/{TEST_TO_EXC_BOND_KEY}.") return False
def create_a_mtu_object_from_a_json(context) -> None: """ JSON file data structure must be as follow: spine01: global_mtu: 1500 interfaces: lo0: 1514 :param context: :param mtu_data: Data to create a MTU object :param hostname: Device hostname :return: """ mtu_data = open_file(path=f"{FEATURES_SRC_PATH}mtu_tests.yml") hostname = list(mtu_data.keys())[0] interface_mtu_lst = ListInterfaceMTU(list()) if hostname in mtu_data.keys(): if MTU_INTER_YAML_KEY in mtu_data.get(hostname): for interface_name in mtu_data.get(hostname).get( MTU_INTER_YAML_KEY): interface_mtu_lst.interface_mtu_lst.append( InterfaceMTU( interface_name=_mapping_interface_name(interface_name), mtu_size=mtu_data.get(hostname).get( MTU_INTER_YAML_KEY).get(interface_name, NOT_SET))) context.object_02 = MTU(hostname=hostname, mtu_global=mtu_data.get(hostname).get( MTU_GLOBAL_YAML_KEY, NOT_SET), interface_mtu_lst=interface_mtu_lst)
def step_impl(context, nb_int_01): """ Create a MTU object and store it in context object. :param context: :param nb_int_01: :return None: """ i = 0 interface_mtu_lst = ListInterfaceMTU(list()) while i < nb_int_01: interface_mtu_lst.interface_mtu_lst.append( InterfaceMTU( interface_name=_mapping_interface_name(f"Gi0/{i}"), mtu_size=(2000 + (200 * i)), )) i += 1 context.object_01 = MTU(hostname="spine01", mtu_global=1500, interface_mtu_lst=interface_mtu_lst)
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)
def _juniper_retrieve_int_name_with_napalm(interface_data: list) -> list: int_name_lst = list() if interface_data is not None: for interface_name in interface_data: if (("em" in interface_name or "lo" in interface_name or "fxp" in interface_name) and "demux" not in interface_name and "local" not in interface_name): int_name_lst.append(_mapping_interface_name(interface_name)) return int_name_lst
def _cumulus_ipv6_converter(hostname: str(), plateform: str(), cmd_output: json, *, filters=dict()) -> ListIPV6Interface: if cmd_output is None or cmd_output == "": return None ipv6_addresses_lst = ListIPV6Interface(ipv6_addresses_lst=list()) for interface_name, facts in cmd_output.items(): # v0 = vrrp / vrr / hsrp / HA ip address # bridge is a Linux bridge if _generic_interface_filter( plateform=plateform, interface_name=_mapping_interface_name(interface_name), filters=filters): for ip_address_in_interface in facts.get("iface_obj").get( 'ip_address').get('allentries'): if str(ip_address_in_interface).find("/") != -1: index_slash = str(ip_address_in_interface).find("/") try: ipaddress.IPv6Address( ip_address_in_interface[:index_slash]) is_valid = True except ipaddress.AddressValueError as e: is_valid = False if is_valid: if "::1/128" not in ip_address_in_interface: ipv6_addresses_lst.ipv6_addresses_lst.append( IPV6Interface( interface_name=_mapping_interface_name( interface_name), ip_address_with_mask=ip_address_in_interface )) return ipv6_addresses_lst
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
def _cumulus_cdp_converter(hostname:str(), cmd_output:json) -> ListCDP: cdp_neighbors_lst = ListCDP(list()) if "lldp" in cmd_output.keys(): if cmd_output.get('lldp', NOT_SET) is NOT_SET: return ListCDP(list()) else: for cdp_neighbor in cmd_output.get('lldp', NOT_SET)[0].get("interface", NOT_SET): if "CDP" in cdp_neighbor.get("via", NOT_SET): neighbor_type_lst = list() if cdp_neighbor.get("chassis", NOT_SET)[0].get("descr", NOT_SET) == NOT_SET: neighbor_os = NOT_SET else: neighbor_os = cdp_neighbor.get("chassis", NOT_SET)[0].get("descr", NOT_SET)[0].get("value", NOT_SET) for capability in cdp_neighbor.get("chassis", NOT_SET)[0].get("capability", NOT_SET): neighbor_type_lst.append(capability.get("type", NOT_SET)) lldp_obj = CDP( local_name=hostname, local_port=_mapping_interface_name(cdp_neighbor.get("name", NOT_SET)), neighbor_mgmt_ip=cdp_neighbor.get("chassis", NOT_SET)[0].get("mgmt-ip", NOT_SET)[0].get("value", NOT_SET), neighbor_name=cdp_neighbor.get("chassis", NOT_SET)[0].get("name", NOT_SET)[0].get("value", NOT_SET), neighbor_port=_mapping_interface_name( cdp_neighbor.get("port", NOT_SET)[0].get("id", NOT_SET)[0].get("value", NOT_SET)), neighbor_os=neighbor_os, neighbor_type=neighbor_type_lst ) cdp_neighbors_lst.cdp_neighbors_lst.append(lldp_obj) return cdp_neighbors_lst
def _arista_lldp_converter(hostname: str(), cmd_output: json) -> ListLLDP: if cmd_output is None or cmd_output == "": return None lldp_neighbors_lst = ListLLDP(lldp_neighbors_lst=list()) if "lldpNeighbors" in cmd_output.keys(): for interface_name, facts in cmd_output.get("lldpNeighbors", NOT_SET).items(): if len(facts.get("lldpNeighborInfo")) > 0: for data in facts.get("lldpNeighborInfo", NOT_SET): neighbor_type_lst = list() for sys_capability in data.get("systemCapabilities", NOT_SET): neighbor_type_lst.append( (str(sys_capability).capitalize())) neighbor_mgmt_ip = str() for address in data.get("managementAddresses", NOT_SET): if address.get("addressType", NOT_SET) == "ipv4": neighbor_mgmt_ip = address.get("address", NOT_SET) lldp_neighbors_lst.lldp_neighbors_lst.append( LLDP( local_name=hostname, local_port=_mapping_interface_name(interface_name), neighbor_mgmt_ip=neighbor_mgmt_ip, neighbor_name=data.get("systemName", NOT_SET), neighbor_port=_mapping_interface_name( data.get("neighborInterfaceInfo", NOT_SET).get("interfaceDescription", NOT_SET)), neighbor_os=data.get("systemDescription", NOT_SET), neighbor_type=neighbor_type_lst)) return lldp_neighbors_lst
def _extreme_vsp_mtu_converter(hostname: str(), cmd_output: dict) -> MTU: if cmd_output is None: return None interface_mtu_lst = ListInterfaceMTU(list()) for interface in cmd_output: interface_mtu_lst.interface_mtu_lst.append( InterfaceMTU(interface_name=_mapping_interface_name(interface[0]), mtu_size=interface[5])) return MTU(hostname=hostname, interface_mtu_lst=interface_mtu_lst)
def _ios_static_converter(cmd_outputs:dict) -> ListStatic: static_routes_lst = ListStatic( static_routes_lst=list() ) for vrf in cmd_outputs: for route in cmd_outputs.get(vrf): nexthops_lst = ListNexthop( nexthops_lst=list() ) out_int = route[4] if str(route[4]).find('.') == -1 else NOT_SET next_hop = _mapping_interface_name(route[4]) if str(route[4]).find('.') == -1 else route[4] nexthops_lst.nexthops_lst.append( Nexthop( ip_address=next_hop, is_in_fib=True, out_interface=_mapping_interface_name( out_int ), preference=route[1] if route[1] != '' else 1 , metric=route[2] if route[2] != '' else 1, active=True ) ) static_routes_lst.static_routes_lst.append( Static( vrf_name=vrf, prefix=route[0], netmask=route[3], nexthop=nexthops_lst ) ) return static_routes_lst
def _juniper_static_converter(hostname:str(), cmd_outputs:json) -> ListStatic: static_routes_lst = ListStatic( static_routes_lst=list() ) if "route-information" in cmd_outputs.keys(): for instance_route in cmd_outputs.get("route-information")[0].get("route-table"): if "rt" in instance_route.keys(): for route in instance_route.get("rt"): nexthops_lst = ListNexthop( nexthops_lst=list() ) for route_entry in route.get("rt-entry"): for nexthop in route_entry.get("nh"): nexthops_lst.nexthops_lst.append( Nexthop( ip_address=nexthop.get('to')[0].get("data", NOT_SET), is_in_fib=nexthop.get('always_true_in_juniper', True), out_interface=_mapping_interface_name( nexthop.get('via')[0].get("data", NOT_SET) ), preference=route_entry.get('preference')[0].get("data", NOT_SET), metric=NOT_SET, active=nexthop.get('always_true_in_juniper', True) ) ) # Example of default table route => "data" : "inet.0" if instance_route.get("table-name")[0].get("data") == "inet.0": vrf_name = "default" else: index_dot = instance_route.get("table-name")[0].get("data").find(".") vrf_name = instance_route.get("table-name")[0].get("data")[:index_dot] # Output is => "data" : "10.255.255.103/32" index_slash = str(route.get("rt-destination")[0].get("data", NOT_SET)).find("/") static_routes_lst.static_routes_lst.append( Static( vrf_name=vrf_name, prefix=str(route.get("rt-destination")[0].get("data", NOT_SET))[:index_slash], netmask=str(route.get("rt-destination")[0].get("data", NOT_SET))[index_slash + 1:], nexthop=nexthops_lst ) ) return static_routes_lst
def _extreme_vsp_ipv4_converter(hostname: str(), plateform: str(), cmd_output: dict, *, filters=dict()) -> IPV4Interface: ipv4_addresses_lst = ListIPV4Interface(hostname=hostname, ipv4_addresses_lst=list()) for vrf in cmd_output: for interface in cmd_output.get(vrf): if _generic_interface_filter( interface_name=_mapping_interface_name(interface[0]), plateform=plateform, filters=filters): ipv4_addresses_lst.ipv4_addresses_lst.append( IPV4Interface(interface_name=_mapping_interface_name( interface[0]), ip_address_with_mask=interface[1], netmask=interface[2])) return ipv4_addresses_lst
def _extreme_vsp_lldp_converter(hostname: str(), cmd_output: list) -> ListLLDP: if cmd_output is None or cmd_output == "": return None lldp_neighbors_lst = ListLLDP(lldp_neighbors_lst=list()) for lldp_neighbor in cmd_output: neighbor_type_lst = list() for sys_capability in lldp_neighbor[4]: neighbor_type_lst.append( _mapping_sys_capabilities(str(sys_capability).capitalize())) lldp_neighbors_lst.lldp_neighbors_lst.append( LLDP(local_name=hostname, local_port=_mapping_interface_name(lldp_neighbor[0]), neighbor_mgmt_ip=lldp_neighbor[7], neighbor_name=lldp_neighbor[3], neighbor_port=_mapping_interface_name(lldp_neighbor[2]), neighbor_os=lldp_neighbor[6], neighbor_type=neighbor_type_lst)) return lldp_neighbors_lst
def _compare_lldp(host_keys, hostname, lldp_host_data, lldp_yaml_data: json): verity_lldp = ListLLDP(list()) if LLDP_DATA_HOST_KEY in host_keys: for lldp_neighbor in lldp_yaml_data.get(hostname, NOT_SET): lldp_obj = LLDP(local_name=hostname, local_port=_mapping_interface_name( lldp_neighbor.get("local_port", NOT_SET)), neighbor_name=lldp_neighbor.get( "neighbor_name", NOT_SET), neighbor_port=_mapping_interface_name( lldp_neighbor.get("neighbor_port", NOT_SET))) verity_lldp.lldp_neighbors_lst.append(lldp_obj) return verity_lldp == lldp_host_data else: print( f"[{HEADER_GET}] {hostname} is not present in {PATH_TO_VERITY_FILES}/{TEST_TO_EXC_LLDP_KEY}." ) return False
def _cumulus_mtu_converter(hostname: str(), cmd_output: json) -> MTU: if cmd_output is None: return None interface_mtu_lst = ListInterfaceMTU(list()) for interface in cmd_output: if "swp" in interface or "eth" in interface: interface_mtu_lst.interface_mtu_lst.append( InterfaceMTU( interface_name=_mapping_interface_name(interface), mtu_size=cmd_output.get(interface).get('iface_obj').get( 'mtu', NOT_SET))) return MTU(hostname=hostname, interface_mtu_lst=interface_mtu_lst)
def _arista_mtu_converter(hostname: str(), cmd_output: json) -> MTU: if cmd_output is None: return None interface_mtu_lst = ListInterfaceMTU(list()) if "interfaces" in cmd_output.keys(): for interface in cmd_output.get("interfaces"): if "Ethernet" in interface or "Management" in interface: interface_mtu_lst.interface_mtu_lst.append( InterfaceMTU( interface_name=_mapping_interface_name(interface), mtu_size=cmd_output.get("interfaces").get( interface).get('mtu', NOT_SET))) return MTU(hostname=hostname, interface_mtu_lst=interface_mtu_lst)