Revision 38ce329e mininet/topo.py

View differences:

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