Skip to content

Commit

Permalink
[change] OpenWrt converter changed to generate new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
codesankalp committed Mar 26, 2022
1 parent 8db6ced commit 8ac49b2
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 174 deletions.
1 change: 1 addition & 0 deletions netjsonconfig/backends/base/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(self, backend):
self.backend = backend
self.netjson = backend.config
self.intermediate_data = backend.intermediate_data
self.dsa = getattr(backend, 'dsa', None)

@classmethod
def should_run_forward(cls, config):
Expand Down
73 changes: 43 additions & 30 deletions netjsonconfig/backends/openwrt/converters/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ def to_intermediate_loop(self, block, result, index=None):
uci_name = self._get_uci_name(block.get('network') or block['name'])
address_list = self.__intermediate_addresses(block)
interface = self.__intermediate_interface(block, uci_name)
uci_device = self.__intermediate_device(interface)
if uci_device:
result['network'].append(self.sorted_dict(uci_device))
if self.dsa:
uci_device = self.__intermediate_device(interface)
if uci_device:
result['network'].append(self.sorted_dict(uci_device))
# create one or more "config interface" UCI blocks
i = 1
for address in address_list:
Expand Down Expand Up @@ -193,10 +194,9 @@ def __intermediate_device(self, interface):
device = {}
# Add L2 options (needed for > OpenWrt 21.02)
self._add_l2_options(device, interface)

if interface.get('type') != 'bridge' and device == {}:
# No L2 options were present in the configuration.
return
# if interface.get('type') != 'bridge' and device == {}:
# # No L2 options were present in the configuration.
# return
device.update(
{
'.type': 'device',
Expand All @@ -222,9 +222,14 @@ def __intermediate_device(self, interface):
device,
interface,
)
for option in self._bridge_interface_options['all']:
device_options = (
self._bridge_interface_options['all']
+ self._bridge_interface_options['stp']
+ self._bridge_interface_options['igmp_snooping']
)
for option in device_options:
if option in interface:
device[option] = interface[option]
device[option] = interface.pop(option, None)
device['ports'] = interface.get('bridge_members', [])
if device['ports'] == []:
device['bridge_empty'] = True
Expand Down Expand Up @@ -258,18 +263,22 @@ def _add_l2_options(device, interface):
if option in interface:
device[option] = interface.pop(option)
if interface.get('macaddr', None):
device['macaddr'] = interface['macaddr']
device['macaddr'] = interface.pop('macaddr')

def __clean_intermediate_bridge(self, interface):
"""
Removes options that are not required in the configuration.
"""
if not interface.get('igmp_snooping', False):
for option in self._bridge_interface_options['igmp_snooping']:
interface.pop(option, None)
if not interface.get('stp', False):
for option in self._bridge_interface_options['stp']:
interface.pop(option, None)
if self.dsa:
repeated_options = (
['ifname', 'type', 'bridge_members']
+ self._bridge_interface_options['stp']
+ self._bridge_interface_options['igmp_snooping']
+ self._bridge_interface_options['all']
)
for attr in repeated_options:
if attr in interface:
del interface[attr]

def __intermediate_bridge(self, interface, i):
"""
Expand All @@ -279,15 +288,15 @@ def __intermediate_bridge(self, interface, i):
# ensure type "bridge" is only given to one logical interface
if interface['type'] == 'bridge' and i < 2:
bridge_members = ' '.join(interface.pop('bridge_members'))
interface['device'] = interface['ifname']
# put bridge members in ifname attribute
if bridge_members:
interface['ifname'] = bridge_members
# if no members, this is an empty bridge
if self.dsa:
interface['device'] = interface['ifname']
else:
interface['bridge_empty'] = True
del interface['ifname']
self.__clean_intermediate_bridge(interface)
if bridge_members:
interface['ifname'] = bridge_members
# if no members, this is an empty bridge
else:
interface['bridge_empty'] = True
del interface['ifname']
# bridge has already been defined
# but we need to add more references to it
elif interface['type'] == 'bridge' and i >= 2:
Expand All @@ -307,6 +316,7 @@ def __intermediate_bridge(self, interface, i):
del interface[attr]
elif interface['type'] != 'bridge':
del interface['type']
self.__clean_intermediate_bridge(interface)
return interface

def __intermediate_proto(self, interface, address):
Expand Down Expand Up @@ -358,14 +368,14 @@ def __update_existing_netjson_bridge(self, interface, result):
Returns "True" if the method updated an existing bridge.
Otherwise, returns "False".
"""
if interface['type'] != 'bridge':
if not self.dsa:
return False
for _interface in result['interfaces']:
if (
interface['name'] == _interface['name']
and interface['type'] == _interface['type']
if _interface['type'] == 'bridge' and _interface['name'] == 'br-{}'.format(
interface['name']
):
_interface.update(interface)
interface.update(_interface)
# del _interface
return True
return False

Expand Down Expand Up @@ -393,9 +403,12 @@ def to_netjson_loop(self, block, result, index):
return result

def __netjson_interface(self, interface):

del interface['.type']
interface['network'] = interface.pop('.name')
interface['name'] = interface.pop('ifname', interface['network'])
if self.dsa and 'device' in interface:
interface['name'] = interface.pop('device')
interface['type'] = self.__netjson_type(interface)
interface = self.__netjson_addresses(interface)
if 'auto' in interface:
Expand Down Expand Up @@ -464,7 +477,7 @@ def __clean_netjson_bridge(self, interface):
device = interface.pop('device', None)
if device:
interface.update(self._device_config.pop(device, {}))
else:
elif self.dsa:
self.__netjson_device(interface)
interface.update(self._device_config[interface['name']])
if interface.pop('bridge_21', False):
Expand Down
4 changes: 2 additions & 2 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class OpenWrt(BaseBackend):
renderer = OpenWrtRenderer
list_identifiers = ['name', 'config_value', 'id']

def __init__(self, dsa=True, *args, **kwargs):
self.dsa = dsa
def __init__(self, *args, **kwargs):
self.dsa = kwargs.pop('dsa', True)
super().__init__(*args, **kwargs)

def _generate_contents(self, tar):
Expand Down
2 changes: 1 addition & 1 deletion tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_schema_radio_wrong_protocol(self):
}

def test_generate(self):
o = OpenWrt(self._config1)
o = OpenWrt(self._config1, dsa=False)
tar = tarfile.open(fileobj=o.generate(), mode='r')
self.assertEqual(len(tar.getmembers()), 2)
# network
Expand Down
4 changes: 2 additions & 2 deletions tests/openwrt/test_dialup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class TestDialup(unittest.TestCase, _TabsMixin):
"""

def test_render_dialup_interface(self):
result = OpenWrt(self._dialup_interface_netjson).render()
result = OpenWrt(self._dialup_interface_netjson, dsa=False).render()
expected = self._tabs(self._dialup_interface_uci)
self.assertEqual(result, expected)

def test_parse_dialup_interface(self):
result = OpenWrt(native=self._dialup_interface_uci).config
result = OpenWrt(native=self._dialup_interface_uci, dsa=False).config
expected = self._dialup_interface_netjson
self.assertDictEqual(result, expected)
Loading

0 comments on commit 8ac49b2

Please sign in to comment.