Skip to content

Commit b81cfd7

Browse files
committed
[refactor] OpenWRT dialup interface #168
Closes #168
1 parent 58b32cc commit b81cfd7

4 files changed

Lines changed: 46 additions & 20 deletions

File tree

docs/source/backends/openwrt.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,12 +1252,12 @@ Will be rendered as follows::
12521252

12531253
package network
12541254

1255-
config interface 'xdsl'
1255+
config interface 'xdsl'
12561256
option ifname 'dsl0'
1257-
option proto 'pppoe'
1258-
option username 'dsluser'
1257+
option proto 'pppoe'
1258+
option username 'dsluser'
12591259
option password 'jf93nf82o023$'
1260-
option mtu '1448'
1260+
option mtu '1448'
12611261

12621262
Radio settings
12631263
--------------

netjsonconfig/backends/openwrt/converters/interfaces.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ def __intermediate_interface(self, interface, uci_name):
109109
del interface['wireless']
110110
if 'addresses' in interface:
111111
del interface['addresses']
112+
# specific transformation
113+
type_ = self._get_uci_name(interface["type"])
114+
method = getattr(self, f'_intermediate_{type_}', None)
115+
if method:
116+
interface = method(interface)
117+
return interface
112118
return interface
113119

114120
_address_keys = ['address', 'mask', 'family', 'gateway']
@@ -225,6 +231,10 @@ def __netjson_interface(self, interface):
225231
interface['mac'] = interface.pop('macaddr')
226232
if interface['network'] == self._get_uci_name(interface['name']):
227233
del interface['network']
234+
# specific transformation
235+
method = getattr(self, f'_netjson_{interface.get("proto")}', None)
236+
if method:
237+
interface = method(interface)
228238
return interface
229239

230240
def __netjson_type(self, interface):
@@ -243,13 +253,17 @@ def __netjson_type(self, interface):
243253
return 'ethernet'
244254

245255
def __netjson_addresses(self, interface):
246-
proto = interface.pop('proto', 'none')
256+
proto = interface.get('proto', 'none')
257+
address_protos = ['static', 'dhcp', 'dhcpv6', 'none']
258+
if 'proto' in interface and proto in address_protos:
259+
del interface['proto']
247260
if 'ipaddr' not in interface and 'ip6addr' not in interface and proto == 'none':
248261
return interface
249-
if proto not in ['static', 'dhcp', 'dhcpv6', 'none']:
250-
interface['proto'] = proto
251-
interface['type'] = self.__get_special_interface_type(interface)
262+
if proto not in address_protos:
263+
interface['type'] = 'other'
264+
return self._add_netjson_addresses(interface, proto)
252265

266+
def _add_netjson_addresses(self, interface, proto):
253267
addresses = []
254268
ipv4 = interface.pop('ipaddr', [])
255269
ipv6 = interface.pop('ip6addr', [])
@@ -273,14 +287,9 @@ def __netjson_addresses(self, interface):
273287
interface['addresses'] = addresses
274288
return interface
275289

276-
def __get_special_interface_type(self, interface):
277-
username = interface.get('username', False)
278-
password = interface.get('password', False)
279-
280-
if username and password:
281-
return 'dialup'
282-
283-
return 'other'
290+
def _netjson_dialup(self, interface):
291+
interface['type'] = 'dialup'
292+
return interface
284293

285294
def __netjson_address(self, address, interface):
286295
ip = ip_interface(address)
@@ -308,7 +317,7 @@ def __netjson_parse_ip(self, ip, netmask=32):
308317
if ip and netmask:
309318
return '{0}/{1}'.format(ip, netmask)
310319
else:
311-
None
320+
return None
312321

313322
def __netjson_dns(self, interface, result):
314323
key_mapping = {'dns': 'dns_servers', 'dns_search': 'dns_search'}
@@ -320,3 +329,19 @@ def __netjson_dns(self, interface, result):
320329
items = items.split()
321330
result.setdefault(netjson_key, [])
322331
result[netjson_key] += items
332+
333+
334+
for proto in [
335+
'3g',
336+
'6in4',
337+
'aiccu',
338+
'l2tp',
339+
'ncm',
340+
'ppp',
341+
'pppoa',
342+
'pppoe',
343+
'pptp',
344+
'qmi',
345+
'wwan',
346+
]:
347+
setattr(Interfaces, f'_netjson_{proto}', Interfaces._netjson_dialup)

netjsonconfig/backends/openwrt/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
},
127127
"proto": {
128128
"type": "string",
129+
"title": "protocol",
129130
"enum": [
130131
"3g",
131132
"6in4",
@@ -140,7 +141,7 @@
140141
"wwan",
141142
],
142143
"default": "pppoe",
143-
"propertyOrder": 8,
144+
"propertyOrder": 1.1,
144145
},
145146
"username": {
146147
"type": "string",

tests/openwrt/test_interfaces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ def test_parse_interface_custom_attrs(self):
690690
option custom_attr 'yes'
691691
option ifname 'mobile0'
692692
option mtu '1400'
693-
option proto '3g'
693+
option proto 'exotic'
694694
"""
695695
)
696696
expected = {
@@ -700,7 +700,7 @@ def test_parse_interface_custom_attrs(self):
700700
"type": "other",
701701
"mtu": 1400,
702702
"custom_attr": "yes",
703-
"proto": "3g",
703+
"proto": "exotic",
704704
}
705705
]
706706
}

0 commit comments

Comments
 (0)