This tutorial creates and registers an InterfaceCDPListener. As a CDP-configured network interface sends periodic messages to a multicast address, these messages are sent to the registered onePK application through a callback mechanism.
The code used in this tutorial is available in the InterfaceCDPChangeTutorial.py file located under <SDK Location>/python/tutorials/interfaces/InterfaceCDPChangeTutorial.py.
router(config)# feature evmed router(config)# sh feature |i evmed evmed 1 enabled
Create a listener that logs the String representation of the CDPEvent received. This listener implements a event handler function which simply prints out name of the interface which sent out the message. To tigger the event execute 'cdp enable' on one of the connected interfaces.
class ExampleInterfaceCDPListener(CDPListener): name = str() def __init__(self, name): super(ExampleInterfaceCDPListener, self).__init__() self.name = name """ Invoked when an event is received from a network element. @param event An event object that indicates that an event has occurred in a network element. @param clientData The clientData is an object that is passed in when the application calls an API to add/register the event listener. The application is responsible for casting the input clientData to the appropriate class before using it. """ def handle_event(self, event, clientData): print "---------------------------" print CDPEvent.CDPEventNotifyType.enumval(event.notify_type) print "---------------------------" print "NetworkInterface = " + event.intf print "neighbor = " + str(event.device_id) print "platform = " + event.platform print "version = " + event.version print "capabilitites = " + event.capabilities print "---------------------------\n"
Register the listener that you have created with all of the network interfaces on the network element. Note that when you attach a listener to any Interface, you receive an event handler identifier that you can use later in the program.
cdpFilter = CDPFilter() cdpFilter.notifyType = CDPEvent.CDPEventNotifyType.ONEP_CDP_ALL clientData = "CDP Event" event_handle = None interface = tutorial.get_an_interface() if interface == None: logger.error("No interfaces are available.") sys.exit(1) print "Adding CDP event listener on interface " + interface.name cdpListener = ExampleInterfaceCDPListener("Interface CDP listener") event_handle = interface.add_cdp_listener(cdpListener, cdpFilter, clientData)
Finally, remove the listener that you registered for Interface CDP using the event handle identifier that you received previously.
interface.remove_cdp_listener(event_handle)
Congratulations! You now know how to create a listener, register the CDP listener on interfaces, and finally, how to remove the listener.