-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
92 lines (67 loc) · 2.15 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from mininet.node import Switch
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import sys
import time
import json
RELEASE_EXECUTABLE = "./target/release/stp-rs"
class EtherSwitch(Switch):
''' A custom extension of the base mininet switch that
runs the executable for each mininet switch. '''
def __init__(self, name: str, **kwargs):
self.name = name
super(EtherSwitch, self).__init__(name, **kwargs)
def start(self, controllers):
self.cmd(
f'mkdir -p logs && {RELEASE_EXECUTABLE} {self.name} > "logs/{self.name}-log.txt" &')
def stop(self):
self.cmd(f'kill {RELEASE_EXECUTABLE}')
class EtherTopo(Topo):
def __init__(self, topo_file: str, **kwargs):
with open(topo_file, 'r') as topo_file:
self.topo = json.loads(topo_file.read())
super(EtherTopo, self).__init__(**kwargs)
def build(self):
hosts = list(self.topo["topology"]["hosts"].keys())
hosts.sort()
for host in hosts:
print(f"adding host: {host}")
self.addHost(host)
for switch in self.topo["topology"]["switches"]:
print(f"adding switch: {switch}")
self.addSwitch(switch, cls=EtherSwitch)
for link in self.topo["topology"]["links"]:
print(f"adding link: {link}")
self.addLink(link[0], link[1])
def run(interactive: bool, topo_file: str):
topo = EtherTopo(topo_file)
net = Mininet(topo=topo)
net.start()
if interactive:
CLI(net)
else:
sleep_sec = 4
print(f"sleeping for {sleep_sec} sec, let STP set up")
time.sleep(sleep_sec)
net.pingAll()
net.stop()
def usage():
print("**.py [interactive/quiet -i/q] [topology filepath]")
print("sudo python run.py -q ./topo/topo.json")
def main():
args = sys.argv
if len(args) != 3:
usage()
return
interactive = None
if args[1] == "-i":
interactive = True
elif args[1] == "-q":
interactive = False
else:
usage()
return
run(interactive, args[2])
if __name__ == "__main__":
main()