Goal

This tutorial shows you how to read RIB (Routing Information Base) routes, update application routes, add listeners for route up and down events in the RIB, and add listeners for application routes.

Tutorial Code

The code used in this tutorial is available in the RIBTutorial.py file located under <SDK Location>/python/tutorials/src/routing/RIBTutorial.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.

Steps In Detail

Example RIB Route Listener

First, create a RIB Route Listener that listens to RIB Route events.

    class ExampleRIBRouteListener(RIBRouteStateListener):
        """
        RIBRouteStateListener implemented as inner class.
        """
        
        def handleEvent(self, event, clientData):
            
            logger.info("RIBRouteStateEvent received...")
            logger.info("Scope: " + event.scope)
            logger.info("Route: " + event.route)
            
            state = event.state
            logger.info("State : ")
            logger.info(RIB.RouteState.enumval(state))
            
            art_event_route = event.route
            nexthoplist = art_event_route.next_hop_list
            for next_hop in nexthoplist:
                try :
                    ni = next_hop.network_interface          
                    na = next_hop.address
                except OnepRemoteProcedureException, e:
                    print "N/A: " + e 
                except OnepConnectionException, e:
                    print str(e)    
                else:
                    print "NextHop ip address is " + na 
                    if ni is not None:
                        print ni.name

Get RIB Table

Next, get the RIB Table from the network element.

#  Create a Routing object for the network element.
routing = Routing.getInstance(tutorial.get_network_element())
#  Get the instance of the RIB table.
rib = routing.getRib()
return rib

Add RIB Route Listener

Next, apply the RIB listener that you created earlier.

        aL3UnicastScope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4 , 
                                            L3UnicastScope.SAFIType.UNICAST, 
                                            "")
        rib_filter = L3UnicastRIBFilter()
        #  Add a listener to receive route state change events.
        #  When events arrive, listener.handleEvent() will be invoked.
        exampleRIBRouteListener = self.ExampleRIBRouteListener()
        logger.info("adding RIB listener...")
#         exampleRIBRouteListenerEventHandle = None
        exampleRIBRouteListenerEventHandle = rib.add_route_state_listener(
                                                exampleRIBRouteListener, 
                                                aL3UnicastScope, 
                                                rib_filter, 
                                                0, 
                                                None);
        logger.info(str(exampleRIBRouteListenerEventHandle))
        return exampleRIBRouteListenerEventHandle

Remove RIB Route Listener

Finally, at the end of your routing tutorial, remove the RIB Route Listener.

#  Remove RIB listener.
rib.remove_route_state_listener(eventHandler)

Result

Congratulations! You are now able to use your onePK application to create routing objects, add application routes, receive notification for route up/down events in the RIB, and also receive application route promote/demote events.

To try out this tutorial code by compiling and running it, you can find the code located in the directory <SDK Location>/tutorials/python/routing/RibTutorial.