-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from n-hop/1-define-interfaces-of-network-host-…
…route chore: IHost,IRouting,INetwork
- Loading branch information
Showing
12 changed files
with
187 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# adapter pattern | ||
from interfaces.host import IHost | ||
|
||
class ContainernetHostAdapter(IHost): | ||
def __init__(self, containernet_host): | ||
self.containernet_host = containernet_host | ||
|
||
def cmd(self, command): | ||
return self.containernet_host.cmd(command) | ||
|
||
def name(self) -> str: | ||
"""Get the name of the host. | ||
""" | ||
return self.containernet_host.name | ||
|
||
def IP(self) -> str: | ||
"""Get the IP address of the host. | ||
""" | ||
return self.containernet_host.IP() | ||
|
||
def deleteIntfs(self): | ||
"""Delete all interfaces. | ||
""" | ||
return self.containernet_host.deleteIntfs() | ||
|
||
def cleanup(self): | ||
"""Cleanup the host. | ||
""" | ||
return self.containernet_host.cleanup() | ||
|
||
def get_host(self): | ||
return self.containernet_host | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# python3.6 | ||
pyyaml | ||
dataclasses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
class IRoutingStrategy(ABC): | ||
@abstractmethod | ||
def setup_routes(self, network): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from interfaces.routing import IRoutingStrategy | ||
|
||
class OLSRRouting(IRoutingStrategy): | ||
"""Summary: | ||
Configure routing for the network with OLSR. | ||
""" | ||
def setup_routes(self, network: 'INetwork'): # type: ignore | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from interfaces.routing import IRoutingStrategy | ||
|
||
class OpenrRouting(IRoutingStrategy): | ||
"""Summary: | ||
Configure routing for the network with open-r. | ||
""" | ||
def setup_routes(self, network: 'INetwork'): # type: ignore | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from interfaces.routing import IRoutingStrategy | ||
|
||
class StaticRouting(IRoutingStrategy): | ||
"""Summary: | ||
Configure static routing for the network. | ||
""" | ||
def __init__(self): | ||
self.pair_to_link_ip = {} | ||
self.net_routes = [] | ||
|
||
def setup_routes(self, network: 'INetwork'): | ||
''' | ||
Setup the routing by ip route. | ||
''' | ||
hosts = network.get_hosts() | ||
self.pair_to_link_ip = network.get_link_table() | ||
self.net_routes = [range(network.get_num_of_host())] | ||
for route in self.net_routes: | ||
route = [hosts[i] for i in route] | ||
self._add_route(route) | ||
|
||
@ staticmethod | ||
def _add_ip_gateway(host, gateway_ip, dst_ip): | ||
host.cmd(f'ip r a {dst_ip} via {gateway_ip}') | ||
|
||
def _add_route(self, route): | ||
for i in range(len(route) - 1): | ||
for j in range(i + 1, len(route)): | ||
host = route[i] | ||
gateway = route[i + 1] | ||
dst_prev = route[j - 1] | ||
dst = route[j] | ||
if j < len(route) - 1: | ||
dst_next = route[j + 1] | ||
|
||
# gateway ip is the ip of the second (right) interface | ||
# of the link (route_i, route_{i+1}) | ||
gateway_ip = self.pair_to_link_ip[(host, gateway)] | ||
|
||
# dst ip is the ip of the second (right) interface in the link | ||
# (route_{j-1}, route_j) | ||
dst_ip = self.pair_to_link_ip[(dst_prev, dst)] | ||
if j < len(route) - 1: | ||
dst_ip_right = self.pair_to_link_ip[(dst_next, dst)] | ||
self._add_ip_gateway(host, gateway_ip, dst_ip) | ||
if j < len(route) - 1: | ||
self._add_ip_gateway(host, gateway_ip, dst_ip_right) | ||
|
||
for i in range(1, len(route)): | ||
for j in range(0, i): | ||
host = route[i] | ||
gateway = route[i - 1] | ||
dst_prev = route[j + 1] | ||
dst = route[j] | ||
|
||
if j >= 1: | ||
dst_next = route[j - 1] | ||
|
||
gateway_ip = self.pair_to_link_ip[(host, gateway)] | ||
dst_ip = self.pair_to_link_ip[(dst_prev, dst)] | ||
if j >= 1: | ||
dst_ip_left = self.pair_to_link_ip[(dst_next, dst)] | ||
self._add_ip_gateway(host, gateway_ip, dst_ip) | ||
if j >= 1: | ||
self._add_ip_gateway(host, gateway_ip, dst_ip_left) |
Oops, something went wrong.