Discovery Topology in POX

openflow.discovery

This component sends specially-crafted LLDP messages out of OpenFlow switches so that it can discover the network topology. It raises events (which you can listen to) when links go up or down.
More specifically, you can listen to LinkEvent events on core.openflow_discovery. When a link is detected, such an event is raised with the .added attribute set to True. When a link is detected as having been removed or failed, the .removed attribute is set to True. LinkEvent also has a .link attribute, which is a Link object, and a port_for_dpid(<dpid>) method (pass it the DPID of one end of the link and it will tell you the port used on that datapath).
Link objects have the following attributes:

name value
dpid1 The DPID of one of the switches involved in the link
dpid2 The DPID of the other switch involved in the link
end[0 or 1] The ends of the link as a tuple, i.e., end[0] = (dpid1,port1)
port1 The port on dpid1 involved in the link
port2 The port on dpid2
uni A "unidirectional" version of the link. This normalizes the order of the DPIDs and ports, allowing you to compare two links (which may be different directions of the same physical links).

A number of the other example components use discovery and can serve as demonstrations for using discovery. Obvious possibilities are misc.gephitopo and forwarding.l2multi.

This example shows how to discovery topology created in previous post.
Component Discovery mentioned above is used to find discovery topology.
This component can return the dpid and ports of switches involved in the link.

Create a python file in ~/pox/ext/topoDiscovery.py

vi ~/pox/ext/topoDiscovery.py  
from pox.core import core  
import pox.openflow.libopenflow_01 as of  
from pox.lib.revent import *  
from pox.lib.recoco import Timer  
from collections import defaultdict  
from pox.openflow.discovery import Discovery  
from pox.lib.util import dpid_to_str  
import time

class topoDiscovery(EventMixin):

    def __init__(self):
        def startup():
            core.openflow.addListeners(self, priority = 0)
            core.openflow_discovery.addListeners(self)
        core.call_when_ready(startup, ('openflow','openflow_discovery'))
        print "init over"
    def _handle_LinkEvent(self, event):
        l = event.link
        sw1 = l.dpid1
        sw2 = l.dpid2
        pt1 = l.port1
        pt2 = l.port2

        print 'link added is %s'%event.added
        print 'link removed is %s' %event.removed
        print 'switch1 %d' %l.dpid1
        print 'port1 %d' %l.port1
        print 'switch2 %d' %l.dpid2
        print 'port2 %d' %l.port2

def launch():  
    core.registerNew(topoDiscovery)

First open a terminal and run topology file in mininet as previous post.

mininet@mininet-vm:~$ sudo ./type1.py  

and open a new terminal and run pox with above code and openflow.discovery

mininet@mininet-vm:~$ sudo ./pox/pox.py topoDiscovery openflow.discovery  
mininet@mininet-vm:~$ sudo ./pox/pox.py topoDiscovery openflow.discovery  
POX 0.2.0 (carp) / Copyright 2011-2013 James McCauley, et al.  
init over  
INFO:core:POX 0.2.0 (carp) is up.  
INFO:openflow.of_01:[00-00-00-00-00-02 4] connected  
INFO:openflow.of_01:[00-00-00-00-00-04 2] connected  
INFO:openflow.of_01:[00-00-00-00-00-06 5] connected  
INFO:openflow.of_01:[00-00-00-00-00-03 3] connected  
INFO:openflow.of_01:[00-00-00-00-00-01 6] connected  
INFO:openflow.of_01:[00-00-00-00-00-05 1] connected  
INFO:openflow.discovery:link detected: 00-00-00-00-00-02.1 -> 00-00-00-00-00-01.2  
link added is True  
link removed is False  
switch1 2  
port1 1  
switch2 1  
port2 2  
INFO:openflow.discovery:link detected: 00-00-00-00-00-02.2 -> 00-00-00-00-00-03.2  
link added is True  
link removed is False  
switch1 2  
port1 2  
switch2 3  
port2 2  
INFO:openflow.discovery:link detected: 00-00-00-00-00-04.1 -> 00-00-00-00-00-01.3  
link added is True  
link removed is False  
switch1 4  
port1 1  
switch2 1  
port2 3  
INFO:openflow.discovery:link detected: 00-00-00-00-00-04.2 -> 00-00-00-00-00-05.1  
link added is True  
link removed is False  
switch1 4  
port1 2  
switch2 5  
port2 1  
INFO:openflow.discovery:link detected: 00-00-00-00-00-06.1 -> 00-00-00-00-00-03.3  
link added is True  
link removed is False  
switch1 6  
port1 1  
switch2 3  
port2 3  
INFO:openflow.discovery:link detected: 00-00-00-00-00-03.3 -> 00-00-00-00-00-06.1  
link added is True  
link removed is False  
switch1 3  
port1 3  
switch2 6  
port2 1  
INFO:openflow.discovery:link detected: 00-00-00-00-00-03.1 -> 00-00-00-00-00-05.2  
link added is True  
link removed is False  
switch1 3  
port1 1  
switch2 5  
port2 2  
INFO:openflow.discovery:link detected: 00-00-00-00-00-03.2 -> 00-00-00-00-00-02.2  
link added is True  
link removed is False  
switch1 3  
port1 2  
switch2 2  
port2 2  
INFO:openflow.discovery:link detected: 00-00-00-00-00-01.3 -> 00-00-00-00-00-04.1  
link added is True  
link removed is False  
switch1 1  
port1 3  
switch2 4  
port2 1  
INFO:openflow.discovery:link detected: 00-00-00-00-00-01.2 -> 00-00-00-00-00-02.1  
link added is True  
link removed is False  
switch1 1  
port1 2  
switch2 2  
port2 1  
INFO:openflow.discovery:link detected: 00-00-00-00-00-05.1 -> 00-00-00-00-00-04.2  
link added is True  
link removed is False  
switch1 5  
port1 1  
switch2 4  
port2 2  
INFO:openflow.discovery:link detected: 00-00-00-00-00-05.2 -> 00-00-00-00-00-03.1  
link added is True  
link removed is False  
switch1 5  
port1 2  
switch2 3  
port2 1  

And in Mininet, let link between s1 and s4 down

mininet> link s1 s4 down  

wait a few seconds, pox outputs following information:

INFO:openflow.discovery:link timeout: 00-00-00-00-00-04.1 -> 00-00-00-00-00-01.3  
link added is False  
link removed is True  
switch1 4  
port1 1  
switch2 1  
port2 3  
INFO:openflow.discovery:link timeout: 00-00-00-00-00-01.3 -> 00-00-00-00-00-04.1  
link added is False  
link removed is True  
switch1 1  
port1 3  
switch2 4  
port2 1  

Next, let link between s1 and s4 up

mininet> link s1 s4 up  

wait a few seconds, pox outputs following information:

INFO:openflow.discovery:link detected: 00-00-00-00-00-04.1 -> 00-00-00-00-00-01.3  
link added is True  
link removed is False  
switch1 4  
port1 1  
switch2 1  
port2 3  
INFO:openflow.discovery:link detected: 00-00-00-00-00-01.3 -> 00-00-00-00-00-04.1  
link added is True  
link removed is False  
switch1 1  
port1 3  
switch2 4  
port2 1  

References:
POX Wiki
Create a custom topology in Mininet (2)