-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample.py
More file actions
59 lines (50 loc) · 1.95 KB
/
example.py
File metadata and controls
59 lines (50 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from pox.core import core
import pox.openflow.discovery
import pox.openflow.spanning_tree
import pox.forwarding.l2_learning
from pox.lib.util import dpid_to_str
from extensions.switch import SwitchController
log = core.getLogger()
class Controller:
def __init__ (self):
self.connections = set()
self.switches = []
# Esperando que los modulos openflow y openflow_discovery esten listos
core.call_when_ready(self.startup, ('openflow', 'openflow_discovery'))
def startup(self):
"""
Esta funcion se encarga de inicializar el controller
Agrega el controller como event handler para los eventos de
openflow y openflow_discovery
"""
core.openflow.addListeners(self)
core.openflow_discovery.addListeners(self)
log.info('Controller initialized')
def _handle_ConnectionUp(self, event):
"""
Esta funcion es llamada cada vez que un nuevo switch establece conexion
Se encarga de crear un nuevo switch controller para manejar los eventos de cada switch
"""
log.info("Switch %s has come up.", dpid_to_str(event.dpid))
if (event.connection not in self.connections):
self.connections.add(event.connection)
sw = SwitchController(event.dpid, event.connection)
self.switches.append(sw)
def _handle_LinkEvent(self, event):
"""
Esta funcion es llamada cada vez que openflow_discovery descubre un nuevo enlace
"""
link = event.link
log.info("Link has been discovered from %s,%s to %s,%s", dpid_to_str(link.dpid1), link.port1, dpid_to_str(link.dpid2), link.port2)
def launch():
# Inicializando el modulo openflow_discovery
pox.openflow.discovery.launch()
# Registrando el Controller en pox.core para que sea ejecutado
core.registerNew(Controller)
"""
Corriendo Spanning Tree Protocol y el modulo l2_learning.
No queremos correrlos para la resolucion del TP.
Aqui lo hacemos a modo de ejemplo
"""
pox.openflow.spanning_tree.launch()
pox.forwarding.l2_learning.launch()