second try
[vsorcdistro/.git] / mininet / build / lib.linux-armv6l-2.7 / mininet / examples / clustercli.py
1 #!/usr/bin/python
2
3 "CLI for Mininet Cluster Edition prototype demo"
4
5 from mininet.cli import CLI
6 from mininet.log import output, error
7
8 # pylint: disable=global-statement
9 nx, graphviz_layout, plt = None, None, None  # Will be imported on demand
10
11
12 class ClusterCLI( CLI ):
13     "CLI with additional commands for Cluster Edition demo"
14
15     @staticmethod
16     def colorsFor( seq ):
17         "Return a list of background colors for a sequence"
18         colors = [ 'red', 'lightgreen', 'cyan', 'yellow', 'orange',
19                    'magenta', 'pink', 'grey', 'brown',
20                    'white' ]
21         slen, clen = len( seq ), len( colors )
22         reps = max( 1, slen / clen )
23         colors = colors * reps
24         colors = colors[ 0 : slen ]
25         return colors
26
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         if not nx:
32             try:
33                 # pylint: disable=import-error,no-member
34                 import networkx
35                 nx = networkx  # satisfy pylint
36                 from matplotlib import pyplot
37                 plt = pyplot   # satisfy pylint
38                 import pygraphviz
39                 assert pygraphviz  # silence pyflakes
40                 # Networkx moved this around
41                 if hasattr( nx, 'graphviz_layout' ):
42                     graphviz_layout = nx.graphviz_layout
43                 else:
44                     graphviz_layout = nx.drawing.nx_agraph.graphviz_layout
45                 # pylint: enable=import-error,no-member
46             except ImportError:
47                 error( 'plot requires networkx, matplotlib and pygraphviz - '
48                        'please install them and try again\n' )
49                 return
50         # Make a networkx Graph
51         g = nx.Graph()
52         mn = self.mn
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 ) ) )
63         # Plot it!
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' ) ]
68                     for h in hosts ]
69         scolors = [ color[ getattr( s, 'server', 'localhost' ) ]
70                     for s in switches ]
71         nx.draw_networkx( g, pos=pos, nodelist=hosts, node_size=800,
72                           label='host', node_color=hcolors, node_shape='s',
73                           **opts )
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
77         fig = plt.gcf()
78         ax = plt.gca()
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' )
83         plt.show()
84
85     def do_status( self, _line ):
86         "Report on node shell status"
87         nodes = self.mn.hosts + self.mn.switches
88         for node in nodes:
89             node.shell.poll()
90         exited = [ node for node in nodes
91                    if node.shell.returncode is not None ]
92         if exited:
93             for node in exited:
94                 output( '%s has exited with code %d\n'
95                         % ( node, node.shell.returncode ) )
96         else:
97             output( 'All nodes are still running.\n' )
98
99     def do_placement( self, _line ):
100         "Describe node placement"
101         mn = self.mn
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 ) ) )