tests: Adding API for static route

Signed-off-by: Ashish Pant <ashish12pant@gmail.com>

Adding API for reading data from JSON and configuring static
routes on the router
This commit is contained in:
Ashish Pant 2019-06-25 05:05:15 +05:30
parent 7fa2079a03
commit e466352711
2 changed files with 104 additions and 1 deletions

View File

@ -112,6 +112,7 @@ def create_common_configuration(tgen, router, data, config_type=None,
config_map = OrderedDict({
"general_config": "! FRR General Config\n",
"interface_config": "! Interfaces Config\n",
"static_route": "! Static Route Config\n",
"bgp": "! BGP Config\n"
})
@ -485,3 +486,103 @@ def create_interfaces_cfg(tgen, topo, build=False):
return result
def create_static_routes(tgen, input_dict, build=False):
"""
Create static routes for given router as defined in input_dict
Parameters
----------
* `tgen` : Topogen object
* `input_dict` : Input dict data, required when configuring from testcase
* `build` : Only for initial setup phase this is set as True.
Usage
-----
input_dict should be in the format below:
# static_routes: list of all routes
# network: network address
# no_of_ip: number of next-hop address that will be configured
# admin_distance: admin distance for route/routes.
# next_hop: starting next-hop address
# tag: tag id for static routes
# delete: True if config to be removed. Default False.
Example:
"routers": {
"r1": {
"static_routes": [
{
"network": "100.0.20.1/32",
"no_of_ip": 9,
"admin_distance": 100,
"next_hop": "10.0.0.1",
"tag": 4001
"delete": true
}
]
}
}
Returns
-------
errormsg(str) or True
"""
result = False
logger.debug("Entering lib API: create_static_routes()")
try:
for router in input_dict.keys():
if "static_routes" not in input_dict[router]:
errormsg = "static_routes not present in input_dict"
logger.info(errormsg)
continue
static_routes_list = []
static_routes = input_dict[router]["static_routes"]
for static_route in static_routes:
del_action = static_route.setdefault("delete", False)
# No of IPs
no_of_ip = static_route.setdefault("no_of_ip", 1)
admin_distance = static_route.setdefault("admin_distance",
None)
tag = static_route.setdefault("tag", None)
if "next_hop" not in static_route or \
"network" not in static_route:
errormsg = "'next_hop' or 'network' missing in" \
" input_dict"
return errormsg
next_hop = static_route["next_hop"]
network = static_route["network"]
ip_list = generate_ips([network], no_of_ip)
for ip in ip_list:
addr_type = validate_ip_address(ip)
if addr_type == "ipv4":
cmd = "ip route {} {}".format(ip, next_hop)
else:
cmd = "ipv6 route {} {}".format(ip, next_hop)
if tag:
cmd = "{} {}".format(cmd, str(tag))
if admin_distance:
cmd = "{} {}".format(cmd, admin_distance)
if del_action:
cmd = "no {}".format(cmd)
static_routes_list.append(cmd)
result = create_common_configuration(tgen, router,
static_routes_list,
"static_route",
build=build)
except InvalidCLIError:
# Traceback
errormsg = traceback.format_exc()
logger.error(errormsg)
return errormsg
logger.debug("Exiting lib API: create_static_routes()")
return result

View File

@ -30,7 +30,8 @@ from lib.topolog import logger
from lib.common_config import (
number_to_row, number_to_column,
load_config_to_router,
create_interfaces_cfg
create_interfaces_cfg,
create_static_routes
)
from lib.bgp import create_router_bgp
@ -168,6 +169,7 @@ def build_config_from_json(tgen, topo, save_bkup=True):
func_dict = OrderedDict([
("links", create_interfaces_cfg),
("static_routes", create_static_routes),
("bgp", create_router_bgp)
])