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..dc331d3de7 --- /dev/null +++ b/virttest/vt_netmgr/vt_netmgr.py @@ -0,0 +1,113 @@ +""" +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 create_network(self, network_name, params): + """ + Create the network by its cartesian params. + + :param network_name: The network tag defined in cartesian params. + :type network_name: String. + :param params: The params for all the network. + :type params: Dict. + """ + network_obj = nt.Network(network_name, params) + network_obj.create() + self._networks.append(network_obj) + + 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_networks(self, descriptor): + """ + Filter the networks based on the descriptor. + NOTE: return all networks if descriptor is None. + + :param descriptor: The network name or the uuid. + :type descriptor: String. + """ + if not descriptor: + return self._networks + res = [] + for obj in self._networks: + if descriptor in (obj.uuid, obj.name, ): + res.append(obj) + return res + + def create_port(self, name, network_uuid, mac_addr, dev_type): + """ + Create the port by its cartesian params. + + :param name: The network tag defined in cartesian params. + :type name: String. + :param network_uuid: The uuid of the network. + :type network_uuid: String. + :param mac_addr: The mac address. + :type mac_addr: String. + :param dev_type: The device type. + :type dev_type: String. + """ + network_obj = port.Port(name, network_uuid, mac_addr, dev_type) + network_obj.create() + self._networks.append(network_obj) + + def delete_port(self): + pass + + def get_ports(self, descriptor): + """ + Filter the networks based on the descriptor. + NOTE: return all networks if descriptor is None. + + :param descriptor: The network name or the uuid. + :type descriptor: String. + """ + if not descriptor: + return self._ports + res = [] + for obj in self._ports: + if descriptor in (obj.uuid, obj.name, obj.mac_addr, + obj.network_uuid, ): + res.append(obj) + return res + + +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..90f42e51c2 --- /dev/null +++ b/virttest/vt_resmgr/resources/network/__init__.py @@ -0,0 +1,13 @@ +from enum import Enum + + +class PortType(Enum): + customized = 0 + tap = 1 + bridge = 2 + ovs = 3 + passt = 4 + sriov = 5 + vhost_vdpa = 6 + vhost_user = 7 + user = 8 diff --git a/virttest/vt_resmgr/resources/network/network.py b/virttest/vt_resmgr/resources/network/network.py new file mode 100644 index 0000000000..d9c9de2e4b --- /dev/null +++ b/virttest/vt_resmgr/resources/network/network.py @@ -0,0 +1,28 @@ +from abc import ABC +import uuid + + +class Network(ABC): + def __init__(self, name, params): + self.uuid = uuid.uuid4().hex + self.name = name + self._type = params["type"] + self._spec = 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..4193e11c02 --- /dev/null +++ b/virttest/vt_resmgr/resources/network/port.py @@ -0,0 +1,26 @@ +from abc import ABC +import uuid + +from virttest.vt_resmgr.resources.network import PortType + + +class Port(ABC): + def __init__(self, name, network_uuid, mac_addr, dev_type): + self.uuid = uuid.uuid4().hex + self.name = name + self.network_uuid = network_uuid + self.mac_addr = mac_addr + if dev_type not in PortType: + raise NotImplementedError("This type is NOT supported!") + self.type = dev_type + self.created_by_vt = None + + def create(self): + # TODO + # create the port + pass + + def delete(self): + # TODO + # delete the port + pass