mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Adding Support For Vxlan In Onyx Switches (#55081)
* Adding Support For Vxlan In Onyx Switches Signed-off-by: Anas Badaha <anasb@mellanox.com> * Fix Pep8 Failures in onyx_vxlan.py Signed-off-by: Anas Badaha <anasb@mellanox.com> * Fix Pep8 Failures in onyx_vxlan phase 2 Signed-off-by: Anas Badaha <anasb@mellanox.com> * Fix Shippable failures Signed-off-by: Anas Badaha <anasb@mellanox.com> * Fix Samer's Comments on PR Signed-off-by: Anas Badaha <anasb@mellanox.com>
This commit is contained in:
parent
b24c037f62
commit
2cc91e26e0
4 changed files with 400 additions and 0 deletions
265
lib/ansible/modules/network/onyx/onyx_vxlan.py
Normal file
265
lib/ansible/modules/network/onyx/onyx_vxlan.py
Normal file
|
@ -0,0 +1,265 @@
|
|||
#!/usr/bin/python
|
||||
#
|
||||
# Copyright: Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
DOCUMENTATION = """
|
||||
---
|
||||
module: onyx_vxlan
|
||||
version_added: "2.8"
|
||||
author: "Anas Badaha (@anasb)"
|
||||
short_description: Configures Vxlan
|
||||
description:
|
||||
- This module provides declarative management of Vxlan configuration
|
||||
on Mellanox ONYX network devices.
|
||||
notes:
|
||||
- Tested on ONYX evpn_dev.031.
|
||||
- nve protocol must be enabled.
|
||||
options:
|
||||
nve_id:
|
||||
description:
|
||||
- nve interface ID.
|
||||
required: true
|
||||
loopback_id:
|
||||
description:
|
||||
- loopback interface ID.
|
||||
bgp:
|
||||
description:
|
||||
- configure bgp on nve interface.
|
||||
type: bool
|
||||
default: true
|
||||
mlag_tunnel_ip:
|
||||
description:
|
||||
- vxlan Mlag tunnel IP
|
||||
vni_vlan_list:
|
||||
description:
|
||||
- Each item in the list has two attributes vlan_id, vni_id.
|
||||
arp_suppression:
|
||||
description:
|
||||
- A flag telling if to configure arp suppression.
|
||||
type: bool
|
||||
default: false
|
||||
"""
|
||||
|
||||
EXAMPLES = """
|
||||
- name: configure Vxlan
|
||||
onyx_vxlan:
|
||||
nve_id: 1
|
||||
loopback_id: 1
|
||||
bgp: yes
|
||||
mlag-tunnel-ip: 100.0.0.1
|
||||
vni_vlan_list:
|
||||
- vlan_id: 10
|
||||
vni_id: 10010
|
||||
- vlan_id: 6
|
||||
vni_id: 10060
|
||||
arp_suppression: yes
|
||||
"""
|
||||
|
||||
RETURN = """
|
||||
commands:
|
||||
description: The list of configuration mode commands to send to the device.
|
||||
returned: always
|
||||
type: list
|
||||
sample:
|
||||
- interface nve 1
|
||||
- interface nve 1 vxlan source interface loopback 1
|
||||
- interface nve 1 nve controller bgp
|
||||
- interface nve 1 vxlan mlag-tunnel-ip 100.0.0.1
|
||||
- interface nve 1 nve vni 10010 vlan 10
|
||||
- interface nve 1 nve vni 10060 vlan 6
|
||||
- interface nve 1 nve neigh-suppression
|
||||
- interface vlan 6
|
||||
- interface vlan 10
|
||||
"""
|
||||
|
||||
import re
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.onyx.onyx import show_cmd
|
||||
from ansible.module_utils.network.onyx.onyx import BaseOnyxModule
|
||||
|
||||
|
||||
class OnyxVxlanModule(BaseOnyxModule):
|
||||
|
||||
LOOPBACK_REGEX = re.compile(r'^loopback (\d+).*')
|
||||
NVE_ID_REGEX = re.compile(r'^Interface NVE (\d+).*')
|
||||
|
||||
def init_module(self):
|
||||
""" initialize module
|
||||
"""
|
||||
vni_vlan_spec = dict(vlan_id=dict(type=int),
|
||||
vni_id=dict(type=int))
|
||||
element_spec = dict(
|
||||
nve_id=dict(type=int),
|
||||
loopback_id=dict(type=int),
|
||||
bgp=dict(default=True, type='bool'),
|
||||
mlag_tunnel_ip=dict(type='str'),
|
||||
vni_vlan_list=dict(type='list',
|
||||
elements='dict',
|
||||
options=vni_vlan_spec),
|
||||
arp_suppression=dict(default=False, type='bool')
|
||||
)
|
||||
argument_spec = dict()
|
||||
argument_spec.update(element_spec)
|
||||
self._module = AnsibleModule(
|
||||
argument_spec=argument_spec,
|
||||
supports_check_mode=True)
|
||||
|
||||
def get_required_config(self):
|
||||
module_params = self._module.params
|
||||
self._required_config = dict(module_params)
|
||||
self.validate_param_values(self._required_config)
|
||||
|
||||
def _set_vxlan_config(self, vxlan_config):
|
||||
vxlan_config = vxlan_config[0]
|
||||
if not vxlan_config:
|
||||
return
|
||||
nve_header = vxlan_config.get("header")
|
||||
match = self.NVE_ID_REGEX.match(nve_header)
|
||||
if match:
|
||||
current_nve_id = int(match.group(1))
|
||||
self._current_config['nve_id'] = current_nve_id
|
||||
if int(current_nve_id) != self._required_config.get("nve_id"):
|
||||
return
|
||||
|
||||
self._current_config['mlag_tunnel_ip'] = vxlan_config.get("Mlag tunnel IP")
|
||||
controller_mode = vxlan_config.get("Controller mode")
|
||||
if controller_mode == "BGP":
|
||||
self._current_config['bgp'] = True
|
||||
else:
|
||||
self._current_config['bgp'] = False
|
||||
|
||||
loopback_str = vxlan_config.get("Source interface")
|
||||
match = self.LOOPBACK_REGEX.match(loopback_str)
|
||||
if match:
|
||||
loopback_id = match.group(1)
|
||||
self._current_config['loopback_id'] = int(loopback_id)
|
||||
|
||||
self._current_config['global_neigh_suppression'] = vxlan_config.get("Global Neigh-Suppression")
|
||||
|
||||
vni_vlan_mapping = self._current_config['vni_vlan_mapping'] = dict()
|
||||
nve_detail = self._show_nve_detail()
|
||||
|
||||
if nve_detail is not None:
|
||||
nve_detail = nve_detail[0]
|
||||
|
||||
if nve_detail:
|
||||
for vlan_id in nve_detail:
|
||||
vni_vlan_mapping[int(vlan_id)] = dict(
|
||||
vni_id=int(nve_detail[vlan_id][0].get("VNI")),
|
||||
arp_suppression=nve_detail[vlan_id][0].get("Neigh Suppression"))
|
||||
|
||||
def _show_vxlan_config(self):
|
||||
cmd = "show interfaces nve"
|
||||
return show_cmd(self._module, cmd, json_fmt=True, fail_on_error=False)
|
||||
|
||||
def _show_nve_detail(self):
|
||||
cmd = "show interface nve {0} detail".format(self._required_config.get("nve_id"))
|
||||
return show_cmd(self._module, cmd, json_fmt=True, fail_on_error=False)
|
||||
|
||||
def load_current_config(self):
|
||||
self._current_config = dict()
|
||||
vxlan_config = self._show_vxlan_config()
|
||||
if vxlan_config:
|
||||
self._set_vxlan_config(vxlan_config)
|
||||
|
||||
def generate_commands(self):
|
||||
nve_id = self._required_config.get("nve_id")
|
||||
current_nve_id = self._current_config.get("nve_id")
|
||||
|
||||
if current_nve_id is None:
|
||||
self._add_nve_commands(nve_id)
|
||||
elif current_nve_id != nve_id:
|
||||
self._add_no_nve_commands(current_nve_id)
|
||||
self._add_nve_commands(nve_id)
|
||||
|
||||
bgp = self._required_config.get("bgp")
|
||||
if bgp is not None:
|
||||
curr_bgp = self._current_config.get("bgp")
|
||||
if bgp and bgp != curr_bgp:
|
||||
self._commands.append('interface nve {0} nve controller bgp'.format(nve_id))
|
||||
|
||||
loopback_id = self._required_config.get("loopback_id")
|
||||
if loopback_id is not None:
|
||||
curr_loopback_id = self._current_config.get("loopback_id")
|
||||
if loopback_id != curr_loopback_id:
|
||||
self._commands.append('interface nve {0} vxlan source interface '
|
||||
'loopback {1} '.format(nve_id, loopback_id))
|
||||
|
||||
mlag_tunnel_ip = self._required_config.get("mlag_tunnel_ip")
|
||||
if mlag_tunnel_ip is not None:
|
||||
curr_mlag_tunnel_ip = self._current_config.get("mlag_tunnel_ip")
|
||||
if mlag_tunnel_ip != curr_mlag_tunnel_ip:
|
||||
self._commands.append('interface nve {0} vxlan '
|
||||
'mlag-tunnel-ip {1}'.format(nve_id, mlag_tunnel_ip))
|
||||
|
||||
vni_vlan_list = self._required_config.get("vni_vlan_list")
|
||||
arp_suppression = self._required_config.get("arp_suppression")
|
||||
if vni_vlan_list is not None:
|
||||
self._generate_vni_vlan_cmds(vni_vlan_list, nve_id, arp_suppression)
|
||||
|
||||
def _generate_vni_vlan_cmds(self, vni_vlan_list, nve_id, arp_suppression):
|
||||
|
||||
current_global_arp_suppression = self._current_config.get('global_neigh_suppression')
|
||||
if arp_suppression is True and current_global_arp_suppression != "Enable":
|
||||
self._commands.append('interface nve {0} nve neigh-suppression'.format(nve_id))
|
||||
|
||||
current_vni_vlan_mapping = self._current_config.get('vni_vlan_mapping')
|
||||
if current_vni_vlan_mapping is None:
|
||||
for vni_vlan in vni_vlan_list:
|
||||
vlan_id = vni_vlan.get("vlan_id")
|
||||
vni_id = vni_vlan.get("vni_id")
|
||||
self._add_vni_vlan_cmds(nve_id, vni_id, vlan_id)
|
||||
self._add_arp_suppression_cmds(arp_suppression, vlan_id)
|
||||
else:
|
||||
for vni_vlan in vni_vlan_list:
|
||||
vlan_id = vni_vlan.get("vlan_id")
|
||||
vni_id = vni_vlan.get("vni_id")
|
||||
|
||||
currt_vlan_id = current_vni_vlan_mapping.get(vlan_id)
|
||||
|
||||
if currt_vlan_id is None:
|
||||
self._add_vni_vlan_cmds(nve_id, vni_id, vlan_id)
|
||||
self._add_arp_suppression_cmds(arp_suppression, vlan_id)
|
||||
else:
|
||||
current_vni_id = currt_vlan_id.get("vni_id")
|
||||
current_arp_suppression = currt_vlan_id.get("arp_suppression")
|
||||
|
||||
if int(current_vni_id) != vni_id:
|
||||
self._add_vni_vlan_cmds(nve_id, vni_id, vlan_id)
|
||||
|
||||
if current_arp_suppression == "Disable":
|
||||
self._add_arp_suppression_cmds(arp_suppression, vlan_id)
|
||||
|
||||
def _add_no_nve_commands(self, current_nve_id):
|
||||
self._commands.append('no interface nve {0}'.format(current_nve_id))
|
||||
|
||||
def _add_nve_commands(self, nve_id):
|
||||
self._commands.append('interface nve {0}'.format(nve_id))
|
||||
self._commands.append('exit')
|
||||
|
||||
def _add_vni_vlan_cmds(self, nve_id, vni_id, vlan_id):
|
||||
self._commands.append('interface nve {0} nve vni {1} '
|
||||
'vlan {2}'.format(nve_id, vni_id, vlan_id))
|
||||
|
||||
def _add_arp_suppression_cmds(self, arp_suppression, vlan_id):
|
||||
if arp_suppression is True:
|
||||
self._commands.append('interface vlan {0}'.format(vlan_id))
|
||||
self._commands.append('exit')
|
||||
|
||||
|
||||
def main():
|
||||
""" main entry point for module execution
|
||||
"""
|
||||
OnyxVxlanModule.main()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -0,0 +1,18 @@
|
|||
[
|
||||
{
|
||||
"Mlag tunnel IP": "192.10.10.1",
|
||||
"Effective tunnel IP": "(not exist)",
|
||||
"NVE member interfaces": "(not configured)",
|
||||
"Admin state": "up",
|
||||
"Source interface": "loopback 1 (ip 0.0.0.0)",
|
||||
"header": "Interface NVE 1 status",
|
||||
"Controller mode": "BGP",
|
||||
"Global Neigh-Suppression": "Enable",
|
||||
"Counters": {
|
||||
"dropped NVE-encapsulated packets": "0",
|
||||
"decapsulated (Rx) NVE packets": "0",
|
||||
"encapsulated (Tx) NVE packets": "0",
|
||||
"NVE-encapsulated packets with errors": "0"
|
||||
}
|
||||
}
|
||||
]
|
|
@ -0,0 +1,16 @@
|
|||
[
|
||||
{
|
||||
"10":[
|
||||
{
|
||||
"Neigh Suppression":"Enable",
|
||||
"VNI":"10010"
|
||||
}
|
||||
],
|
||||
"6":[
|
||||
{
|
||||
"Neigh Suppression":"Enable",
|
||||
"VNI":"10060"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
101
test/units/modules/network/onyx/test_onyx_vxlan.py
Normal file
101
test/units/modules/network/onyx/test_onyx_vxlan.py
Normal file
|
@ -0,0 +1,101 @@
|
|||
#
|
||||
# Copyright: Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from units.compat.mock import patch
|
||||
from ansible.modules.network.onyx import onyx_vxlan
|
||||
from units.modules.utils import set_module_args
|
||||
from .onyx_module import TestOnyxModule, load_fixture
|
||||
|
||||
|
||||
class TestOnyxVxlanModule(TestOnyxModule):
|
||||
|
||||
module = onyx_vxlan
|
||||
arp_suppression = True
|
||||
|
||||
def setUp(self):
|
||||
super(TestOnyxVxlanModule, self).setUp()
|
||||
self.mock_get_vxlan_config = patch.object(
|
||||
onyx_vxlan.OnyxVxlanModule, "_show_vxlan_config")
|
||||
self.get_vxlan_config = self.mock_get_vxlan_config.start()
|
||||
|
||||
self.mock_load_config = patch(
|
||||
'ansible.module_utils.network.onyx.onyx.load_config')
|
||||
self.load_config = self.mock_load_config.start()
|
||||
|
||||
self.mock_get_nve_detail = patch.object(
|
||||
onyx_vxlan.OnyxVxlanModule, "_show_nve_detail")
|
||||
self.get_nve_detail = self.mock_get_nve_detail.start()
|
||||
|
||||
def tearDown(self):
|
||||
super(TestOnyxVxlanModule, self).tearDown()
|
||||
self.mock_get_vxlan_config.stop()
|
||||
self.mock_load_config.stop()
|
||||
self.mock_get_nve_detail.stop()
|
||||
|
||||
def load_fixtures(self, commands=None, transport='cli'):
|
||||
interfaces_nve_config_file = 'onyx_show_interfaces_nve.cfg'
|
||||
interfaces_nve_detail_config_file = 'onyx_show_interfaces_nve_detail.cfg'
|
||||
self.get_nve_detail.return_value = None
|
||||
interfaces_nve_detail_data = load_fixture(interfaces_nve_detail_config_file)
|
||||
interfaces_nv_data = load_fixture(interfaces_nve_config_file)
|
||||
self.get_nve_detail.return_value = interfaces_nve_detail_data
|
||||
if self.arp_suppression is False:
|
||||
interfaces_nve_detail_data[0]["10"][0]["Neigh Suppression"] = "Disable"
|
||||
interfaces_nve_detail_data[0]["6"][0]["Neigh Suppression"] = "Disable"
|
||||
self.get_nve_detail.return_value = interfaces_nve_detail_data
|
||||
self.get_vxlan_config.return_value = interfaces_nv_data
|
||||
|
||||
self.load_config.return_value = None
|
||||
|
||||
def test_configure_vxlan_no_change(self):
|
||||
set_module_args(dict(nve_id=1, loopback_id=1, bgp=True, mlag_tunnel_ip='192.10.10.1',
|
||||
vni_vlan_list=[dict(vlan_id=10, vni_id=10010), dict(vlan_id=6, vni_id=10060)],
|
||||
arp_suppression=True))
|
||||
self.execute_module(changed=False)
|
||||
|
||||
def test_configure_vxlan_with_change(self):
|
||||
set_module_args(dict(nve_id=2, loopback_id=1, bgp=True, mlag_tunnel_ip='192.10.10.1',
|
||||
vni_vlan_list=[dict(vlan_id=10, vni_id=10010), dict(vlan_id=6, vni_id=10060)],
|
||||
arp_suppression=True))
|
||||
commands = [
|
||||
"no interface nve 1", "interface nve 2", "exit",
|
||||
"interface nve 2 vxlan source interface loopback 1 ",
|
||||
"interface nve 2 nve controller bgp", "interface nve 2 vxlan mlag-tunnel-ip 192.10.10.1",
|
||||
"interface nve 2 nve neigh-suppression", "interface nve 2 nve vni 10010 vlan 10",
|
||||
"interface vlan 10", "exit", "interface nve 2 nve vni 10060 vlan 6", "interface vlan 6", "exit"
|
||||
]
|
||||
self.execute_module(changed=True, commands=commands)
|
||||
|
||||
def test_loopback_id_with_change(self):
|
||||
set_module_args(dict(nve_id=1, loopback_id=2, bgp=True, mlag_tunnel_ip='192.10.10.1',
|
||||
vni_vlan_list=[dict(vlan_id=10, vni_id=10010), dict(vlan_id=6, vni_id=10060)],
|
||||
arp_suppression=True))
|
||||
commands = ["interface nve 1 vxlan source interface loopback 2 "]
|
||||
self.execute_module(changed=True, commands=commands)
|
||||
|
||||
def test_mlag_tunnel_ip_with_change(self):
|
||||
set_module_args(dict(nve_id=1, loopback_id=1, bgp=True, mlag_tunnel_ip='192.10.10.10',
|
||||
vni_vlan_list=[dict(vlan_id=10, vni_id=10010), dict(vlan_id=6, vni_id=10060)],
|
||||
arp_suppression=True))
|
||||
commands = ["interface nve 1 vxlan mlag-tunnel-ip 192.10.10.10"]
|
||||
self.execute_module(changed=True, commands=commands)
|
||||
|
||||
def test_vni_vlan_list_with_change(self):
|
||||
set_module_args(dict(nve_id=1, loopback_id=1, bgp=True, mlag_tunnel_ip='192.10.10.1',
|
||||
vni_vlan_list=[dict(vlan_id=11, vni_id=10011), dict(vlan_id=7, vni_id=10061)],
|
||||
arp_suppression=False))
|
||||
commands = ["interface nve 1 nve vni 10011 vlan 11", "interface nve 1 nve vni 10061 vlan 7"]
|
||||
self.execute_module(changed=True, commands=commands)
|
||||
|
||||
def test_arp_suppression_with_change(self):
|
||||
self.arp_suppression = False
|
||||
set_module_args(dict(nve_id=1, loopback_id=1, bgp=True, mlag_tunnel_ip='192.10.10.1',
|
||||
vni_vlan_list=[dict(vlan_id=10, vni_id=10010), dict(vlan_id=6, vni_id=10060)],
|
||||
arp_suppression=True))
|
||||
commands = ["interface vlan 10", "exit", "interface vlan 6", "exit"]
|
||||
self.execute_module(changed=True, commands=commands)
|
Loading…
Reference in a new issue