3 "CLI for Mininet Cluster Edition prototype demo"
5 from mininet.cli import CLI
6 from mininet.log import output, error
8 # pylint: disable=global-statement
9 nx, graphviz_layout, plt = None, None, None # Will be imported on demand
12 class ClusterCLI( CLI ):
13 "CLI with additional commands for Cluster Edition demo"
17 "Return a list of background colors for a sequence"
18 colors = [ 'red', 'lightgreen', 'cyan', 'yellow', 'orange',
19 'magenta', 'pink', 'grey', 'brown',
21 slen, clen = len( seq ), len( colors )
22 reps = max( 1, slen / clen )
23 colors = colors * reps
24 colors = colors[ 0 : slen ]
27 def do_plot( self, _line ):
28 "Plot topology colored by node placement"
29 # Import networkx if needed
30 global nx, plt, graphviz_layout
33 # pylint: disable=import-error,no-member
35 nx = networkx # satisfy pylint
36 from matplotlib import pyplot
37 plt = pyplot # satisfy pylint
39 assert pygraphviz # silence pyflakes
40 # Networkx moved this around
41 if hasattr( nx, 'graphviz_layout' ):
42 graphviz_layout = nx.graphviz_layout
44 graphviz_layout = nx.drawing.nx_agraph.graphviz_layout
45 # pylint: enable=import-error,no-member
47 error( 'plot requires networkx, matplotlib and pygraphviz - '
48 'please install them and try again\n' )
50 # Make a networkx Graph
53 servers = getattr( mn, 'servers', [ 'localhost' ] )
54 hosts, switches = mn.hosts, mn.switches
55 nodes = hosts + switches
56 g.add_nodes_from( nodes )
57 links = [ ( link.intf1.node, link.intf2.node )
58 for link in self.mn.links ]
59 g.add_edges_from( links )
60 # Pick some shapes and colors
61 # shapes = hlen * [ 's' ] + slen * [ 'o' ]
62 color = dict( zip( servers, self.colorsFor( servers ) ) )
64 pos = graphviz_layout( g )
65 opts = { 'ax': None, 'font_weight': 'bold',
66 'width': 2, 'edge_color': 'darkblue' }
67 hcolors = [ color[ getattr( h, 'server', 'localhost' ) ]
69 scolors = [ color[ getattr( s, 'server', 'localhost' ) ]
71 nx.draw_networkx( g, pos=pos, nodelist=hosts, node_size=800,
72 label='host', node_color=hcolors, node_shape='s',
74 nx.draw_networkx( g, pos=pos, nodelist=switches, node_size=1000,
75 node_color=scolors, node_shape='o', **opts )
76 # Get rid of axes, add title, and show
79 ax.get_xaxis().set_visible( False )
80 ax.get_yaxis().set_visible( False )
81 fig.canvas.set_window_title( 'Mininet')
82 plt.title( 'Node Placement', fontweight='bold' )
85 def do_status( self, _line ):
86 "Report on node shell status"
87 nodes = self.mn.hosts + self.mn.switches
90 exited = [ node for node in nodes
91 if node.shell.returncode is not None ]
94 output( '%s has exited with code %d\n'
95 % ( node, node.shell.returncode ) )
97 output( 'All nodes are still running.\n' )
99 def do_placement( self, _line ):
100 "Describe node placement"
102 nodes = mn.hosts + mn.switches + mn.controllers
103 for server in mn.servers:
104 names = [ n.name for n in nodes if hasattr( n, 'server' )
105 and n.server == server ]
106 output( '%s: %s\n' % ( server, ' '.join( names ) ) )