Merge pull request #1 from bocon13/master
Simplified and refactored examples/dynamicnet.py
This commit is contained in:
Regular → Executable
+15
-102
@@ -1,4 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#!/usr/bin/python
|
||||
|
||||
"""
|
||||
This script builds a network using mininet for using with
|
||||
@@ -17,118 +17,28 @@ it make links between each switch.
|
||||
|
||||
"""
|
||||
|
||||
from os import environ
|
||||
from sys import argv
|
||||
from sys import exit
|
||||
from getopt import getopt
|
||||
from getopt import GetoptError
|
||||
from optparse import OptionParser
|
||||
|
||||
from mininet.topo import Topo
|
||||
from mininet.topo import LinearTopo
|
||||
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__":
|
||||
|
||||
def main():
|
||||
# Defines the log level
|
||||
setLogLevel('info')
|
||||
|
||||
# parses command line arguments
|
||||
parser = ArgParser(argv[1:])
|
||||
parser = OptionParser()
|
||||
parser.add_option('-H', dest='hosts', default=5,
|
||||
help='Number of hosts per switch')
|
||||
parser.add_option('-S', dest='switches', default=2,
|
||||
help='Number of switches')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
# 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
|
||||
# Build network topology (see mininet/topo.py)
|
||||
topo = LinearTopo(int(options.switches), int(options.hosts))
|
||||
|
||||
# Creates the Network using a remote controller
|
||||
net = Mininet(topo,
|
||||
@@ -140,3 +50,6 @@ if __name__ == "__main__":
|
||||
CLI(net)
|
||||
# Stop the network
|
||||
net.stop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
+13
-6
@@ -236,23 +236,30 @@ class SingleSwitchReversedTopo(Topo):
|
||||
port1=0, port2=(k - h + 1))
|
||||
|
||||
class LinearTopo(Topo):
|
||||
"Linear topology of k switches, with one host per switch."
|
||||
"Linear topology of k switches, with n hosts per switch."
|
||||
|
||||
def __init__(self, k=2, **opts):
|
||||
def __init__(self, k=2, n=1, **opts):
|
||||
"""Init.
|
||||
k: number of switches (and hosts)
|
||||
k: number of switches
|
||||
n: number of hosts per switch
|
||||
hconf: host configuration options
|
||||
lconf: link configuration options"""
|
||||
|
||||
super(LinearTopo, self).__init__(**opts)
|
||||
|
||||
self.k = k
|
||||
self.n = n
|
||||
|
||||
lastSwitch = None
|
||||
for i in irange(1, k):
|
||||
host = self.addHost('h%s' % i)
|
||||
# Add switch
|
||||
switch = self.addSwitch('s%s' % i)
|
||||
self.addLink( host, switch)
|
||||
# Add hosts to switch
|
||||
for j in irange(1, n):
|
||||
hostNum = (i-1)*n + j
|
||||
host = self.addHost('h%s' % hostNum)
|
||||
self.addLink(host, switch)
|
||||
# Connect switch to previous
|
||||
if lastSwitch:
|
||||
self.addLink( switch, lastSwitch)
|
||||
self.addLink(switch, lastSwitch)
|
||||
lastSwitch = switch
|
||||
|
||||
Reference in New Issue
Block a user