-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
57 lines (49 loc) · 1.66 KB
/
test.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
"""
Run: sudo mn --custom mytopo.py --topo mytopo --switch ovsk --controller remote --mac --link tc
Topo:
-- h2
|-- s2 |
c1 -- s1 -| -- h3
|-- s3 -- h4
"""
import sys, os, json
from mininet.topo import Topo
def LoadTopo(file_name):
# Read file
json_str = ''
with open(file_name, "r") as file:
for line in file:
json_str += line.replace('\n', '').strip()
# Load JSON
topo = json.loads(json_str)
return topo
class MyTopo( Topo ):
def __init__( self ):
"Create custom topo."
# Initialize topology
Topo.__init__( self )
# Load topology from file
topo_data = LoadTopo("topo.json")
# Add hosts and switches
node = {}
for name in topo_data["node"]:
if name[0] == 's':
# Switch
node[name] = self.addSwitch( str(name) )
elif name[0] in {'c', 'h'}:
# Camara or Host
node[name] = self.addHost( str(name) )
# Add links
for link_obj in topo_data["link"]:
# Data mapping
src = node[ link_obj["src"] ]
dst = node[ link_obj["dst"] ]
p1 = link_obj["p1"]
p2 = link_obj["p2"]
linkopts = dict( bw = link_obj.setdefault('bw', 10),
delay = link_obj.setdefault('delay', '10ms'),
jitter = link_obj.setdefault('jitter', '10ms'),
loss = link_obj.setdefault('loss', 1.5) )
# Set link
self.addLink( src, dst, port1=p1, port2=p2, **linkopts )
topos = { 'mytopo': ( lambda: MyTopo() ) }