From 698e018a1952c78fe30e1ceb260ca7714731d44b Mon Sep 17 00:00:00 2001 From: Ivan Pepelnjak Date: Tue, 10 Oct 2023 08:52:01 +0200 Subject: [PATCH] Use _config_name plugin metadata instead of API call (implements #895) --- netsim/api/__init__.py | 8 ++ netsim/augment/plugin.py | 3 +- netsim/extra/ebgp.multihop/plugin.py | 8 +- tests/topology/expected/ebgp.utils.yml | 101 ++++++++++++++++--------- tests/topology/input/ebgp.utils.yml | 11 ++- 5 files changed, 88 insertions(+), 43 deletions(-) diff --git a/netsim/api/__init__.py b/netsim/api/__init__.py index d5473aae9..177658789 100644 --- a/netsim/api/__init__.py +++ b/netsim/api/__init__.py @@ -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: @@ -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',[]) diff --git a/netsim/augment/plugin.py b/netsim/augment/plugin.py index 86af2561c..56c23bf7b 100644 --- a/netsim/augment/plugin.py +++ b/netsim/augment/plugin.py @@ -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' @@ -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) diff --git a/netsim/extra/ebgp.multihop/plugin.py b/netsim/extra/ebgp.multihop/plugin.py index 35de0e922..ac104f440 100644 --- a/netsim/extra/ebgp.multihop/plugin.py +++ b/netsim/extra/ebgp.multihop/plugin.py @@ -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( @@ -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) ] diff --git a/tests/topology/expected/ebgp.utils.yml b/tests/topology/expected/ebgp.utils.yml index df7ffea00..7e1e24e7e 100644 --- a/tests/topology/expected/ebgp.utils.yml +++ b/tests/topology/expected/ebgp.utils.yml @@ -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: @@ -15,6 +35,8 @@ groups: as65002: members: - r2 + as65003: + members: - r3 input: - topology/input/ebgp.utils.yml @@ -22,11 +44,11 @@ input: 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 @@ -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 @@ -84,7 +106,7 @@ nodes: type: ebgp - activate: ipv4: true - as: 65002 + as: 65003 default_originate: true ifindex: 2 ipv4: 10.1.0.6 @@ -94,19 +116,19 @@ 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 @@ -114,7 +136,7 @@ nodes: - bgp: password: SomethingElse ifindex: 2 - ifname: eth2 + ifname: Ethernet2 ipv4: 10.1.0.5/30 linkindex: 2 name: r1 -> r3 @@ -122,7 +144,7 @@ nodes: - bgp: allowas_in: 1 password: SomethingElse - ifname: eth1 + ifname: Ethernet1 ipv4: 10.1.0.6/30 node: r3 role: external @@ -130,7 +152,7 @@ nodes: 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: @@ -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 @@ -164,22 +180,32 @@ 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 @@ -187,7 +213,7 @@ nodes: 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: @@ -198,7 +224,7 @@ nodes: ipv4: true bgp: advertise_loopback: true - as: 65002 + as: 65003 community: ebgp: - standard @@ -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 @@ -222,27 +242,37 @@ 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 @@ -250,7 +280,7 @@ nodes: 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: @@ -258,4 +288,5 @@ nodes: name: r3 plugin: - ebgp.utils +- ebgp.multihop provider: libvirt diff --git a/tests/topology/input/ebgp.utils.yml b/tests/topology/input/ebgp.utils.yml index 970ccf221..23993d138 100644 --- a/tests/topology/input/ebgp.utils.yml +++ b/tests/topology/input/ebgp.utils.yml @@ -1,11 +1,16 @@ # # 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 @@ -13,7 +18,7 @@ nodes: r2: bgp.as: 65002 r3: - bgp.as: 65002 + bgp.as: 65003 links: - r1: