def factory(path, force_type=None): """ Factory to create parse file on argument path and return LogParser class object an type of parser can be forced if force_type argument is one of the following: 'timos' : force parsing 'show router [id] route-table' 'timosbgp' : force parsing 'show router [id] bgp routes [family]' 'hvrp' : force parsing 'display ip routing-table [vpn-instance <name>]' """ with open(path, mode="r") as stream: content = stream.read() if force_type is None: if routetableTimos.RouteTableTimosParser().inspect(content): product = routetableTimos.RouteTableTimosParser() elif routetableBgpTimos.RouteTableBGPTimosParser().inspect( content): product = routetableBgpTimos.RouteTableBGPTimosParser() elif routetableHVRP.RouteTableHVRPParser().inspect(content): product = routetableHVRP.RouteTableHVRPParser() else: product = routetableTimos.RouteTableTimosParser() else: logging.debug("forcing type to {}".format(force_type)) if force_type == "sros": product = routetableTimos.RouteTableTimosParser() elif force_type == "sros bgp": product = routetableBgpTimos.RouteTableBGPTimosParser() elif force_type == "hvrp": product = routetableHVRP.RouteTableHVRPParser() else: product = routetableTimos.RouteTableTimosParser() logging.debug("Log Parser object created {}".format(product)) product.process(content) return product
def test_nexthop_noIP(self): text = """*A:7750# show router 12345 bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- *u>? 1.1.1.2/32 100 None 999.991.131.3 1779 131045 65002 *u>? 1.1.1.3/32 100 None 999.991.131.3 1779 131045 65002 ------------------------------------------------------------------------------- """ parser = routetableBgpTimos.RouteTableBGPTimosParser() parser.process(text) self.assertEqual( [] , parser.routes("12345") )
def test_routes_longASpath_multiline_multiroute(self): text = """*A:7750# show router 12345 bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- *u>? 1.1.1.2/32 100 None 14.191.131.3 1779 131045 65002 65001 65000 65999 65999 65909 6599 60999 5999 1234 12344 65002 65001 65000 65999 65999 65909 6599 60999 5999 *u>? 1.1.1.3/32 100 None 14.191.131.3 1779 131045 65002 65001 65000 65999 65999 65909 6599 60999 5999 1234 12344 65002 65001 65000 65999 65999 65909 6599 60999 5999 ------------------------------------------------------------------------------- """ parser = routetableBgpTimos.RouteTableBGPTimosParser() parser.process(text) self.assertEqual( sorted ( [("1.1.1.2/32",), ("1.1.1.3/32",)] ), sorted ( list(parser.routes("12345") ) ) )
def test_family_vprn(self): text = """*A:7750# show router 123 bgp routes vpn-ipv4 =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- u*>? 10.3.1.2/28 100 None 14.191.131.3 1779 131045 65002 ------------------------------------------------------------------------------- """ parser = routetableBgpTimos.RouteTableBGPTimosParser() parser.process(text) self.assertEqual( [("bgp vpn-ipv4", "10.3.1.2/28",)] , parser.routes_protocol("123") )
def test_routersID_base(self): text = """*A:7750# show router bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- u*>? 1.1.1.3/32 100 None 14.191.131.3 1779 131045 65002 u*>? 1.1.2.3/32 100 None 14.191.131.3 1781 131045 65002 u*>? 10.238.3.0/31 100 0 14.191.131.3 1894 131019 No As-Path ------------------------------------------------------------------------------- """ parser = routetableBgpTimos.RouteTableBGPTimosParser() parser.process(text) self.assertEqual( ["Base"] , list(parser.routersID()) )
def test_multiple_routes_nexthop_protocol(self): text = """*A:7750# show router 923823 bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- u*>? 1.1.1.2/32 100 None 10.123.121.1 1779 131045 65002 u*>? 1.1.1.3/32 100 145 192.168.10.1 1781 131045 65002 u*>? 1.1.2.1/32 100 10 10.20.30.40 1894 131 No As-Path u*>? 89.75.232.0/24 100 632 14.191.131.3 4 119 No As-Path ------------------------------------------------------------------------------- """ parser = routetableBgpTimos.RouteTableBGPTimosParser() parser.process(text) self.assertEqual( sorted( [("bgp ipv4","1.1.1.2/32","10.123.121.1"), ("bgp ipv4","1.1.1.3/32","192.168.10.1"), ("bgp ipv4","1.1.2.1/32","10.20.30.40"), ("bgp ipv4","89.75.232.0/24","14.191.131.3")] ) , sorted ( parser.routes_nexthop_protocol("923823") ) )
def test_empty_display(self): text = """*A:7750# show router 923823 bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- """ parser = routetableBgpTimos.RouteTableBGPTimosParser() parser.process(text) self.assertEqual( sorted( [] ) , sorted ( parser.routes_nexthop_protocol("923823") ) )
product = routetableHVRP.RouteTableHVRPParser() else: product = routetableTimos.RouteTableTimosParser() else: logging.debug("forcing type to {}".format(force_type)) if force_type == "sros": product = routetableTimos.RouteTableTimosParser() elif force_type == "sros bgp": product = routetableBgpTimos.RouteTableBGPTimosParser() elif force_type == "hvrp": product = routetableHVRP.RouteTableHVRPParser() else: product = routetableTimos.RouteTableTimosParser() logging.debug("Log Parser object created {}".format(product)) product.process(content) return product if __name__ == "__main__": import sys print("hola") a = routetableBgpTimos.RouteTableBGPTimosParser() b = routetableTimos.RouteTableTimosParser() c = routetableHVRP.RouteTableHVRPParser() logging.debug(a) logging.debug(b) logging.debug(c) d = LogParser().factory(sys.argv[1]) logging.debug(d)
def test_multiple_tables(self): text = """*A:7750# show router bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- u*>? 1.1.1.3/32 100 None 14.191.131.3 1779 131045 65002 ------------------------------------------------------------------------------- *A:7750# show router 43214321 bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- u*>? 1.1.1.3/32 100 None 14.191.131.3 1779 131045 65002 ------------------------------------------------------------------------------- *A:7750# show router 12345 bgp routes =============================================================================== BGP Router ID:14.191.131.2 AS:65000 Local AS:65000 =============================================================================== Legend - Status codes : u - used, s - suppressed, h - history, d - decayed, * - valid l - leaked, x - stale, > - best, b - backup, p - purge Origin codes : i - IGP, e - EGP, ? - incomplete =============================================================================== BGP IPv4 Routes =============================================================================== Flag Network LocalPref MED Nexthop (Router) Path-Id Label As-Path ------------------------------------------------------------------------------- u*>? 1.1.1.3/32 100 None 14.191.131.3 1779 131045 65002 ------------------------------------------------------------------------------- """ parser = routetableBgpTimos.RouteTableBGPTimosParser() parser.process(text) self.assertEqual( sorted(["12345", "43214321", "Base"]) , sorted(list(parser.routersID())) )