Newer
Older
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
"""
This script builds a network using mininet for using with
a remote controller like POX.
The script receives from command line two arguments. The number
of Switches and the number of Hosts per Switch. Then, it will build
the network topology based on this arguments.
First of all, it build a topology and add the Switches to the network.
After that, add the same number of Hosts for each Switch added. Lastly
it make links between each switch.
@author: Gustavo Pantuza
@since: 18.07.2013
"""
from os import environ
from sys import argv
from sys import exit
from getopt import getopt
from getopt import GetoptError
from mininet.topo import Topo
from mininet.log import setLogLevel
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.node import RemoteController
class MyTopo(Topo):
""" Topology Class Used in all classes to add network components """
def __init__(self, **opts):
super(MyTopo, self).__init__(**opts)
class Switches():
""" Class to control switches addition """
def __init__(self, num, topo):
for i in xrange(num):
topo.addSwitch('s%s' % str(i))
class Hosts():
""" Class to control host addition """
def __init__(self):
self._seq = 0
def add(self, num, switch, topo):
""" Adds hosts and its links """
for i in xrange(num):
host = topo.addHost("h%s" % str(self._seq))
topo.addLink(host, switch)
self._seq += 1
class Links():
""" Create links between two switches """
def __init__(self, topo, switch0, switch1):
topo.addLink(switch0, switch1)
class ArgParser(object):
""" Command line arguments parser class """
# possible command line options
OPTS = "s:h:"
def __init__(self, argv):
""" Tries to get options from command line """
try:
self.opts, self.args = getopt(argv, ArgParser.OPTS, ["help"])
except GetoptError:
self.usage()
self.switches = 2 # default value for the number of switches
self.hosts = 5 # default value for the number of hosts
self.parse()
def parse(self):
""" Parses the arguments and set values to the script execution """
for opt, args in self.opts:
if opt == "--help":
self.usage()
elif opt == "-s":
self.switches = int(args)
elif opt == "-h":
self.hosts = int(args)
def usage(self):
""" Prints how to usage the program """
print "python dinamicnet.py -s <Number-of-Switches>" \
" -h <hosts-per-switch>"
exit()
if __name__ == "__main__":
# Defines the log level
setLogLevel('info')
# parses command line arguments
parser = ArgParser(argv[1:])
# Build network topology
topo = MyTopo()
# Adds Switches to network
Switches(parser.switches, topo)
# Adds Hosts to network
hosts = Hosts()
for i, switch in enumerate(topo.switches()):
hosts.add(parser.hosts, switch, topo)
try: # Add links between switches
Links(topo, switch, topo.switches()[i + 1])
except IndexError:
break
# Creates the Network using a remote controller
net = Mininet(topo,
controller=lambda a: RemoteController(a, ip='127.0.0.1'))
# Starts the network
net.start()
# Run the mininet client
CLI(net)
# Stop the network
net.stop()