Commit aeaa6c27 authored by Alberts S's avatar Alberts S
Browse files

Keep track of created routes in router

parent 292ab1b4
......@@ -22,6 +22,7 @@ class Router(CapybaraNetty):
self.interfaces = []
self.interface_group_id = self.config["router_interface_group_id"]
self.managed_interfaces = {}
self.managed_routes = {}
self.dummy_interface_name = self.config["router_dummy_interface_name"]
@classmethod
......@@ -60,6 +61,7 @@ class Router(CapybaraNetty):
async def del_all_config_routes(self):
await self.get_config_routes()
await self.vty.del_config_routes(self.config_routes)
self.del_managed_routes_info()
def get_default_interface_name(self):
try:
......@@ -82,6 +84,8 @@ class Router(CapybaraNetty):
return self.default_gateway
async def add_routes(self, routes):
for route in routes:
self.add_managed_route_info(route)
await self.vty.add_routes(routes)
async def del_existing_interfaces(self):
......@@ -102,6 +106,12 @@ class Router(CapybaraNetty):
def del_managed_interfaces_info(self):
self.managed_interfaces = {}
def add_managed_route_info(self, route):
self.managed_routes.update({route["prefix"]: route})
def del_managed_routes_info(self):
self.managed_routes = {}
async def add_gre_interface(
self, interface_name, local_ip: ipaddress.ip_address, remote_ip: ipaddress.ip_address, ttl=255
):
......@@ -123,6 +133,9 @@ class Router(CapybaraNetty):
async def add_default_dummy_interface_ip(self, ip: ipaddress.ip_interface):
await self.add_interface_ip(self.dummy_interface_name, ip)
def get_default_dummy_interface_ip(self):
return self.managed_interfaces[self.get_interface_name(self.dummy_interface_name)]["ip"]
def get_interface_name(self, name):
return self.interface_prefix + name
......@@ -132,3 +145,6 @@ class Router(CapybaraNetty):
async def ssh_exec(self, command, **kwargs):
return await super(Router, self).ssh_exec(self.ip, self.ssh_user, command=command)
def __str__(self):
return f"{self.name} @ {self.ip} with {len(self.managed_interfaces)} interfaces"
......@@ -40,11 +40,12 @@ class Vty(CapybaraNetty):
# Make sure commands are batched up otherwise new routes will be slow to apply since 1 command = 1 ssh connection
async def add_routes(self, routes: list):
commands = []
for route in routes:
commands.append(self.generate_route_config_cmd(**route))
await self.ssh_exec(
self.build_vty_exec(commands),
)
if len(routes) > 0:
for route in routes:
commands.append(self.generate_route_config_cmd(**route))
await self.ssh_exec(
self.build_vty_exec(commands),
)
def generate_route_config_cmd(self, prefix, gateway, interface, distance=1, **kwargs):
additional_cmd_args = ""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment