]> git.puffer.fish Git - matthieu/frr.git/commitdiff
tests: Adding API for prefix-list configuration
authorAshish Pant <ashish12pant@gmail.com>
Mon, 24 Jun 2019 23:46:32 +0000 (05:16 +0530)
committerAshish Pant <ashish12pant@gmail.com>
Tue, 9 Jul 2019 04:56:53 +0000 (10:26 +0530)
Signed-off-by: Ashish Pant <ashish12pant@gmail.com>
Adding API for prefix-list that reads data from JSON and configures
on the router.

tests/topotests/lib/common_config.py
tests/topotests/lib/topojson.py

index 23a3b09e2d34d65d3dcd32ae42bd23c26a4f1893..0c1054fbf208758964a45ad652ec9503fe8d280a 100644 (file)
@@ -113,6 +113,7 @@ def create_common_configuration(tgen, router, data, config_type=None,
         "general_config": "! FRR General Config\n",
         "interface_config": "! Interfaces Config\n",
         "static_route": "! Static Route Config\n",
+        "prefix_list": "! Prefix List Config\n",
         "bgp": "! BGP Config\n"
     })
 
@@ -585,4 +586,119 @@ def create_static_routes(tgen, input_dict, build=False):
         return errormsg
 
     logger.debug("Exiting lib API: create_static_routes()")
-    return result
\ No newline at end of file
+    return result
+
+
+def create_prefix_lists(tgen, input_dict, build=False):
+    """
+    Create ip prefix lists as per the config provided in input
+    JSON or 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
+    -----
+    # pf_lists_1: name of prefix-list, user defined
+    # seqid: prefix-list seqid, auto-generated if not given by user
+    # network: criteria for applying prefix-list
+    # action: permit/deny
+    # le: less than or equal number of bits
+    # ge: greater than or equal number of bits
+
+    Example
+    -------
+    input_dict = {
+        "r1": {
+            "prefix_lists":{
+                "ipv4": {
+                    "pf_list_1": [
+                        {
+                            "seqid": 10,
+                            "network": "any",
+                            "action": "permit",
+                            "le": "32",
+                            "ge": "30",
+                            "delete": True
+                        }
+                    ]
+                }
+            }
+        }
+    }
+
+    Returns
+    -------
+    errormsg or True
+    """
+
+    logger.debug("Entering lib API: create_prefix_lists()")
+    result = False
+    try:
+        for router in input_dict.keys():
+            if "prefix_lists" not in input_dict[router]:
+                errormsg = "prefix_lists not present in input_dict"
+                logger.info(errormsg)
+                continue
+
+            config_data = []
+            prefix_lists = input_dict[router]["prefix_lists"]
+            for addr_type, prefix_data in prefix_lists.iteritems():
+                if not check_address_types(addr_type):
+                    continue
+
+                for prefix_name, prefix_list in prefix_data.iteritems():
+                    for prefix_dict in prefix_list:
+                        if "action" not in prefix_dict or \
+                                "network" not in prefix_dict:
+                            errormsg = "'action' or network' missing in" \
+                                       " input_dict"
+                            return errormsg
+
+                        network_addr = prefix_dict["network"]
+                        action = prefix_dict["action"]
+                        le = prefix_dict.setdefault("le", None)
+                        ge = prefix_dict.setdefault("ge", None)
+                        seqid = prefix_dict.setdefault("seqid", None)
+                        del_action = prefix_dict.setdefault("delete", False)
+                        if seqid is None:
+                            seqid = get_seq_id("prefix_lists", router,
+                                               prefix_name)
+                        else:
+                            set_seq_id("prefix_lists", router, seqid,
+                                       prefix_name)
+
+                        if addr_type == "ipv4":
+                            protocol = "ip"
+                        else:
+                            protocol = "ipv6"
+
+                        cmd = "{} prefix-list {} seq {} {} {}".format(
+                            protocol, prefix_name, seqid, action, network_addr
+                        )
+                        if le:
+                            cmd = "{} le {}".format(cmd, le)
+                        if ge:
+                            cmd = "{} ge {}".format(cmd, ge)
+
+                        if del_action:
+                            cmd = "no {}".format(cmd)
+
+                        config_data.append(cmd)
+            result = create_common_configuration(tgen, router,
+                                                 config_data,
+                                                 "prefix_list",
+                                                 build=build)
+
+    except InvalidCLIError:
+        # Traceback
+        errormsg = traceback.format_exc()
+        logger.error(errormsg)
+        return errormsg
+
+    logger.debug("Exiting lib API: create_prefix_lists()")
+    return result
+
index 3d216d07858b7687183c150d422907a0bf18f429..7b5a2f9c15ac8ffd5dbcf45e18ed588e7689de3b 100644 (file)
@@ -31,7 +31,8 @@ from lib.common_config import (
     number_to_row, number_to_column,
     load_config_to_router,
     create_interfaces_cfg,
-    create_static_routes
+    create_static_routes,
+    create_prefix_lists,
 )
 
 from lib.bgp import create_router_bgp
@@ -170,6 +171,7 @@ def build_config_from_json(tgen, topo, save_bkup=True):
     func_dict = OrderedDict([
         ("links", create_interfaces_cfg),
         ("static_routes", create_static_routes),
+        ("prefix_lists", create_prefix_lists),
         ("bgp", create_router_bgp)
     ])