1#!/usr/bin/env python
2
3#
4# Copyright (c) 2019 by VMware, Inc. ("VMware")
5# Used Copyright (c) 2018 by Network Device Education Foundation,
6# Inc. ("NetDEF") in this file.
7#
8# Permission to use, copy, modify, and/or distribute this software
9# for any purpose with or without fee is hereby granted, provided
10# that the above copyright notice and this permission notice appear
11# in all copies.
12#
13# THE SOFTWARE IS PROVIDED "AS IS" AND VMWARE DISCLAIMS ALL WARRANTIES
14# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL VMWARE BE LIABLE FOR
16# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
17# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20# OF THIS SOFTWARE.
21#
22
23#################################
24# TOPOLOGY
25#################################
26"""
27
28                    +-------+
29           +------- |  R2   |
30          |         +-------+
31         |               |
32     +-------+           |
33     |  R1   |           |
34     +-------+           |
35        |                |
36        |           +-------+          +-------+
37        +---------- |  R3   |----------|  R4   |
38                    +-------+          +-------+
39
40"""
41
42#################################
43# TEST SUMMARY
44#################################
45"""
46Following tests are covered to test route-map functionality:
47TC_34:
48    Verify if route-maps is applied in both inbound and
49    outbound direction to same neighbor/interface.
50TC_36:
51    Test permit/deny statements operation in route-maps with a
52    permutation and combination of permit/deny in prefix-lists
53TC_35:
54    Test multiple sequence numbers in a single route-map for different
55    match/set clauses.
56TC_37:
57    Test add/remove route-maps with multiple set
58    clauses and without any match statement.(Set only)
59TC_38:
60    Test add/remove route-maps with multiple match
61    clauses and without any set statement.(Match only)
62"""
63
64import sys
65import json
66import time
67import pytest
68import inspect
69import os
70from time import sleep
71
72# Save the Current Working Directory to find configuration files.
73CWD = os.path.dirname(os.path.realpath(__file__))
74sys.path.append(os.path.join(CWD, "../"))
75
76# pylint: disable=C0413
77# Import topogen and topotest helpers
78from lib import topotest
79from lib.topogen import Topogen, get_topogen
80from mininet.topo import Topo
81
82# Required to instantiate the topology builder class.
83from lib.topojson import *
84from lib.common_config import (
85    start_topology,
86    write_test_header,
87    write_test_footer,
88    verify_bgp_community,
89    verify_rib,
90    delete_route_maps,
91    create_bgp_community_lists,
92    interface_status,
93    create_route_maps,
94    create_prefix_lists,
95    verify_route_maps,
96    check_address_types,
97    shutdown_bringup_interface,
98    verify_prefix_lists,
99    reset_config_on_routers,
100)
101from lib.topolog import logger
102from lib.bgp import (
103    verify_bgp_convergence,
104    create_router_bgp,
105    clear_bgp_and_verify,
106    verify_bgp_attributes,
107)
108from lib.topojson import build_topo_from_json, build_config_from_json
109
110
111# Global variables
112bgp_convergence = False
113BGP_CONVERGENCE = False
114ADDR_TYPES = check_address_types()
115# Reading the data from JSON File for topology and configuration creation
116jsonFile = "{}/bgp_route_map_topo1.json".format(CWD)
117try:
118    with open(jsonFile, "r") as topoJson:
119        topo = json.load(topoJson)
120except IOError:
121    assert False, "Could not read file {}".format(jsonFile)
122
123# Global variables
124bgp_convergence = False
125NETWORK = {"ipv4": ["11.0.20.1/32", "20.0.20.1/32"], "ipv6": ["1::1/128", "2::1/128"]}
126MASK = {"ipv4": "32", "ipv6": "128"}
127NEXT_HOP = {"ipv4": "10.0.0.2", "ipv6": "fd00::2"}
128ADDR_TYPES = check_address_types()
129
130
131class CreateTopo(Topo):
132    """
133    Test topology builder
134
135
136    * `Topo`: Topology object
137    """
138
139    def build(self, *_args, **_opts):
140        """Build function"""
141        tgen = get_topogen(self)
142
143        # Building topology from json file
144        build_topo_from_json(tgen, topo)
145
146
147def setup_module(mod):
148    """
149    Sets up the pytest environment
150
151    * `mod`: module name
152    """
153    global ADDR_TYPES
154    testsuite_run_time = time.asctime(time.localtime(time.time()))
155    logger.info("Testsuite start time: {}".format(testsuite_run_time))
156    logger.info("=" * 40)
157
158    logger.info("Running setup_module to create topology")
159
160    # This function initiates the topology build with Topogen...
161    tgen = Topogen(CreateTopo, mod.__name__)
162    # ... and here it calls Mininet initialization functions.
163
164    # Starting topology, create tmp files which are loaded to routers
165    #  to start deamons and then start routers
166    start_topology(tgen)
167
168    # Creating configuration from JSON
169    build_config_from_json(tgen, topo)
170
171    # Checking BGP convergence
172    global bgp_convergence
173
174    # Don"t run this test if we have any failure.
175    if tgen.routers_have_failure():
176        pytest.skip(tgen.errors)
177
178    # Api call verify whether BGP is converged
179    bgp_convergence = verify_bgp_convergence(tgen, topo)
180    assert bgp_convergence is True, "setup_module :Failed \n Error:" " {}".format(
181        bgp_convergence
182    )
183
184    logger.info("Running setup_module() done")
185
186
187def teardown_module():
188    """
189    Teardown the pytest environment
190
191    * `mod`: module name
192    """
193
194    logger.info("Running teardown_module to delete topology")
195
196    tgen = get_topogen()
197
198    # Stop toplogy and Remove tmp files
199    tgen.stop_topology()
200
201    logger.info(
202        "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
203    )
204    logger.info("=" * 40)
205
206
207def test_route_map_inbound_outbound_same_neighbor_p0(request):
208    """
209    TC_34:
210    Verify if route-maps is applied in both inbound and
211    outbound direction to same neighbor/interface.
212    """
213
214    tc_name = request.node.name
215    write_test_header(tc_name)
216    tgen = get_topogen()
217
218    # Don"t run this test if we have any failure.
219    if tgen.routers_have_failure():
220        pytest.skip(tgen.errors)
221
222    # Creating configuration from JSON
223    reset_config_on_routers(tgen)
224
225    for adt in ADDR_TYPES:
226
227        # Create Static routes
228        input_dict = {
229            "r1": {
230                "static_routes": [
231                    {
232                        "network": NETWORK[adt][0],
233                        "no_of_ip": 9,
234                        "next_hop": NEXT_HOP[adt],
235                    }
236                ]
237            }
238        }
239
240        result = create_static_routes(tgen, input_dict)
241        assert result is True, "Testcase {} : Failed \n Error: {}".format(
242            tc_name, result
243        )
244
245        # Api call to redistribute static routes
246        input_dict_1 = {
247            "r1": {
248                "bgp": {
249                    "local_as": 100,
250                    "address_family": {
251                        "ipv4": {
252                            "unicast": {
253                                "redistribute": [
254                                    {"redist_type": "static"},
255                                    {"redist_type": "connected"},
256                                ]
257                            }
258                        },
259                        "ipv6": {
260                            "unicast": {
261                                "redistribute": [
262                                    {"redist_type": "static"},
263                                    {"redist_type": "connected"},
264                                ]
265                            }
266                        },
267                    },
268                }
269            }
270        }
271
272        result = create_router_bgp(tgen, topo, input_dict_1)
273        assert result is True, "Testcase {} : Failed \n Error: {}".format(
274            tc_name, result
275        )
276
277        input_dict_2 = {
278            "r4": {
279                "static_routes": [
280                    {
281                        "network": NETWORK[adt][1],
282                        "no_of_ip": 9,
283                        "next_hop": NEXT_HOP[adt],
284                    }
285                ]
286            }
287        }
288
289        result = create_static_routes(tgen, input_dict_2)
290        assert result is True, "Testcase {} : Failed \n Error: {}".format(
291            tc_name, result
292        )
293
294        # Api call to redistribute static routes
295        input_dict_5 = {
296            "r1": {
297                "bgp": {
298                    "address_family": {
299                        "ipv4": {
300                            "unicast": {
301                                "redistribute": [
302                                    {"redist_type": "static"},
303                                    {"redist_type": "connected"},
304                                ]
305                            }
306                        },
307                        "ipv6": {
308                            "unicast": {
309                                "redistribute": [
310                                    {"redist_type": "static"},
311                                    {"redist_type": "connected"},
312                                ]
313                            }
314                        },
315                    }
316                }
317            }
318        }
319        result = create_router_bgp(tgen, topo, input_dict_5)
320        assert result is True, "Testcase {} : Failed \n Error: {}".format(
321            tc_name, result
322        )
323
324        input_dict_2 = {
325            "r3": {
326                "prefix_lists": {
327                    "ipv4": {
328                        "pf_list_1_ipv4": [
329                            {
330                                "seqid": 10,
331                                "action": "permit",
332                                "network": NETWORK["ipv4"][0],
333                            }
334                        ],
335                        "pf_list_2_ipv4": [
336                            {
337                                "seqid": 10,
338                                "action": "permit",
339                                "network": NETWORK["ipv4"][1],
340                            }
341                        ],
342                    },
343                    "ipv6": {
344                        "pf_list_1_ipv6": [
345                            {
346                                "seqid": 100,
347                                "action": "permit",
348                                "network": NETWORK["ipv6"][0],
349                            }
350                        ],
351                        "pf_list_2_ipv6": [
352                            {
353                                "seqid": 100,
354                                "action": "permit",
355                                "network": NETWORK["ipv6"][1],
356                            }
357                        ],
358                    },
359                }
360            }
361        }
362        result = create_prefix_lists(tgen, input_dict_2)
363        assert result is True, "Testcase {} : Failed \n Error: {}".format(
364            tc_name, result
365        )
366
367        # Create route map
368        for addr_type in ADDR_TYPES:
369            input_dict_6 = {
370                "r3": {
371                    "route_maps": {
372                        "rmap_match_tag_1_{}".format(addr_type): [
373                            {
374                                "action": "deny",
375                                "match": {
376                                    addr_type: {
377                                        "prefix_lists": "pf_list_1_{}".format(addr_type)
378                                    }
379                                },
380                            }
381                        ],
382                        "rmap_match_tag_2_{}".format(addr_type): [
383                            {
384                                "action": "permit",
385                                "match": {
386                                    addr_type: {
387                                        "prefix_lists": "pf_list_2_{}".format(addr_type)
388                                    }
389                                },
390                            }
391                        ],
392                    }
393                }
394            }
395            result = create_route_maps(tgen, input_dict_6)
396            assert result is True, "Testcase {} : Failed \n Error: {}".format(
397                tc_name, result
398            )
399
400        # Configure neighbor for route map
401        input_dict_7 = {
402            "r3": {
403                "bgp": {
404                    "address_family": {
405                        "ipv4": {
406                            "unicast": {
407                                "neighbor": {
408                                    "r4": {
409                                        "dest_link": {
410                                            "r3": {
411                                                "route_maps": [
412                                                    {
413                                                        "name": "rmap_match_tag_1_ipv4",
414                                                        "direction": "in",
415                                                    },
416                                                    {
417                                                        "name": "rmap_match_tag_1_ipv4",
418                                                        "direction": "out",
419                                                    },
420                                                ]
421                                            }
422                                        }
423                                    }
424                                }
425                            }
426                        },
427                        "ipv6": {
428                            "unicast": {
429                                "neighbor": {
430                                    "r4": {
431                                        "dest_link": {
432                                            "r3": {
433                                                "route_maps": [
434                                                    {
435                                                        "name": "rmap_match_tag_1_ipv6",
436                                                        "direction": "in",
437                                                    },
438                                                    {
439                                                        "name": "rmap_match_tag_1_ipv6",
440                                                        "direction": "out",
441                                                    },
442                                                ]
443                                            }
444                                        }
445                                    }
446                                }
447                            }
448                        },
449                    }
450                }
451            }
452        }
453
454        result = create_router_bgp(tgen, topo, input_dict_7)
455        assert result is True, "Testcase {} : Failed \n Error: {}".format(
456            tc_name, result
457        )
458
459    for adt in ADDR_TYPES:
460        # Verifying RIB routes
461        dut = "r3"
462        protocol = "bgp"
463        input_dict_2 = {
464            "r4": {
465                "static_routes": [
466                    {
467                        "network": [NETWORK[adt][1]],
468                        "no_of_ip": 9,
469                        "next_hop": NEXT_HOP[adt],
470                    }
471                ]
472            }
473        }
474
475        result = verify_rib(
476            tgen, adt, dut, input_dict_2, protocol=protocol, expected=False
477        )
478        assert result is not True, "Testcase {} : Failed \n"
479        "routes are not present in rib \n Error: {}".format(tc_name, result)
480        logger.info("Expected behaviour: {}".format(result))
481
482        # Verifying RIB routes
483        dut = "r4"
484        input_dict = {
485            "r1": {
486                "static_routes": [
487                    {
488                        "network": [NETWORK[adt][0]],
489                        "no_of_ip": 9,
490                        "next_hop": NEXT_HOP[adt],
491                    }
492                ]
493            }
494        }
495        result = verify_rib(
496            tgen, adt, dut, input_dict, protocol=protocol, expected=False
497        )
498        assert result is not True, "Testcase {} : Failed \n "
499        "routes are not present in rib \n Error: {}".format(tc_name, result)
500        logger.info("Expected behaviour: {}".format(result))
501
502    write_test_footer(tc_name)
503
504
505@pytest.mark.parametrize(
506    "prefix_action, rmap_action",
507    [("permit", "permit"), ("permit", "deny"), ("deny", "permit"), ("deny", "deny")],
508)
509def test_route_map_with_action_values_combination_of_prefix_action_p0(
510    request, prefix_action, rmap_action
511):
512    """
513    TC_36:
514    Test permit/deny statements operation in route-maps with a permutation and
515    combination of permit/deny in prefix-lists
516    """
517    tc_name = request.node.name
518    write_test_header(tc_name)
519    tgen = get_topogen()
520
521    # Don"t run this test if we have any failure.
522    if tgen.routers_have_failure():
523        pytest.skip(tgen.errors)
524
525    # Creating configuration from JSON
526    reset_config_on_routers(tgen)
527
528    for adt in ADDR_TYPES:
529        # Create Static routes
530        input_dict = {
531            "r1": {
532                "static_routes": [
533                    {
534                        "network": NETWORK[adt][0],
535                        "no_of_ip": 9,
536                        "next_hop": NEXT_HOP[adt],
537                    }
538                ]
539            }
540        }
541
542        result = create_static_routes(tgen, input_dict)
543        assert result is True, "Testcase {} : Failed \n Error: {}".format(
544            tc_name, result
545        )
546
547        # Api call to redistribute static routes
548        input_dict_1 = {
549            "r1": {
550                "bgp": {
551                    "local_as": 100,
552                    "address_family": {
553                        "ipv4": {
554                            "unicast": {
555                                "redistribute": [
556                                    {"redist_type": "static"},
557                                    {"redist_type": "connected"},
558                                ]
559                            }
560                        },
561                        "ipv6": {
562                            "unicast": {
563                                "redistribute": [
564                                    {"redist_type": "static"},
565                                    {"redist_type": "connected"},
566                                ]
567                            }
568                        },
569                    },
570                }
571            }
572        }
573
574        result = create_router_bgp(tgen, topo, input_dict_1)
575        assert result is True, "Testcase {} : Failed \n Error: {}".format(
576            tc_name, result
577        )
578
579        # Permit in perfix list and route-map
580        input_dict_2 = {
581            "r3": {
582                "prefix_lists": {
583                    "ipv4": {
584                        "pf_list_1_ipv4": [
585                            {"seqid": 10, "network": "any", "action": prefix_action}
586                        ]
587                    },
588                    "ipv6": {
589                        "pf_list_1_ipv6": [
590                            {"seqid": 100, "network": "any", "action": prefix_action}
591                        ]
592                    },
593                }
594            }
595        }
596        result = create_prefix_lists(tgen, input_dict_2)
597        assert result is True, "Testcase {} : Failed \n Error: {}".format(
598            tc_name, result
599        )
600
601        # Create route map
602        for addr_type in ADDR_TYPES:
603            input_dict_3 = {
604                "r3": {
605                    "route_maps": {
606                        "rmap_match_pf_1_{}".format(addr_type): [
607                            {
608                                "action": rmap_action,
609                                "match": {
610                                    addr_type: {
611                                        "prefix_lists": "pf_list_1_{}".format(addr_type)
612                                    }
613                                },
614                            }
615                        ]
616                    }
617                }
618            }
619            result = create_route_maps(tgen, input_dict_3)
620            assert result is True, "Testcase {} : Failed \n Error: {}".format(
621                tc_name, result
622            )
623
624        # Configure neighbor for route map
625        input_dict_7 = {
626            "r3": {
627                "bgp": {
628                    "address_family": {
629                        "ipv4": {
630                            "unicast": {
631                                "neighbor": {
632                                    "r1": {
633                                        "dest_link": {
634                                            "r3": {
635                                                "route_maps": [
636                                                    {
637                                                        "name": "rmap_match_pf_1_ipv4",
638                                                        "direction": "in",
639                                                    }
640                                                ]
641                                            }
642                                        }
643                                    }
644                                }
645                            }
646                        },
647                        "ipv6": {
648                            "unicast": {
649                                "neighbor": {
650                                    "r1": {
651                                        "dest_link": {
652                                            "r3": {
653                                                "route_maps": [
654                                                    {
655                                                        "name": "rmap_match_pf_1_ipv6",
656                                                        "direction": "in",
657                                                    }
658                                                ]
659                                            }
660                                        }
661                                    }
662                                }
663                            }
664                        },
665                    }
666                }
667            }
668        }
669
670        result = create_router_bgp(tgen, topo, input_dict_7)
671        assert result is True, "Testcase {} : Failed \n Error: {}".format(
672            tc_name, result
673        )
674
675        dut = "r3"
676        protocol = "bgp"
677        input_dict_2 = {
678            "r1": {
679                "static_routes": [
680                    {
681                        "network": [NETWORK[adt][0]],
682                        "no_of_ip": 9,
683                        "next_hop": NEXT_HOP[adt],
684                    }
685                ]
686            }
687        }
688
689        # tgen.mininet_cli()
690        result = verify_rib(
691            tgen, adt, dut, input_dict_2, protocol=protocol, expected=False
692        )
693        if "deny" in [prefix_action, rmap_action]:
694            assert result is not True, "Testcase {} : Failed \n "
695            "Routes are still present \n Error: {}".format(tc_name, result)
696            logger.info("Expected behaviour: {}".format(result))
697        else:
698            assert result is True, "Testcase {} : Failed \n Error: {}".format(
699                tc_name, result
700            )
701
702
703def test_route_map_multiple_seq_different_match_set_clause_p0(request):
704    """
705    TC_35:
706    Test multiple sequence numbers in a single route-map for different
707    match/set clauses.
708    """
709
710    tgen = get_topogen()
711    # test case name
712    tc_name = request.node.name
713    write_test_header(tc_name)
714
715    # Creating configuration from JSON
716    reset_config_on_routers(tgen)
717
718    for adt in ADDR_TYPES:
719        # Create Static routes
720        input_dict = {
721            "r1": {
722                "static_routes": [
723                    {
724                        "network": NETWORK[adt][0],
725                        "no_of_ip": 1,
726                        "next_hop": NEXT_HOP[adt],
727                    }
728                ]
729            }
730        }
731        result = create_static_routes(tgen, input_dict)
732        assert result is True, "Testcase {} : Failed \n Error: {}".format(
733            tc_name, result
734        )
735
736        # Api call to redistribute static routes
737        input_dict_1 = {
738            "r1": {
739                "bgp": {
740                    "address_family": {
741                        "ipv4": {
742                            "unicast": {
743                                "redistribute": [
744                                    {"redist_type": "static"},
745                                    {"redist_type": "connected"},
746                                ]
747                            }
748                        },
749                        "ipv6": {
750                            "unicast": {
751                                "redistribute": [
752                                    {"redist_type": "static"},
753                                    {"redist_type": "connected"},
754                                ]
755                            }
756                        },
757                    }
758                }
759            }
760        }
761        result = create_router_bgp(tgen, topo, input_dict_1)
762        assert result is True, "Testcase {} : Failed \n Error: {}".format(
763            tc_name, result
764        )
765
766        # Create ip prefix list
767        input_dict_2 = {
768            "r3": {
769                "prefix_lists": {
770                    "ipv4": {
771                        "pf_list_1_ipv4": [
772                            {"seqid": 10, "network": "any", "action": "permit"}
773                        ]
774                    },
775                    "ipv6": {
776                        "pf_list_1_ipv6": [
777                            {"seqid": 100, "network": "any", "action": "permit"}
778                        ]
779                    },
780                }
781            }
782        }
783        result = create_prefix_lists(tgen, input_dict_2)
784        assert result is True, "Testcase {} : Failed \n Error: {}".format(
785            tc_name, result
786        )
787
788        # Create route map
789        for addr_type in ADDR_TYPES:
790            input_dict_3 = {
791                "r3": {
792                    "route_maps": {
793                        "rmap_match_pf_1_{}".format(addr_type): [
794                            {
795                                "action": "permit",
796                                "match": {
797                                    addr_type: {
798                                        "prefix_lists": "pf_list_2_{}".format(addr_type)
799                                    }
800                                },
801                                "set": {"path": {"as_num": 500}},
802                            },
803                            {
804                                "action": "permit",
805                                "match": {
806                                    addr_type: {
807                                        "prefix_lists": "pf_list_2_{}".format(addr_type)
808                                    }
809                                },
810                                "set": {"locPrf": 150,},
811                            },
812                            {
813                                "action": "permit",
814                                "match": {
815                                    addr_type: {
816                                        "prefix_lists": "pf_list_1_{}".format(addr_type)
817                                    }
818                                },
819                                "set": {"metric": 50},
820                            },
821                        ]
822                    }
823                }
824            }
825            result = create_route_maps(tgen, input_dict_3)
826            assert result is True, "Testcase {} : Failed \n Error: {}".format(
827                tc_name, result
828            )
829
830        # Configure neighbor for route map
831        input_dict_4 = {
832            "r3": {
833                "bgp": {
834                    "address_family": {
835                        "ipv4": {
836                            "unicast": {
837                                "neighbor": {
838                                    "r1": {
839                                        "dest_link": {
840                                            "r3": {
841                                                "route_maps": [
842                                                    {
843                                                        "name": "rmap_match_pf_1_ipv4",
844                                                        "direction": "in",
845                                                    }
846                                                ]
847                                            }
848                                        }
849                                    },
850                                    "r4": {
851                                        "dest_link": {
852                                            "r3": {
853                                                "route_maps": [
854                                                    {
855                                                        "name": "rmap_match_pf_1_ipv4",
856                                                        "direction": "out",
857                                                    }
858                                                ]
859                                            }
860                                        }
861                                    },
862                                }
863                            }
864                        },
865                        "ipv6": {
866                            "unicast": {
867                                "neighbor": {
868                                    "r1": {
869                                        "dest_link": {
870                                            "r3": {
871                                                "route_maps": [
872                                                    {
873                                                        "name": "rmap_match_pf_1_ipv6",
874                                                        "direction": "in",
875                                                    }
876                                                ]
877                                            }
878                                        }
879                                    },
880                                    "r4": {
881                                        "dest_link": {
882                                            "r3": {
883                                                "route_maps": [
884                                                    {
885                                                        "name": "rmap_match_pf_1_ipv6",
886                                                        "direction": "out",
887                                                    }
888                                                ]
889                                            }
890                                        }
891                                    },
892                                }
893                            }
894                        },
895                    }
896                }
897            }
898        }
899        result = create_router_bgp(tgen, topo, input_dict_4)
900        assert result is True, "Testcase {} : Failed \n Error: {}".format(
901            tc_name, result
902        )
903
904    for adt in ADDR_TYPES:
905        # Verifying RIB routes
906        dut = "r3"
907        protocol = "bgp"
908        input_dict = {
909            "r3": {"route_maps": {"rmap_match_pf_list1": [{"set": {"metric": 50,}}],}}
910        }
911
912        static_routes = [NETWORK[adt][0]]
913
914        time.sleep(2)
915        result = verify_bgp_attributes(
916            tgen, adt, dut, static_routes, "rmap_match_pf_list1", input_dict
917        )
918        assert result is True, "Test case {} : Failed \n Error: {}".format(
919            tc_name, result
920        )
921
922        dut = "r4"
923        result = verify_bgp_attributes(
924            tgen, adt, dut, static_routes, "rmap_match_pf_list1", input_dict
925        )
926        assert result is True, "Test case {} : Failed \n Error: {}".format(
927            tc_name, result
928        )
929
930        logger.info("Testcase " + tc_name + " :Passed \n")
931
932        # Uncomment next line for debugging
933        # tgen.mininet_cli()
934
935
936def test_route_map_set_only_no_match_p0(request):
937    """
938    TC_37:
939    Test add/remove route-maps with multiple set
940    clauses and without any match statement.(Set only)
941    """
942
943    tgen = get_topogen()
944    # test case name
945    tc_name = request.node.name
946    write_test_header(tc_name)
947
948    # Creating configuration from JSON
949    reset_config_on_routers(tgen)
950
951    for adt in ADDR_TYPES:
952        # Create Static routes
953        input_dict = {
954            "r1": {
955                "static_routes": [
956                    {
957                        "network": NETWORK[adt][0],
958                        "no_of_ip": 1,
959                        "next_hop": NEXT_HOP[adt],
960                    }
961                ]
962            }
963        }
964        result = create_static_routes(tgen, input_dict)
965        assert result is True, "Testcase {} : Failed \n Error: {}".format(
966            tc_name, result
967        )
968
969        # Api call to redistribute static routes
970        input_dict_1 = {
971            "r1": {
972                "bgp": {
973                    "address_family": {
974                        "ipv4": {
975                            "unicast": {
976                                "redistribute": [
977                                    {"redist_type": "static"},
978                                    {"redist_type": "connected"},
979                                ]
980                            }
981                        },
982                        "ipv6": {
983                            "unicast": {
984                                "redistribute": [
985                                    {"redist_type": "static"},
986                                    {"redist_type": "connected"},
987                                ]
988                            }
989                        },
990                    }
991                }
992            }
993        }
994        result = create_router_bgp(tgen, topo, input_dict_1)
995        assert result is True, "Testcase {} : Failed \n Error: {}".format(
996            tc_name, result
997        )
998
999        # Create route map
1000        input_dict_3 = {
1001            "r3": {
1002                "route_maps": {
1003                    "rmap_match_pf_1": [
1004                        {
1005                            "action": "permit",
1006                            "set": {"metric": 50, "locPrf": 150, "weight": 4000},
1007                        }
1008                    ]
1009                }
1010            }
1011        }
1012        result = create_route_maps(tgen, input_dict_3)
1013        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1014            tc_name, result
1015        )
1016
1017        # Configure neighbor for route map
1018        input_dict_4 = {
1019            "r3": {
1020                "bgp": {
1021                    "address_family": {
1022                        "ipv4": {
1023                            "unicast": {
1024                                "neighbor": {
1025                                    "r1": {
1026                                        "dest_link": {
1027                                            "r3": {
1028                                                "route_maps": [
1029                                                    {
1030                                                        "name": "rmap_match_pf_1",
1031                                                        "direction": "in",
1032                                                    }
1033                                                ]
1034                                            }
1035                                        }
1036                                    },
1037                                    "r4": {
1038                                        "dest_link": {
1039                                            "r3": {
1040                                                "route_maps": [
1041                                                    {
1042                                                        "name": "rmap_match_pf_1",
1043                                                        "direction": "out",
1044                                                    }
1045                                                ]
1046                                            }
1047                                        }
1048                                    },
1049                                }
1050                            }
1051                        },
1052                        "ipv6": {
1053                            "unicast": {
1054                                "neighbor": {
1055                                    "r1": {
1056                                        "dest_link": {
1057                                            "r3": {
1058                                                "route_maps": [
1059                                                    {
1060                                                        "name": "rmap_match_pf_1",
1061                                                        "direction": "in",
1062                                                    }
1063                                                ]
1064                                            }
1065                                        }
1066                                    },
1067                                    "r4": {
1068                                        "dest_link": {
1069                                            "r3": {
1070                                                "route_maps": [
1071                                                    {
1072                                                        "name": "rmap_match_pf_1",
1073                                                        "direction": "out",
1074                                                    }
1075                                                ]
1076                                            }
1077                                        }
1078                                    },
1079                                }
1080                            }
1081                        },
1082                    }
1083                }
1084            }
1085        }
1086        result = create_router_bgp(tgen, topo, input_dict_4)
1087        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1088            tc_name, result
1089        )
1090
1091    time.sleep(2)
1092    for adt in ADDR_TYPES:
1093        input_dict_4 = {
1094            "r3": {
1095                "route_maps": {
1096                    "rmap_match_pf_1": [{"action": "permit", "set": {"metric": 50,}}]
1097                }
1098            }
1099        }
1100        # Verifying RIB routes
1101        static_routes = [NETWORK[adt][0]]
1102        result = verify_bgp_attributes(
1103            tgen, adt, "r3", static_routes, "rmap_match_pf_1", input_dict_3
1104        )
1105        assert result is True, "Test case {} : Failed \n Error: {}".format(
1106            tc_name, result
1107        )
1108
1109        result = verify_bgp_attributes(
1110            tgen, adt, "r4", static_routes, "rmap_match_pf_1", input_dict_4
1111        )
1112        assert result is True, "Test case {} : Failed \n Error: {}".format(
1113            tc_name, result
1114        )
1115
1116        logger.info("Testcase " + tc_name + " :Passed \n")
1117
1118        # Uncomment next line for debugging
1119        # tgen.mininet_cli()
1120
1121
1122def test_route_map_match_only_no_set_p0(request):
1123    """
1124    TC_38:
1125    Test add/remove route-maps with multiple match
1126    clauses and without any set statement.(Match only)
1127    """
1128
1129    tgen = get_topogen()
1130    # test case name
1131    tc_name = request.node.name
1132    write_test_header(tc_name)
1133
1134    # Creating configuration from JSON
1135    reset_config_on_routers(tgen)
1136
1137    for adt in ADDR_TYPES:
1138        # Create Static routes
1139        input_dict = {
1140            "r1": {
1141                "static_routes": [
1142                    {
1143                        "network": NETWORK[adt][0],
1144                        "no_of_ip": 1,
1145                        "next_hop": NEXT_HOP[adt],
1146                    }
1147                ]
1148            }
1149        }
1150        result = create_static_routes(tgen, input_dict)
1151        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1152            tc_name, result
1153        )
1154
1155        # Api call to redistribute static routes
1156        input_dict_1 = {
1157            "r1": {
1158                "bgp": {
1159                    "address_family": {
1160                        "ipv4": {
1161                            "unicast": {
1162                                "redistribute": [
1163                                    {"redist_type": "static"},
1164                                    {"redist_type": "connected"},
1165                                ]
1166                            }
1167                        },
1168                        "ipv6": {
1169                            "unicast": {
1170                                "redistribute": [
1171                                    {"redist_type": "static"},
1172                                    {"redist_type": "connected"},
1173                                ]
1174                            }
1175                        },
1176                    }
1177                }
1178            }
1179        }
1180        result = create_router_bgp(tgen, topo, input_dict_1)
1181        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1182            tc_name, result
1183        )
1184
1185        # Create ip prefix list
1186        input_dict_2 = {
1187            "r1": {
1188                "prefix_lists": {
1189                    "ipv4": {
1190                        "pf_list_1_ipv4": [
1191                            {"seqid": 10, "network": "any", "action": "permit"}
1192                        ]
1193                    },
1194                    "ipv6": {
1195                        "pf_list_1_ipv6": [
1196                            {"seqid": 100, "network": "any", "action": "permit"}
1197                        ]
1198                    },
1199                }
1200            }
1201        }
1202        result = create_prefix_lists(tgen, input_dict_2)
1203        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1204            tc_name, result
1205        )
1206
1207        # Create route map
1208        for addr_type in ADDR_TYPES:
1209            input_dict_3 = {
1210                "r1": {
1211                    "route_maps": {
1212                        "rmap_match_pf_1_{}".format(addr_type): [
1213                            {"action": "permit", "set": {"metric": 50, "locPrf": 150,}}
1214                        ]
1215                    }
1216                }
1217            }
1218            result = create_route_maps(tgen, input_dict_3)
1219            assert result is True, "Testcase {} : Failed \n Error: {}".format(
1220                tc_name, result
1221            )
1222
1223        # Configure neighbor for route map
1224        input_dict_4 = {
1225            "r1": {
1226                "bgp": {
1227                    "address_family": {
1228                        "ipv4": {
1229                            "unicast": {
1230                                "neighbor": {
1231                                    "r3": {
1232                                        "dest_link": {
1233                                            "r1": {
1234                                                "route_maps": [
1235                                                    {
1236                                                        "name": "rmap_match_pf_1_ipv4",
1237                                                        "direction": "out",
1238                                                    }
1239                                                ]
1240                                            }
1241                                        }
1242                                    }
1243                                }
1244                            }
1245                        },
1246                        "ipv6": {
1247                            "unicast": {
1248                                "neighbor": {
1249                                    "r3": {
1250                                        "dest_link": {
1251                                            "r1": {
1252                                                "route_maps": [
1253                                                    {
1254                                                        "name": "rmap_match_pf_1_ipv6",
1255                                                        "direction": "out",
1256                                                    }
1257                                                ]
1258                                            }
1259                                        }
1260                                    }
1261                                }
1262                            }
1263                        },
1264                    }
1265                }
1266            }
1267        }
1268        result = create_router_bgp(tgen, topo, input_dict_4)
1269        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1270            tc_name, result
1271        )
1272
1273        # Create ip prefix list
1274        input_dict_5 = {
1275            "r3": {
1276                "prefix_lists": {
1277                    "ipv4": {
1278                        "pf_list_1_ipv4": [
1279                            {"seqid": 10, "network": "any", "action": "permit"}
1280                        ]
1281                    },
1282                    "ipv6": {
1283                        "pf_list_1_ipv6": [
1284                            {"seqid": 100, "network": "any", "action": "permit"}
1285                        ]
1286                    },
1287                }
1288            }
1289        }
1290        result = create_prefix_lists(tgen, input_dict_5)
1291        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1292            tc_name, result
1293        )
1294
1295        # Create route map
1296        for addr_type in ADDR_TYPES:
1297            input_dict_6 = {
1298                "r3": {
1299                    "route_maps": {
1300                        "rmap_match_pf_2_{}".format(addr_type): [
1301                            {
1302                                "action": "permit",
1303                                "match": {
1304                                    addr_type: {
1305                                        "prefix_lists": "pf_list_1_{}".format(addr_type)
1306                                    }
1307                                },
1308                            }
1309                        ]
1310                    }
1311                }
1312            }
1313            result = create_route_maps(tgen, input_dict_6)
1314            assert result is True, "Testcase {} : Failed \n Error: {}".format(
1315                tc_name, result
1316            )
1317
1318        # Configure neighbor for route map
1319        input_dict_7 = {
1320            "r3": {
1321                "bgp": {
1322                    "address_family": {
1323                        "ipv4": {
1324                            "unicast": {
1325                                "neighbor": {
1326                                    "r1": {
1327                                        "dest_link": {
1328                                            "r3": {
1329                                                "route_maps": [
1330                                                    {
1331                                                        "name": "rmap_match_pf_2_ipv4",
1332                                                        "direction": "in",
1333                                                    }
1334                                                ]
1335                                            }
1336                                        }
1337                                    },
1338                                    "r4": {
1339                                        "dest_link": {
1340                                            "r3": {
1341                                                "route_maps": [
1342                                                    {
1343                                                        "name": "rmap_match_pf_2_ipv4",
1344                                                        "direction": "out",
1345                                                    }
1346                                                ]
1347                                            }
1348                                        }
1349                                    },
1350                                }
1351                            }
1352                        },
1353                        "ipv6": {
1354                            "unicast": {
1355                                "neighbor": {
1356                                    "r1": {
1357                                        "dest_link": {
1358                                            "r3": {
1359                                                "route_maps": [
1360                                                    {
1361                                                        "name": "rmap_match_pf_2_ipv6",
1362                                                        "direction": "in",
1363                                                    }
1364                                                ]
1365                                            }
1366                                        }
1367                                    },
1368                                    "r4": {
1369                                        "dest_link": {
1370                                            "r3": {
1371                                                "route_maps": [
1372                                                    {
1373                                                        "name": "rmap_match_pf_2_ipv6",
1374                                                        "direction": "out",
1375                                                    }
1376                                                ]
1377                                            }
1378                                        }
1379                                    },
1380                                }
1381                            }
1382                        },
1383                    }
1384                }
1385            }
1386        }
1387        result = create_router_bgp(tgen, topo, input_dict_7)
1388        assert result is True, "Testcase {} : Failed \n Error: {}".format(
1389            tc_name, result
1390        )
1391
1392    for adt in ADDR_TYPES:
1393        # Verifying RIB routes
1394        static_routes = [NETWORK[adt][0]]
1395        result = verify_bgp_attributes(
1396            tgen, adt, "r3", static_routes, "rmap_match_pf_1", input_dict_3
1397        )
1398        assert result is True, "Test case {} : Failed \n Error: {}".format(
1399            tc_name, result
1400        )
1401
1402
1403if __name__ == "__main__":
1404    args = ["-s"] + sys.argv[1:]
1405    sys.exit(pytest.main(args))
1406