Skip to content
Snippets Groups Projects
Commit 0b5609f5 authored by Bob Lantz's avatar Bob Lantz
Browse files

Check (and canonicalize) dpid arguments to Switch()

This seems slightly ugly, but it has bitten many people.

Closes #268
parent ef677432
No related branches found
No related tags found
No related merge requests found
...@@ -752,28 +752,32 @@ class Switch( Node ): ...@@ -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
raise Exception( 'Unable to derive default datapath ID - ' nums = re.findall( r'\d+', self.name )
'please either specify a dpid or use a ' if nums:
'canonical switch name such as s23.' ) dpid = hex( int( nums[ 0 ] ) )[ 2: ]
else:
raise Exception( 'Unable to derive default datapath ID - '
'please either specify a dpid or use a '
'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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment