summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriyEshenko <snooppy@mail.ua>2019-08-23 22:00:57 +0000
committerDmitriyEshenko <snooppy@mail.ua>2019-08-23 22:00:57 +0000
commit84957a3418db23716b9e80f38733ed5e0bd4252e (patch)
tree2c30e32e15c049703c120b69ee9495700efa3624
parent87be8bbd814f2e387c3b60fbd3f44e55a21b2bce (diff)
downloadvyos-1x-84957a3418db23716b9e80f38733ed5e0bd4252e.tar.gz
vyos-1x-84957a3418db23716b9e80f38733ed5e0bd4252e.zip
[dummy] T1609 migrate to vyos.interfaceconfig, adding check ip-cidr, adding vyos.interfaceconfig common ipv4/ipv6 functions
-rw-r--r--interface-definitions/interfaces-dummy.xml3
-rw-r--r--python/vyos/interfaceconfig.py56
-rwxr-xr-xsrc/conf_mode/interface-dummy.py19
3 files changed, 69 insertions, 9 deletions
diff --git a/interface-definitions/interfaces-dummy.xml b/interface-definitions/interfaces-dummy.xml
index 4450b69b7..c9860fe3b 100644
--- a/interface-definitions/interfaces-dummy.xml
+++ b/interface-definitions/interfaces-dummy.xml
@@ -28,6 +28,9 @@
<description>IPv6 address and prefix length</description>
</valueHelp>
<multi/>
+ <constraint>
+ <validator name="ip-cidr"/>
+ </constraint>
</properties>
</leafNode>
<leafNode name="description">
diff --git a/python/vyos/interfaceconfig.py b/python/vyos/interfaceconfig.py
index 83e7c03c0..56e2d515c 100644
--- a/python/vyos/interfaceconfig.py
+++ b/python/vyos/interfaceconfig.py
@@ -21,6 +21,7 @@ import re
import json
import socket
import subprocess
+import ipaddress
dhclient_conf_dir = r'/var/lib/dhcp/dhclient_'
@@ -188,6 +189,29 @@ class Interface:
self._debug(e)
return None
+ def get_addr(self, ret_prefix=None):
+ """
+ universal: reads all IPs assigned to an interface and returns it in a list,
+ or None if no IP address is assigned to the interface. Also may return
+ in prefix format if set ret_prefix
+ """
+ ips = []
+ try:
+ ret = subprocess.check_output(['ip -j addr show dev ' + self._ifname], stderr=subprocess.STDOUT, shell=True).decode()
+ j = json.loads(ret)
+ for i in j:
+ if len(i) != 0:
+ for addr in i['addr_info']:
+ if ret_prefix:
+ ips.append(addr['local'] + "/" + str(addr['prefixlen']))
+ else:
+ ips.append(addr['local'])
+ return ips
+ except subprocess.CalledProcessError as e:
+ if self._debug():
+ self._debug(e)
+ return None
+
def get_ipv4_addr(self):
"""
reads all IPs assigned to an interface and returns it in a list,
@@ -227,6 +251,38 @@ class Interface:
self._debug(e)
return None
+ def add_addr(self, ipaddr=[]):
+ """
+ universal: add ipv4/ipv6 addresses on the interface
+ """
+ for ip in ipaddr:
+ proto = '-4'
+ if ipaddress.ip_address(ip.split(r'/')[0]).version == 6:
+ proto = '-6'
+
+ try:
+ ret = subprocess.check_output(['ip ' + proto + ' address add ' + ip + ' dev ' + self._ifname], stderr=subprocess.STDOUT, shell=True).decode()
+ except subprocess.CalledProcessError as e:
+ if self._debug():
+ self._debug(e)
+ return None
+ return True
+
+ def del_addr(self, ipaddr=[]):
+ """
+ universal: delete ipv4/ipv6 addresses on the interface
+ """
+ for ip in ipaddr:
+ proto = '-4'
+ if ipaddress.ip_address(ip.split(r'/')[0]).version == 6:
+ proto = '-6'
+ try:
+ ret = subprocess.check_output(['ip ' + proto + ' address del ' + ip + ' dev ' + self._ifname], stderr=subprocess.STDOUT, shell=True).decode()
+ except subprocess.CalledProcessError as e:
+ if self._debug():
+ self._debug(e)
+ return None
+ return True
def add_ipv4_addr(self, ipaddr=[]):
"""
diff --git a/src/conf_mode/interface-dummy.py b/src/conf_mode/interface-dummy.py
index 8c939ce95..c7c5ac99c 100755
--- a/src/conf_mode/interface-dummy.py
+++ b/src/conf_mode/interface-dummy.py
@@ -20,8 +20,6 @@ import os
import sys
import copy
-import vyos.configinterface as VyIfconfig
-
from vyos.interfaceconfig import Interface
from vyos.config import Config
from vyos import ConfigError
@@ -86,20 +84,23 @@ def generate(dummy):
def apply(dummy):
# Remove dummy interface
if dummy['deleted']:
- VyIfconfig.remove_interface(dummy['intf'])
+ Interface(dummy['intf']).remove_interface()
else:
# Interface will only be added if it yet does not exist
- VyIfconfig.add_interface('dummy', dummy['intf'])
+ Interface(dummy['intf'], 'dummy')
# update interface description used e.g. within SNMP
- VyIfconfig.set_description(dummy['intf'], dummy['description'])
+ if dummy['description']:
+ Interface(dummy['intf']).ifalias = dummy['description']
# Configure interface address(es)
- for addr in dummy['address_remove']:
- VyIfconfig.remove_interface_address(dummy['intf'], addr)
+ if len(dummy['address_remove']) > 0:
+ Interface(dummy['intf']).del_addr(dummy['address_remove'])
- for addr in dummy['address']:
- VyIfconfig.add_interface_address(dummy['intf'], addr)
+ if len(dummy['address']) > 0:
+ # delete already existing addreses from list
+ addresess = diff(dummy['address'], Interface(dummy['intf']).get_addr(1))
+ Interface(dummy['intf']).add_addr(addresess)
if dummy['disable']:
Interface(dummy['intf']).linkstate = 'down'