How to identify a specific switch in Mininet when connected to a pox controller
Question: I have a custom topology running on Mininet and it has 2 switches s1, and s2.
I am using pox as the controller. I have written a python code to identify the
switches, is this the correct way to do it? Are there any other better methods
that i can use? could any body suggest other alternatives?

Code:

    
    
    from pox.core import core
    import pox.openflow.libopenflow_01 as of
    from pox.lib.util import dpidToStr
    log = core.getLogger()
    s1_dpid=0
    s2_dpid=0
    def _handle_ConnectionUp (event):
    global s1_dpid, s2_dpid
    print "ConnectionUp: ", 
    dpidToStr(event.connection.dpid)
    #remember the connection dpid for switch 
    for m in event.connection.features.ports:
    if m.name == "s1-eth1":
    s1_dpid = event.connection.dpid
    print "s1_dpid=", s1_dpid
    elif m.name == "s2-eth1":
    s2_dpid = event.connection.dpid
    print "s2_dpid=", s2_dpid
    


Answer: This link <http://squarey.me/cloud-virtualization/pox-controller-learning-
four.html> provides examples of POX component that listens to ConnectionUp
events from all switches and get the dpid

**1\. Use the component script "connectionDown.py" inside POX directory:**

    
    
    #!/usr/bin/python
    from pox.core import core
    from pox.lib.util import dpid_to_str
    from pox.lib.revent import *
    
    log = core.getLogger()
    
    class ConnectionUp(Event):
        def __init__(self,connection,ofp):
            Event.__init__(self)
            self.connection = connection
            self.dpid = connection.dpid
            self.ofp = ofp
    class ConnectionDown(Event):
        def __init__(self,connection,ofp):
            Event.__init__(self)
            self.connection = connection
            self.dpid = connection.dpid
    
    class MyComponent(object):
        def __init__(self):
            core.openflow.addListeners(self)
    
        def _handle_ConnectionUp(self,event):
            ConnectionUp(event.connection,event.ofp)
            log.info("Switch %s has come up.",dpid_to_str(event.dpid))
    
        def _handle_ConnectionDown(self,event):
            ConnectionDown(event.connection,event.dpid)
            log.info("Switch %s has shutdown.",dpid_to_str(event.dpid))
    
    def launch():
        core.registerNew(MyComponent)
    

**2- (POX controller xterm) Start the POX controller with custom component**

    
    
    mininet@mininet-vm:~/pox$ ./pox.py connectionDown
    POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
    INFO:core:POX 0.1.0 (betta) is up.
    

**3- (mininet xterm) start mininet topology with multiple switches**

    
    
    mininet@mininet-vm:~$ sudo mn --topo linear --mac --controller remote --switch ovsk
    *** Creating network
    *** Adding controller
    *** Adding hosts:
    h1 h2
    *** Adding switches:
    s1 s2
    *** Adding links:
    (h1, s1) (h2, s2) (s1, s2)
    *** Configuring hosts
    h1 h2
    *** Starting controller
    *** Starting 2 switches
    s1 s2
    *** Starting CLI:
    mininet>
    

**4- Back to POX controller xterm, here is what observe in POX xterm:**

    
    
    ./pox.py connectionDown
    POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
    INFO:core:POX 0.1.0 (betta) is up.
    INFO:openflow.of_01:[00-00-00-00-00-02 2] connected
    INFO:connectionDown:Switch 00-00-00-00-00-02 has come up.
    INFO:openflow.of_01:[00-00-00-00-00-01 1] connected
    INFO:connectionDown:Switch 00-00-00-00-00-01 has come up.
    

**5- S#POX should react to any changes to rhe switches:**

    
    
    mininet> py s1.stop()
    mininet> py s1.start([c0])
    mininet>
    

**4- Back to POX controller xterm:**

    
    
    mininet@mininet-vm:~/pox$ ./pox.py connectionDown
    POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
    INFO:core:POX 0.1.0 (betta) is up.
    INFO:openflow.of_01:[00-00-00-00-00-02 2] connected
    INFO:connectionDown:Switch 00-00-00-00-00-02 has come up.
    INFO:openflow.of_01:[00-00-00-00-00-01 1] connected
    INFO:connectionDown:Switch 00-00-00-00-00-01 has come up.
    

>
>     INFO:openflow.of_01:[00-00-00-00-00-01 1] closed
>     INFO:connectionDown:Switch 00-00-00-00-00-01 has shutdown.
>     INFO:openflow.of_01:[00-00-00-00-00-01 3] connected
>     INFO:connectionDown:Switch 00-00-00-00-00-01 has come up.
>  

