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

58 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-23 03:27 +0000

1# 

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

3# 

4# Copyright (c) 2019-2024, 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 typing import List, Optional 

22 

23from ...globals import BirdConfigGlobals 

24from ..constants import SectionConstants 

25from ..functions import SectionFunctions 

26from ..tables import SectionTables 

27from .base import SectionProtocolBase 

28 

29__all__ = ["ProtocolDirect"] 

30 

31 

32class ProtocolDirect(SectionProtocolBase): 

33 """BIRD direct protocol configuration.""" 

34 

35 _name_suffix: str 

36 _interfaces: List[str] 

37 

38 def __init__( # noqa: CFQ002 # pylint: disable=too-many-arguments 

39 self, 

40 birdconfig_globals: BirdConfigGlobals, 

41 constants: SectionConstants, 

42 functions: SectionFunctions, 

43 tables: SectionTables, 

44 name: str = "", 

45 interfaces: Optional[List[str]] = None, 

46 ): 

47 """Initialize the object.""" 

48 super().__init__(birdconfig_globals, 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 = [] 

72 for interface in self.interfaces: 

73 interface_list.append(f'"{interface}"') 

74 # Drop in a comma between them 

75 interface_str = ", ".join(interface_list) 

76 interface_lines.append("") 

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

78 

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

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

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

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

83 

84 self._setup_protocol("4", interface_lines) 

85 self._setup_protocol("6", interface_lines) 

86 

87 def _setup_protocol(self, ipv: str, lines: List[str]) -> None: 

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

89 

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

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

92 self.conf.add("") 

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

94 self.conf.add("") 

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

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

97 self.conf.add("") 

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

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

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

101 self.conf.add(lines) 

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

103 self.conf.add("") 

104 

105 @property 

106 def name_suffix(self) -> str: 

107 """Return our name suffix.""" 

108 return self._name_suffix 

109 

110 @property 

111 def interfaces(self) -> List[str]: 

112 """Return our interfaces.""" 

113 return self._interfaces