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

Speed up initialization

parent 015fba10
......@@ -57,3 +57,11 @@ class CapybaraNetty(metaclass=MetaCapybaraNetty):
@staticmethod
def log_format_ip(ip):
return f"{ip:<16s}"
# https://stackoverflow.com/a/60353457
@staticmethod
async def chain_coroutines(*aws):
ret = None
for aw in aws:
ret = await aw
return ret
......@@ -98,9 +98,9 @@ class ControllerStatic(CapybaraNetty):
next_router = G.nodes[next_hop_name]["obj"]
inbound_interface_name = next_router.get_interface_name(current_router.name_simplified)
exit_interface_name = current_router.get_interface_name(next_router.name_simplified)
next_router_interfaces = await next_router.get_interfaces()
next_router_interfaces = next_router.get_managed_interfaces()
exit_gateway_ip = ipaddress.ip_interface(
next_router_interfaces[inbound_interface_name]["ipAddresses"][0]["address"]
next_router_interfaces[inbound_interface_name]["ip"]
).ip
current_router_improved_routes.append(
{
......@@ -118,8 +118,10 @@ class ControllerStatic(CapybaraNetty):
return current_best_routes
async def install_viable_external_routes(self, current_best_routes):
coroutines = []
# Compare existing routes and the ones which are CURRENT best routes
# If there are any existing routes which are not used
# If there are any existing routes which are not used in CURRENT - delete them
for router, new_routes in current_best_routes.items():
# Convert dict to list of routes
current_managed_routes = list(router.managed_routes.values())
......@@ -127,9 +129,11 @@ class ControllerStatic(CapybaraNetty):
routes_to_be_added = [new_route for new_route in new_routes if new_route not in current_managed_routes]
self.__logger.debug(f"{router.log_name()} has {len(routes_to_be_deleted)} DEL ROUTE actions")
self.__logger.debug(f"{router.log_name()} has {len(routes_to_be_added)} ADD ROUTE actions")
# todo async
await router.del_routes(routes_to_be_deleted)
await router.add_routes(routes_to_be_added)
coroutines.append(
self.chain_coroutines(router.del_routes(routes_to_be_deleted), router.add_routes(routes_to_be_added))
)
return asyncio.gather(*coroutines)
@staticmethod
def get_improvement_obj(target, path, optimization_ms):
......
import asyncio
import ipaddress
import asyncssh
......@@ -39,15 +40,18 @@ class Router(CapybaraNetty):
if not await self.is_alive():
self.__logger.warning(f"{self.ip} skipped due to SSH connection failure")
return self
await self.get_routes()
await self.get_config_routes()
await self.get_interfaces()
await asyncio.gather(
self.chain_coroutines(self.get_routes(), self.del_all_config_routes()),
self.get_config_routes(),
self.get_interfaces(),
self.chain_coroutines(
self.del_existing_interfaces(), self.add_dummy_interface(interface_name=self.dummy_interface_name)
),
)
self.get_default_interface_name()
self.get_default_interface_ip()
self.get_default_gateway()
await self.del_all_config_routes()
await self.del_existing_interfaces()
await self.add_dummy_interface(interface_name=self.dummy_interface_name)
return self
......@@ -156,6 +160,9 @@ class Router(CapybaraNetty):
self.interfaces = await self.vty.get_interfaces()
return self.interfaces
def get_managed_interfaces(self):
return self.managed_interfaces
# By default checks if our managed routes - exist on the router
# if must_not_exist is passed - will check for the on-existence of passed routes
async def assert_route_state(self, must_not_exist=None):
......@@ -185,4 +192,4 @@ class Router(CapybaraNetty):
return f"{self.log_name()} @ {self.ip} with {len(self.managed_interfaces)} interfaces"
def log_name(self):
return f"{self.name:<20s}"
return f"{self.name_simplified:<12s}"
......@@ -54,7 +54,7 @@ class Vty(CapybaraNetty):
commands = []
if len(routes) > 0:
for route in routes:
commands.append(f"no {self.generate_route_config_cmd(**route)} tag 2")
commands.append(f"no {self.generate_route_config_cmd(**route)}")
return await self.ssh_exec(
self.build_vty_exec(commands, echo_commands=True),
)
......
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