From e794ed5a485954d4375d230ee65f0b9aa39994f0 Mon Sep 17 00:00:00 2001 From: "Houqi (Nick) Zuo" Date: Thu, 29 Aug 2024 16:15:16 +0800 Subject: [PATCH] add network system of multi-host migration add support for network system. Signed-off-by: Houqi (Nick) Zuo --- virttest/vt_netmgr/__init__.py | 1 + virttest/vt_netmgr/vt_netmgr.py | 192 ++++++++++++++++++ .../vt_resmgr/resources/network/__init__.py | 0 .../vt_resmgr/resources/network/network.py | 29 +++ virttest/vt_resmgr/resources/network/port.py | 27 +++ 5 files changed, 249 insertions(+) create mode 100644 virttest/vt_netmgr/__init__.py create mode 100644 virttest/vt_netmgr/vt_netmgr.py create mode 100644 virttest/vt_resmgr/resources/network/__init__.py create mode 100644 virttest/vt_resmgr/resources/network/network.py create mode 100644 virttest/vt_resmgr/resources/network/port.py diff --git a/virttest/vt_netmgr/__init__.py b/virttest/vt_netmgr/__init__.py new file mode 100644 index 0000000000..058862d824 --- /dev/null +++ b/virttest/vt_netmgr/__init__.py @@ -0,0 +1 @@ +from .vt_netmgr import vt_netmgr diff --git a/virttest/vt_netmgr/vt_netmgr.py b/virttest/vt_netmgr/vt_netmgr.py new file mode 100644 index 0000000000..5f71ad92b6 --- /dev/null +++ b/virttest/vt_netmgr/vt_netmgr.py @@ -0,0 +1,192 @@ +""" +The upper-level network manager. + +from virttest.vt_netgr import vt_netmgr +""" + +import logging + +from virttest.vt_cluster import cluster +from virttest.vt_resmgr.resources.network import network as nt +from virttest.vt_resmgr.resources.network import port + +LOG = logging.getLogger("avocado." + __name__) + + +class _VTNetworkManager(object): + """ + The Network Manager. + """ + + def __init__(self): + self._networks = list() + self._ports = list() + + def startup(self): + LOG.info(f"Start the network manager!") + + def teardown(self): + LOG.info(f"Stop the network manager!") + + def define_network_config(self, params): + """ + Translate the parameters of user layer to parameters of vt framework + layer. + + :param params: The parameters of user level. + :type params: dict. + + :return: The parsed parameters in vt framework layer. + :rtype: dict. + """ + pass + + def create_network(self, network_params, vm_name): + """ + Create the network by its cartesian params. + + :param network_params: The network tag defined in cartesian params. + :type network_params: String. + :param vm_name: The vm name. + :type vm_name: String. + + :return: The uuid of created network. + :rtype: String. + """ + network_obj = nt.Network(network_params, vm_name) + network_obj.create() + self._networks.append(network_obj) + return network_obj.uuid + + def delete_network(self, descriptor): + """ + Delete the networks by its descriptor. + + :param descriptor: The network name or the uuid. + :type descriptor: String. + """ + be_deleted = [] + for obj in self._networks: + if descriptor in ( + obj.uuid, + obj.name, + ): + obj.delete() + be_deleted.append(obj) + + for obj in be_deleted: + self._networks.remove(obj) + + def get_network_by_name(self, network_name, vm_name): + """ + Filter the networks based on the network name and vm name. + NOTE: return all networks if descriptor is None. + + :param network_name: The network name. + :type network_name: String. + :param vm_name: The vm name. + :type vm_name: String. + + :return: The uuid of network or None If not found. + :rtype: String. + """ + for obj in self._networks: + if network_name in (obj.name,) and vm_name in (obj.vm_name,): + return obj.uuid + return None + + def query_network(self, uuid): + """ + Get the network by an uuid. + + :param uuid: The uuid. + :type uuid: String. + + :return: The configuration of network or None if not found. + e.g.: {"uuid": uuid, + "name": name, + "vm_name": vm_name, + "type": type, + "spec": spec} + :rtype: dict. + """ + for obj in self._networks: + if uuid in (obj.uuid,): + return { + "uuid": obj.uuid, + "name": obj.name, + "vm_name": obj.vm_name, + "type": obj.type, + "spec": obj.spec, + } + return None + + def create_port(self, name, network_uuid, vm_name, mac_nic, net_type): + """ + Create the port by its params. + + :param name: The port name. + :type name: String. + :param network_uuid: The network uuid which this port belongs to. + :type network_uuid: String. + :param vm_name: The vm name. + :type vm_name: String. + :param mac_nic: The mac address. + :type mac_nic: String. + :param net_type: The net type. + :type net_type: String. + """ + port_obj = port.Port(name, network_uuid, vm_name, mac_nic, net_type) + port_obj.create() + self._networks.append(port_obj) + + def delete_port(self): + pass + + # TODO: + # delete? + def get_port_by_name(self, nic_name, context): + """ + Filter the ports based on the nic name and + context(vm name or network uuid). + + :param nic_name: The network name or the uuid. + :type nic_name: String. + :param context: The vm name or network_uuid. + :type context: String. + + :return: The uuid of network or None If not found. + :rtype: String. + """ + for obj in self._ports: + if nic_name in (obj.name,) and ( + context in (obj.vm_name,) or context in (obj.network_uuid,) + ): + return obj.uuid + return None + + def query_port(self, uuid): + """ + Get the port configuration. + + :param uuid: The uuid. + :type uuid: String. + + :return: The port configuration. + :rtype: dict. + """ + for obj in self._ports: + if uuid in (obj.uuid, ): + return { + "uuid": obj.uuid, + "name": obj.name, + "network_uuid": obj.network_uuid, + "vm_name": obj.vm_name, + "mac_nic": obj.mac_nic, + "net_type": obj.net_type, + "created_by_vt": obj.created_by_vt, + } + return None + + +vt_netmgr = _VTNetworkManager() diff --git a/virttest/vt_resmgr/resources/network/__init__.py b/virttest/vt_resmgr/resources/network/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/virttest/vt_resmgr/resources/network/network.py b/virttest/vt_resmgr/resources/network/network.py new file mode 100644 index 0000000000..4150d5c424 --- /dev/null +++ b/virttest/vt_resmgr/resources/network/network.py @@ -0,0 +1,29 @@ +from abc import ABC +import uuid + + +class Network(ABC): + def __init__(self, network_params, vm_name): + self.uuid = uuid.uuid4().hex + self.name = network_params.keys()[0] + self.vm_name = vm_name + self._type = network_params["type"] + self._spec = network_params["spec"] + + @property + def type(self): + return self._type + + @property + def spec(self): + return self._spec + + def create(self): + # TODO + # create the port for the self._spec + pass + + def delete(self): + # TODO + # delete the port for the self._spec + pass diff --git a/virttest/vt_resmgr/resources/network/port.py b/virttest/vt_resmgr/resources/network/port.py new file mode 100644 index 0000000000..df150f57b7 --- /dev/null +++ b/virttest/vt_resmgr/resources/network/port.py @@ -0,0 +1,27 @@ +from abc import ABC +import uuid + +# from virttest.vt_resmgr.resources.network import PortType + + +class Port(ABC): + def __init__(self, name, network_uuid, vm_name, mac_nic, net_type): + self.uuid = uuid.uuid4().hex + self.name = name + self.network_uuid = network_uuid + self.vm_name = vm_name + self.mac_nic = mac_nic + # if dev_type not in PortType: + # raise NotImplementedError("This type is NOT supported!") + self.net_type = net_type + self.created_by_vt = None + + def create(self): + # TODO + # create the port + pass + + def delete(self): + # TODO + # delete the port + pass