Coverage for birdplan/bird_config/sections/protocols/static.py: 99%
70 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-23 03:27 +0000
« 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/>.
19"""BIRD static protocol configuration."""
21from typing import Dict
23from ....exceptions import BirdPlanError
24from ...globals import BirdConfigGlobals
25from ..constants import SectionConstants
26from ..functions import SectionFunctions
27from ..tables import SectionTables
28from .base import SectionProtocolBase
29from .pipe import ProtocolPipe
31__all__ = ["ProtocolStatic"]
34StaticRoutes = Dict[str, str]
37class ProtocolStatic(SectionProtocolBase):
38 """BIRD static protocol configuration."""
40 _routes: StaticRoutes
42 def __init__(
43 self, birdconfig_globals: BirdConfigGlobals, constants: SectionConstants, functions: SectionFunctions, tables: SectionTables
44 ):
45 """Initialize the object."""
46 super().__init__(birdconfig_globals, constants, functions, tables)
48 # Set section header
49 self._section = "Static Protocol"
51 # Initialize our route list
52 self._routes = {}
54 def add_route(self, route: str) -> None:
55 """Add static route."""
56 (prefix, route_info) = route.split(" ", 1)
57 self.routes[prefix] = route_info
59 def configure(self) -> None:
60 """Configure the static protocol."""
61 super().configure()
63 # Work out static v4 and v6 routes
64 routes_ipv4 = []
65 routes_ipv6 = []
66 for prefix in sorted(self.routes.keys()):
67 info = self.routes[prefix]
68 if "." in prefix:
69 routes_ipv4.append(f"{prefix} {info}")
70 elif ":" in prefix:
71 routes_ipv6.append(f"{prefix} {info}")
72 else:
73 raise BirdPlanError(f"The static route '{prefix}' is odd")
75 self.tables.conf.append("# Static Protocol")
76 self.tables.conf.append("ipv4 table t_static4;")
77 self.tables.conf.append("ipv6 table t_static6;")
78 self.tables.conf.append("")
80 self.conf.add("protocol static static4 {")
81 self.conf.add(' description "Static protocol for IPv4";')
82 self.conf.add("")
83 # FIXME - remove at some stage # pylint:disable=fixme
84 self.conf.add("debug all;")
85 self.conf.add("")
86 self.conf.add(" ipv4 {")
87 self.conf.add(" table t_static4;")
88 self.conf.add(" export none;")
89 self.conf.add(" import all;")
90 self.conf.add(" };")
91 # If we have IPv4 routes
92 if routes_ipv4:
93 self.conf.add("")
94 # Output the routes
95 for route in routes_ipv4:
96 self.conf.add(f" route {route};")
97 self.conf.add("};")
98 self.conf.add("")
99 self.conf.add("protocol static static6 {")
100 self.conf.add(' description "Static protocol for IPv6";')
101 self.conf.add("")
102 self.conf.add(" ipv6 {")
103 self.conf.add(" table t_static6;")
104 self.conf.add(" export none;")
105 self.conf.add(" import all;")
106 self.conf.add(" };")
107 # If we have IPv6 routes
108 if routes_ipv6:
109 self.conf.add("")
110 # Output the routes
111 for route in routes_ipv6:
112 self.conf.add(f" route {route};")
113 self.conf.add("};")
114 self.conf.add("")
116 # Configure static route pipe to the kernel
117 static_kernel_pipe = ProtocolPipe(
118 birdconfig_globals=self.birdconfig_globals,
119 table_from="static",
120 table_to="master",
121 table_export="all",
122 table_import="none",
123 )
124 self.conf.add(static_kernel_pipe)
126 @property
127 def routes(self) -> StaticRoutes:
128 """Return our static routes."""
129 return self._routes