From dfda2c7f305df94690cc4538a74ab213d25c579c Mon Sep 17 00:00:00 2001 From: Viacheslav Hletenko Date: Mon, 27 Jan 2025 18:01:57 +0000 Subject: Use common class for none ethernet interfaces state --- python/vyos/vpp/interface/__init__.py | 2 ++ python/vyos/vpp/interface/gre.py | 10 ++++++++-- python/vyos/vpp/interface/ipip.py | 17 ++++++++++++++--- python/vyos/vpp/interface/loopback.py | 11 +++++++---- python/vyos/vpp/interface/vxlan.py | 18 +++++++++++++++--- python/vyos/vpp/interface/xconnect.py | 2 ++ 6 files changed, 48 insertions(+), 12 deletions(-) (limited to 'python') diff --git a/python/vyos/vpp/interface/__init__.py b/python/vyos/vpp/interface/__init__.py index 14659ed7b..2048e07ca 100644 --- a/python/vyos/vpp/interface/__init__.py +++ b/python/vyos/vpp/interface/__init__.py @@ -20,6 +20,7 @@ from .bridge import BridgeInterface from .ethernet import EthernetInterface from .geneve import GeneveInterface from .gre import GREInterface +from .interface import Interface from .ipip import IPIPInterface from .loopback import LoopbackInterface from .vxlan import VXLANInterface @@ -32,6 +33,7 @@ __all__ = [ 'EthernetInterface', 'GeneveInterface', 'GREInterface', + 'Interface', 'IPIPInterface', 'LoopbackInterface', 'VXLANInterface', diff --git a/python/vyos/vpp/interface/gre.py b/python/vyos/vpp/interface/gre.py index 8f3dbebae..5bef280c6 100644 --- a/python/vyos/vpp/interface/gre.py +++ b/python/vyos/vpp/interface/gre.py @@ -17,6 +17,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from vyos.vpp import VPPControl +from vyos.vpp.interface.interface import Interface def show(): @@ -29,7 +30,7 @@ def show(): return vpp.api.gre_tunnel_dump() -class GREInterface: +class GREInterface(Interface): """ Class representing a GRE (Generic Routing Encapsulation) interface. @@ -64,6 +65,7 @@ class GREInterface: tunnel_type: str = 'l3', mode: str = 'point-to-point', kernel_interface: str = '', + state: str = 'up', ): """ Initialize a GREInterface instance. @@ -75,7 +77,9 @@ class GREInterface: mode (str): The mode of the GRE tunnel. Options are 'point-to-point' and 'point-to-multipoint'. Defaults to 'point-to-point'. tunnel_type (str): The type of GRE tunnel. Defaults to 'l3'. kernel_interface (str): The associated kernel interface. Defaults to an empty string. + state (str): The state of the interface. Defaults to 'up'. """ + super().__init__(ifname) self.instance = int(ifname.removeprefix('gre')) self.ifname = ifname self.src_address = source_address @@ -83,7 +87,7 @@ class GREInterface: self.tunnel_type = self.TUNNEL_TYPE_MAP[tunnel_type] self.mode = self.MODE_MAP[mode] self.kernel_interface = kernel_interface - self.vpp = VPPControl() + self.initial_state = state def add(self): """Create GRE interface @@ -103,6 +107,8 @@ class GREInterface: 'type': self.tunnel_type, }, ) + # Set interface state + self.set_state(self.initial_state) def delete(self): """Delete GRE interface diff --git a/python/vyos/vpp/interface/ipip.py b/python/vyos/vpp/interface/ipip.py index 1aa1e22f2..4779a8292 100644 --- a/python/vyos/vpp/interface/ipip.py +++ b/python/vyos/vpp/interface/ipip.py @@ -17,6 +17,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from vyos.vpp import VPPControl +from vyos.vpp.interface.interface import Interface def show(): @@ -29,14 +30,22 @@ def show(): return vpp.api.ipip_tunnel_dump() -class IPIPInterface: - def __init__(self, ifname, source_address, remote, kernel_interface: str = ''): +class IPIPInterface(Interface): + def __init__( + self, + ifname, + source_address, + remote, + kernel_interface: str = '', + state: str = 'up', + ): + super().__init__(ifname) self.instance = int(ifname.removeprefix('ipip')) self.ifname = ifname self.src_address = source_address self.dst_address = remote self.kernel_interface = kernel_interface - self.vpp = VPPControl() + self.initial_state = state def add(self): """Create IPIP interface @@ -53,6 +62,8 @@ class IPIPInterface: 'instance': self.instance, }, ) + # Set interface state + self.set_state(self.initial_state) def delete(self): """Delete IPIP interface diff --git a/python/vyos/vpp/interface/loopback.py b/python/vyos/vpp/interface/loopback.py index 8a5414d54..d417f627d 100644 --- a/python/vyos/vpp/interface/loopback.py +++ b/python/vyos/vpp/interface/loopback.py @@ -16,17 +16,18 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from vyos.vpp import VPPControl +from vyos.vpp.interface.interface import Interface -class LoopbackInterface: +class LoopbackInterface(Interface): """Interface Loopback""" - def __init__(self, ifname, kernel_interface: str = ''): + def __init__(self, ifname, kernel_interface: str = '', state: str = 'up'): + super().__init__(ifname) self.instance = int(ifname.removeprefix('lo')) self.ifname = f'loop{self.instance}' self.kernel_interface = kernel_interface - self.vpp = VPPControl() + self.initial_state = state def add(self): """Create Loopback interface @@ -39,6 +40,8 @@ class LoopbackInterface: self.vpp.api.create_loopback_instance( is_specified=True, user_instance=self.instance ) + # Set interface state + self.set_state(self.initial_state) def delete(self): """Delete Loopback interface diff --git a/python/vyos/vpp/interface/vxlan.py b/python/vyos/vpp/interface/vxlan.py index 36206e8c4..eacc901dc 100644 --- a/python/vyos/vpp/interface/vxlan.py +++ b/python/vyos/vpp/interface/vxlan.py @@ -17,6 +17,7 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from vyos.vpp import VPPControl +from vyos.vpp.interface.interface import Interface def show(): @@ -29,17 +30,26 @@ def show(): return vpp.api.vxlan_tunnel_dump() -class VXLANInterface: +class VXLANInterface(Interface): """Interface VXLAN""" - def __init__(self, ifname, source_address, remote, vni, kernel_interface: str = ''): + def __init__( + self, + ifname, + source_address, + remote, + vni, + kernel_interface: str = '', + state: str = 'up', + ): + super().__init__(ifname) self.instance = int(ifname.removeprefix('vxlan')) self.ifname = f'vxlan_tunnel{self.instance}' self.src_address = source_address self.dst_address = remote self.vni = vni self.kernel_interface = kernel_interface - self.vpp = VPPControl() + self.initial_state = state def add(self): """Create VXLAN interface @@ -59,6 +69,8 @@ class VXLANInterface: decap_next_index=1, is_l3=False, ) + # Set interface state + self.set_state(self.initial_state) def delete(self): """Delete VXLAN interface diff --git a/python/vyos/vpp/interface/xconnect.py b/python/vyos/vpp/interface/xconnect.py index 41cf773d9..badece6e8 100644 --- a/python/vyos/vpp/interface/xconnect.py +++ b/python/vyos/vpp/interface/xconnect.py @@ -25,10 +25,12 @@ class XconnectInterface: self, ifname: str, members: list = [], + state: str = 'up', ): self.ifname = ifname self.members = members self.vpp = VPPControl() + self.initial_state = state def add_l2_xconnect(self): """Add l2 cross connect -- cgit v1.2.3