rename Topo() methods for consistency: add_node() -> addNode()

This commit is contained in:
Bob Lantz
2012-08-16 18:48:41 -07:00
parent 01e0758e5d
commit ce15c4f67d
5 changed files with 39 additions and 39 deletions
+4 -4
View File
@@ -28,10 +28,10 @@ class MyTopo( Topo ):
rightHost = 4 rightHost = 4
# Add nodes # Add nodes
self.add_node( leftSwitch, Node( is_switch=True ) ) self.addNode( leftSwitch, Node( isSwitch=True ) )
self.add_node( rightSwitch, Node( is_switch=True ) ) self.addNode( rightSwitch, Node( isSwitch=True ) )
self.add_node( leftHost, Node( is_switch=False ) ) self.addNode( leftHost, Node( isSwitch=False ) )
self.add_node( rightHost, Node( is_switch=False ) ) self.addNode( rightHost, Node( isSwitch=False ) )
# Add edges # Add edges
self.add_edge( leftHost, leftSwitch ) self.add_edge( leftHost, leftSwitch )
+5 -5
View File
@@ -41,22 +41,22 @@ class LinearTestTopo( Topo ):
Topo.__init__( self, **params ) Topo.__init__( self, **params )
# Create switches and hosts # Create switches and hosts
hosts = [ self.add_host( 'h%s' % h ) hosts = [ self.addHost( 'h%s' % h )
for h in irange( 1, N ) ] for h in irange( 1, N ) ]
switches = [ self.add_switch( 's%s' % s ) switches = [ self.addSwitch( 's%s' % s )
for s in irange( 1, N - 1 ) ] for s in irange( 1, N - 1 ) ]
# Wire up switches # Wire up switches
last = None last = None
for switch in switches: for switch in switches:
if last: if last:
self.add_link( last, switch ) self.addLink( last, switch )
last = switch last = switch
# Wire up hosts # Wire up hosts
self.add_link( hosts[ 0 ], switches[ 0 ] ) self.addLink( hosts[ 0 ], switches[ 0 ] )
for host, switch in zip( hosts[ 1: ], switches ): for host, switch in zip( hosts[ 1: ], switches ):
self.add_link( host, switch ) self.addLink( host, switch )
def linearBandwidthTest( lengths ): def linearBandwidthTest( lengths ):
+3 -3
View File
@@ -15,13 +15,13 @@ class SingleSwitchTopo(Topo):
"Single switch connected to n hosts." "Single switch connected to n hosts."
def __init__(self, n=2, **opts): def __init__(self, n=2, **opts):
Topo.__init__(self, **opts) Topo.__init__(self, **opts)
switch = self.add_switch('s1') switch = self.addSwitch('s1')
for h in range(n): for h in range(n):
# Each host gets 50%/n of system CPU # Each host gets 50%/n of system CPU
host = self.add_host('h%s' % (h + 1), host = self.addHost('h%s' % (h + 1),
cpu=.5 / n) cpu=.5 / n)
# 10 Mbps, 5ms delay, 10% loss # 10 Mbps, 5ms delay, 10% loss
self.add_link(host, switch, self.addLink(host, switch,
bw=10, delay='5ms', loss=10, use_htb=True) bw=10, delay='5ms', loss=10, use_htb=True)
def perfTest(): def perfTest():
+24 -24
View File
@@ -34,7 +34,7 @@ class Topo(object):
self.lopts = {} if lopts is None else lopts self.lopts = {} if lopts is None else lopts
self.ports = {} # ports[src][dst] is port on src that connects to dst self.ports = {} # ports[src][dst] is port on src that connects to dst
def add_node(self, name, **opts): def addNode(self, name, **opts):
"""Add Node to graph. """Add Node to graph.
name: name name: name
opts: node options opts: node options
@@ -43,26 +43,26 @@ class Topo(object):
self.node_info[name] = opts self.node_info[name] = opts
return name return name
def add_host(self, name, **opts): def addHost(self, name, **opts):
"""Convenience method: Add host to graph. """Convenience method: Add host to graph.
name: host name name: host name
opts: host options opts: host options
returns: host name""" returns: host name"""
if not opts and self.hopts: if not opts and self.hopts:
opts = self.hopts opts = self.hopts
return self.add_node(name, **opts) return self.addNode(name, **opts)
def add_switch(self, name, **opts): def addSwitch(self, name, **opts):
"""Convenience method: Add switch to graph. """Convenience method: Add switch to graph.
name: switch name name: switch name
opts: switch options opts: switch options
returns: switch name""" returns: switch name"""
if not opts and self.sopts: if not opts and self.sopts:
opts = self.sopts opts = self.sopts
result = self.add_node(name, is_switch=True, **opts) result = self.addNode(name, isSwitch=True, **opts)
return result return result
def add_link(self, node1, node2, port1=None, port2=None, def addLink(self, node1, node2, port1=None, port2=None,
**opts): **opts):
"""node1, node2: nodes to link together """node1, node2: nodes to link together
port1, port2: ports (optional) port1, port2: ports (optional)
@@ -70,13 +70,13 @@ class Topo(object):
returns: link info key""" returns: link info key"""
if not opts and self.lopts: if not opts and self.lopts:
opts = self.lopts opts = self.lopts
self.add_port(node1, node2, port1, port2) self.addPort(node1, node2, port1, port2)
key = tuple(self.sorted([node1, node2])) key = tuple(self.sorted([node1, node2]))
self.link_info[key] = opts self.link_info[key] = opts
self.g.add_edge(*key) self.g.add_edge(*key)
return key return key
def add_port(self, src, dst, sport=None, dport=None): def addPort(self, src, dst, sport=None, dport=None):
'''Generate port mapping for new edge. '''Generate port mapping for new edge.
@param src source switch name @param src source switch name
@param dst destination switch name @param dst destination switch name
@@ -84,8 +84,8 @@ class Topo(object):
self.ports.setdefault(src, {}) self.ports.setdefault(src, {})
self.ports.setdefault(dst, {}) self.ports.setdefault(dst, {})
# New port: number of outlinks + base # New port: number of outlinks + base
src_base = 1 if self.is_switch(src) else 0 src_base = 1 if self.isSwitch(src) else 0
dst_base = 1 if self.is_switch(dst) else 0 dst_base = 1 if self.isSwitch(dst) else 0
if sport is None: if sport is None:
sport = len(self.ports[src]) + src_base sport = len(self.ports[src]) + src_base
if dport is None: if dport is None:
@@ -100,24 +100,24 @@ class Topo(object):
else: else:
return self.g.nodes() return self.g.nodes()
def is_switch(self, n): def isSwitch(self, n):
'''Returns true if node is a switch.''' '''Returns true if node is a switch.'''
info = self.node_info[n] info = self.node_info[n]
return info and info.get('is_switch', False) return info and info.get('isSwitch', False)
def switches(self, sort=True): def switches(self, sort=True):
'''Return switches. '''Return switches.
sort: sort switches alphabetically sort: sort switches alphabetically
@return dpids list of dpids @return dpids list of dpids
''' '''
return [n for n in self.nodes(sort) if self.is_switch(n)] return [n for n in self.nodes(sort) if self.isSwitch(n)]
def hosts(self, sort=True): def hosts(self, sort=True):
'''Return hosts. '''Return hosts.
sort: sort hosts alphabetically sort: sort hosts alphabetically
@return dpids list of dpids @return dpids list of dpids
''' '''
return [n for n in self.nodes(sort) if not self.is_switch(n)] return [n for n in self.nodes(sort) if not self.isSwitch(n)]
def links(self, sort=True): def links(self, sort=True):
'''Return links. '''Return links.
@@ -180,10 +180,10 @@ class SingleSwitchTopo(Topo):
self.k = k self.k = k
switch = self.add_switch('s1') switch = self.addSwitch('s1')
for h in irange(1, k): for h in irange(1, k):
host = self.add_host('h%s' % h) host = self.addHost('h%s' % h)
self.add_link(host, switch) self.addLink(host, switch)
class SingleSwitchReversedTopo(Topo): class SingleSwitchReversedTopo(Topo):
@@ -201,10 +201,10 @@ class SingleSwitchReversedTopo(Topo):
''' '''
super(SingleSwitchReversedTopo, self).__init__(**opts) super(SingleSwitchReversedTopo, self).__init__(**opts)
self.k = k self.k = k
switch = self.add_switch('s1') switch = self.addSwitch('s1')
for h in irange(1, k): for h in irange(1, k):
host = self.add_host('h%s' % h) host = self.addHost('h%s' % h)
self.add_link(host, switch, self.addLink(host, switch,
port1=0, port2=(k - h + 1)) port1=0, port2=(k - h + 1))
class LinearTopo(Topo): class LinearTopo(Topo):
@@ -222,9 +222,9 @@ class LinearTopo(Topo):
lastSwitch = None lastSwitch = None
for i in irange(1, k): for i in irange(1, k):
host = self.add_host('h%s' % i) host = self.addHost('h%s' % i)
switch = self.add_switch('s%s' % i) switch = self.addSwitch('s%s' % i)
self.add_link( host, switch) self.addLink( host, switch)
if lastSwitch: if lastSwitch:
self.add_link( switch, lastSwitch) self.addLink( switch, lastSwitch)
lastSwitch = switch lastSwitch = switch
+3 -3
View File
@@ -19,13 +19,13 @@ class TreeTopo( Topo ):
returns: last node added""" returns: last node added"""
isSwitch = depth > 0 isSwitch = depth > 0
if isSwitch: if isSwitch:
node = self.add_switch( 's%s' % self.switchNum ) node = self.addSwitch( 's%s' % self.switchNum )
self.switchNum += 1 self.switchNum += 1
for _ in range( fanout ): for _ in range( fanout ):
child = self.addTree( depth - 1, fanout ) child = self.addTree( depth - 1, fanout )
self.add_link( node, child ) self.addLink( node, child )
else: else:
node = self.add_host( 'h%s' % self.hostNum ) node = self.addHost( 'h%s' % self.hostNum )
self.hostNum += 1 self.hostNum += 1
return node return node