This tutorial shows you how to register for commonly occurring events that are used throughout the onePK API.
For this tutorial, you use the SyslogEventTutorial as a general guide for onePK events.
The code used in this tutorial is available in the SyslogEventTutorial.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 eventing 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 very simple implementation of the SyslogListener. In this example, you handle the event by printing out the message that was received from the event.
class ExampleSyslogListener(SyslogListener): name = str() """ Creates an Example Syslog Event Listener. @param name Identifies the instance. """ def __init__(self, name): super(ExampleSyslogListener, 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 self.name + " has received SysLog Event\n" print "Message = " + event.message print "Message Count = " + str(event.msg_count) print "Priority = " + onep.element.NetworkElement.OnepSyslogSeverity.enumval(event.priority) print "---------------------------\n"
Next, you register the listener that you have created to the onePK-enabled network element. The code snippet below shows that you used your listener, which you created in the previous step, passed it into the NetworkElement object, then entered the correct parameters. Finally you receive back a handler in the form of an python integer.
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.
By using our same NetworkElement object, we can register multiple listeners to the same NetworkElement. A full list of which listener types can be added to the NetworkElement object, please view the onePK NetworkElement Pydoc documentation.
syslogListener = ExampleSyslogListener("Syslog Tutorial") syslogFilter = SyslogFilter('Interface') syslogFilter.periodMsec = 1000 syslogFilter.priority = onep.element.NetworkElement.OnepSyslogSeverity.ONEP_SYSLOG_NOTICE clientData = None network_element = tutorial.get_network_element() # # Add a syslog event listener # print 'Adding a Syslog event listener' eventHandle = network_element.add_syslog_listener(syslogListener, syslogFilter, clientData)
Congratulations! By following this tutorial, you can now model other types of events and event listeners, similar to those in this tutorial.