Changed custom feature so that params aren't added to globals.
There is probably a better way of doing this, but currently parseCustomFile can modify globals (e.g. TOPOS) as well as instance variables (self.validate) and classes (e.g. MyTopo), which are also in the global name space. Inconveniently enough, lambdas don't seem to be full closures in Python; if they were, this trickiness would be unnecessary. Even so, using execfile() seems like it might be a bit dubious...
This commit is contained in:
@@ -81,19 +81,30 @@ class MininetRunner( object ):
|
||||
self.parseArgs()
|
||||
self.setup()
|
||||
self.begin()
|
||||
|
||||
def parseCustomFile( self, custom ):
|
||||
"Parse custom file and add params before parsing cmd-line options."
|
||||
if os.path.isfile( custom ):
|
||||
execfile( custom, globals(), globals() )
|
||||
if 'topos' in globals(): TOPOS.update( topos )
|
||||
if 'switches' in globals(): SWITCHES.update( switches )
|
||||
if 'hosts' in globals(): HOSTS.update( hosts )
|
||||
if 'controllers' in globals(): CONTROLLERS.update( controllers )
|
||||
if 'validate' in globals(): self.validate = validate
|
||||
|
||||
def setCustom( self, name, value ):
|
||||
print "got", self, name, value
|
||||
if name in ( 'topos', 'switches', 'hosts', 'controllers' ):
|
||||
# Update dictionaries
|
||||
param = name.upper()
|
||||
globals()[ param ].update( value )
|
||||
elif name == 'validate':
|
||||
# Add custom validate function
|
||||
self.validate = value
|
||||
else:
|
||||
raise Exception( 'could not find custom file: %s' % custom )
|
||||
|
||||
# Add or modify global variable or class
|
||||
globals()[ name ] = value
|
||||
|
||||
def parseCustomFile( self, fileName ):
|
||||
"Parse custom file and add params before parsing cmd-line options."
|
||||
custom = {}
|
||||
if os.path.isfile( fileName ):
|
||||
execfile( fileName, custom, custom )
|
||||
for name in custom:
|
||||
self.setCustom( name, custom[ name ] )
|
||||
else:
|
||||
raise Exception( 'could not find custom file: %s' % fileName )
|
||||
|
||||
def parseArgs( self ):
|
||||
"""Parse command-line args and return options object.
|
||||
returns: opts parse options dict"""
|
||||
@@ -161,7 +172,7 @@ class MininetRunner( object ):
|
||||
port=self.options.port )
|
||||
|
||||
if self.validate:
|
||||
self.validate(self.options)
|
||||
self.validate( self.options )
|
||||
|
||||
controllerParams = ControllerParams( 0x0a000000, 8 ) # 10.0.0.0/8
|
||||
inNamespace = self.options.inNamespace
|
||||
|
||||
@@ -13,10 +13,10 @@ topology enables one to pass in '--topo=mytopo' from the command line.
|
||||
from mininet.topo import Topo, Node
|
||||
|
||||
class MyTopo( Topo ):
|
||||
"""Simple topology example."""
|
||||
"Simple topology example."
|
||||
|
||||
def __init__( self, enable_all = True ):
|
||||
"""Create custom topo."""
|
||||
"Create custom topo."
|
||||
|
||||
# Add default members to class.
|
||||
super( MyTopo, self ).__init__()
|
||||
@@ -42,4 +42,6 @@ class MyTopo( Topo ):
|
||||
self.enable_all()
|
||||
|
||||
|
||||
topos = { 'mytopo': ( lambda: MyTopo() ) }
|
||||
topos = { 'mytopo': MyTopo }
|
||||
|
||||
# topos = { 'mytopo': ( lambda: MyTopo() ) }
|
||||
+1
-2
@@ -88,8 +88,7 @@ class CLI( Cmd ):
|
||||
|
||||
def do_net( self, args ):
|
||||
"List network connections."
|
||||
for switchDpid in self.mn.topo.switches():
|
||||
switch = self.mn.nodes[ switchDpid ]
|
||||
for switch in self.mn.switches:
|
||||
info( '%s <->', switch.name )
|
||||
for intf in switch.intfs:
|
||||
node = switch.connection[ intf ]
|
||||
|
||||
+2
-2
@@ -303,14 +303,14 @@ class Mininet( object ):
|
||||
ip = topo.ip( hostId )
|
||||
host = self.addHost( name, defaultIp=ip, defaultMac=mac )
|
||||
self.idToNode[ hostId ] = host
|
||||
info( name )
|
||||
info( name + ' ' )
|
||||
info( '\n*** Adding switches:\n' )
|
||||
for switchId in sorted( topo.switches() ):
|
||||
name = 's' + topo.name( switchId )
|
||||
mac = macColonHex( switchId) if self.setMacs else None
|
||||
switch = self.addSwitch( name, defaultMac=mac )
|
||||
self.idToNode[ switchId ] = switch
|
||||
info( name )
|
||||
info( name + ' ' )
|
||||
info( '\n*** Adding edges:\n' )
|
||||
for srcId, dstId in sorted( topo.edges() ):
|
||||
src, dst = self.idToNode[ srcId ], self.idToNode[ dstId ]
|
||||
|
||||
Reference in New Issue
Block a user