Skip to content

OSPF

ANTA catalog for routing-ospf tests

OSPF test functions

VerifyOSPFNeighborCount

Bases: AntaTest

Verifies the number of OSPF neighbors in FULL state is the one we expect.

Parameters:

Name Type Description Default
number int

The expected number of OSPF neighbors in FULL state.

required
Source code in anta/tests/routing/ospf.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class VerifyOSPFNeighborCount(AntaTest):
    """
    Verifies the number of OSPF neighbors in FULL state is the one we expect.

    Args:
        number (int): The expected number of OSPF neighbors in FULL state.
    """

    name = "VerifyOSPFNeighborCount"
    description = "Verifies the number of OSPF neighbors in FULL state is the one we expect."
    categories = ["routing", "ospf"]
    commands = [AntaCommand(command="show ip ospf neighbor")]

    @AntaTest.anta_test
    def test(self, number: Optional[int] = None) -> None:
        """Run VerifyOSPFNeighborCount validation"""
        if not (isinstance(number, int) and number >= 0):
            self.result.is_skipped(f"VerifyOSPFNeighborCount was not run as the number given '{number}' is not a valid value.")
            return

        command_output = self.instance_commands[0].json_output

        if (neighbor_count := _count_ospf_neighbor(command_output)) == 0:
            self.result.is_skipped("no OSPF neighbor found")
            return

        self.result.is_success()

        if neighbor_count != number:
            self.result.is_failure(f"device has {neighbor_count} neighbors (expected {number})")

        not_full_neighbors = _get_not_full_ospf_neighbors(command_output)
        print(not_full_neighbors)
        if not_full_neighbors:
            self.result.is_failure(f"Some neighbors are not correctly configured: {not_full_neighbors}.")

test

test(number: Optional[int] = None) -> None

Run VerifyOSPFNeighborCount validation

Source code in anta/tests/routing/ospf.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@AntaTest.anta_test
def test(self, number: Optional[int] = None) -> None:
    """Run VerifyOSPFNeighborCount validation"""
    if not (isinstance(number, int) and number >= 0):
        self.result.is_skipped(f"VerifyOSPFNeighborCount was not run as the number given '{number}' is not a valid value.")
        return

    command_output = self.instance_commands[0].json_output

    if (neighbor_count := _count_ospf_neighbor(command_output)) == 0:
        self.result.is_skipped("no OSPF neighbor found")
        return

    self.result.is_success()

    if neighbor_count != number:
        self.result.is_failure(f"device has {neighbor_count} neighbors (expected {number})")

    not_full_neighbors = _get_not_full_ospf_neighbors(command_output)
    print(not_full_neighbors)
    if not_full_neighbors:
        self.result.is_failure(f"Some neighbors are not correctly configured: {not_full_neighbors}.")

VerifyOSPFNeighborState

Bases: AntaTest

Verifies all OSPF neighbors are in FULL state.

Source code in anta/tests/routing/ospf.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class VerifyOSPFNeighborState(AntaTest):
    """
    Verifies all OSPF neighbors are in FULL state.
    """

    name = "VerifyOSPFNeighborState"
    description = "Verifies all OSPF neighbors are in FULL state."
    categories = ["routing", "ospf"]
    commands = [AntaCommand(command="show ip ospf neighbor")]

    @AntaTest.anta_test
    def test(self) -> None:
        """Run VerifyOSPFNeighborState validation"""

        command_output = self.instance_commands[0].json_output

        if _count_ospf_neighbor(command_output) == 0:
            self.result.is_skipped("no OSPF neighbor found")
            return

        self.result.is_success()

        not_full_neighbors = _get_not_full_ospf_neighbors(command_output)
        if not_full_neighbors:
            self.result.is_failure(f"Some neighbors are not correctly configured: {not_full_neighbors}.")

test

test() -> None

Run VerifyOSPFNeighborState validation

Source code in anta/tests/routing/ospf.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@AntaTest.anta_test
def test(self) -> None:
    """Run VerifyOSPFNeighborState validation"""

    command_output = self.instance_commands[0].json_output

    if _count_ospf_neighbor(command_output) == 0:
        self.result.is_skipped("no OSPF neighbor found")
        return

    self.result.is_success()

    not_full_neighbors = _get_not_full_ospf_neighbors(command_output)
    if not_full_neighbors:
        self.result.is_failure(f"Some neighbors are not correctly configured: {not_full_neighbors}.")

Last update: July 19, 2023