ivc | c4730b1 | 2019-01-23 07:56:53 +0300 | [diff] [blame] | 1 | import netaddr |
| 2 | import itertools |
| 3 | |
| 4 | |
| 5 | class Route(object): |
| 6 | def __init__(self, interface, router, cidr, next_hop): |
| 7 | self.interface = interface |
| 8 | self.router = router |
| 9 | self.cidr = cidr |
| 10 | self.next_hop = next_hop |
| 11 | |
| 12 | def __str__(self): |
| 13 | return "%s via %s dev %s src %s # router: %s" % ( |
| 14 | self.cidr, |
| 15 | self.next_hop, |
| 16 | self.interface.name, |
| 17 | self.interface.cidr.ip, |
| 18 | self.router.name) |
| 19 | |
| 20 | |
| 21 | class Interface(object): |
| 22 | def __init__(self, name, cidr): |
| 23 | self.name = name |
| 24 | self.cidr = cidr |
| 25 | |
| 26 | def __str__(self): |
| 27 | return self.name |
| 28 | |
| 29 | @staticmethod |
| 30 | def from_dict(data, default_name=None): |
| 31 | try: |
| 32 | cidr = netaddr.IPNetwork("%(address)s/%(netmask)s" % data) |
| 33 | except KeyError: |
| 34 | cidr = None |
| 35 | return Interface( |
| 36 | name = data.get('name', default_name), |
| 37 | cidr = cidr) |
| 38 | |
| 39 | |
| 40 | class Router(object): |
| 41 | def __init__(self, |
| 42 | name, |
| 43 | addresses=None, |
| 44 | networks=None, |
| 45 | implicit_routes=True): |
| 46 | self.name = name |
| 47 | self.addresses = list(addresses or []) |
| 48 | self.networks = list(networks or []) |
| 49 | if implicit_routes: |
| 50 | self.networks.extend(addr.cidr for addr in self.addresses) |
| 51 | |
| 52 | if any(addr.ip == addr.network for addr in self.addresses): |
| 53 | raise ValueError("Invalid router address") |
| 54 | if any(cidr.ip != cidr.network for cidr in self.networks): |
| 55 | raise ValueError("Invalid destination CIDR") |
| 56 | |
| 57 | def __str__(self): |
| 58 | return self.name |
| 59 | |
| 60 | @staticmethod |
| 61 | def from_dict(data, default_name=None): |
| 62 | options = data.get('options', {}) |
| 63 | return Router( |
| 64 | name = data.get('name', default_name), |
| 65 | addresses = [ |
| 66 | netaddr.IPNetwork(router_addr) |
| 67 | for router_addr in data.get('addresses', []) |
| 68 | ], |
| 69 | networks = [ |
| 70 | netaddr.IPNetwork(dest_cidr).cidr |
| 71 | for dest_cidr in data.get('networks', []) |
| 72 | ], |
| 73 | implicit_routes = options.get('implicit_routes', True) |
| 74 | ) |
| 75 | |
| 76 | def get_routes(self, interface): |
| 77 | next_hops = [addr.ip for addr in self.addresses |
| 78 | if addr.ip in interface.cidr] |
| 79 | |
| 80 | return [ |
| 81 | Route(interface, self, dest_cidr, next_hop) |
| 82 | for next_hop in next_hops |
| 83 | for dest_cidr in self.networks if next_hop not in dest_cidr |
| 84 | ] |
| 85 | |
| 86 | |
| 87 | def get_routes(interfaces, routers): |
| 88 | def sort_key_cidr(route): |
| 89 | return route.cidr.sort_key() |
| 90 | |
| 91 | def sort_key_ifname(route): |
| 92 | return (route.interface.name, route.cidr.sort_key()) |
| 93 | |
| 94 | interfaces = [Interface.from_dict(data, key) |
| 95 | for key, data in interfaces.items()] |
| 96 | routers = [Router.from_dict(data, key) |
| 97 | for key, data in routers.items()] |
| 98 | routes = [route |
| 99 | for interface in interfaces if interface.cidr |
| 100 | for router in routers |
| 101 | for route in router.get_routes(interface)] |
| 102 | |
| 103 | routes_by_cidr = {k: list(g) for k, g in itertools.groupby( |
| 104 | sorted(routes, key=sort_key_cidr), |
| 105 | lambda route: route.cidr) |
| 106 | } |
| 107 | |
| 108 | for cidr, duplicates in routes_by_cidr.items(): |
| 109 | if len(duplicates) > 1: |
| 110 | raise ValueError("Found multiple routes for %s: %s" % (cidr, |
| 111 | ["[via %s dev %s]" % (route.next_hop, route.interface) |
| 112 | for route in duplicates])) |
| 113 | |
| 114 | routes_by_ifname = {k: list(g) for k, g in itertools.groupby( |
| 115 | sorted(routes, key=sort_key_ifname), |
| 116 | lambda route: route.interface.name) |
| 117 | } |
| 118 | |
| 119 | return routes_by_ifname |