From f3de8a7b9c2c424626826867366fc974faaff5ca Mon Sep 17 00:00:00 2001 From: AWS Dev Date: Mon, 20 Oct 2014 17:15:01 -0700 Subject: [PATCH] Adding onepk_routes.py --- onepk/onepk_routes.py | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 onepk/onepk_routes.py diff --git a/onepk/onepk_routes.py b/onepk/onepk_routes.py new file mode 100644 index 0000000..170901f --- /dev/null +++ b/onepk/onepk_routes.py @@ -0,0 +1,64 @@ + +from onepk_helper import NetworkDevice + +from onep.routing import Routing,RouteRange,L3UnicastScope,L3UnicastRouteRange,L3UnicastRIBFilter +from onep.routing import L3UnicastRIBFilter +from onep.interfaces import NetworkPrefix + + +def display_routes(net_element): + + ROUTES_TO_RETURN = 10 + + # Create a Routing object + routing = Routing.get_instance(net_element) + + # IPv4 Unicast routes only + scope = L3UnicastScope("", L3UnicastScope.AFIType.IPV4, L3UnicastScope.SAFIType.UNICAST, "") + + # Get all routes (limited by ROUTES_TO_RETURN) + prefix = NetworkPrefix("0.0.0.0", 0) + range = L3UnicastRouteRange(prefix, RouteRange.RangeType.EQUAL_OR_LARGER, ROUTES_TO_RETURN) + + # Create a blank filter object + filter = L3UnicastRIBFilter() + + # Get the routes + route_list = routing.rib.get_route_list(scope, filter, range) + + for route in route_list: + print route.prefix.address + "/" + str(route.prefix.prefix_length) + + +if __name__ == "__main__": + + pynet_rtr1 = dict( + ip = '10.10.10.10', + username = 'pyclass', + password = '********', + pin_file = 'pynet-rtr1-pin.txt', + port = 15002 + ) + + pynet_rtr2 = dict( + ip = '10.10.10.10', + username = 'pyclass', + password = '********', + pin_file = 'pynet-rtr2-pin.txt', + port = 8002 + ) + + rtr1_obj = NetworkDevice(**pynet_rtr1) + rtr2_obj = NetworkDevice(**pynet_rtr2) + + print "\nPrinting routing table from pynet_rtr1" + rtr1_obj.establish_session() + display_routes(rtr1_obj.net_element) + rtr1_obj.disconnect() + + print "\nPrinting routing table from pynet_rtr2" + rtr2_obj.establish_session() + display_routes(rtr2_obj.net_element) + rtr2_obj.disconnect() + + print