From 928c0761a02fbd8d80a2baab44bbaf1fb12b6315 Mon Sep 17 00:00:00 2001 From: Brandon Heller Date: Wed, 30 May 2012 00:08:01 -0700 Subject: [PATCH] Move code from mn into mininet/util to enable reuse Any code in mn is not usable by other Python code. Hence, move this code into util, so other scripts can use it. --- bin/mn | 56 +++---------------------------------------------- mininet/util.py | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 53 deletions(-) diff --git a/bin/mn b/bin/mn index 02a7934..91a6369 100755 --- a/bin/mn +++ b/bin/mn @@ -26,33 +26,8 @@ from mininet.node import ( Host, CPULimitedHost, Controller, OVSController, from mininet.link import Link, TCLink from mininet.topo import SingleSwitchTopo, LinearTopo, SingleSwitchReversedTopo from mininet.topolib import TreeTopo -from mininet.util import makeNumeric, custom - - -def customConstructor( constructors, argStr ): - """Return custom constructor based on argStr - - The args and key/val pairs in argsStr will be automatically applied - when the generated constructor is later used. - """ - cname, newargs, kwargs = splitArgs( argStr ) - constructor = constructors.get( cname, None ) - - if not constructor: - raise Exception( "error: %s is unknown - please specify one of %s" % - ( cname, constructors.keys() ) ) - - def customized( name, *args, **params ): - "Customized constructor, useful for Node, Link, and other classes" - params.update( kwargs ) - if not newargs: - return constructor( name, *args, **params ) - if args: - warn( 'warning: %s replacing %s with %s\n' % ( - constructor, args, newargs ) ) - return constructor( name, *newargs, **params ) - - return customized +from mininet.util import makeNumeric, custom, customConstructor, splitArgs +from mininet.util import buildTopo # built in topologies, created only when run @@ -93,31 +68,6 @@ ALTSPELLING = { 'pingall': 'pingAll', 'pingpair': 'pingPair', 'iperfudp': 'iperfUdp', 'iperfUDP': 'iperfUdp', 'prefixlen': 'prefixLen' } -def splitArgs( argstr ): - """Split argument string into usable python arguments - argstr: argument string with format fn,arg2,kw1=arg3... - returns: fn, args, kwargs""" - split = argstr.split( ',' ) - fn = split[ 0 ] - params = split[ 1: ] - # Convert int and float args; removes the need for function - # to be flexible with input arg formats. - args = [ makeNumeric( s ) for s in params if '=' not in s ] - kwargs = {} - for s in [ p for p in params if '=' in p ]: - key, val = s.split( '=' ) - kwargs[ key ] = makeNumeric( val ) - return fn, args, kwargs - - -def buildTopo( topoStr ): - "Create topology from string with format (object, arg1, arg2,...)." - topo, args, kwargs = splitArgs( topoStr ) - if topo not in TOPOS: - raise Exception( 'Invalid topo name %s' % topo ) - return TOPOS[ topo ]( *args, **kwargs ) - - def addDictOption( opts, choicesDict, default, name, helpStr=None ): """Convenience function to add choices dicts to OptionParser. opts: OptionParser instance @@ -248,7 +198,7 @@ class MininetRunner( object ): start = time.time() - topo = buildTopo( self.options.topo ) + topo = buildTopo( TOPOS, self.options.topo ) switch = customConstructor( SWITCHES, self.options.switch ) host = customConstructor( HOSTS, self.options.host ) controller = customConstructor( CONTROLLERS, self.options.controller ) diff --git a/mininet/util.py b/mininet/util.py index af4b17d..19cb552 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -400,3 +400,54 @@ def custom( cls, **params ): kwargs.update( params ) return cls( *args, **kwargs ) return customized + +def splitArgs( argstr ): + """Split argument string into usable python arguments + argstr: argument string with format fn,arg2,kw1=arg3... + returns: fn, args, kwargs""" + split = argstr.split( ',' ) + fn = split[ 0 ] + params = split[ 1: ] + # Convert int and float args; removes the need for function + # to be flexible with input arg formats. + args = [ makeNumeric( s ) for s in params if '=' not in s ] + kwargs = {} + for s in [ p for p in params if '=' in p ]: + key, val = s.split( '=' ) + kwargs[ key ] = makeNumeric( val ) + return fn, args, kwargs + +def customConstructor( constructors, argStr ): + """Return custom constructor based on argStr + + The args and key/val pairs in argsStr will be automatically applied + when the generated constructor is later used. + """ + cname, newargs, kwargs = splitArgs( argStr ) + constructor = constructors.get( cname, None ) + + if not constructor: + raise Exception( "error: %s is unknown - please specify one of %s" % + ( cname, constructors.keys() ) ) + + def customized( name, *args, **params ): + "Customized constructor, useful for Node, Link, and other classes" + params.update( kwargs ) + if not newargs: + return constructor( name, *args, **params ) + if args: + warn( 'warning: %s replacing %s with %s\n' % ( + constructor, args, newargs ) ) + return constructor( name, *newargs, **params ) + + return customized + +def buildTopo( topos, topoStr ): + """Create topology from string with format (object, arg1, arg2,...). + + input topos is a dict of topo names to constructors, possibly w/args. + """ + topo, args, kwargs = splitArgs( topoStr ) + if topo not in topos: + raise Exception( 'Invalid topo name %s' % topo ) + return topos[ topo ]( *args, **kwargs )