Support advertised networks in bgp protocol (#34728)

Signed-off-by: Samer Deeb <samerd@mellanox.com>
This commit is contained in:
Samer Deeb 2018-01-11 09:40:39 -08:00 committed by John R Barker
parent fcf6da0a9d
commit cb206abea3
4 changed files with 123 additions and 30 deletions

View file

@ -141,7 +141,7 @@ def get_interfaces_config(module, interface_type, flags=None, json_fmt=True):
def get_bgp_summary(module): def get_bgp_summary(module):
cmd = "show ip bgp summary" cmd = "show running-config protocol bgp"
return show_cmd(module, cmd, json_fmt=False, fail_on_error=False) return show_cmd(module, cmd, json_fmt=False, fail_on_error=False)

View file

@ -41,6 +41,9 @@ options:
description: description:
- Neighbor IP address. - Neighbor IP address.
required: true required: true
networks:
description:
- List of advertised networks.
state: state:
description: description:
- BGP state. - BGP state.
@ -57,6 +60,8 @@ EXAMPLES = """
- remote_as: 321 - remote_as: 321
neighbor: 10.3.3.4 neighbor: 10.3.3.4
state: present state: present
networks:
- 172.16.1.0/24
""" """
RETURN = """ RETURN = """
@ -69,6 +74,7 @@ commands:
- exit - exit
- router bgp 172 router-id 2.3.4.5 force - router bgp 172 router-id 2.3.4.5 force
- router bgp 172 neighbor 2.3.4.6 remote-as 173 - router bgp 172 neighbor 2.3.4.6 remote-as 173
- router bgp 172 network 172.16.1.0 /24
""" """
import re import re
@ -80,10 +86,13 @@ from ansible.module_utils.network.mlnxos.mlnxos import BaseMlnxosModule
class MlnxosBgpModule(BaseMlnxosModule): class MlnxosBgpModule(BaseMlnxosModule):
LOCAL_AS_REGEX = \ LOCAL_AS_REGEX = re.compile(r'^\s+router bgp\s+(\d+).*')
r"BGP router identifier ([0-9\.]+), local AS number (\d+)" ROUTER_ID_REGEX = re.compile(
NEIGHBOR_REGEX = \ r'^\s+router bgp\s+(\d+).*router-id\s+(\S+)\s+.*')
r"^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+\d+\s+(\d+)" NEIGHBOR_REGEX = re.compile(
r'^\s+router bgp\s+(\d+).*neighbor\s+(\S+)\s+remote\-as\s+(\S+).*')
NETWORK_REGEX = re.compile(
r'^\s+router bgp\s+(\d+).*network\s+(\S+)\s+(\S+).*')
def init_module(self): def init_module(self):
""" initialize module """ initialize module
@ -97,6 +106,7 @@ class MlnxosBgpModule(BaseMlnxosModule):
router_id=dict(), router_id=dict(),
neighbors=dict(type='list', elements='dict', neighbors=dict(type='list', elements='dict',
options=neighbor_spec), options=neighbor_spec),
networks=dict(type='list', elements='str'),
state=dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
) )
argument_spec = dict() argument_spec = dict()
@ -108,28 +118,47 @@ class MlnxosBgpModule(BaseMlnxosModule):
def get_required_config(self): def get_required_config(self):
module_params = self._module.params module_params = self._module.params
bgp_params = { req_neighbors = list()
'as_number': module_params['as_number'], self._required_config = dict(
'router_id': module_params['router_id'], as_number=module_params['as_number'],
'state': module_params['state'], router_id=module_params['router_id'],
} state=module_params['state'],
neighbors = module_params['neighbors'] or [] neighbors=req_neighbors,
req_neighbors = bgp_params['neighbors'] = [] networks=module_params['networks'])
neighbors = module_params['neighbors'] or list()
for neighbor_data in neighbors: for neighbor_data in neighbors:
req_neighbors.append( req_neighbors.append(
(neighbor_data['neighbor'], neighbor_data['remote_as'])) (neighbor_data['neighbor'], neighbor_data['remote_as']))
self.validate_param_values(bgp_params) self.validate_param_values(self._required_config)
self._required_config = bgp_params
def _set_bgp_config(self, bgp_config): def _set_bgp_config(self, bgp_config):
match = re.search(self.LOCAL_AS_REGEX, bgp_config, re.M) lines = bgp_config.split('\n')
if match: self._current_config['router_id'] = None
self._current_config['router_id'] = match.group(1) self._current_config['as_number'] = None
self._current_config['as_number'] = int(match.group(2))
matches = re.findall(self.NEIGHBOR_REGEX, bgp_config, re.M) or []
neighbors = self._current_config['neighbors'] = [] neighbors = self._current_config['neighbors'] = []
for match in matches: networks = self._current_config['networks'] = []
neighbors.append((match[0], int(match[1]))) for line in lines:
if line.startswith('#'):
continue
if not self._current_config['as_number']:
match = self.LOCAL_AS_REGEX.match(line)
if match:
self._current_config['as_number'] = int(match.group(1))
continue
if not self._current_config['router_id']:
match = self.ROUTER_ID_REGEX.match(line)
if match:
self._current_config['router_id'] = match.group(2)
continue
match = self.NEIGHBOR_REGEX.match(line)
if match:
neighbors.append((match.group(2), int(match.group(3))))
continue
match = self.NETWORK_REGEX.match(line)
if match:
network = match.group(2) + match.group(3)
networks.append(network)
continue
def _get_bgp_summary(self): def _get_bgp_summary(self):
return get_bgp_summary(self._module) return get_bgp_summary(self._module)
@ -162,7 +191,10 @@ class MlnxosBgpModule(BaseMlnxosModule):
if req_router_id and req_router_id != curr_route_id or bgp_removed: if req_router_id and req_router_id != curr_route_id or bgp_removed:
self._commands.append('router bgp %d router-id %s force' % self._commands.append('router bgp %d router-id %s force' %
(as_number, req_router_id)) (as_number, req_router_id))
self._generate_neighbors_cmds(as_number, bgp_removed)
self._generate_networks_cmds(as_number, bgp_removed)
def _generate_neighbors_cmds(self, as_number, bgp_removed):
req_neighbors = self._required_config['neighbors'] req_neighbors = self._required_config['neighbors']
curr_neighbors = self._current_config.get('neighbors', []) curr_neighbors = self._current_config.get('neighbors', [])
if not bgp_removed: if not bgp_removed:
@ -180,6 +212,33 @@ class MlnxosBgpModule(BaseMlnxosModule):
'router bgp %s neighbor %s remote-as %s' % 'router bgp %s neighbor %s remote-as %s' %
(as_number, neighbor, remote_as)) (as_number, neighbor, remote_as))
def _generate_networks_cmds(self, as_number, bgp_removed):
req_networks = self._required_config['networks'] or []
curr_networks = self._current_config.get('networks', [])
if not bgp_removed:
for network in curr_networks:
if network not in req_networks:
net_attrs = network.split('/')
if len(net_attrs) != 2:
self._module.fail_json(
msg='Invalid network %s' % network)
net_address, netmask = net_attrs
cmd = 'router bgp %s no network %s /%s' % (
as_number, net_address, netmask)
self._commands.append(cmd)
for network in req_networks:
if bgp_removed or network not in curr_networks:
net_attrs = network.split('/')
if len(net_attrs) != 2:
self._module.fail_json(
msg='Invalid network %s' % network)
net_address, netmask = net_attrs
cmd = 'router bgp %s network %s /%s' % (
as_number, net_address, netmask)
self._commands.append(cmd)
def _generate_no_bgp_cmds(self): def _generate_no_bgp_cmds(self):
as_number = self._required_config['as_number'] as_number = self._required_config['as_number']
curr_as_num = self._current_config.get('as_number') curr_as_num = self._current_config.get('as_number')

View file

@ -1,6 +1,23 @@
BGP router identifier 1.2.3.4, local AS number 172 ##
BGP table version is 1, main routing table version 1 ## Running database "initial"
-------- - -- ------- ------- ------ --- ---- ------- ------------ ## Generated at 2018/01/10 23:13:17 +0000
Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd ## Hostname: r-neo-sw12
-------- - -- ------- ------- ------ --- ---- ------- ------------ ##
10.2.3.4 4 173 0 0 1 0 0 Never ACTIVE
##
## Running-config temporary prefix mode setting
##
no cli default prefix-modes enable
##
## BGP configuration
##
protocol bgp
router bgp 172 vrf default
router bgp 172 vrf default router-id 1.2.3.4 force
router bgp 172 vrf default neighbor 10.2.3.4 remote-as 173
router bgp 172 vrf default network 172.16.1.0 /24
##
## Persistent prefix mode setting
##
cli default prefix-modes enable

View file

@ -39,7 +39,8 @@ class TestMlnxosBgpModule(TestMlnxosModule):
def test_bgp_no_change(self): def test_bgp_no_change(self):
neighbor = dict(remote_as=173, neighbor='10.2.3.4') neighbor = dict(remote_as=173, neighbor='10.2.3.4')
set_module_args(dict(as_number=172, router_id='1.2.3.4', set_module_args(dict(as_number=172, router_id='1.2.3.4',
neighbors=[neighbor])) neighbors=[neighbor],
networks=['172.16.1.0/24']))
self.execute_module(changed=False) self.execute_module(changed=False)
def test_bgp_remove(self): def test_bgp_remove(self):
@ -60,11 +61,27 @@ class TestMlnxosBgpModule(TestMlnxosModule):
neighbors = [dict(remote_as=173, neighbor='10.2.3.4'), neighbors = [dict(remote_as=173, neighbor='10.2.3.4'),
dict(remote_as=175, neighbor='10.2.3.5')] dict(remote_as=175, neighbor='10.2.3.5')]
set_module_args(dict(as_number=172, router_id='1.2.3.4', set_module_args(dict(as_number=172, router_id='1.2.3.4',
neighbors=neighbors)) neighbors=neighbors,
networks=['172.16.1.0/24']))
commands = ['router bgp 172 neighbor 10.2.3.5 remote-as 175'] commands = ['router bgp 172 neighbor 10.2.3.5 remote-as 175']
self.execute_module(changed=True, commands=commands) self.execute_module(changed=True, commands=commands)
def test_bgp_del_neighbor(self): def test_bgp_del_neighbor(self):
set_module_args(dict(as_number=172)) set_module_args(dict(as_number=172,
networks=['172.16.1.0/24']))
commands = ['router bgp 172 no neighbor 10.2.3.4 remote-as 173'] commands = ['router bgp 172 no neighbor 10.2.3.4 remote-as 173']
self.execute_module(changed=True, commands=commands) self.execute_module(changed=True, commands=commands)
def test_bgp_add_network(self):
neighbors = [dict(remote_as=173, neighbor='10.2.3.4')]
set_module_args(dict(as_number=172, router_id='1.2.3.4',
neighbors=neighbors,
networks=['172.16.1.0/24', '172.16.2.0/24']))
commands = ['router bgp 172 network 172.16.2.0 /24']
self.execute_module(changed=True, commands=commands)
def test_bgp_del_network(self):
neighbors = [dict(remote_as=173, neighbor='10.2.3.4')]
set_module_args(dict(as_number=172, neighbors=neighbors))
commands = ['router bgp 172 no network 172.16.1.0 /24']
self.execute_module(changed=True, commands=commands)