-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvng_w_STP.py
More file actions
executable file
·122 lines (103 loc) · 3.71 KB
/
vng_w_STP.py
File metadata and controls
executable file
·122 lines (103 loc) · 3.71 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
"""
This imports config from a VirtualNetworkGraph.py .out file and creates
a network with the same node connectivity.
"""
import sys
import argparse
from mininet.net import Mininet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.nodelib import LinuxBridge
def createNet(filename):
linkList = []
switchList = []
hostList = []
"Create an empty network and add nodes to it."
net = Mininet( controller=Controller )
info( '*** Adding controller\n' )
net.addController( 'c0' )
try:
f = open(filename)
except IOError:
print "Cannot open config file! Aborting..."
sys.exit()
for line in f:
if (len(line.split()) > 3):
continue
else:
# Parse lines and create a list of link pairs
node1, node2, weight = line.split()
s1 = "s"+str(node1).strip()
s2 = "s"+str(node2).strip()
linkList.append((s1,s2))
h1 = "h"+str(node1).strip()
h2 = "h"+str(node2).strip()
if h1 not in hostList:
hostList.append(h1)
if h2 not in hostList:
hostList.append(h2)
f.close()
info( '*** Adding switches\n' )
for (node1,node2) in linkList:
if node1 not in switchList:
switchList.append(node1)
net.addSwitch(node1, cls=LinuxBridge, stp=True)
#print switchList
if node2 not in switchList:
switchList.append(node2)
net.addSwitch(node2, cls=LinuxBridge, stp=True)
#print switchList
info( '*** Adding hosts\n' )
for host in hostList:
net.addHost(host)
nodes = [node for node in switchList]
currHost = 0
# Expects specific filenames. :\
if (filename == "linear.out"):
info( '*** Creating switch and host links for LINEAR network\n' )
for (node1,node2) in linkList:
net.addLink(node1,node2)
net.addLink(hostList[currHost],node1)
currHost = currHost + 1
# Add last host to last node2 switch...
net.addLink(hostList[currHost], linkList[currHost-1][1])
elif ((filename == "full.out") or (filename == "random.out")):
info( '*** Creating switch and host links for FULL network\n' )
for (node1, node2) in linkList:
net.addLink(node1,node2)
# Stops duplicate additions where a switch has connected neighbors
if node1 in nodes:
net.addLink(hostList[currHost],node1)
nodes.remove(node1)
currHost = currHost + 1
elif (filename == "star.out"):
info( '*** Creating switch and host links for STAR network\n' )
for (node1, node2) in linkList:
net.addLink(node1,node2)
net.addLink(hostList[currHost], node2)
currHost = currHost + 1
# Add last host to root switch, s0; still maintains star configuration
net.addLink(hostList[currHost], linkList[0][0])
info( '*** Starting network\n')
net.start()
### Comment out next 2 lines if you don't want this...
# Need STP or something better to handle multiple links
if ((filename == "full.out") or (filename == "random.out")):
info( '*** Wait for STP convergence...\n')
net.waitConnected()
info( '*** Running CLI\n' )
CLI( net )
info( '*** Stopping network' )
net.stop()
if __name__ == '__main__':
parser = argparse.ArgumentParser(prog=sys.argv[0], usage="python %(prog)s <configfile>")
parser.add_argument('filename')
try:
args = parser.parse_args()
except IOError:
parser.print_help()
sys.exit(2)
setLogLevel( 'info' )
createNet(args.filename)