Contents
This tutorial shows how a onePK application can register to receive CDP (Cisco Discovery Protocol) events from the network element it is connected to. As a CDP-configured network element sends periodic messages to a multicast address, these messages are sent to the registered onePK application through a callback mechanism.
For this tutorial, you use the CDPEventTutorial as a general guide for onePK CDP events.
The code used in this tutorial is available in the CDPEventTutorial.py file located under <SDK Location>/python/tutorials/events
router(config)# feature evmed router(config)# sh feature |i evmed evmed 1 enabled
If onePK is enabled on the network element, and a connection by the application is possible, no further configuration is needed to register for events. The events infrastructure for onePK is provided on the network element by the Embedded Event Manager (EEM) infrastructure. Various EEM commands can be run to show information such as registered events and an event history.
NE100# show event manager policy registered NE100# show event manager history events
For every event, there is always a listener. The main purpose of a listener is to handle any incoming events from the Network Element. All listeners in the onePK API are Python Interfaces in which the application developer is responsible for writing concrete methods to handle the incoming onePK events. The following example shows a basic implementation of the CDPListener. In this example, you handle the event by printing out the CDP details that have been received by the event.
class ExampleNeCDPListener(CDPListener): name = str() def __init__(self, name): super(ExampleNeCDPListener, 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 = " + str(event.intf) print "neighbor = " + str(event.device_id) print "platform = " + event.platform print "version = " + event.version print "capabilitites = " + event.capabilities print "---------------------------\n"
Next, you register the listener that you have created to the onePK-enabled network element. In order to register the listener, you need a CDPFilter and an InterfaceFilter object to get notification for a specific CDP event type on a specific interface type.
In this example CDPFilter is set to the ONEP_CDP_ALL notification type. Other notification types are as follows:
In this example the InterfaceFilter is set on ONEP_IF_TYPE_ETHERNET interface type. Other interface types are as follows:
Additionally the clientData (client context) can be passed in as the last parameter to the registration. This data is passed to the callback handler when the registered event triggers. Finally you receive back a handler in the form of a Python integer.
cdpListener = ExampleNeCDPListener("CDP Tutorial") cdpFilter = CDPFilter() cdpFilter.notifyType = CDPEvent.CDPEventNotifyType.ONEP_CDP_ALL ifFilter = InterfaceFilter() clientData = None network_element = tutorial.get_network_element() print "Adding CDP listener to network element" event_handle = network_element.add_cdp_listener(cdpListener, ifFilter, cdpFilter, clientData)
You can then remove the listener by passing back the integer that you just received to the corresponding remove listener method. NOTE The listener is removed automatically once the onePK application terminates.
cdpListener = ExampleNeCDPListener("CDP Tutorial") cdpFilter = CDPFilter() cdpFilter.notifyType = CDPEvent.CDPEventNotifyType.ONEP_CDP_ALL ifFilter = InterfaceFilter() clientData = None network_element = tutorial.get_network_element() print "Adding CDP listener to network element" event_handle = network_element.add_cdp_listener(cdpListener, ifFilter, cdpFilter, clientData)
By using the same network_element object, you can register multiple listeners with the same network_element. For a complete list of listener types that can be added to the network_element object, see the onePK network_element ePydoc documentation.
Congratulations! By following this tutorial, you can now model an events listener on a network element.