def test_N2G_ospf_lsdb_huawei(): with open("./mock_data/huawei_display_ospf_lsdb_router.txt", "r") as f: data = f.read() template = get_template(path="misc/N2G/cli_ospf_data/Huawei.txt") # print(template) parser = ttp(data=data, template=template) parser.parse() res = parser.result() pprint.pprint(res) assert res == [[{ 'ospf_processes': { '123': { 'local_rid': '123.123.24.158', 'router_lsa': [{ 'area': '0.0.0.123', 'originator_rid': '10.123.0.91', 'ptp_peers': [{ 'link_data': '123.123.60.206', 'link_id': '123.123.24.6', 'metric': '9000' }, { 'link_data': '123.123.1.220', 'link_id': '10.123.2.7', 'metric': '3000' }] }, { 'area': '0.0.0.123', 'connected_stub': [{ 'link_data': '255.255.255.252', 'link_id': '123.123.60.108', 'metric': '1' }], 'originator_rid': '10.123.0.92', 'ptp_peers': [{ 'link_data': '123.123.60.109', 'link_id': '123.123.24.31', 'metric': '1' }, { 'link_data': '123.123.60.201', 'link_id': '123.123.24.5', 'metric': '9000' }] }] } } }]] # test_N2G_ospf_lsdb_huawei()
def test_N2G_ospf_lsdb_Cisco_IOSXR(): with open( "./mock_data/cisco_xr_show_ip_ospf_database_router_external_summary_router-1.txt", "r") as f: data1 = f.read() with open( "./mock_data/cisco_xr_show_ip_ospf_database_router_external_summary_router-2.txt", "r") as f: data2 = f.read() template = get_template(path="misc/N2G/cli_ospf_data/cisco_xr.txt") # print(template) parser = ttp(template=template) parser.add_input(data1) parser.add_input(data2) parser.parse() res = parser.result() pprint.pprint(res) assert res == [[{ 'ospf_processes': { '1': { 'external_lsa': [{ 'mask': '0', 'metric': '1', 'metric_type': '2', 'originator_rid': '10.3.22.190', 'subnet': '0.0.0.0', 'tag': '10' }, { 'mask': '0', 'metric': '1', 'metric_type': '2', 'originator_rid': '10.3.25.22', 'subnet': '0.0.0.0', 'tag': '10' }, { 'mask': '8', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.3.20.95', 'subnet': '10.0.0.0', 'tag': '0' }, { 'mask': '24', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.3.22.83', 'subnet': '10.0.2.0', 'tag': '0' }], 'local_rid': '10.1.2.2', 'router_lsa': [{ 'area': '0.0.0.0', 'asbr': True, 'bma_peers': [{ 'link_data': '10.3.162.14', 'link_id': '10.3.162.13', 'metric': '1' }, { 'link_data': '10.3.162.10', 'link_id': '10.3.162.9', 'metric': '1' }], 'connected_stub': [{ 'link_data': '255.255.255.252', 'link_id': '10.0.61.0', 'metric': '9100' }], 'originator_rid': '10.1.0.91', 'ptp_peers': [{ 'link_data': '10.0.61.1', 'link_id': '10.1.1.251', 'metric': '9100' }, { 'link_data': '10.0.61.94', 'link_id': '10.1.2.6', 'metric': '65535' }, { 'link_data': '0.0.1.220', 'link_id': '10.1.2.7', 'metric': '3000' }] }, { 'area': '0.0.0.0', 'asbr': True, 'connected_stub': [{ 'link_data': '255.255.255.252', 'link_id': '10.0.61.96', 'metric': '9000' }], 'originator_rid': '10.1.0.92', 'ptp_peers': [{ 'link_data': '0.0.2.5', 'link_id': '10.1.2.6', 'metric': '1100' }, { 'link_data': '0.0.2.67', 'link_id': '10.1.2.8', 'metric': '3000' }, { 'link_data': '0.0.2.69', 'link_id': '10.1.2.7', 'metric': '3000' }] }], 'summary_lsa': [{ 'area': '0.0.0.0', 'mask': '32', 'metric': '2312', 'originator_rid': '10.0.24.1', 'subnet': '10.1.0.1' }, { 'area': '0.0.0.0', 'mask': '32', 'metric': '1806', 'originator_rid': '10.0.24.2', 'subnet': '10.1.0.1' }, { 'area': '0.0.0.0', 'mask': '32', 'metric': '1312', 'originator_rid': '10.0.25.192', 'subnet': '10.1.0.1' }, { 'area': '0.0.0.0', 'mask': '32', 'metric': '806', 'originator_rid': '10.0.25.193', 'subnet': '10.1.0.1' }, { 'area': '0.0.0.32', 'mask': '32', 'metric': '2312', 'originator_rid': '10.0.24.1', 'subnet': '10.1.0.1' }] } } }, { 'ospf_processes': { '1': { 'local_rid': '10.1.2.2', 'router_lsa': [{ 'area': '0.0.0.0', 'asbr': True, 'connected_stub': [{ 'link_data': '255.255.255.252', 'link_id': '10.0.60.204', 'metric': '9000' }, { 'link_data': '255.255.255.252', 'link_id': '10.0.60.196', 'metric': '9000' }], 'originator_rid': '10.1.0.91', 'ptp_peers': [{ 'link_data': '10.0.60.206', 'link_id': '10.0.24.6', 'metric': '9000' }, { 'link_data': '10.0.60.197', 'link_id': '10.1.0.92', 'metric': '9000' }] }, { 'area': '0.0.0.0', 'asbr': True, 'connected_stub': [{ 'link_data': '255.255.255.252', 'link_id': '10.0.60.108', 'metric': '1' }, { 'link_data': '255.255.255.252', 'link_id': '10.0.60.200', 'metric': '9000' }], 'originator_rid': '10.1.0.92', 'ptp_peers': [{ 'link_data': '10.0.60.109', 'link_id': '10.0.24.31', 'metric': '1' }, { 'link_data': '10.0.60.201', 'link_id': '10.0.24.5', 'metric': '9000' }] }, { 'area': '0.0.0.1', 'asbr': True, 'originator_rid': '10.1.0.91', 'ptp_peers': [{ 'link_data': '10.0.60.206', 'link_id': '10.0.24.6', 'metric': '9000' }] }], 'summary_lsa': [{ 'area': '0.0.0.0', 'mask': '32', 'metric': '2312', 'originator_rid': '10.0.24.1', 'subnet': '10.1.0.1' }, { 'area': '0.0.0.0', 'mask': '32', 'metric': '1806', 'originator_rid': '10.0.24.2', 'subnet': '10.1.0.1' }, { 'area': '0.0.0.0', 'mask': '32', 'metric': '1312', 'originator_rid': '10.0.25.192', 'subnet': '10.1.0.1' }, { 'area': '0.0.0.0', 'mask': '32', 'metric': '806', 'originator_rid': '10.0.25.193', 'subnet': '10.1.0.1' }] }, '10': { 'external_lsa': [{ 'mask': '0', 'metric': '1', 'metric_type': '2', 'originator_rid': '10.3.22.190', 'subnet': '0.0.0.0', 'tag': '10' }, { 'mask': '0', 'metric': '1', 'metric_type': '2', 'originator_rid': '10.3.25.22', 'subnet': '0.0.0.0', 'tag': '10' }, { 'mask': '8', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.3.20.95', 'subnet': '10.0.0.0', 'tag': '0' }, { 'mask': '24', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.3.22.83', 'subnet': '10.0.2.0', 'tag': '0' }], 'local_rid': '10.3.22.75' } } }]]
def test_cisco_ios_show_ip_ospf_database_router(): with open( "./mock_data/cisco_ios_show_ip_ospf_database_router_IOL4_ABR.txt", "r") as f: data = f.read() template = get_template(platform="cisco_ios", command="show ip ospf database router") # print(template) parser = ttp(data=data, template=template) parser.parse() res = parser.result() # pprint.pprint(res) assert res == [[{ "ospf_processes": { "1": { "local_rid": "10.0.0.4", "router_lsa": [ { "area": "0", "asbr": False, "bma_peers": [{ "link_data": "10.1.117.3", "link_id": "10.1.117.7", "metric": "10", }], "originator_rid": "10.0.0.3", }, { "area": "0", "asbr": False, "bma_peers": [{ "link_data": "10.1.117.4", "link_id": "10.1.117.7", "metric": "10", }], "connected_stub": [{ "link_data": "255.255.255.128", "link_id": "10.1.14.0", "metric": "10", }], "originator_rid": "10.0.0.4", "ptp_peers": [{ "link_data": "10.1.14.4", "link_id": "10.0.0.10", "metric": "10", }], }, { "area": "0", "asbr": False, "bma_peers": [{ "link_data": "10.1.117.7", "link_id": "10.1.117.7", "metric": "10", }], "connected_stub": [ { "link_data": "255.255.255.255", "link_id": "10.0.0.7", "metric": "1", }, { "link_data": "255.255.255.252", "link_id": "10.1.107.0", "metric": "10", }, { "link_data": "255.255.255.0", "link_id": "10.1.37.0", "metric": "10", }, ], "originator_rid": "10.0.0.7", "ptp_peers": [{ "link_data": "10.1.107.2", "link_id": "10.0.0.10", "metric": "10", }], }, { "area": "0", "asbr": True, "connected_stub": [ { "link_data": "255.255.255.255", "link_id": "10.0.0.10", "metric": "1", }, { "link_data": "255.255.255.252", "link_id": "10.1.107.0", "metric": "10", }, { "link_data": "255.255.255.128", "link_id": "10.1.14.0", "metric": "10", }, ], "originator_rid": "10.0.0.10", "ptp_peers": [ { "link_data": "10.1.107.1", "link_id": "10.0.0.7", "metric": "10", }, { "link_data": "10.1.14.1", "link_id": "10.0.0.4", "metric": "10", }, ], }, { "area": "100", "asbr": False, "connected_stub": [{ "link_data": "255.255.255.254", "link_id": "10.1.45.2", "metric": "10", }], "originator_rid": "10.0.0.4", "ptp_peers": [{ "link_data": "10.1.45.2", "link_id": "10.0.5.101", "metric": "10", }], }, { "area": "100", "asbr": True, "connected_stub": [{ "link_data": "255.255.255.254", "link_id": "10.1.45.2", "metric": "10", }], "originator_rid": "10.0.5.101", "ptp_peers": [{ "link_data": "10.1.45.3", "link_id": "10.0.0.4", "metric": "10", }], }, ], } } }]]
def test_N2G_ospf_lsdb_Cisco_IOS(): with open( "./mock_data/cisco_ios_show_ip_ospf_database_router_external_summary_IOL4_ABR.txt", "r") as f: data = f.read() template = get_template(path="misc/N2G/cli_ospf_data/Cisco_IOS.txt") # print(template) parser = ttp(data=data, template=template) parser.parse() res = parser.result() pprint.pprint(res) assert res == [[{ 'ospf_processes': { '1': { 'external_lsa': [{ 'mask': '32', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.0.0.10', 'subnet': '10.0.0.100', 'tag': '0' }, { 'mask': '32', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.0.0.10', 'subnet': '10.0.0.101', 'tag': '0' }, { 'mask': '32', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.0.0.10', 'subnet': '10.0.0.102', 'tag': '0' }, { 'mask': '32', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.0.0.10', 'subnet': '10.0.0.103', 'tag': '0' }, { 'mask': '32', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.0.5.101', 'subnet': '10.0.5.100', 'tag': '0' }, { 'mask': '32', 'metric': '20', 'metric_type': '2', 'originator_rid': '10.0.5.101', 'subnet': '10.0.5.101', 'tag': '0' }], 'local_rid': '10.0.0.4', 'router_lsa': [{ 'area': '0', 'asbr': False, 'bma_peers': [{ 'link_data': '10.1.117.3', 'link_id': '10.1.117.7', 'metric': '10' }], 'originator_rid': '10.0.0.3' }, { 'area': '0', 'asbr': False, 'bma_peers': [{ 'link_data': '10.1.117.4', 'link_id': '10.1.117.7', 'metric': '10' }], 'connected_stub': [{ 'link_data': '255.255.255.128', 'link_id': '10.1.14.0', 'metric': '10' }], 'originator_rid': '10.0.0.4', 'ptp_peers': [{ 'link_data': '10.1.14.4', 'link_id': '10.0.0.10', 'metric': '10' }] }, { 'area': '0', 'asbr': False, 'bma_peers': [{ 'link_data': '10.1.117.7', 'link_id': '10.1.117.7', 'metric': '10' }], 'connected_stub': [{ 'link_data': '255.255.255.255', 'link_id': '10.0.0.7', 'metric': '1' }, { 'link_data': '255.255.255.252', 'link_id': '10.1.107.0', 'metric': '10' }, { 'link_data': '255.255.255.0', 'link_id': '10.1.37.0', 'metric': '10' }], 'originator_rid': '10.0.0.7', 'ptp_peers': [{ 'link_data': '10.1.107.2', 'link_id': '10.0.0.10', 'metric': '10' }] }, { 'area': '0', 'asbr': True, 'connected_stub': [{ 'link_data': '255.255.255.255', 'link_id': '10.0.0.10', 'metric': '1' }, { 'link_data': '255.255.255.252', 'link_id': '10.1.107.0', 'metric': '10' }, { 'link_data': '255.255.255.128', 'link_id': '10.1.14.0', 'metric': '10' }], 'originator_rid': '10.0.0.10', 'ptp_peers': [{ 'link_data': '10.1.107.1', 'link_id': '10.0.0.7', 'metric': '10' }, { 'link_data': '10.1.14.1', 'link_id': '10.0.0.4', 'metric': '10' }] }, { 'area': '100', 'asbr': False, 'connected_stub': [{ 'link_data': '255.255.255.254', 'link_id': '10.1.45.2', 'metric': '10' }], 'originator_rid': '10.0.0.4', 'ptp_peers': [{ 'link_data': '10.1.45.2', 'link_id': '10.0.5.101', 'metric': '10' }] }, { 'area': '100', 'asbr': True, 'connected_stub': [{ 'link_data': '255.255.255.254', 'link_id': '10.1.45.2', 'metric': '10' }], 'originator_rid': '10.0.5.101', 'ptp_peers': [{ 'link_data': '10.1.45.3', 'link_id': '10.0.0.4', 'metric': '10' }] }], 'summary_lsa': [{ 'area': '0', 'mask': '31', 'metric': '10', 'originator_rid': '10.0.0.4', 'subnet': '10.1.45.2' }, { 'area': '100', 'mask': '32', 'metric': '11', 'originator_rid': '10.0.0.4', 'subnet': '10.0.0.7' }, { 'area': '100', 'mask': '32', 'metric': '11', 'originator_rid': '10.0.0.4', 'subnet': '10.0.0.10' }, { 'area': '100', 'mask': '25', 'metric': '10', 'originator_rid': '10.0.0.4', 'subnet': '10.1.14.0' }, { 'area': '100', 'mask': '24', 'metric': '20', 'originator_rid': '10.0.0.4', 'subnet': '10.1.37.0' }, { 'area': '100', 'mask': '30', 'metric': '20', 'originator_rid': '10.0.0.4', 'subnet': '10.1.107.0' }, { 'area': '100', 'mask': '24', 'metric': '10', 'originator_rid': '10.0.0.4', 'subnet': '10.1.117.0' }] } } }]]
def test_cisco_ios_show_ip_ospf_database_external(): with open( "./mock_data/cisco_ios_show_ip_ospf_database_external_IOL4_ABR.txt", "r") as f: data = f.read() template = get_template(platform="cisco_ios", command="show ip ospf database external") # print(template) parser = ttp(data=data, template=template) parser.parse() res = parser.result() pprint.pprint(res) assert res == [[{ "ospf_processes": { "1": { "external_lsa": [ { "mask": "32", "metric": "20", "metric_type": "2", "originator_rid": "10.0.0.10", "subnet": "10.0.0.100", "tag": "0", }, { "mask": "32", "metric": "20", "metric_type": "2", "originator_rid": "10.0.0.10", "subnet": "10.0.0.101", "tag": "0", }, { "mask": "32", "metric": "20", "metric_type": "2", "originator_rid": "10.0.0.10", "subnet": "10.0.0.102", "tag": "0", }, { "mask": "32", "metric": "20", "metric_type": "2", "originator_rid": "10.0.0.10", "subnet": "10.0.0.103", "tag": "0", }, { "mask": "32", "metric": "20", "metric_type": "2", "originator_rid": "10.0.5.101", "subnet": "10.0.5.100", "tag": "0", }, { "mask": "32", "metric": "20", "metric_type": "2", "originator_rid": "10.0.5.101", "subnet": "10.0.5.101", "tag": "0", }, ], "local_rid": "10.0.0.4", } } }]]
def test_cisco_ios_show_ip_ospf_database_summary(): with open( "./mock_data/cisco_ios_show_ip_ospf_database_summary_IOL4_ABR.txt", "r") as f: data = f.read() template = get_template(platform="cisco_ios", command="show ip ospf database summary") # print(template) parser = ttp(data=data, template=template) parser.parse() res = parser.result() pprint.pprint(res) assert res == [[{ "ospf_processes": { "1": { "local_rid": "10.0.0.4", "summary_lsa": [ { "area": "0", "mask": "31", "metric": "10", "originator_rid": "10.0.0.4", "subnet": "10.1.45.2", }, { "area": "100", "mask": "32", "metric": "11", "originator_rid": "10.0.0.4", "subnet": "10.0.0.7", }, { "area": "100", "mask": "32", "metric": "11", "originator_rid": "10.0.0.4", "subnet": "10.0.0.10", }, { "area": "100", "mask": "25", "metric": "10", "originator_rid": "10.0.0.4", "subnet": "10.1.14.0", }, { "area": "100", "mask": "24", "metric": "20", "originator_rid": "10.0.0.4", "subnet": "10.1.37.0", }, { "area": "100", "mask": "30", "metric": "20", "originator_rid": "10.0.0.4", "subnet": "10.1.107.0", }, { "area": "100", "mask": "24", "metric": "10", "originator_rid": "10.0.0.4", "subnet": "10.1.117.0", }, ], } } }]]
def test_get_template(): template = get_template(path="yang/ietf-interfaces_cisco_ios.txt") # print(template) assert isinstance(template, str)
def test_cisco_ios(): data1 = """ interface GigabitEthernet1/3.251 description Customer #32148 encapsulation dot1q 251 ip address 172.16.33.10 255.255.255.128 shutdown ! interface GigabitEthernet1/4 description vCPEs access control ip address 172.16.33.10 255.255.255.128 ! """ data2 = """ interface GigabitEthernet1/5 description Works data ip mtu 9000 ! interface GigabitEthernet1/7 description Works data v6 ipv6 address 2001::1/64 ipv6 address 2001:1::1/64 """ template = get_template(yang="ietf-interfaces", platform="cisco_ios") # print(template) parser = ttp(template=template, vars={"validate_with_yangson": True}) parser.add_input(data1) parser.add_input(data2) parser.parse() res = parser.result() # pprint.pprint(res) assert res == [{ "comment": "", "exception": {}, "result": [ { "ietf-interfaces:interfaces": { "interface": [ { "admin-status": "down", "description": "Customer " "#32148", "enabled": False, "ietf-ip:ipv4": { "address": [{ "ip": "172.16.33.10", "netmask": "255.255.255.128", "origin": "static", }] }, "if-index": 1, "link-up-down-trap-enable": "enabled", "name": "GigabitEthernet1/3.251", "oper-status": "unknown", "statistics": { "discontinuity-time": "1970-01-01T00:00:00+00:00" }, "type": "iana-if-type:ethernetCsmacd", }, { "admin-status": "up", "description": "vCPEs " "access " "control", "enabled": True, "ietf-ip:ipv4": { "address": [{ "ip": "172.16.33.10", "netmask": "255.255.255.128", "origin": "static", }] }, "if-index": 1, "link-up-down-trap-enable": "enabled", "name": "GigabitEthernet1/4", "oper-status": "unknown", "statistics": { "discontinuity-time": "1970-01-01T00:00:00+00:00" }, "type": "iana-if-type:ethernetCsmacd", }, ] } }, { "ietf-interfaces:interfaces": { "interface": [ { "admin-status": "up", "description": "Works " "data", "enabled": True, "ietf-ip:ipv4": { "mtu": 9000 }, "if-index": 1, "link-up-down-trap-enable": "enabled", "name": "GigabitEthernet1/5", "oper-status": "unknown", "statistics": { "discontinuity-time": "1970-01-01T00:00:00+00:00" }, "type": "iana-if-type:ethernetCsmacd", }, { "admin-status": "up", "description": "Works " "data " "v6", "enabled": True, "ietf-ip:ipv6": { "address": [ { "ip": "2001::1", "origin": "static", "prefix-length": 64, }, { "ip": "2001:1::1", "origin": "static", "prefix-length": 64, }, ] }, "if-index": 1, "link-up-down-trap-enable": "enabled", "name": "GigabitEthernet1/7", "oper-status": "unknown", "statistics": { "discontinuity-time": "1970-01-01T00:00:00+00:00" }, "type": "iana-if-type:ethernetCsmacd", }, ] } }, ], "valid": { 0: True, 1: True }, }]
def test_cisco_ios_show_ip_ospf_database_router(): with open("./mock_data/cisco_xr_show_isis_database_verbose.txt", "r") as f: data = f.read() template = get_template(platform="cisco_xr", command="show isis database verbose") # print(template) parser = ttp(data=data, template=template) parser.parse() res = parser.result() # pprint.pprint(res) assert res == [[{ 'isis_processes': { '100': { 'LSP': [{ 'hostname': 'R1-X1', 'isis_area': '49.0001', 'level': 'Level-2', 'links': [{ 'affinity': '0x00000000', 'bw_kbit': '10000000', 'local_intf_id': '9', 'local_ip': '10.123.0.17', 'metric': '16777214', 'peer_intf_id': '50', 'peer_ip': '10.123.0.18', 'peer_name': 'R1-X2' }, { 'affinity': '0x00000000', 'bw_kbit': '10000000', 'local_intf_id': '7', 'local_ip': '10.123.0.25', 'metric': '123', 'peer_intf_id': '53', 'peer_ip': '10.123.0.26', 'peer_name': 'R2-X1' }], 'lsp_id': 'R1-X1', 'networks': [{ 'metric': '0', 'network': '10.111.1.1/32' }], 'rid': '10.111.1.1' }, { 'hostname': 'R1-X2', 'isis_area': '49.0001', 'level': 'Level-2', 'links': [{ 'affinity': '0x00000000', 'bw_kbit': '10000000', 'local_intf_id': '48', 'local_ip': '10.123.0.33', 'metric': '456', 'peer_intf_id': '53', 'peer_ip': '10.123.0.34', 'peer_name': 'R2-X2' }, { 'affinity': '0x00000000', 'bw_kbit': '10000000', 'local_intf_id': '50', 'local_ip': '10.123.0.18', 'metric': '123', 'peer_intf_id': '9', 'peer_ip': '10.123.0.17', 'peer_name': 'R1-X1' }], 'lsp_id': 'R1-X2', 'networks': [{ 'metric': '0', 'network': '10.111.1.2/32' }], 'rid': '10.111.1.2' }] }, '200': { 'LSP': [{ 'hostname': 'R1-X1', 'isis_area': '49.0001', 'level': 'Level-2', 'links': [{ 'bw_kbit': '10000000', 'delay_avg_us': '1', 'delay_max_us': '1', 'delay_min_us': '1', 'delay_variation_us': '0', 'local_intf_id': '68', 'metric': '10', 'peer_intf_id': '57', 'peer_name': 'R2-X1', 'srv6_endx_sid': [{ 'algo': '0', 'args_length': '0', 'block_length': '32', 'func_length': '16', 'node_id_length': '16', 'sid': 'fdff:0:1:e000::' }, { 'algo': '128', 'args_length': '0', 'block_length': '32', 'func_length': '16', 'node_id_length': '16', 'sid': 'fdff:0:1001:e001::' }] }], 'lsp_id': 'R1-X1', 'networks': [{ 'metric': '0', 'network': 'fddd:2:c101::1/128' }, { 'metric': '1', 'network': 'fdff:0:1::/48' }, { 'metric': '0', 'network': 'fdff:3::/36' }, { 'metric': '0', 'network': 'fdff::/36' }], 'rid': 'fddd:2:c101::1', 'srv6_locators': [{ 'algo': '0', 'locator': 'fdff:0:1::', 'mask': '48' }, { 'algo': '128', 'locator': 'fdff:0:1001::', 'mask': '48' }] }, { 'hostname': 'R1-X2', 'isis_area': '49.0001', 'level': 'Level-2', 'links': [{ 'bw_kbit': '10000000', 'delay_avg_us': '1', 'delay_max_us': '1', 'delay_min_us': '1', 'delay_variation_us': '0', 'local_intf_id': '68', 'metric': '10', 'peer_intf_id': '60', 'peer_name': 'R2-X2', 'srv6_endx_sid': [{ 'algo': '0', 'args_length': '0', 'block_length': '32', 'func_length': '16', 'node_id_length': '16', 'sid': 'fdff:0:2:e000::' }, { 'algo': '128', 'args_length': '0', 'block_length': '32', 'func_length': '16', 'node_id_length': '16', 'sid': 'fdff:0:4444:e001::' }] }], 'lsp_id': 'R1-X2', 'networks': [{ 'metric': '0', 'network': 'fdff::/36' }, { 'metric': '1', 'network': 'fdff:0:2::/48' }, { 'metric': '0', 'network': 'fdff:2::/36' }, { 'metric': '0', 'network': 'fdff:3::/36' }, { 'metric': '0', 'network': 'fddd:2:c101::2/128' }], 'rid': 'fddd:2:c101::2', 'srv6_locators': [{ 'algo': '0', 'locator': 'fdff:0:2::', 'mask': '48' }, { 'algo': '128', 'locator': 'fdff:0:4444::', 'mask': '48' }] }] } } }]] # test_cisco_ios_show_ip_ospf_database_router()
def test_huwei_display_ospf_lsdb_router(): with open("./mock_data/huawei_display_ospf_lsdb_router.txt", "r") as f: data = f.read() template = get_template(platform="huawei", command="display ospf lsdb router") # print(template) parser = ttp(data=data, template=template) parser.parse() res = parser.result() # pprint.pprint(res) assert res == [ [ { "ospf_processes": { "123": { "local_rid": "123.123.24.158", "router_lsa": [ { "area": "0.0.0.123", "originator_rid": "10.123.0.91", "ptp_peers": [ { "link_data": "123.123.60.206", "link_id": "123.123.24.6", "metric": "9000", }, { "link_data": "123.123.1.220", "link_id": "10.123.2.7", "metric": "3000", }, ], }, { "area": "0.0.0.123", "connected_stub": [ { "link_data": "255.255.255.252", "link_id": "123.123.60.108", "metric": "1", } ], "originator_rid": "10.123.0.92", "ptp_peers": [ { "link_data": "123.123.60.109", "link_id": "123.123.24.31", "metric": "1", }, { "link_data": "123.123.60.201", "link_id": "123.123.24.5", "metric": "9000", }, ], }, ], } } } ] ]
def load_files(path, extensions=[], filters=[], read=False): """ Method to load files from path, and filter file names with REs filters and extensions. Args: path (str): string that contains OS path extensions (list): list of strings files' extensions like ['txt', 'log', 'conf'] filters (list): list of strings regexes to filter files read (bool): if False will return file names, if true will Returns: List of (type, text_data) tuples or empty list [] if read True, if read False return (type, url,) or [] """ files = [] # need to use path[:5000] cause if path is actually text of the template # and has length more then X symbols, os.path will choke with "path too long" # error, hence the safe-assumption that no os path exists longer then 5000 symbols # check if structured, non text, data given, return it as is if so # to process within input macro/function if not isinstance(path, str): return [( "structured_data", path, )] elif _ttp_["python_major_version"] == 2: if not isinstance( path, ( unicode, str, ), ): return [( "structured_data", path, )] # check if path is a reference to template in ttp_templates collection if path.startswith("ttp://"): from ttp_templates import get_template return [("text_data", get_template(path=path.replace("ttp://", "")))] # check if path is a path to file: elif os.path.isfile(path[:5000]): if read: try: if _ttp_["python_major_version"] == 2: with open(path, "r") as file_obj: return [( "text_data", file_obj.read(), )] with open(path, "r", encoding="utf-8") as file_obj: return [( "text_data", file_obj.read(), )] except UnicodeDecodeError: log.warning( 'ttp_utils.load_files: Unicode read error, file "{}"'. format(path)) else: return [( "file_name", path, )] # check if path is a directory: elif os.path.isdir(path[0:5000]): from re import search as re_search files = [f for f in os.listdir(path) if os.path.isfile(path + f)] if extensions: files = [f for f in files if f.split(".")[-1] in extensions] for filter in filters: files = [f for f in files if re_search(filter, f)] if read: ret = [] for f in files: if _ttp_["python_major_version"] == 2: with open((path + f), "r") as file_obj: ret.append(( "text_data", file_obj.read(), )) elif _ttp_["python_major_version"] == 3: with open((path + f), "r", encoding="utf-8") as file_obj: ret.append(( "text_data", file_obj.read(), )) return ret else: return [( "file_name", path + f, ) for f in files] # check if path is a string: elif isinstance(path, str): return [( "text_data", path, )] # check if py2, if so check if path is unicode string: elif _ttp_["python_major_version"] == 2: if isinstance(path, unicode): return [( "text_data", path, )] else: return []
def test_get_template_by_ttp_path_implicit(): template = get_template("ttp://yang/ietf-interfaces_cisco_ios.txt") # print(template) assert isinstance(template, str)
def test_cisco_ios(): data1 = """ interface GigabitEthernet1/3.251 description Customer #32148 encapsulation dot1q 251 ip address 172.16.33.10 255.255.255.128 shutdown ! interface GigabitEthernet1/4 description vCPEs access control ip address 172.16.33.10 255.255.255.128 ! """ data2 = """ interface GigabitEthernet1/5 description Works data ip mtu 9000 ! interface GigabitEthernet1/7 description Works data v6 ipv6 address 2001::1/64 ipv6 address 2001:1::1/64 """ template = get_template(yang="ietf-interfaces", platform="cisco_ios") # print(template) parser = ttp(template=template) parser.add_input(data1) parser.add_input(data2) parser.parse() res = parser.result() # pprint.pprint(res) assert res == [{'comment': '', 'exception': {}, 'result': [{'ietf-interfaces:interfaces': {'interface': [{'admin-status': 'down', 'description': 'Customer ' '#32148', 'enabled': False, 'ietf-ip:ipv4': {'address': [{'ip': '172.16.33.10', 'netmask': '255.255.255.128', 'origin': 'static'}]}, 'if-index': 1, 'link-up-down-trap-enable': 'enabled', 'name': 'GigabitEthernet1/3.251', 'oper-status': 'unknown', 'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'}, 'type': 'iana-if-type:ethernetCsmacd'}, {'admin-status': 'up', 'description': 'vCPEs ' 'access ' 'control', 'enabled': True, 'ietf-ip:ipv4': {'address': [{'ip': '172.16.33.10', 'netmask': '255.255.255.128', 'origin': 'static'}]}, 'if-index': 1, 'link-up-down-trap-enable': 'enabled', 'name': 'GigabitEthernet1/4', 'oper-status': 'unknown', 'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'}, 'type': 'iana-if-type:ethernetCsmacd'}]}}, {'ietf-interfaces:interfaces': {'interface': [{'admin-status': 'up', 'description': 'Works ' 'data', 'enabled': True, 'ietf-ip:ipv4': {'mtu': 9000}, 'if-index': 1, 'link-up-down-trap-enable': 'enabled', 'name': 'GigabitEthernet1/5', 'oper-status': 'unknown', 'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'}, 'type': 'iana-if-type:ethernetCsmacd'}, {'admin-status': 'up', 'description': 'Works ' 'data ' 'v6', 'enabled': True, 'ietf-ip:ipv6': {'address': [{'ip': '2001::1', 'origin': 'static', 'prefix-length': 64}, {'ip': '2001:1::1', 'origin': 'static', 'prefix-length': 64}]}, 'if-index': 1, 'link-up-down-trap-enable': 'enabled', 'name': 'GigabitEthernet1/7', 'oper-status': 'unknown', 'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'}, 'type': 'iana-if-type:ethernetCsmacd'}]}}], 'valid': {0: True, 1: True}}] # test_cisco_ios()