Check (and canonicalize) dpid arguments to Switch()
This seems slightly ugly, but it has bitten many people. Closes #268
This commit is contained in:
+15
-11
@@ -752,28 +752,32 @@ class Switch( Node ):
|
|||||||
dpidLen = 16 # digits in dpid passed to switch
|
dpidLen = 16 # digits in dpid passed to switch
|
||||||
|
|
||||||
def __init__( self, name, dpid=None, opts='', listenPort=None, **params):
|
def __init__( self, name, dpid=None, opts='', listenPort=None, **params):
|
||||||
"""dpid: dpid for switch (or None to derive from name, e.g. s1 -> 1)
|
"""dpid: dpid hex string (or None to derive from name, e.g. s1 -> 1)
|
||||||
opts: additional switch options
|
opts: additional switch options
|
||||||
listenPort: port to listen on for dpctl connections"""
|
listenPort: port to listen on for dpctl connections"""
|
||||||
Node.__init__( self, name, **params )
|
Node.__init__( self, name, **params )
|
||||||
self.dpid = ( ( '0' * self.dpidLen + dpid.translate( None, ':' ) )
|
self.dpid = self.defaultDpid( dpid )
|
||||||
[ -self.dpidLen: ] if dpid else self.defaultDpid() )
|
|
||||||
self.opts = opts
|
self.opts = opts
|
||||||
self.listenPort = listenPort
|
self.listenPort = listenPort
|
||||||
if not self.inNamespace:
|
if not self.inNamespace:
|
||||||
self.controlIntf = Intf( 'lo', self, port=0 )
|
self.controlIntf = Intf( 'lo', self, port=0 )
|
||||||
|
|
||||||
def defaultDpid( self ):
|
def defaultDpid( self, dpid=None ):
|
||||||
"Derive dpid from switch name, s1 -> 1"
|
"Return correctly formatted dpid from dpid or switch name (s1 -> 1)"
|
||||||
try:
|
if dpid:
|
||||||
dpid = int( re.findall( r'\d+', self.name )[ 0 ] )
|
# Remove any colons and make sure it's a good hex number
|
||||||
dpid = hex( dpid )[ 2: ]
|
dpid = dpid.translate( None, ':' )
|
||||||
dpid = '0' * ( self.dpidLen - len( dpid ) ) + dpid
|
assert len( dpid ) <= self.dpidLen and int( dpid, 16 ) >= 0
|
||||||
return dpid
|
else:
|
||||||
except IndexError:
|
# Use hex of the first number in the switch name
|
||||||
|
nums = re.findall( r'\d+', self.name )
|
||||||
|
if nums:
|
||||||
|
dpid = hex( int( nums[ 0 ] ) )[ 2: ]
|
||||||
|
else:
|
||||||
raise Exception( 'Unable to derive default datapath ID - '
|
raise Exception( 'Unable to derive default datapath ID - '
|
||||||
'please either specify a dpid or use a '
|
'please either specify a dpid or use a '
|
||||||
'canonical switch name such as s23.' )
|
'canonical switch name such as s23.' )
|
||||||
|
return ( '0' * self.dpidLen + dpid )[ -self.dpidLen : ]
|
||||||
|
|
||||||
def defaultIntf( self ):
|
def defaultIntf( self ):
|
||||||
"Return control interface"
|
"Return control interface"
|
||||||
|
|||||||
Reference in New Issue
Block a user