7
7
# mypy: disable-error-code=attr-defined
8
8
from __future__ import annotations
9
9
10
- from typing import ClassVar , TypeVar
10
+ from typing import Any , ClassVar , TypeVar
11
11
12
12
from pydantic import field_validator
13
13
@@ -1716,24 +1716,24 @@ class VerifyBGPRedistribution(AntaTest):
1716
1716
anta.tests.routing:
1717
1717
bgp:
1718
1718
- VerifyBGPRedistribution:
1719
- vrfs:
1720
- - vrf: default
1721
- address_families:
1722
- - afi_safi: ipv4Unicast
1723
- redistributed_routes:
1724
- - proto: Connected
1725
- include_leaked: True
1726
- route_map: RM-CONN-2-BGP
1727
- - proto: Static
1728
- include_leaked: True
1729
- route_map: RM-CONN-2-BGP
1730
- - afi_safi: IPv6 Unicast
1731
- redistributed_routes:
1732
- - proto: Dynamic
1733
- route_map: RM-CONN-2-BGP
1734
- - proto: Static
1735
- include_leaked: True
1736
- route_map: RM-CONN-2-BGP
1719
+ vrfs:
1720
+ - vrf: default
1721
+ address_families:
1722
+ - afi_safi: ipv4Unicast
1723
+ redistributed_routes:
1724
+ - proto: Connected
1725
+ include_leaked: True
1726
+ route_map: RM-CONN-2-BGP
1727
+ - proto: Static
1728
+ include_leaked: True
1729
+ route_map: RM-CONN-2-BGP
1730
+ - afi_safi: IPv6 Unicast
1731
+ redistributed_routes:
1732
+ - proto: Dynamic
1733
+ route_map: RM-CONN-2-BGP
1734
+ - proto: Static
1735
+ include_leaked: True
1736
+ route_map: RM-CONN-2-BGP
1737
1737
```
1738
1738
"""
1739
1739
@@ -1746,6 +1746,22 @@ class Input(AntaTest.Input):
1746
1746
vrfs : list [BgpVrf ]
1747
1747
"""List of address families."""
1748
1748
1749
+ def _validate_redistribute_route_details (self , vrf_data : str , address_family : dict [str , Any ], afi_safi_configs : list [dict [str , Any ]]) -> None :
1750
+ """Validate the redstributed route details for a given address family."""
1751
+ for route_info in address_family .redistributed_routes :
1752
+ # If the redistributed route protocol does not match the expected value, test fails.
1753
+ if not (actual_route := get_item (afi_safi_configs .get ("redistributedRoutes" ), "proto" , route_info .proto )):
1754
+ self .result .is_failure (f"{ vrf_data } , { address_family } , { route_info } - Not configured" )
1755
+ continue
1756
+
1757
+ # If includes leaked field applicable, and it does not matches the expected value, test fails.
1758
+ if all ([route_info .include_leaked is not None , (act_include_leaked := actual_route .get ("includeLeaked" , "Not Found" )) != route_info .include_leaked ]):
1759
+ self .result .is_failure (f"{ vrf_data } , { address_family } , { route_info } - Value for included leaked mismatch - Actual: { act_include_leaked } " )
1760
+
1761
+ # If route map is required and it is not matching the expected value, test fails.
1762
+ if all ([route_info .route_map , (act_route_map := actual_route .get ("routeMap" , "Not Found" )) != route_info .route_map ]):
1763
+ self .result .is_failure (f"{ vrf_data } , { address_family } , { route_info } - Route map mismatch - Actual: { act_route_map } " )
1764
+
1749
1765
@AntaTest .anta_test
1750
1766
def test (self ) -> None :
1751
1767
"""Main test function for VerifyBGPRedistribution."""
@@ -1758,16 +1774,4 @@ def test(self) -> None:
1758
1774
if not (afi_safi_configs := get_value (command_output , f"vrfs.{ vrf_data .vrf } .afiSafiConfig.{ address_family .afi_safi } " )):
1759
1775
self .result .is_failure (f"{ vrf_data } , { address_family } - Not configured" )
1760
1776
continue
1761
- for route_info in address_family .redistributed_routes :
1762
- # If the redistributed route protocol does not match the expected value, test fails.
1763
- if not (actual_route := get_item (afi_safi_configs .get ("redistributedRoutes" ), "proto" , route_info .proto )):
1764
- self .result .is_failure (f"{ vrf_data } , { address_family } , { route_info } - Not configured" )
1765
- continue
1766
-
1767
- # If includes leaked field applicable, and it does not matches the expected value, test fails.
1768
- if all ([route_info .include_leaked is not None , (act_include_leaked := actual_route .get ("includeLeaked" )) != route_info .include_leaked ]):
1769
- self .result .is_failure (f"{ vrf_data } , { address_family } , { route_info } - Value for included leaked mismatch - Actual: { act_include_leaked } " )
1770
-
1771
- # If route map is required and it is not matching the expected value, test fails.
1772
- if all ([route_info .route_map , (act_route_map := actual_route .get ("routeMap" , "Not Found" )) != route_info .route_map ]):
1773
- self .result .is_failure (f"{ vrf_data } , { address_family } , { route_info } - Route map mismatch - Actual: { act_route_map } " )
1777
+ self ._validate_redistribute_route_details (str (vrf_data ), address_family , afi_safi_configs )
0 commit comments