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_multiple_routes_nexthop_protocol(self): text = """*A:7750$ show router 12345 route-table =============================================================================== Route Table (Service: 12345) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 1.1.1.2/32 Local Local 76d07h31m 0 loop-1 0 1.1.1.3/32 Remote BGP 14d07h27m 170 14.191.131.3 (tunneled:RSVP:2) 0 1.1.2.1/32 Remote ISIS 38d19h02m 18 14.191.131.118 10 10.10.10.1/32 Blackh* Static 00h01m22s 5 Black Hole 1 ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual( sorted([("Local", "1.1.1.2/32", "loop-1"), ("BGP", "1.1.1.3/32", "14.191.131.3"), ("ISIS", "1.1.2.1/32", "14.191.131.118"), ("Static", "10.10.10.1/32", "Black Hole")]), sorted(parser.routes_nexthop_protocol("12345")))
def test_empty_display(self): text = """*A:7750$ show router 11111 route-table =============================================================================== Route Table (Service: Base) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual(sorted([]), sorted(parser.routes_nexthop_protocol("11111")))
def test_one_route(self): text = """*A:7750$ show router 12345 route-table =============================================================================== Route Table (Service: 12345) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 1.1.1.2/32 Local Local 76d07h31m 0 loop-1 0 ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual([("1.1.1.2/32", )], parser.routes("12345"))
def test_routersID(self): text = """*A:7750>config>service>vprn$ show router 99 route-table =============================================================================== Route Table (Service: 99) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 1.1.1.2/32 Local Local 76d07h31m 0 loop-1 0 ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual(["99"], list(parser.routersID()))
def test_route_protocol(self): text = """*A:7750$ show router route-table =============================================================================== Route Table (Service: Base) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 1.1.2.3/32 Remote BGP 14d07h27m 170 14.191.131.3 (tunneled:RSVP:2) 0 ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual([("BGP", "1.1.2.3/32", "14.191.131.3")], parser.routes_nexthop_protocol("Base"))
def test_black_hole_nexthop(self): text = """*A:7750$ show router 11111 route-table =============================================================================== Route Table (Service: 11111) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 10.10.10.1/32 Blackh* Static 00h01m22s 5 Black Hole 1 10.10.10.2/32 Blackh* Static 00h01m22s 5 Black Hole 1 ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual( sorted([("Static", "10.10.10.1/32", "Black Hole"), ("Static", "10.10.10.2/32", "Black Hole")]), sorted(parser.routes_nexthop_protocol("11111")))
def test_multiple_tables(self): text = """*A:7750$ show router 12345 route-table =============================================================================== Route Table (Service: 12345) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 1.1.1.2/32 Local Local 76d07h31m 0 loop-1 0 ------------------------------------------------------------------------------- *A:7750$ show router 43214321 route-table =============================================================================== Route Table (Service: 43214321) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- *A:7750$ show router route-table =============================================================================== Route Table (Service: Base) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 1.1.1.2/32 Local Local 76d07h31m 0 loop-1 0 ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual(sorted(["12345", "43214321", "Base"]), sorted(list(parser.routersID())))
def test_wrong_display(self): text = """*A:7750$ show router 11111 route-table =============================================================================== Route Table (Service: Base) =============================================================================== Dest Prefix[Flags] Type Proto Age Pref Next Hop[Interface Name] Metric ------------------------------------------------------------------------------- 1.1.1.2/32 Local Local 76d07h31m 0 loop-1 0 1.1.1.3/32 Remote BGP 14d07h27m 170 14.191.131.3 (tunneled:RSVP:2) 0 1.1.2.1/32 Remote ISIS 38d19h02m 18 14.191.131.118 10 10.10.10.1/32 Blackh* Static 00h01m22s 5 Black Hole 1 ------------------------------------------------------------------------------- """ parser = routetableTimos.RouteTableTimosParser() parser.process(text) self.assertEqual(sorted([]), sorted(parser.routes_nexthop_protocol("11111")))
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)