import ipaddress import random from enum import Enum from typing import Optional, List, Dict, Union class IpMethod(Enum): Manual = 0 Management = 1 Auto4 = 2 Auto6 = 3 Dhcp4 = 4 Dhcp6 = 5 class Interface: def __init__(self, method: IpMethod, rate: Optional[int] = None): self._method: IpMethod self._node: Optional[Node] = None self._rate: Optional[int] = None self._bridge: Optional[Bridge] = None self._method = method self._rate = rate self._address: ipaddress.ip_address = None def get_method(self): return self._method def set_node(self, node): self._node = node def get_node(self): return self._node def set_bridge(self, bridge): self._bridge = bridge def get_bridge(self): return self._bridge def set_address(self, addr: ipaddress.ip_address): self._address = addr def get_address(self) -> ipaddress.ip_address: return self._address def get_rate(self) -> Optional[int]: return self._rate def set_rate(self, rate: Optional[int]): self._rate = rate class Bridge: def __init__(self, *interfaces: Interface): self._interfaces: List[Interface] = [] self._name: str = '' for interface in interfaces: self._interfaces.append(interface) interface.set_bridge(self) # Generate a random class c private range by default (10.0.0.0) self._addr: ipaddress.ip_address = None self._network_iterator: iter = None self.netmask = 24 self.new_network() def get_interfaces(self) -> List[Interface]: return self._interfaces def set_name(self, name: str): self._name = name def get_name(self) -> str: return self._name def set_netmask(self, mask: int): self.netmask = mask self._network_iterator = ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False).hosts() def get_ip_address(self) -> ipaddress.ip_address: return next(self._network_iterator) def get_network(self) -> ipaddress.ip_network: return ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False) def get_network_string(self) -> str: return str(ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False)) def new_network(self): self._addr: ipaddress.ip_address = ipaddress.ip_address('10.0.0.0') + random.randint(0, 16777216) self._network_iterator = ipaddress.ip_network('{}/{}'.format(self._addr, self.netmask), False).hosts() class Node: def __init__(self, interfaces: List[Interface], setup_params: Dict = None): self._id: Union[int, None] = None self._interfaces: List[Interface] = interfaces self._interfaces.append(Interface(IpMethod.Management)) for interface in self._interfaces: interface.set_node(self) self.setup_params = {} if setup_params is None else setup_params def get_interfaces(self): return self._interfaces def set_id(self, new_id): self._id = new_id def get_id(self): return self._id def get_core_count(self) -> int: return 2 def get_memory_mb(self) -> int: return 2048 def get_internet_setup(self) -> Optional[str]: return None def get_setup(self) -> Optional[str]: return None def ssh(self, *args, **kwargs): raise RuntimeError('ssh not implemented')