Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / mininet / examples / clustercli.py
1 #!/usr/bin/python3
2
3 "CLI pepe 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         output('entre a la funcion\n')
32         if not nx:
33             try:
34                 # pylint: disable=import-error,no-member
35                 output('entre al try\n')
36                 import networkx
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
42                 import pygraphviz
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
48                 else:
49                     graphviz_layout = nx.drawing.nx_agraph.graphviz_layout
50                 # pylint: enable=import-error,no-member
51                 output('salio del if\n')
52             except ImportError:
53                 error( 'plot requires networkx, matplotlib and pygraphviz - '
54                        'please install them and try again\n' )
55                 return
56         # Make a networkx Graph
57         g = nx.Graph()
58         mn = self.mn
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 ) ) )
69         # Plot it!
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' ) ]
74                     for h in hosts ]
75         scolors = [ color[ getattr( s, 'server', 'localhost' ) ]
76                     for s in switches ]
77         nx.draw_networkx( g, pos=pos, nodelist=hosts, node_size=800,
78                           label='host', node_color=hcolors, node_shape='s',
79                           **opts )
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
83         fig = plt.gcf()
84         ax = plt.gca()
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' )
89         plt.show()
90
91     def do_status( self, _line ):
92         "Report on node shell status"
93         nodes = self.mn.hosts + self.mn.switches
94         for node in nodes:
95             node.shell.poll()
96         exited = [ node for node in nodes
97                    if node.shell.returncode is not None ]
98         if exited:
99             for node in exited:
100                 output( '%s has exited with code %d\n'
101                         % ( node, node.shell.returncode ) )
102         else:
103             output( 'All nodes are still running.\n' )
104
105     def do_placement( self, _line ):
106         "Describe node placement"
107         mn = self.mn
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 ) ) )