3 "CLI pepe 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
31 output('entre a la funcion\n')
34 # pylint: disable=import-error,no-member
35 output('entre al try\n')
37 output('se importo networkx\n')
38 nx = networkx # satisfy pylint
39 from matplotlib import pyplot
40 output('se importo matplot y pyplot\n')
41 plt = pyplot # satisfy pylint
43 output('se importo pygraphviz\n')
44 assert pygraphviz # silence pyflakes
45 # Networkx moved this around
46 if hasattr( nx, 'graphviz_layout' ):
47 graphviz_layout = nx.graphviz_layout
49 graphviz_layout = nx.drawing.nx_agraph.graphviz_layout
50 # pylint: enable=import-error,no-member
51 output('salio del if\n')
53 error( 'plot requires networkx, matplotlib and pygraphviz - '
54 'please install them and try again\n' )
56 # Make a networkx Graph
59 servers = getattr( mn, 'servers', [ 'localhost' ] )
60 hosts, switches = mn.hosts, mn.switches
61 nodes = hosts + switches
62 g.add_nodes_from( nodes )
63 links = [ ( link.intf1.node, link.intf2.node )
64 for link in self.mn.links ]
65 g.add_edges_from( links )
66 # Pick some shapes and colors
67 # shapes = hlen * [ 's' ] + slen * [ 'o' ]
68 color = dict( zip( servers, self.colorsFor( servers ) ) )
70 pos = graphviz_layout( g )
71 opts = { 'ax': None, 'font_weight': 'bold',
72 'width': 2, 'edge_color': 'darkblue' }
73 hcolors = [ color[ getattr( h, 'server', 'localhost' ) ]
75 scolors = [ color[ getattr( s, 'server', 'localhost' ) ]
77 nx.draw_networkx( g, pos=pos, nodelist=hosts, node_size=800,
78 label='host', node_color=hcolors, node_shape='s',
80 nx.draw_networkx( g, pos=pos, nodelist=switches, node_size=1000,
81 node_color=scolors, node_shape='o', **opts )
82 # Get rid of axes, add title, and show
85 ax.get_xaxis().set_visible( False )
86 ax.get_yaxis().set_visible( False )
87 fig.canvas.set_window_title( 'Mininet')
88 plt.title( 'Node Placement', fontweight='bold' )
91 def do_status( self, _line ):
92 "Report on node shell status"
93 nodes = self.mn.hosts + self.mn.switches
96 exited = [ node for node in nodes
97 if node.shell.returncode is not None ]
100 output( '%s has exited with code %d\n'
101 % ( node, node.shell.returncode ) )
103 output( 'All nodes are still running.\n' )
105 def do_placement( self, _line ):
106 "Describe node placement"
108 nodes = mn.hosts + mn.switches + mn.controllers
109 for server in mn.servers:
110 names = [ n.name for n in nodes if hasattr( n, 'server' )
111 and n.server == server ]
112 output( '%s : %s\n' % ( server, ' '.join( names ) ) )