Revision 38ce329e

View differences:

mininet/net.py
337 337
            paramDict: dictionary of additional link params (optional)
338 338
            params: additional link params (optional)
339 339
            returns: link object"""
340
        mac1 = self.randMac()
341
        mac2 = self.randMac()
342
        paramDict = {} if paramDict is None else paramDict
343
        paramDict.update( params )
344 340
        # Ugly: try to ensure that node1 and node2 line up correctly with
345
        # other link parameters
346
        node1 = self[ paramDict.pop( 'node1', node1.name ) ]
347
        node2 = self[ paramDict.pop( 'node2', node2.name ) ]
341
        # other link parameters, and allow either nodes or names
342
        paramDict = {} if paramDict is None else dict( paramDict )
343
        paramDict.update( params )
344
        node1 = paramDict.pop( 'node1', node1 )
345
        node2 = paramDict.pop( 'node2', node1 )
346
        node1 = node1 if type( node1 ) != str else self[ node1 ]
347
        node2 = node2 if type( node2 ) != str else self[ node2 ]
348 348
        paramDict.setdefault( 'port1', port1 )
349 349
        paramDict.setdefault( 'port2', port2 )
350
        paramDict.setdefault( 'addr1', mac1 )
351
        paramDict.setdefault( 'addr2', mac2 )
350
        paramDict.setdefault( 'addr1', self.randMac() )
351
        paramDict.setdefault( 'addr2', self.randMac() )
352 352
        cls = self.link if cls is None else cls
353 353
        link = cls( node1, node2, **paramDict )
354 354
        self.links.append( link )
......
408 408
            info( switchName + ' ' )
409 409

  
410 410
        info( '\n*** Adding links:\n' )
411
        for srcName, dstName in topo.links( sort=True ):
412
            src, dst = self.nameToNode[ srcName ], self.nameToNode[ dstName ]
413
            params = topo.linkInfo( srcName, dstName )
414
            srcPort, dstPort = topo.port( srcName, dstName )
415
            self.addLink( src, dst, srcPort, dstPort, paramDict=params )
416
            info( '(%s, %s) ' % ( src.name, dst.name ) )
411
        for srcName, dstName, params in topo.links(
412
                sort=True, withInfo=True ):
413
            self.addLink( srcName, dstName, paramDict=params )
414
            info( '(%s, %s) ' % ( srcName, dstName ) )
417 415

  
418 416
        info( '\n' )
419 417

  
mininet/topo.py
53 53
           data: return list of ( node, attrs)"""
54 54
        return self.node.items() if data else self.node.keys()
55 55

  
56
    def edges_iter( self, data=False ):
56
    def edges_iter( self, data=False, key=False):
57 57
        "Iterator: return graph edges"
58 58
        for src, entry in self.edge.iteritems():
59 59
            for dst, keys in entry.iteritems():
60 60
                if [ src, dst ] != sorted( [ src, dst ] ):
61 61
                    # Skip duplicate edges
62 62
                    continue
63
                for key, attrs in keys.iteritems():
63
                for k, attrs in keys.iteritems():
64 64
                    if data:
65 65
                        yield( ( src, dst, attrs ) )
66
                    elif key:
67
                        yield( ( src, dst, k ) )
66 68
                    else:
67 69
                        yield( ( src, dst ) )
68 70

  
69
    def edges( self, data=False ):
71
    def edges( self, data=False, key=False ):
70 72
        "Return list of graph edges"
71
        return list( self.edges_iter( data ) )
73
        return list( self.edges_iter( data=data, key=key ) )
72 74

  
73 75

  
74 76
    def __getitem__( self, node ):
......
88 90
        return g
89 91

  
90 92

  
91
class Topo(object):
93
class Topo( object ):
92 94
    "Data center network representation for structured multi-trees."
93 95

  
94 96
    def __init__( self, *args, **params ):
......
146 148
            opts = self.lopts
147 149
        port1, port2 = self.addPort(node1, node2, port1, port2)
148 150
        opts.update( node1=node1, node2=node2, port1=port1, port2=port2 )
151
        assert 'node1' in opts
149 152
        self.g.add_edge(node1, node2, key, opts )
150 153
        return key
151 154

  
......
172 175
           returns: list of hosts"""
173 176
        return [ n for n in self.nodes( sort ) if not self.isSwitch( n ) ]
174 177

  
175
    def links( self, sort=True, withKeys=False ):
178
    def links( self, sort=True, withInfo=False, withKeys=False ):
176 179
        """Return links.
177 180
           sort: sort links alphabetically
178 181
           withKeys: return key in tuple
179 182
           @return links list of ( src, dst [,key ] )"""
180 183
        if not sort:
181
            return self.g.edges( withKeys )
184
            return self.g.edges( data=withInfo, key=withKeys )
182 185
        else:
183
            if withKeys:
184
                links = [ tuple( self.sorted( ( s, d ) ) ) + [ k ]
185
                          for s, d, k in self.g.edges( data=True ) ]
186
            if withInfo:
187
                links = [ tuple( self.sorted( ( s, d ) ) + [ info ] )
188
                          for s, d, info in self.g.edges( data=True ) ]
189
            elif withKeys:
190
                links = [ tuple( self.sorted( ( s, d ) ) + [ key ] )
191
                          for s, d, key in self.g.edges( key=True ) ]
186 192
            else:
187
                links = [ tuple ( self.sorted( e ) ) for e in self.g.edges() ]
188
            return sorted( links, key=naturalSeq )
193
                links = [ tuple ( self.sorted( e ) )
194
                          for e in self.g.edges() ]
195
            return self.sorted( links )
189 196

  
190 197
    # This legacy port management mechanism is clunky and will probably
191 198
    # be removed at some point.

Also available in: Unified diff