Skip to content

Commit

Permalink
issue-499: removed Ipv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
MaheshGSLAB committed Jan 12, 2024
1 parent 2e33101 commit 0f14509
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 62 deletions.
55 changes: 17 additions & 38 deletions anta/tests/bfd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023 Arista Networks, Inc.
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""
Expand All @@ -8,34 +8,15 @@
# mypy: disable-error-code=attr-defined
from __future__ import annotations

from ipaddress import IPv4Address, IPv6Address, ip_address
from typing import Any, Dict, List, Union
from ipaddress import IPv4Address
from typing import Any, List

from pydantic import BaseModel

from anta.models import AntaCommand, AntaTest
from anta.tools.get_value import get_value


def create_bfd_peer_key(vrf: str, peer: str) -> str:
"""
Create a key for retrieving BFD peer information based on the VRF and peer's IP type.
Parameters:
- vrf (str): Virtual Routing and Forwarding context.
- peer (str): IPv4 or IPv6 address of the BFD peer.
Returns:
str: Key used to retrieve BFD peer information from the command output.
Example:
>>> create_bfd_peer_key("default", "192.168.1.1")
'vrfs..default..ipv4Neighbors..192.168.1.1..peerStats..'
"""
ip_type = "ipv4" if isinstance(ip_address(peer), IPv4Address) else "ipv6"
return f"vrfs..{vrf}..{ip_type}Neighbors..{peer}..peerStats.."


class VerifyBFDSpecificPeers(AntaTest):
"""
This class verifies if the BFD peer's sessions are UP and remote disc is non-zero in the specified VRF.
Expand All @@ -48,7 +29,7 @@ class VerifyBFDSpecificPeers(AntaTest):
name = "VerifyBFDSpecificPeers"
description = "Verifies the BFD peer's sessions and remote disc in the specified VRF."
categories = ["bfd"]
commands = [AntaCommand(command="show bfd peers", revision=1)]
commands = [AntaCommand(command="show bfd peers")]

class Input(AntaTest.Input):
"""
Expand All @@ -63,21 +44,20 @@ class BFDPeers(BaseModel):
This class defines the details of a BFD peer.
"""

peer: Union[IPv4Address, IPv6Address]
"""IPv4/IPv6 BFD peer"""
peer_address: IPv4Address
"""IPv4 address of a BFD peer"""
vrf: str = "default"
"""VRF context"""
"""Optional VRF for BGP peer. If not provided, it defaults to `default`."""

@AntaTest.anta_test
def test(self) -> None:
failures: Dict[str, Dict[str, Union[str, Dict[str, Any]]]] = {}
failures: dict[Any, Any] = {}

# Iterating over BFD peers
for bfd_peer in self.inputs.bfd_peers:
peer = str(bfd_peer.peer)
peer = str(bfd_peer.peer_address)
vrf = bfd_peer.vrf
bfd_key = create_bfd_peer_key(vrf, peer)
bfd_output = get_value(self.instance_commands[0].json_output, f"{bfd_key}", separator="..")
bfd_output = get_value(self.instance_commands[0].json_output, f"vrfs..{vrf}..ipv4Neighbors..{peer}..peerStats..", separator="..")

# Check if BFD peer configured
if not bfd_output:
Expand Down Expand Up @@ -106,7 +86,7 @@ class VerifyBFDPeersIntervals(AntaTest):
name = "VerifyBFDPeersIntervals"
description = "Verifies the timers of the BFD peers in the specified VRF."
categories = ["bfd"]
commands = [AntaCommand(command="show bfd peers detail", revision=1)]
commands = [AntaCommand(command="show bfd peers detail")]

class Input(AntaTest.Input):
"""
Expand All @@ -121,10 +101,10 @@ class BFDPeers(BaseModel):
This class defines the details of a BFD peer.
"""

peer: Union[IPv4Address, IPv6Address]
"""IPv4/IPv6 BFD peer"""
peer_address: IPv4Address
"""IPv4 address of a BFD peer"""
vrf: str = "default"
"""VRF context"""
"""Optional VRF for BGP peer. If not provided, it defaults to `default`."""
tx_interval: int
"""Tx interval of BFD peer"""
rx_interval: int
Expand All @@ -134,17 +114,16 @@ class BFDPeers(BaseModel):

@AntaTest.anta_test
def test(self) -> None:
failures: Dict[str, Dict[str, Union[str, Dict[str, Any]]]] = {}
failures: dict[Any, Any] = {}

# Iterating over BFD peers
for bfd_peers in self.inputs.bfd_peers:
peer = str(bfd_peers.peer)
peer = str(bfd_peers.peer_address)
vrf = bfd_peers.vrf
tx_interval = bfd_peers.tx_interval
rx_interval = bfd_peers.rx_interval
multiplier = bfd_peers.multiplier
bfd_key = create_bfd_peer_key(vrf, peer)
bfd_output = get_value(self.instance_commands[0].json_output, f"{bfd_key}", separator="..")
bfd_output = get_value(self.instance_commands[0].json_output, f"vrfs..{vrf}..ipv4Neighbors..{peer}..peerStats..", separator="..")

# Check if BFD peer configured
if not bfd_output:
Expand Down
14 changes: 10 additions & 4 deletions examples/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ anta.tests.aaa:
- dot1x

anta.tests.bfd:
- VerifyBFDPeers:
bfd_neighbors:
- neighbor: 192.0.255.8
- VerifyBFDSpecificPeers:
bfd_peers:
- peer_address: 192.0.255.8
vrf: default
- peer_address: 192.0.255.7
vrf: default
- VerifyBFDPeersIntervals:
bfd_peers:
- peer_address: 192.0.255.8
vrf: default
tx_interval: 1200000
rx_interval: 1200000
multiplier: 3
- neighbor: 192.0.255.7
- peer_address: 192.0.255.7
vrf: default
tx_interval: 1200000
rx_interval: 1200000
Expand Down
Loading

0 comments on commit 0f14509

Please sign in to comment.