Coverage for src/birdplan/bird_config/sections/protocols/direct.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-25 07:38 +0000

1# 

2# SPDX-License-Identifier: GPL-3.0-or-later 

3# 

4# Copyright (c) 2019-2025, AllWorldIT 

5# 

6# This program is free software: you can redistribute it and/or modify 

7# it under the terms of the GNU General Public License as published by 

8# the Free Software Foundation, either version 3 of the License, or 

9# (at your option) any later version. 

10# 

11# This program is distributed in the hope that it will be useful, 

12# but WITHOUT ANY WARRANTY; without even the implied warranty of 

13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14# GNU General Public License for more details. 

15# 

16# You should have received a copy of the GNU General Public License 

17# along with this program. If not, see <http://www.gnu.org/licenses/>. 

18 

19"""BIRD direct protocol configuration.""" 

20 

21from ...globals import BirdConfigGlobals 

22from ..bird_attributes import SectionBirdAttributes 

23from ..constants import SectionConstants 

24from ..functions import SectionFunctions 

25from ..tables import SectionTables 

26from .base import SectionProtocolBase 

27 

28__all__ = ["ProtocolDirect"] 

29 

30 

31class ProtocolDirect(SectionProtocolBase): 

32 """BIRD direct protocol configuration.""" 

33 

34 _name_suffix: str 

35 _interfaces: list[str] 

36 

37 def __init__( # noqa: PLR0913,PLR0917 

38 self, 

39 birdconfig_globals: BirdConfigGlobals, 

40 birdattributes: SectionBirdAttributes, 

41 constants: SectionConstants, 

42 functions: SectionFunctions, 

43 tables: SectionTables, 

44 name: str = "", 

45 interfaces: list[str] | None = None, 

46 ) -> None: 

47 """Initialize the object.""" 

48 super().__init__(birdconfig_globals, birdattributes, constants, functions, tables) 

49 

50 # Add a suffix if we have a name 

51 if name: 

52 self._name_suffix = f"_{name}" 

53 else: 

54 self._section = "Direct Protocol" 

55 self._name_suffix = "" 

56 

57 # Grab the list of interfaces we need 

58 if interfaces: 

59 self._interfaces = interfaces 

60 else: 

61 self._interfaces = [] 

62 

63 def configure(self) -> None: 

64 """Configure the direct protocol.""" 

65 super().configure() 

66 

67 # If we have a list of interfaces, create the config lines 

68 interface_lines = [] 

69 if self.interfaces: 

70 # Create list of quoted interfaces 

71 interface_list = [f'"{interface}"' for interface in self.interfaces] 

72 # Drop in a comma between them 

73 interface_str = ", ".join(interface_list) 

74 interface_lines.append("") 

75 interface_lines.append(f" interface {interface_str};") 

76 

77 self.tables.conf.append(f"# Direct Tables: {self.name_suffix}") 

78 self.tables.conf.append(f"ipv4 table t_direct4{self.name_suffix};") 

79 self.tables.conf.append(f"ipv6 table t_direct6{self.name_suffix};") 

80 self.tables.conf.append("") 

81 

82 self._setup_protocol("4", interface_lines) 

83 self._setup_protocol("6", interface_lines) 

84 

85 def _setup_protocol(self, ipv: str, lines: list[str]) -> None: 

86 protocol_name = f"direct{ipv}{self.name_suffix}" 

87 

88 self.conf.add(f"protocol direct {protocol_name} {{") 

89 self.conf.add(f' description "Direct protocol for IPv{ipv}";') 

90 self.conf.add("") 

91 self.conf.add(f" vrf {self.birdconfig_globals.vrf};") 

92 self.conf.add("") 

93 self.conf.add(f" ipv{ipv} {{") 

94 self.conf.add(f" table t_{protocol_name};") 

95 self.conf.add("") 

96 self.conf.add(" export none;") 

97 self.conf.add(" import all;") 

98 self.conf.add(" };") 

99 self.conf.add(lines) 

100 self.conf.add("};") 

101 self.conf.add("") 

102 

103 @property 

104 def name_suffix(self) -> str: 

105 """Return our name suffix.""" 

106 return self._name_suffix 

107 

108 @property 

109 def interfaces(self) -> list[str]: 

110 """Return our interfaces.""" 

111 return self._interfaces