Goal

This tutorial creates and registers an InterfaceStateListener, and then causes events to be generated by changing the status of the network interfaces.

Tutorial Code

The code used in this tutorial is available in the InterfaceStateChangeTutorial.py file located under <SDK Location>/python/tutorials/interfaces/InterfaceStateChangeTutorial.py.

Requirements/Prerequisites

  • These steps assume the application can connect properly to a network element. Please see the Connecting to a Network Element tutorial for information on how to make the initial connection.
  • For NX-OS platforms only: To configure the network element to run an Events tutorial, you must first enable the EVMED feature, which allows developers to use Embedded Event Manager (EEM) to monitor Application-, CLI-, and Interface Statistics-based events and then schedule actions on the occurrence of those events:
router(config)# feature evmed
router(config)# sh feature |i evmed
evmed           1               enabled

Steps In Detail

Example InterfaceStateListener

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 State Change Listener on Element

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 State Change Listener on Interfaces

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')

Remove State Change Listener on Interface

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)

Result

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.