Contents
This tutorial shows how a onePK application can register to receive CLI (Command Line Interface) command events that run on the network element it is connected to. When CLI commands are entered on the network element, they are sent to the registered onePK application through a callback mechanism. The callback handler in this tutorial demonstrates how to handle a CLI event and alter its default behavior.
For this tutorial, you use the CliEventTutorial as a general guide for onePK CLI events.
The code used in this tutorial is available in the CliEventTutorial.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 CLI event listener must implement the inherited abstract methods get_sync_reply(), handle_sync_event() and handle_event(). The following example shows a basic implementation of the CLIListener. In this example, you handle the event by altering the behavior of the show location CLI command (as specified during the registration of the listener) by altering its output string.
class ExampleCliListener(CLIListener): name = '' """ Creates an Example CLI Event Listener. @param name Identifies the instance. """ def __init__(self, name): super(ExampleCliListener, 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 "CLI Event: handle_event\n" print " eventHandle" + str(event.event_handle) print " msgCount = " + str(event.msg_count) print " sync = " + str(event.sync) print " sync = " + str(event.tty) print " message = " + event.message print "---------------------------\n" """ Invoked when a sync 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_sync_event(self,event,clientData): print "---------------------------" print "CLI Event: handle_sync_event\n" print " eventHandle" + str(event.event_handle) print " msgCount = " + str(event.msg_count) print " sync = " + str(event.sync) print " sync = " + str(event.tty) print " message = " + event.message print "---------------------------\n" def get_sync_reply(self): print "---------------------------" print "CLI Event: get_sync_reply\n" print "---------------------------\n"
Next, you register the listener that you have created with the onePK-enabled network element. In order to register the listener, you need a CliFilter object with a specified regular expression pattern. CLI commands matching this pattern will trigger the event and the listener's get_sync_reply() function will be run synchronously with the default command. 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.
cliListener = ExampleCliListener("CLI event listener") cliFilter = CLIFilter('clock') clientData = None network_element = tutorial.get_network_element() # # Add a CLI event listener # print "Adding a CLI event listener'" eventHandle = network_element.add_cli_listener(cliListener, cliFilter, clientData) print "To verify please enter 'show clock' command on the connected device"
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.
network_element.remove_cli_listener(eventHandle)
By using the same network_element object, you can register multiple listeners to 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.