Skip to content

Commit 862ac64

Browse files
authored
Refactor "clear BGP session" logic from ipspace#2630 (ipspace#2638)
1 parent f9ed607 commit 862ac64

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

netsim/extra/bgp.policy/plugin.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,7 @@ def check_attribute_direction(ndata: Box, ngb: Box, topology: Box, attr: str, at
159159
def apply_config(node: Box, ngb: Box) -> None:
160160
global _config_name
161161
api.node_config(node,_config_name) # Remember that we have to do extra configuration
162-
for af in ('ipv4','ipv6'): # ... and add sessions that have to be cleared
163-
if af in ngb and '_src_vrf' not in ngb:
164-
data.append_to_list(node.bgp,'_session_clear',ngb[af])
165-
elif af in ngb and '_src_vrf' in ngb:
166-
for vname,vdata in node.vrfs.items():
167-
if vname == ngb['_src_vrf']:
168-
data.append_to_list(node.vrfs[vname].bgp,'_session_clear',ngb[af])
162+
_bgp.clear_bgp_session(node,ngb)
169163

170164
'''
171165
Apply attributes supported by bgp.policy plugin to a single neighbor

netsim/extra/bgp.session/plugin.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from box import Box
44

5-
from netsim import api, data, modules
5+
from netsim import api, modules
66
from netsim.utils import log
77
from netsim.utils import routing as _bgp
88

@@ -34,13 +34,7 @@ def mark_plugin_config(node: Box, ngb: Box) -> None:
3434
global _config_name
3535

3636
api.node_config(node,_config_name) # Remember that we have to do extra configuration
37-
for af in ('ipv4','ipv6'): # ... and add sessions that have to be cleared
38-
if af in ngb and '_src_vrf' not in ngb:
39-
data.append_to_list(node.bgp,'_session_clear',ngb[af])
40-
elif af in ngb and '_src_vrf' in ngb:
41-
for vname,vdata in node.vrfs.items():
42-
if vname == ngb['_src_vrf']:
43-
data.append_to_list(node.vrfs[vname].bgp,'_session_clear',ngb[af])
37+
_bgp.clear_bgp_session(node,ngb)
4438

4539
'''
4640
Apply attributes supported by bgp.session plugin to a single neighbor

netsim/utils/routing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from box import Box
99

10+
from .. import data
1011
from ..augment import devices
1112
from . import log
1213

@@ -68,6 +69,19 @@ def intf_neighbors(node: Box, vrf: bool = True, select: list = ['ibgp','ebgp'])
6869
if ngb.get('ifindex',None) == intf.ifindex and ngb.type in select:
6970
yield (intf,ngb)
7071

72+
'''
73+
Mark a BGP session that needs to be cleared in the configuration template
74+
'''
75+
def clear_bgp_session(node: Box, ngb: Box) -> None:
76+
for af in log.AF_LIST: # Check all relevant address families
77+
if af not in ngb: # ... neighbor not using this AF, move on
78+
continue
79+
80+
# Otherwise, add the neighbor address to the global- or VRF bgp._session_clear list
81+
#
82+
bgp_data = node.bgp if '_src_vrf' not in ngb else node.vrfs[ngb._src_vrf].bgp
83+
data.append_to_list(bgp_data,'_session_clear',ngb[af])
84+
7185
'''
7286
rp_data: iterate over routing protocol instances (global and VRF)
7387
'''

0 commit comments

Comments
 (0)