Skip to content

Commit

Permalink
Use _config_name plugin metadata instead of API call (implements #895)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipspace committed Oct 10, 2023
1 parent 6ab6529 commit 698e018
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 43 deletions.
8 changes: 8 additions & 0 deletions netsim/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from ..data.validate import must_be_list
from ..utils import log,strings

'''
get_config_name: get the name of configuration to be added to node.config attribute
This function is deprecated. Plugins should use 'global _config_name' instead.
'''
def get_config_name(g: dict) -> typing.Optional[str]:
config_name = g.get('config_name',None)
if config_name:
Expand All @@ -16,6 +21,9 @@ def get_config_name(g: dict) -> typing.Optional[str]:
log.fatal("Cannot get configuration template name for plugin %s" % g.get('__file__'),'plugin')
return None

'''
node_config: add custom configuration item to the node.config list
'''
def node_config(node: Box, config_name: typing.Optional[str]) -> None:
if config_name:
config = node.get('config',[])
Expand Down
3 changes: 2 additions & 1 deletion netsim/augment/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def load_plugin_from_path(path: str, plugin: str, topology: Box) -> typing.Optio

if config_name:
setattr(pymodule,'config_name',config_name)
setattr(pymodule,'_config_name',config_name)

if plugin_is_dir:
defaults_file = dir_path + '/defaults.yml'
Expand Down Expand Up @@ -147,5 +148,5 @@ def execute(action: str, topology: Box) -> None:
if hasattr(plugin,action):
func = getattr(plugin,action)
if log.debug_active('plugin'):
print(f'plug INIT: {topology.Plugin}')
print(f'plug {action}: {plugin}')
func(topology)
8 changes: 4 additions & 4 deletions netsim/extra/ebgp.multihop/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
_execute_after = [ 'ebgp.utils', 'bgp.session' ]

def pre_transform(topology: Box) -> None:
config_name = api.get_config_name(globals()) # Get the plugin configuration name
global _config_name
session_idx = data.find_in_list(['ebgp.utils','bgp.session'],topology.plugin)
multihop_idx = data.find_in_list([ config_name ],topology.plugin)
multihop_idx = data.find_in_list([ _config_name ],topology.plugin)

if session_idx is not None and session_idx > multihop_idx:
log.error(
Expand Down Expand Up @@ -132,14 +132,14 @@ def augment_af_activation(ndata: Box, topology: Box) -> None:
* Augment address family activation
'''
def post_transform(topology: Box) -> None:
config_name = api.get_config_name(globals()) # Get the plugin configuration name
global _config_name
for ndata in topology.nodes.values():
intf_count = len(ndata.interfaces)
ndata.interfaces = [ intf for intf in ndata.interfaces if not intf.get('_bgp_session',None) ]

if len(ndata.interfaces) != intf_count: # Did the interface count change?
check_multihop_support(ndata,topology)
api.node_config(ndata,config_name) # We must have some multihop sessions, add extra config
api.node_config(ndata,_config_name) # We must have some multihop sessions, add extra config
augment_af_activation(ndata,topology)

topology.links = [ link for link in topology.links if not link.get('_bgp_session',None) ]
101 changes: 66 additions & 35 deletions tests/topology/expected/ebgp.utils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ bgp:
ibgp:
- standard
- extended
multihop:
sessions:
- _linkname: bgp.multihop[1]
bgp:
password: Funny
interfaces:
- _bgp_session: true
bgp:
multihop: 255
ifindex: 20000
ifname: Tunnel0
ipv4: 10.0.0.2/32
node: r2
- _bgp_session: true
bgp:
multihop: 255
ifindex: 20000
ifname: Tunnel0
ipv4: 10.0.0.3/32
node: r3
next_hop_self: true
password: Secret
groups:
Expand All @@ -15,18 +35,20 @@ groups:
as65002:
members:
- r2
as65003:
members:
- r3
input:
- topology/input/ebgp.utils.yml
- package:topology-defaults.yml
links:
- interfaces:
- ifindex: 1
ifname: eth1
ifname: Ethernet1
ipv4: 10.1.0.1/30
node: r1
- ifindex: 1
ifname: eth1
ifname: Ethernet1
ipv4: 10.1.0.2/30
node: r2
linkindex: 1
Expand All @@ -39,13 +61,13 @@ links:
password: SomethingElse
interfaces:
- ifindex: 2
ifname: eth2
ifname: Ethernet2
ipv4: 10.1.0.5/30
node: r1
- bgp:
allowas_in: 1
ifindex: 1
ifname: eth1
ifname: Ethernet1
ipv4: 10.1.0.6/30
node: r3
linkindex: 2
Expand Down Expand Up @@ -84,7 +106,7 @@ nodes:
type: ebgp
- activate:
ipv4: true
as: 65002
as: 65003
default_originate: true
ifindex: 2
ipv4: 10.1.0.6
Expand All @@ -94,43 +116,43 @@ nodes:
next_hop_self: true
password: Secret
router_id: 10.0.0.1
box: vyos/current
box: arista/veos
config:
- ebgp.utils
device: vyos
device: eos
id: 1
interfaces:
- ifindex: 1
ifname: eth1
ifname: Ethernet1
ipv4: 10.1.0.1/30
linkindex: 1
name: r1 -> r2
neighbors:
- ifname: eth1
- ifname: Ethernet1
ipv4: 10.1.0.2/30
node: r2
role: external
type: p2p
- bgp:
password: SomethingElse
ifindex: 2
ifname: eth2
ifname: Ethernet2
ipv4: 10.1.0.5/30
linkindex: 2
name: r1 -> r3
neighbors:
- bgp:
allowas_in: 1
password: SomethingElse
ifname: eth1
ifname: Ethernet1
ipv4: 10.1.0.6/30
node: r3
role: external
type: p2p
loopback:
ipv4: 10.0.0.1/32
mgmt:
ifname: eth0
ifname: Management1
ipv4: 192.168.121.101
mac: 08:4f:a9:00:00:01
module:
Expand All @@ -150,12 +172,6 @@ nodes:
- extended
ipv4: true
neighbors:
- activate:
ipv4: true
as: 65002
ipv4: 10.0.0.3
name: r3
type: ibgp
- activate:
ipv4: true
as: 65001
Expand All @@ -164,30 +180,40 @@ nodes:
name: r1
password: Secret
type: ebgp
- activate:
ipv4: true
as: 65003
ifindex: 20000
ipv4: 10.0.0.3
multihop: 255
name: r3
password: Secret
type: ebgp
next_hop_self: true
password: Secret
router_id: 10.0.0.2
box: vyos/current
box: arista/veos
config:
- ebgp.utils
device: vyos
- ebgp.multihop
device: eos
id: 2
interfaces:
- ifindex: 1
ifname: eth1
ifname: Ethernet1
ipv4: 10.1.0.2/30
linkindex: 1
name: r2 -> r1
neighbors:
- ifname: eth1
- ifname: Ethernet1
ipv4: 10.1.0.1/30
node: r1
role: external
type: p2p
loopback:
ipv4: 10.0.0.2/32
mgmt:
ifname: eth0
ifname: Management1
ipv4: 192.168.121.102
mac: 08:4f:a9:00:00:02
module:
Expand All @@ -198,7 +224,7 @@ nodes:
ipv4: true
bgp:
advertise_loopback: true
as: 65002
as: 65003
community:
ebgp:
- standard
Expand All @@ -207,12 +233,6 @@ nodes:
- extended
ipv4: true
neighbors:
- activate:
ipv4: true
as: 65002
ipv4: 10.0.0.2
name: r2
type: ibgp
- activate:
ipv4: true
allowas_in: 1
Expand All @@ -222,40 +242,51 @@ nodes:
name: r1
password: SomethingElse
type: ebgp
- activate:
ipv4: true
as: 65002
ifindex: 20000
ipv4: 10.0.0.2
multihop: 255
name: r2
password: Secret
type: ebgp
next_hop_self: true
password: Secret
router_id: 10.0.0.3
box: vyos/current
box: arista/veos
config:
- ebgp.utils
device: vyos
- ebgp.multihop
device: eos
id: 3
interfaces:
- bgp:
allowas_in: 1
password: SomethingElse
ifindex: 1
ifname: eth1
ifname: Ethernet1
ipv4: 10.1.0.6/30
linkindex: 2
name: r3 -> r1
neighbors:
- bgp:
password: SomethingElse
ifname: eth2
ifname: Ethernet2
ipv4: 10.1.0.5/30
node: r1
role: external
type: p2p
loopback:
ipv4: 10.0.0.3/32
mgmt:
ifname: eth0
ifname: Management1
ipv4: 192.168.121.103
mac: 08:4f:a9:00:00:03
module:
- bgp
name: r3
plugin:
- ebgp.utils
- ebgp.multihop
provider: libvirt
11 changes: 8 additions & 3 deletions tests/topology/input/ebgp.utils.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#
# Testing ebgp-utils plugin
#
plugin: [ ebgp.utils ]
plugin: [ ebgp.multihop,ebgp.utils ]
module: [ bgp ]
defaults.device: vyos
defaults.device: eos
bgp.password: Secret

bgp.multihop.sessions:
- r2:
r3:
bgp.password: Funny

nodes:
r1:
bgp.as: 65001
bgp.default_originate: True
r2:
bgp.as: 65002
r3:
bgp.as: 65002
bgp.as: 65003

links:
- r1:
Expand Down

0 comments on commit 698e018

Please sign in to comment.