Route Application Examples - amybuck/SONiC-NAS GitHub Wiki

Create Route using Python

NOTE: Refer to the dell-base-route.yang model which defines the route object and attributes before you configure route settings.

1. Import the CPS and CPS object Python library.

import cps_utils
import socket
import netaddr as net

2. Define the protocol version, route prefix, and prefix length of the route attributes.

version = 'ipv4' 
route_ip = '70.5.5.0' 
obj = cps_utils.CPSObject('base-route/obj/entry') 
obj.add_attr("vrf-id", 0) 
if version == 'ipv4':
    obj.add_attr("af", socket.AF_INET) 
elif version == 'ipv6':
    obj.add_attr("af", socket.AF_INET6) ip = net.IPNetwork(route_ip)
obj.add_attr_type("route-prefix", version) 
obj.add_attr("route-prefix", str(ip.network)) 
obj.add_attr("prefix-len", int(ip.prefixlen))

3. Define the next-hop attributes and create the CPS object. Add multiple next-hop attributes to create ECMP routes.

nh_addr = '1.1.1.2'
lst = ["nh-list", "0", "nh-addr"] 
obj.add_embed_attr(lst, nh_addr) 
obj.add_attr("nh-count", 1)

4. Create the CPS object.

cps_update = ('create', obj.get()) 
transaction = cps_utils.CPSTransaction([cps_update])

5. Commit the transaction.

ret = transaction.commit()

6. Verify the return value.

if not ret:
    raise RuntimeError ("Error creating Route")

See route-create.py to view the Python application example.

Verify Route Creation using Linux

$ ip route
1.1.1.0/24 dev e101-001-0 proto kernel scope link src 1.1.1.1
70.5.5.0 via 1.1.1.2 dev e101-001-0 proto none

Delete Route using Python

1. Import the CPS and CPS object Python library.

import cps_utils
import socket
import netaddr as net

2. Define the protocol version, route prefix, and prefix length of the route attributes.

version = 'ipv4'
route_ip = '70.5.5.0'
obj = cps_utils.CPSObject('base-route/obj/entry')
obj.add_attr("vrf-id", 0)
if version == 'ipv4':
obj.add_attr("af", socket.AF_INET)
elif version == 'ipv6': 
obj.add_attr("af", socket.AF_INET6)
ip = net.IPNetwork(route_ip)

obj.add_attr_type("route-prefix", version)
obj.add_attr("route-prefix", str(ip.network))
obj.add_attr("prefix-len", int(ip.prefixlen))

3.. Define the CPS object and create the transaction.

cps_update = ('delete', obj.get())
transaction = cps_utils.CPSTransaction([cps_update])

4. Commit the transaction.

ret = transaction.commit()

5. Verify the return value.

if not ret:
    raise RuntimeError ("Error creating Route")

See route-delete.py to view the Python application example.

Verify Route Deletion using Linux

$ ip route
1.1.1.0/24 dev e101-001-0 proto kernel scope link src 1.1.1.1
70.5.5.0 via 1.1.1.2 dev e101-001-0 proto none

$ python route-delete
{'data': {'base-route/obj/entry/prefix-len': bytearray(b' \x00\x00\x00'), 'base-route/obj/entry/vrf-id': bytearray(b'\x00\x00\x00\x00'), 'base-route/obj/entry/af': bytearray(b'\x02\x00\x00\x00'), 'base- route/obj/entry/route-prefix': 'F\x05\x05\x00'}, 'key':'1.26.1704016.1703992.1703995.1703980.1703978.1703979.'}

$ ip route
1.1.1.0/24 dev e101-001-0 proto kernel scope link src 1.1.1.1