Coverage for birdplan/plugins/cmdline/bgp/peer/graceful_shutdown/show.py: 45%
67 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"""BirdPlan commandline options for BGP peer graceful shutdown show."""
21import argparse
22import io
23from typing import Any, Dict
25from birdplan import BirdPlanBGPPeerGracefulShutdownStatus
27from ......cmdline import BirdPlanCommandLine, BirdPlanCommandlineResult
28from ......console.colors import colored
29from ....cmdline_plugin import BirdPlanCmdlinePluginBase
31__all__ = ["BirdPlanCmdlineBGPPeerGracefulShutdownShow"]
34class BirdPlanCmdlineBGPPeerGracefulShutdownShowResult(BirdPlanCommandlineResult):
35 """BirdPlan BGP peer graceful shutdown show result."""
37 def as_text(self) -> str: # pylint: disable= too-many-branches
38 """
39 Return data in text format.
41 Returns
42 -------
43 str
44 Return data in text format.
46 """
48 ob = io.StringIO()
50 ob.write("BGP peer graceful shutdown overrides:\n")
51 ob.write("-------------------------------------\n")
53 # Loop with sorted override list
54 for peer in sorted(self.data["overrides"]):
55 # Print out override
56 status = colored("Enabled", "red") if self.data["overrides"][peer] else colored("Disabled", "green")
57 ob.write(f" {peer}: {status}\n")
58 # If we have no overrides, just print out --none--
59 if not self.data["overrides"]:
60 ob.write("--none--\n")
62 ob.write("\n")
64 # Get a list of all peers we know about
65 peers_all = list(self.data["current"].keys()) + list(self.data["pending"].keys())
66 peers_all = sorted(set(peers_all))
68 ob.write("BGP peer graceful shutdown status:\n")
69 ob.write("----------------------------------\n")
71 # Loop with sorted peer list
72 for peer in peers_all:
73 # Grab pending status
74 pending_status = None
75 if peer in self.data["pending"]: # noqa: SIM908
76 pending_status = self.data["pending"][peer]
78 # Grab current status
79 current_status = None
80 if peer in self.data["current"]: # noqa: SIM908
81 current_status = self.data["current"][peer]
83 # Work out our status string
84 status_str = ""
85 if pending_status is None:
86 status_str = colored("REMOVED", "magenta")
87 else:
88 if current_status and not pending_status:
89 status_str = colored("PENDING-GRACEFUL-SHUTDOWN-ENTER", "blue")
90 elif not current_status and pending_status:
91 status_str = colored("PENDING-GRACEFUL-SHUTDOWN-EXIT", "yellow")
92 elif current_status is None:
93 status_str = colored("NEW", "green")
94 elif pending_status:
95 status_str = colored("GRACEFUL-SHUTDOWN", "red")
96 else:
97 status_str = "OK"
99 ob.write(" Peer: " + colored(peer, "cyan") + "\n")
100 ob.write(f" State: {status_str}\n")
101 ob.write("\n")
103 return ob.getvalue()
106class BirdPlanCmdlineBGPPeerGracefulShutdownShow(BirdPlanCmdlinePluginBase):
107 """BirdPlan "bgp peer graceful-shutdown show" command."""
109 def __init__(self) -> None:
110 """Initialize object."""
112 super().__init__()
114 # Plugin setup
115 self.plugin_description = "birdplan bgp peer graceful-shutdown show"
116 self.plugin_order = 30
118 def register_parsers(self, args: Dict[str, Any]) -> None:
119 """
120 Register commandline parsers.
122 Parameters
123 ----------
124 args : Dict[str, Any]
125 Method argument(s).
127 """
129 plugins = args["plugins"]
131 parent_subparsers = plugins.call_plugin("birdplan.plugins.cmdline.bgp.peer.graceful_shutdown", "get_subparsers", {})
133 # CMD: bgp peer graceful-shutdown show
134 subparser = parent_subparsers.add_parser("show", help="Show BGP peer graceful shutdown status")
135 subparser.add_argument(
136 "--action",
137 action="store_const",
138 const="bgp_peer_graceful_shutdown_show",
139 default="bgp_peer_graceful_shutdown_show",
140 help=argparse.SUPPRESS,
141 )
143 # Set our internal subparser property
144 self._subparser = subparser
145 self._subparsers = None
147 def cmd_bgp_peer_graceful_shutdown_show(self, args: Any) -> Any:
148 """
149 Commandline handler for "bgp peer graceful-shutdown show" action.
151 Parameters
152 ----------
153 args : Dict[str, Any]
154 Method argument(s).
156 """
158 if not self._subparser:
159 raise RuntimeError()
161 cmdline: BirdPlanCommandLine = args["cmdline"]
163 # Suppress info output
164 cmdline.birdplan.birdconf.birdconfig_globals.suppress_info = True
166 # Load BirdPlan configuration using the cache
167 cmdline.birdplan_load_config(ignore_irr_changes=True, ignore_peeringdb_changes=True, use_cached=True)
169 # Grab peer list
170 res: BirdPlanBGPPeerGracefulShutdownStatus = cmdline.birdplan.state_bgp_peer_graceful_shutdown_status()
172 return BirdPlanCmdlineBGPPeerGracefulShutdownShowResult(res)