Contents
This tutorial creates and registers an InterfaceStateListener, and then causes events to be generated by changing the status of the network interfaces.
The code used in this tutorial is available in the InterfaceStateChangeTutorial.py file located under <SDK Location>/python/tutorials/interfaces/InterfaceStateChangeTutorial.py.
router(config)# feature evmed router(config)# sh feature |i evmed evmed 1 enabled
Create a listener to listen for any state changes. This listener implements a event handler function, which simply prints out the event details.
class MyInterfaceStateListener(InterfaceStateListener): name = str() def __init__(self, name): super(MyInterfaceStateListener, 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 self.name + " has received state change event - from Event Handler " print "Interface has state " + InterfaceState.enumval(event.interface_state) + "\n"
Register the listener that you have just created with the network element. Note that when you register a listener with the network element, you receive an event handler identifier that you can use later in the program.
elementStateListener = MyInterfaceStateListener("Network Element State Change") network_element = tutorial.get_network_element() ifFilter = onep.interfaces.InterfaceFilter() print "Adding Interface State Change event listener on network element " + tutorial.get_element_hostname() eventHandle1 = network_element.add_interface_state_listener(elementStateListener,ifFilter,InterfaceStateEventType.ONEP_IF_STATE_EVENT_LINK,'Client Data')
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.
interfaceStateListener = MyInterfaceStateListener("Interface State Change") networkInterface = tutorial.get_an_interface() if networkInterface == None: logger.error("No interfaces are available.") sys.exit(1) print "Adding State Change event listener on interface " + networkInterface.name eventHandle2 = networkInterface.add_state_listener(interfaceStateListener,InterfaceStateEventType.ONEP_IF_STATE_EVENT_LINK,'Client Data')
Finally, remove the listener that you registered for interface state changes using the event handle identifier that you received previously.
networkInterface.remove_state_listener(eventHandle2) network_element.remove_interface_state_listener(eventHandle1)
Congratulations! You now know how to create a listener, register the state change listener to both the network element and its interfaces, and finally, how to remove the listener.