This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.py
91 lines (70 loc) · 2.53 KB
/
topology.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
from parsgml import GmlManager
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.node import RemoteController, OVSSwitch
from mininet.log import info, setLogLevel
from time import sleep
import sys
class TopologyManager:
def __init__(self):
tmp = GmlManager()
tmp.parse()
self.topologies = tmp.topologies
with open("unlinks.txt", "r") as unlinks_file:
unlinks = map(str.strip, unlinks_file.readlines())
self.unlinks = [list(map(int, t.split(","))) for t in unlinks]
self.net = None
self.cnt_name = None
def addController(self, name, ip, port):
info(f"*** Adding controller: {name} | {ip = } | {port = }\n")
self.cnt_name = name
return self.net.addController(
name=name, controller=RemoteController, ip=ip, port=int(port)
)
def addSwitch(self, name, dpid, proto="OpenFlow13"):
info(f"*** Adding switch: {name} | {dpid = }\n")
return self.net.addSwitch(
name=name, dpid=str(dpid), protocols=proto, cls=OVSSwitch
)
def addLink(self, S1, S2):
info(f"*** Adding link between: {int(S1.dpid)} | {int(S2.dpid)}\n")
self.net.addLink(S1, S2)
def delLink(self, S1, S2):
info(f"*** Delete link between: {int(S1.dpid)} | {int(S2.dpid)}\n")
self.net.delLinkBetween(S1, S2)
def run(self, ip, port, n_topo):
setLogLevel("info")
info("*** Prepairing topology\n")
info("\n*** Start emulating ***\n")
self.net = Mininet(controller=RemoteController)
topo = self.topologies[n_topo]
unlinks = self.unlinks[n_topo]
c0 = self.addController("C", ip, port)
switches = []
for i, node in enumerate(topo.nodes):
switches.append(self.addSwitch(f"S{i + 1}", i + 1))
for edge in topo.edges:
self.addLink(
switches[edge.source],
switches[edge.target],
)
for sw in switches:
sw.start([c0])
self.net.start()
for i in range(1, 20):
info(f"Sleep for {i}\n")
sleep(1)
for i in unlinks:
edge = topo.edges[i]
self.delLink(
switches[edge.source],
switches[edge.target],
)
sleep(1)
CLI(self.net)
def __del__(self):
self.net.stop()
if __name__ == "__main__":
N_TOPO = int(sys.argv[1])
manager = TopologyManager()
manager.run("127.0.0.1", "6653", N_TOPO)