Goal

This tutorial shows how to create Routing Service Set object, how to read ART (Application Routing Table) routes, update application routes, add listeners for route up and down events in the ART, and add listeners for application routes.

Tutorial Code

The code used in this tutorial is available in the ARTTutorial.py file located under <SDK Location>/python/tutorials/src/routing/ARTTutorial.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 Application Route Table (ART) Listener

Create an ART Listener that listens to ART events.

class ExampleARTRouteListener (ARTRouteStateListener):
    def handle_event(self,event,clientData):
        logger.info("\n\n\nEntering handle event for my ART Listener")
        scope_A = event.scope
        route_A = event.route
        try:
            logger.info(scope_A)
            state_A =  event.state
            logger.info("State is :")
            logger.info(AppRouteTable.RouteState.enumval(state_A))

            logger.info(route_A)
            nexthoplist = route_A.next_hop_list
            for next_hop in nexthoplist:
                try:
                    ni = next_hop.network_interface
                    na = next_hop.address
                except OnepRemoteProcedureException,e:
                    logger.info("N/A: " + e)
                except OnepConnectionException, e:
                    logger.info(str(e))
                except OnepIllegalArgumentException, e:
                    logger.info(e)
                else:
                    logger.info("Next hop ip address is " + na)
                    if ni is not None:
                        logger.info(ni.name)
        except (OnepIllegalArgumentException, OnepRemoteProcedureException, OnepConnectionException) ,e:    
            logger.info(e)
            logger.info("Exiting gracefully")

Get Application Route Table

Next, get the Application Route Table from the Network Element.

#  Create a Routing object for the network element.
routing = Routing.get_instance(tutorial.get_network_element())

#  Get the instance of application route table.
approutetable = routing.get_app_route_table()
return approutetable

Add Application Routes

Next, add some Application Routes.

        eth_interface = tutorial.get_an_interface()
        if eth_interface == None:
            logger.error("Could not find a suitable interface to add routes to.")
            return
        logger.info(eth_interface)
        
        route_scope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4 , L3UnicastScope.SAFIType.UNICAST, "")
        
        #aL3UnicastNextHopList = HashSet()
        aL3UnicastNextHopList = list()
        aL3UnicastNextHop = L3UnicastNextHop(eth_interface, "10.1.1.24")
        aL3UnicastNextHopList.append(aL3UnicastNextHop)
        aL3UnicastNextHopList.append(L3UnicastNextHop(eth_interface, "10.1.1.25"))
        
        destNetworkPrefix = NetworkPrefix("160.10.0.0", 16)

        aRoute = L3UnicastRoute(destNetworkPrefix, aL3UnicastNextHopList)
        aRoute.admin_distance = 1
        #  Now update the app route table with this route.
        routeOperation = L3UnicastRouteOperation(0, aRoute)
        
        routeOperationList = list()
        routeOperationList.append(routeOperation)
        mylist = approutetable.update_routes(route_scope, routeOperationList)
        logger.info(mylist)

Add Application Route Listener

Next, apply the ART listener that you created earlier.

aL3UnicastScope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4 , 
                                    L3UnicastScope.SAFIType.UNICAST, 
                                    "")
#  Add a listener to receive route state change events. When events arrive, listener.handleEvent() will be invoked.
artRouteListener = ExampleARTRouteListener()
aARTEventHandler = approutetable.add_route_state_listener(
            artRouteListener, 
            aL3UnicastScope, 
            0,
            None)
logger.info("aARTEventHandler : ")
logger.info(str(aARTEventHandler))

Remove ART Listener

When you have finished with the ART Listener, you can remove the listener from the network element.

#  Remove Application Route listener.
approutetable.remove_route_state_listener(eventHandler)

Result

Congratulations! You are now able to use your onePK application to create ART routing objects, add application routes, receive notification for route up/down events in the ART, 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/ArtTutorial.