Skip to content

Commit

Permalink
[openwrt] Fixed bug in switch backward conversion #149
Browse files Browse the repository at this point in the history
Improves and closes #149
  • Loading branch information
nemesifier committed Apr 11, 2020
1 parent 772cec1 commit 5182887
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
26 changes: 11 additions & 15 deletions netjsonconfig/backends/base/converter.py
Expand Up @@ -88,12 +88,13 @@ def to_intermediate_loop(self, block, result, index=None): # pragma: nocover

def to_netjson(self, remove_block=True):
"""
Converts the intermediate data structure (``self.intermediate_datra``)
Converts the intermediate data structure (``self.intermediate_data``)
to a NetJSON configuration dictionary (``self.config``)
"""
result = OrderedDict()
# Clean intermediate data
intermediate_data = self.clean_intermediate_data(list(self.intermediate_data[self.intermediate_key]))
# clean intermediate data
intermediate_data = self.to_netjson_clean(self.intermediate_data[self.intermediate_key])
# intermediate_data = list(self.intermediate_data[self.intermediate_key])
# iterate over copied intermediate data structure
for index, block in enumerate(intermediate_data):
if self.should_skip_block(block):
Expand All @@ -109,18 +110,13 @@ def to_netjson(self, remove_block=True):
# return result, expects dict
return result

def clean_intermediate_data(self, intermediate_data):
"""
Utility method called to clean data for backend in ``to_netjson``
"""
clean_intermediate_data, appendto_clean_intermediate_data = [], []
for block in intermediate_data:
if '.type' in block and block['.type'] == 'switch_vlan':
appendto_clean_intermediate_data.append(block)
else:
clean_intermediate_data.append(block)
clean_intermediate_data.extend(appendto_clean_intermediate_data)
return clean_intermediate_data
def to_netjson_clean(self, intermediate_data):
"""
Utility method called to pre-process the intermediate data structure
during backward conversion (``to_netjson``)
"""
# returns a copy in order to avoid modifying the original structure
return list(intermediate_data)

def to_netjson_loop(self, block, result, index=None): # pragma: nocover
"""
Expand Down
10 changes: 10 additions & 0 deletions netjsonconfig/backends/openwrt/converters/switch.py
Expand Up @@ -21,6 +21,16 @@ def to_intermediate_loop(self, block, result, index=None):
result['network'] += switch_vlan
return result

def to_netjson_clean(self, intermediate_data):
reordered_data, last_items = [], []
for block in super().to_netjson_clean(intermediate_data):
if '.type' in block and block['.type'] == 'switch_vlan':
last_items.append(block)
else:
reordered_data.append(block)
reordered_data.extend(last_items)
return reordered_data

def __intermediate_switch(self, switch):
switch.update({
'.type': 'switch',
Expand Down

0 comments on commit 5182887

Please sign in to comment.