2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
session.py: defines the Session class used by the core-daemon daemon program
|
|
|
|
that manages a CORE session.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2019-02-16 17:50:19 +00:00
|
|
|
import logging
|
2021-06-04 05:37:26 +01:00
|
|
|
import math
|
2017-04-25 16:45:34 +01:00
|
|
|
import os
|
2019-04-30 07:31:47 +01:00
|
|
|
import pwd
|
2017-04-25 16:45:34 +01:00
|
|
|
import shutil
|
2016-09-05 22:11:10 +01:00
|
|
|
import subprocess
|
2020-08-04 00:04:07 +01:00
|
|
|
import sys
|
2017-04-25 16:45:34 +01:00
|
|
|
import tempfile
|
|
|
|
import threading
|
|
|
|
import time
|
2020-10-11 16:22:33 +01:00
|
|
|
from pathlib import Path
|
2022-05-25 18:51:42 +01:00
|
|
|
from typing import Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar, Union
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2019-09-28 07:29:15 +01:00
|
|
|
from core import constants, utils
|
2020-06-10 04:03:32 +01:00
|
|
|
from core.configservice.manager import ConfigServiceManager
|
2020-07-02 23:37:51 +01:00
|
|
|
from core.emane.emanemanager import EmaneManager, EmaneState
|
2019-09-28 06:31:56 +01:00
|
|
|
from core.emane.nodes import EmaneNet
|
2020-06-10 04:03:32 +01:00
|
|
|
from core.emulator.data import (
|
|
|
|
ConfigData,
|
|
|
|
EventData,
|
|
|
|
ExceptionData,
|
|
|
|
FileData,
|
2020-06-16 20:50:24 +01:00
|
|
|
InterfaceData,
|
2020-06-10 04:03:32 +01:00
|
|
|
LinkData,
|
2020-06-16 20:50:24 +01:00
|
|
|
LinkOptions,
|
2020-06-10 04:03:32 +01:00
|
|
|
NodeData,
|
|
|
|
)
|
2019-10-17 19:10:59 +01:00
|
|
|
from core.emulator.distributed import DistributedController
|
2020-03-22 23:38:15 +00:00
|
|
|
from core.emulator.enumerations import (
|
|
|
|
EventTypes,
|
|
|
|
ExceptionLevels,
|
|
|
|
MessageFlags,
|
|
|
|
NodeTypes,
|
|
|
|
)
|
2022-03-17 22:28:38 +00:00
|
|
|
from core.emulator.links import CoreLink, LinkManager
|
2019-10-30 20:27:12 +00:00
|
|
|
from core.emulator.sessionconfig import SessionConfig
|
2019-09-28 07:29:15 +01:00
|
|
|
from core.errors import CoreError
|
2019-04-30 07:31:47 +01:00
|
|
|
from core.location.event import EventLoop
|
2020-02-21 23:54:55 +00:00
|
|
|
from core.location.geo import GeoLocation
|
2019-11-21 20:29:33 +00:00
|
|
|
from core.location.mobility import BasicRangeModel, MobilityManager
|
2022-05-25 18:51:42 +01:00
|
|
|
from core.nodes.base import CoreNode, CoreNodeBase, NodeBase, NodeOptions, Position
|
2019-09-26 21:00:12 +01:00
|
|
|
from core.nodes.docker import DockerNode
|
2021-12-21 16:59:48 +00:00
|
|
|
from core.nodes.interface import DEFAULT_MTU, CoreInterface
|
2019-09-26 21:00:12 +01:00
|
|
|
from core.nodes.lxd import LxcNode
|
|
|
|
from core.nodes.network import (
|
|
|
|
CtrlNet,
|
|
|
|
GreTapBridge,
|
|
|
|
HubNode,
|
|
|
|
PtpNet,
|
|
|
|
SwitchNode,
|
|
|
|
TunnelNode,
|
|
|
|
WlanNode,
|
|
|
|
)
|
|
|
|
from core.nodes.physical import PhysicalNode, Rj45Node
|
2022-03-29 20:16:07 +01:00
|
|
|
from core.nodes.wireless import WirelessNode
|
2019-04-30 07:31:47 +01:00
|
|
|
from core.plugins.sdt import Sdt
|
2020-02-15 00:25:05 +00:00
|
|
|
from core.services.coreservices import CoreServices
|
2019-09-10 22:20:51 +01:00
|
|
|
from core.xml import corexml, corexmldeployment
|
2019-05-02 07:17:46 +01:00
|
|
|
from core.xml.corexml import CoreXmlReader, CoreXmlWriter
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-09-26 21:00:12 +01:00
|
|
|
# maps for converting from API call node type values to classes and vice versa
|
2020-06-14 06:01:07 +01:00
|
|
|
NODES: Dict[NodeTypes, Type[NodeBase]] = {
|
2019-09-26 21:00:12 +01:00
|
|
|
NodeTypes.DEFAULT: CoreNode,
|
|
|
|
NodeTypes.PHYSICAL: PhysicalNode,
|
|
|
|
NodeTypes.SWITCH: SwitchNode,
|
|
|
|
NodeTypes.HUB: HubNode,
|
|
|
|
NodeTypes.WIRELESS_LAN: WlanNode,
|
|
|
|
NodeTypes.RJ45: Rj45Node,
|
|
|
|
NodeTypes.TUNNEL: TunnelNode,
|
2019-09-28 06:31:56 +01:00
|
|
|
NodeTypes.EMANE: EmaneNet,
|
2019-09-26 21:00:12 +01:00
|
|
|
NodeTypes.TAP_BRIDGE: GreTapBridge,
|
|
|
|
NodeTypes.PEER_TO_PEER: PtpNet,
|
|
|
|
NodeTypes.CONTROL_NET: CtrlNet,
|
|
|
|
NodeTypes.DOCKER: DockerNode,
|
|
|
|
NodeTypes.LXC: LxcNode,
|
2022-03-31 05:13:28 +01:00
|
|
|
NodeTypes.WIRELESS: WirelessNode,
|
2019-09-26 21:00:12 +01:00
|
|
|
}
|
2020-06-14 06:01:07 +01:00
|
|
|
NODES_TYPE: Dict[Type[NodeBase], NodeTypes] = {NODES[x]: x for x in NODES}
|
|
|
|
CONTAINER_NODES: Set[Type[NodeBase]] = {DockerNode, LxcNode}
|
|
|
|
CTRL_NET_ID: int = 9001
|
|
|
|
LINK_COLORS: List[str] = ["green", "blue", "orange", "purple", "turquoise"]
|
|
|
|
NT: TypeVar = TypeVar("NT", bound=NodeBase)
|
2022-03-31 05:13:28 +01:00
|
|
|
WIRELESS_TYPE: Tuple[Type[WlanNode], Type[EmaneNet], Type[WirelessNode]] = (
|
|
|
|
WlanNode,
|
|
|
|
EmaneNet,
|
|
|
|
WirelessNode,
|
|
|
|
)
|
2019-09-26 21:00:12 +01:00
|
|
|
|
2018-01-04 16:19:34 +00:00
|
|
|
|
2019-10-23 17:31:07 +01:00
|
|
|
class Session:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
CORE session manager.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def __init__(
|
|
|
|
self, _id: int, config: Dict[str, str] = None, mkdir: bool = True
|
|
|
|
) -> None:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Create a Session instance.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param _id: session id
|
|
|
|
:param config: session configuration
|
|
|
|
:param mkdir: flag to determine if a directory should be made
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
2020-06-10 04:03:32 +01:00
|
|
|
self.id: int = _id
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
# define and create session directory when desired
|
2021-03-19 23:56:54 +00:00
|
|
|
self.directory: Path = Path(tempfile.gettempdir()) / f"pycore.{self.id}"
|
2013-10-25 16:21:08 +01:00
|
|
|
if mkdir:
|
2021-03-19 23:56:54 +00:00
|
|
|
self.directory.mkdir()
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-06-10 04:03:32 +01:00
|
|
|
self.name: Optional[str] = None
|
2021-03-19 23:54:24 +00:00
|
|
|
self.file_path: Optional[Path] = None
|
|
|
|
self.thumbnail: Optional[Path] = None
|
2020-06-10 04:03:32 +01:00
|
|
|
self.user: Optional[str] = None
|
|
|
|
self.event_loop: EventLoop = EventLoop()
|
|
|
|
self.link_colors: Dict[int, str] = {}
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2019-04-30 07:31:47 +01:00
|
|
|
# dict of nodes: all nodes and nets
|
2020-06-10 04:03:32 +01:00
|
|
|
self.nodes: Dict[int, NodeBase] = {}
|
2022-03-17 22:28:38 +00:00
|
|
|
self.nodes_lock: threading.Lock = threading.Lock()
|
|
|
|
self.link_manager: LinkManager = LinkManager()
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-06-13 04:22:51 +01:00
|
|
|
# states and hooks handlers
|
2020-06-10 04:03:32 +01:00
|
|
|
self.state: EventTypes = EventTypes.DEFINITION_STATE
|
2020-06-13 04:22:51 +01:00
|
|
|
self.state_time: float = time.monotonic()
|
2020-07-28 02:19:51 +01:00
|
|
|
self.hooks: Dict[EventTypes, List[Tuple[str, str]]] = {}
|
2020-06-13 04:22:51 +01:00
|
|
|
self.state_hooks: Dict[EventTypes, List[Callable[[EventTypes], None]]] = {}
|
2019-09-10 23:10:24 +01:00
|
|
|
self.add_state_hook(
|
2020-03-07 06:35:23 +00:00
|
|
|
state=EventTypes.RUNTIME_STATE, hook=self.runtime_state_hook
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2017-08-07 23:37:41 +01:00
|
|
|
# handlers for broadcasting information
|
2020-06-10 04:03:32 +01:00
|
|
|
self.event_handlers: List[Callable[[EventData], None]] = []
|
|
|
|
self.exception_handlers: List[Callable[[ExceptionData], None]] = []
|
|
|
|
self.node_handlers: List[Callable[[NodeData], None]] = []
|
|
|
|
self.link_handlers: List[Callable[[LinkData], None]] = []
|
|
|
|
self.file_handlers: List[Callable[[FileData], None]] = []
|
|
|
|
self.config_handlers: List[Callable[[ConfigData], None]] = []
|
2017-08-07 23:37:41 +01:00
|
|
|
|
2018-06-12 16:37:39 +01:00
|
|
|
# session options/metadata
|
2022-04-01 19:46:28 +01:00
|
|
|
self.options: SessionConfig = SessionConfig(config)
|
2020-06-10 04:03:32 +01:00
|
|
|
self.metadata: Dict[str, str] = {}
|
2018-06-12 16:37:39 +01:00
|
|
|
|
2019-10-17 19:10:59 +01:00
|
|
|
# distributed support and logic
|
2020-06-10 04:03:32 +01:00
|
|
|
self.distributed: DistributedController = DistributedController(self)
|
2019-10-11 20:57:37 +01:00
|
|
|
|
2018-06-12 16:37:39 +01:00
|
|
|
# initialize session feature helpers
|
2020-06-10 04:03:32 +01:00
|
|
|
self.location: GeoLocation = GeoLocation()
|
|
|
|
self.mobility: MobilityManager = MobilityManager(self)
|
|
|
|
self.services: CoreServices = CoreServices(self)
|
|
|
|
self.emane: EmaneManager = EmaneManager(self)
|
|
|
|
self.sdt: Sdt = Sdt(self)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-18 05:09:51 +00:00
|
|
|
# config services
|
2020-06-10 04:03:32 +01:00
|
|
|
self.service_manager: Optional[ConfigServiceManager] = None
|
2020-01-18 05:09:51 +00:00
|
|
|
|
2019-09-26 21:00:12 +01:00
|
|
|
@classmethod
|
2020-01-11 06:37:19 +00:00
|
|
|
def get_node_class(cls, _type: NodeTypes) -> Type[NodeBase]:
|
2019-09-26 21:00:12 +01:00
|
|
|
"""
|
|
|
|
Retrieve the class for a given node type.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param _type: node type to get class for
|
2019-09-26 21:00:12 +01:00
|
|
|
:return: node class
|
|
|
|
"""
|
|
|
|
node_class = NODES.get(_type)
|
|
|
|
if node_class is None:
|
2019-10-18 18:33:31 +01:00
|
|
|
raise CoreError(f"invalid node type: {_type}")
|
2019-09-26 21:00:12 +01:00
|
|
|
return node_class
|
|
|
|
|
|
|
|
@classmethod
|
2020-01-11 06:37:19 +00:00
|
|
|
def get_node_type(cls, _class: Type[NodeBase]) -> NodeTypes:
|
2019-09-26 21:00:12 +01:00
|
|
|
"""
|
|
|
|
Retrieve node type for a given node class.
|
|
|
|
|
|
|
|
:param _class: node class to get a node type for
|
|
|
|
:return: node type
|
2020-01-18 05:12:14 +00:00
|
|
|
:raises CoreError: when node type does not exist
|
2019-09-26 21:00:12 +01:00
|
|
|
"""
|
|
|
|
node_type = NODES_TYPE.get(_class)
|
|
|
|
if node_type is None:
|
2019-10-18 18:33:31 +01:00
|
|
|
raise CoreError(f"invalid node class: {_class}")
|
2019-09-26 21:00:12 +01:00
|
|
|
return node_type
|
|
|
|
|
2020-06-26 06:05:10 +01:00
|
|
|
def use_ovs(self) -> bool:
|
2022-04-04 23:13:31 +01:00
|
|
|
return self.options.get_int("ovs") == 1
|
2020-06-26 06:05:10 +01:00
|
|
|
|
2022-03-21 22:42:14 +00:00
|
|
|
def linked(
|
|
|
|
self, node1_id: int, node2_id: int, iface1_id: int, iface2_id: int, linked: bool
|
2020-06-11 21:59:29 +01:00
|
|
|
) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
2022-03-21 22:42:14 +00:00
|
|
|
Links or unlinks wired core link interfaces from being connected to the same
|
|
|
|
bridge.
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2022-03-21 22:42:14 +00:00
|
|
|
:param node1_id: first node in link
|
|
|
|
:param node2_id: second node in link
|
|
|
|
:param iface1_id: node1 interface
|
|
|
|
:param iface2_id: node2 interface
|
|
|
|
:param linked: True if interfaces should be connected, False for disconnected
|
2019-05-02 07:17:46 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2022-03-21 22:42:14 +00:00
|
|
|
node1 = self.get_node(node1_id, NodeBase)
|
|
|
|
node2 = self.get_node(node2_id, NodeBase)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2022-03-21 22:42:14 +00:00
|
|
|
"link node(%s):interface(%s) node(%s):interface(%s) linked(%s)",
|
2020-06-13 00:52:41 +01:00
|
|
|
node1.name,
|
2022-03-21 22:42:14 +00:00
|
|
|
iface1_id,
|
2020-06-13 00:52:41 +01:00
|
|
|
node2.name,
|
2022-03-21 22:42:14 +00:00
|
|
|
iface2_id,
|
|
|
|
linked,
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2022-03-21 22:42:14 +00:00
|
|
|
iface1 = node1.get_iface(iface1_id)
|
|
|
|
iface2 = node2.get_iface(iface2_id)
|
|
|
|
core_link = self.link_manager.get_link(node1, iface1, node2, iface2)
|
|
|
|
if not core_link:
|
|
|
|
raise CoreError(
|
|
|
|
f"there is no link for node({node1.name}):interface({iface1_id}) "
|
|
|
|
f"node({node2.name}):interface({iface2_id})"
|
|
|
|
)
|
|
|
|
if linked:
|
|
|
|
core_link.ptp.attach(iface1)
|
|
|
|
core_link.ptp.attach(iface2)
|
|
|
|
else:
|
|
|
|
core_link.ptp.detach(iface1)
|
|
|
|
core_link.ptp.detach(iface2)
|
2020-06-26 06:05:10 +01:00
|
|
|
|
2019-09-10 23:10:24 +01:00
|
|
|
def add_link(
|
|
|
|
self,
|
2020-06-13 00:52:41 +01:00
|
|
|
node1_id: int,
|
|
|
|
node2_id: int,
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_data: InterfaceData = None,
|
|
|
|
iface2_data: InterfaceData = None,
|
2020-06-09 21:46:26 +01:00
|
|
|
options: LinkOptions = None,
|
2022-03-17 22:28:38 +00:00
|
|
|
) -> Tuple[Optional[CoreInterface], Optional[CoreInterface]]:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
Add a link between nodes.
|
|
|
|
|
2020-06-13 00:52:41 +01:00
|
|
|
:param node1_id: node one id
|
|
|
|
:param node2_id: node two id
|
2020-06-16 17:30:16 +01:00
|
|
|
:param iface1_data: node one interface
|
2020-01-11 06:37:19 +00:00
|
|
|
data, defaults to none
|
2020-06-16 17:30:16 +01:00
|
|
|
:param iface2_data: node two interface
|
2020-01-11 06:37:19 +00:00
|
|
|
data, defaults to none
|
2020-06-09 21:46:26 +01:00
|
|
|
:param options: data for creating link,
|
2020-01-11 06:37:19 +00:00
|
|
|
defaults to no options
|
2020-02-14 21:18:05 +00:00
|
|
|
:return: tuple of created core interfaces, depending on link
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
2022-03-17 22:28:38 +00:00
|
|
|
options = options if options else LinkOptions()
|
2021-12-21 16:59:48 +00:00
|
|
|
# set mtu
|
2022-04-04 23:13:31 +01:00
|
|
|
mtu = self.options.get_int("mtu") or DEFAULT_MTU
|
2021-12-21 16:59:48 +00:00
|
|
|
if iface1_data:
|
|
|
|
iface1_data.mtu = mtu
|
|
|
|
if iface2_data:
|
|
|
|
iface2_data.mtu = mtu
|
2022-03-17 22:28:38 +00:00
|
|
|
node1 = self.get_node(node1_id, NodeBase)
|
|
|
|
node2 = self.get_node(node2_id, NodeBase)
|
|
|
|
# check for invalid linking
|
2022-03-21 22:42:14 +00:00
|
|
|
if (
|
|
|
|
isinstance(node1, WIRELESS_TYPE)
|
|
|
|
and isinstance(node2, WIRELESS_TYPE)
|
|
|
|
or isinstance(node1, WIRELESS_TYPE)
|
|
|
|
and not isinstance(node2, CoreNodeBase)
|
|
|
|
or not isinstance(node1, CoreNodeBase)
|
|
|
|
and isinstance(node2, WIRELESS_TYPE)
|
|
|
|
):
|
2022-03-17 22:28:38 +00:00
|
|
|
raise CoreError(f"cannot link node({type(node1)}) node({type(node2)})")
|
|
|
|
# custom links
|
|
|
|
iface1 = None
|
|
|
|
iface2 = None
|
2022-03-29 20:16:07 +01:00
|
|
|
if isinstance(node1, (WlanNode, WirelessNode)):
|
2022-03-17 22:28:38 +00:00
|
|
|
iface2 = self._add_wlan_link(node2, iface2_data, node1)
|
2022-03-29 20:16:07 +01:00
|
|
|
elif isinstance(node2, (WlanNode, WirelessNode)):
|
2022-03-17 22:28:38 +00:00
|
|
|
iface1 = self._add_wlan_link(node1, iface1_data, node2)
|
|
|
|
elif isinstance(node1, EmaneNet) and isinstance(node2, CoreNode):
|
|
|
|
iface2 = self._add_emane_link(node2, iface2_data, node1)
|
|
|
|
elif isinstance(node2, EmaneNet) and isinstance(node1, CoreNode):
|
|
|
|
iface1 = self._add_emane_link(node1, iface1_data, node2)
|
2020-06-11 21:59:29 +01:00
|
|
|
else:
|
2022-03-17 22:28:38 +00:00
|
|
|
iface1, iface2 = self._add_wired_link(
|
|
|
|
node1, node2, iface1_data, iface2_data, options
|
|
|
|
)
|
|
|
|
# configure tunnel nodes
|
|
|
|
key = options.key
|
|
|
|
if isinstance(node1, TunnelNode):
|
|
|
|
logger.info("setting tunnel key for: %s", node1.name)
|
|
|
|
node1.setkey(key, iface1_data)
|
|
|
|
if isinstance(node2, TunnelNode):
|
|
|
|
logger.info("setting tunnel key for: %s", node2.name)
|
|
|
|
node2.setkey(key, iface2_data)
|
2020-06-13 00:52:41 +01:00
|
|
|
self.sdt.add_link(node1_id, node2_id)
|
2020-06-16 17:30:16 +01:00
|
|
|
return iface1, iface2
|
2020-02-14 21:18:05 +00:00
|
|
|
|
2022-03-17 22:28:38 +00:00
|
|
|
def _add_wlan_link(
|
2022-03-31 05:13:28 +01:00
|
|
|
self,
|
|
|
|
node: NodeBase,
|
|
|
|
iface_data: InterfaceData,
|
|
|
|
net: Union[WlanNode, WirelessNode],
|
2022-03-17 22:28:38 +00:00
|
|
|
) -> CoreInterface:
|
|
|
|
"""
|
|
|
|
Create a wlan link.
|
|
|
|
|
|
|
|
:param node: node to link to wlan network
|
|
|
|
:param iface_data: data to create interface with
|
|
|
|
:param net: wlan network to link to
|
|
|
|
:return: interface created for node
|
|
|
|
"""
|
|
|
|
# create interface
|
|
|
|
iface = node.create_iface(iface_data)
|
|
|
|
# attach to wlan
|
|
|
|
net.attach(iface)
|
|
|
|
# track link
|
|
|
|
core_link = CoreLink(node, iface, net, None)
|
|
|
|
self.link_manager.add(core_link)
|
|
|
|
return iface
|
|
|
|
|
|
|
|
def _add_emane_link(
|
|
|
|
self, node: CoreNode, iface_data: InterfaceData, net: EmaneNet
|
|
|
|
) -> CoreInterface:
|
|
|
|
"""
|
|
|
|
Create am emane link.
|
|
|
|
|
|
|
|
:param node: node to link to emane network
|
|
|
|
:param iface_data: data to create interface with
|
|
|
|
:param net: emane network to link to
|
|
|
|
:return: interface created for node
|
|
|
|
"""
|
|
|
|
# create iface tuntap
|
|
|
|
iface = net.create_tuntap(node, iface_data)
|
|
|
|
# track link
|
|
|
|
core_link = CoreLink(node, iface, net, None)
|
|
|
|
self.link_manager.add(core_link)
|
|
|
|
return iface
|
|
|
|
|
|
|
|
def _add_wired_link(
|
2019-09-10 23:10:24 +01:00
|
|
|
self,
|
2022-03-17 22:28:38 +00:00
|
|
|
node1: NodeBase,
|
|
|
|
node2: NodeBase,
|
|
|
|
iface1_data: InterfaceData = None,
|
|
|
|
iface2_data: InterfaceData = None,
|
|
|
|
options: LinkOptions = None,
|
|
|
|
) -> Tuple[CoreInterface, CoreInterface]:
|
|
|
|
"""
|
|
|
|
Create a wired link between two nodes.
|
|
|
|
|
|
|
|
:param node1: first node to be linked
|
|
|
|
:param node2: second node to be linked
|
|
|
|
:param iface1_data: data to create interface for node1
|
|
|
|
:param iface2_data: data to create interface for node2
|
|
|
|
:param options: options to configure interfaces with
|
|
|
|
:return: interfaces created for both nodes
|
|
|
|
"""
|
2022-03-18 22:53:13 +00:00
|
|
|
# create interfaces
|
2022-03-17 22:28:38 +00:00
|
|
|
iface1 = node1.create_iface(iface1_data, options)
|
|
|
|
iface2 = node2.create_iface(iface2_data, options)
|
|
|
|
# join and attach to ptp bridge
|
|
|
|
ptp = self.create_node(PtpNet, self.state.should_start())
|
|
|
|
ptp.attach(iface1)
|
|
|
|
ptp.attach(iface2)
|
|
|
|
# track link
|
|
|
|
core_link = CoreLink(node1, iface1, node2, iface2, ptp)
|
|
|
|
self.link_manager.add(core_link)
|
2022-03-18 19:31:04 +00:00
|
|
|
# setup link for gre tunnels if needed
|
|
|
|
if ptp.up:
|
|
|
|
self.distributed.create_gre_tunnels(core_link)
|
2022-03-17 22:28:38 +00:00
|
|
|
return iface1, iface2
|
|
|
|
|
|
|
|
def delete_link(
|
|
|
|
self, node1_id: int, node2_id: int, iface1_id: int = None, iface2_id: int = None
|
2020-01-11 06:37:19 +00:00
|
|
|
) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
Delete a link between nodes.
|
|
|
|
|
2020-06-13 00:52:41 +01:00
|
|
|
:param node1_id: node one id
|
|
|
|
:param node2_id: node two id
|
2020-06-16 17:30:16 +01:00
|
|
|
:param iface1_id: interface id for node one
|
|
|
|
:param iface2_id: interface id for node two
|
2019-05-02 07:17:46 +01:00
|
|
|
:return: nothing
|
2019-09-11 21:12:42 +01:00
|
|
|
:raises core.CoreError: when no common network is found for link being deleted
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = self.get_node(node1_id, NodeBase)
|
|
|
|
node2 = self.get_node(node2_id, NodeBase)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2022-03-17 22:28:38 +00:00
|
|
|
"deleting link node(%s):interface(%s) node(%s):interface(%s)",
|
2020-06-11 21:59:29 +01:00
|
|
|
node1.name,
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_id,
|
2020-06-11 21:59:29 +01:00
|
|
|
node2.name,
|
2020-06-16 17:30:16 +01:00
|
|
|
iface2_id,
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2022-03-17 22:28:38 +00:00
|
|
|
iface1 = None
|
|
|
|
iface2 = None
|
2022-03-31 05:13:28 +01:00
|
|
|
if isinstance(node1, (WlanNode, WirelessNode)):
|
2022-03-17 22:28:38 +00:00
|
|
|
iface2 = node2.delete_iface(iface2_id)
|
|
|
|
node1.detach(iface2)
|
2022-03-31 05:13:28 +01:00
|
|
|
elif isinstance(node2, (WlanNode, WirelessNode)):
|
2022-03-17 22:28:38 +00:00
|
|
|
iface1 = node1.delete_iface(iface1_id)
|
|
|
|
node2.detach(iface1)
|
|
|
|
elif isinstance(node1, EmaneNet):
|
|
|
|
iface2 = node2.delete_iface(iface2_id)
|
|
|
|
node1.detach(iface2)
|
|
|
|
elif isinstance(node2, EmaneNet):
|
|
|
|
iface1 = node1.delete_iface(iface1_id)
|
|
|
|
node2.detach(iface1)
|
2020-06-11 21:59:29 +01:00
|
|
|
else:
|
2022-03-17 22:28:38 +00:00
|
|
|
iface1 = node1.delete_iface(iface1_id)
|
|
|
|
iface2 = node2.delete_iface(iface2_id)
|
|
|
|
core_link = self.link_manager.delete(node1, iface1, node2, iface2)
|
|
|
|
if core_link.ptp:
|
|
|
|
self.delete_node(core_link.ptp.id)
|
2020-06-13 00:52:41 +01:00
|
|
|
self.sdt.delete_link(node1_id, node2_id)
|
2020-02-28 05:39:18 +00:00
|
|
|
|
2019-09-10 23:10:24 +01:00
|
|
|
def update_link(
|
|
|
|
self,
|
2020-06-13 00:52:41 +01:00
|
|
|
node1_id: int,
|
|
|
|
node2_id: int,
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_id: int = None,
|
|
|
|
iface2_id: int = None,
|
2020-06-09 21:46:26 +01:00
|
|
|
options: LinkOptions = None,
|
2020-01-11 06:37:19 +00:00
|
|
|
) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
Update link information between nodes.
|
|
|
|
|
2020-06-13 00:52:41 +01:00
|
|
|
:param node1_id: node one id
|
|
|
|
:param node2_id: node two id
|
2020-06-16 17:30:16 +01:00
|
|
|
:param iface1_id: interface id for node one
|
|
|
|
:param iface2_id: interface id for node two
|
2020-06-09 21:46:26 +01:00
|
|
|
:param options: data to update link with
|
2019-05-02 07:17:46 +01:00
|
|
|
:return: nothing
|
2020-06-11 21:59:29 +01:00
|
|
|
:raises core.CoreError: when updating a wireless type link, when there is a
|
|
|
|
unknown link between networks
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
2020-06-09 21:46:26 +01:00
|
|
|
if not options:
|
|
|
|
options = LinkOptions()
|
2020-06-13 00:52:41 +01:00
|
|
|
node1 = self.get_node(node1_id, NodeBase)
|
|
|
|
node2 = self.get_node(node2_id, NodeBase)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2022-03-17 22:28:38 +00:00
|
|
|
"update link node(%s):interface(%s) node(%s):interface(%s)",
|
2020-06-11 21:59:29 +01:00
|
|
|
node1.name,
|
2020-06-16 17:30:16 +01:00
|
|
|
iface1_id,
|
2020-06-11 21:59:29 +01:00
|
|
|
node2.name,
|
2020-06-16 17:30:16 +01:00
|
|
|
iface2_id,
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2022-03-17 22:28:38 +00:00
|
|
|
iface1 = node1.get_iface(iface1_id) if iface1_id is not None else None
|
|
|
|
iface2 = node2.get_iface(iface2_id) if iface2_id is not None else None
|
|
|
|
core_link = self.link_manager.get_link(node1, iface1, node2, iface2)
|
|
|
|
if not core_link:
|
|
|
|
raise CoreError(
|
|
|
|
f"there is no link for node({node1.name}):interface({iface1_id}) "
|
|
|
|
f"node({node2.name}):interface({iface2_id})"
|
|
|
|
)
|
|
|
|
if iface1:
|
|
|
|
iface1.options.update(options)
|
2022-03-17 23:31:03 +00:00
|
|
|
iface1.set_config()
|
2022-03-17 22:28:38 +00:00
|
|
|
if iface2 and not options.unidirectional:
|
|
|
|
iface2.options.update(options)
|
2022-03-17 23:31:03 +00:00
|
|
|
iface2.set_config()
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2020-06-13 04:22:51 +01:00
|
|
|
def next_node_id(self) -> int:
|
2020-06-09 08:56:34 +01:00
|
|
|
"""
|
|
|
|
Find the next valid node id, starting from 1.
|
|
|
|
|
|
|
|
:return: next node id
|
|
|
|
"""
|
|
|
|
_id = 1
|
|
|
|
while True:
|
|
|
|
if _id not in self.nodes:
|
|
|
|
break
|
|
|
|
_id += 1
|
|
|
|
return _id
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def add_node(
|
2022-05-25 18:51:42 +01:00
|
|
|
self,
|
|
|
|
_class: Type[NT],
|
|
|
|
_id: int = None,
|
|
|
|
name: str = None,
|
|
|
|
server: str = None,
|
|
|
|
position: Position = None,
|
|
|
|
options: NodeOptions = None,
|
2020-05-21 06:14:03 +01:00
|
|
|
) -> NT:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
Add a node to the session, based on the provided node data.
|
|
|
|
|
2020-05-21 06:14:03 +01:00
|
|
|
:param _class: node class to create
|
2020-01-16 19:00:57 +00:00
|
|
|
:param _id: id for node, defaults to None for generated id
|
2022-05-25 18:51:42 +01:00
|
|
|
:param name: name to assign to node
|
|
|
|
:param server: distributed server for node, if desired
|
|
|
|
:param position: geo or x/y/z position to set
|
|
|
|
:param options: options to create node with
|
2019-05-02 07:17:46 +01:00
|
|
|
:return: created node
|
2019-09-26 21:00:12 +01:00
|
|
|
:raises core.CoreError: when an invalid node type is given
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
# set node start based on current session state, override and check when rj45
|
2020-03-07 06:35:23 +00:00
|
|
|
start = self.state.should_start()
|
2022-04-04 23:13:31 +01:00
|
|
|
enable_rj45 = self.options.get_int("enablerj45") == 1
|
2020-05-21 06:14:03 +01:00
|
|
|
if _class == Rj45Node and not enable_rj45:
|
2019-05-02 07:17:46 +01:00
|
|
|
start = False
|
2022-05-25 18:51:42 +01:00
|
|
|
# generate options if not provided
|
|
|
|
options = options if options else _class.create_options()
|
2019-10-07 19:58:27 +01:00
|
|
|
# verify distributed server
|
2022-05-25 18:51:42 +01:00
|
|
|
dist_server = None
|
|
|
|
if server is not None:
|
|
|
|
dist_server = self.distributed.servers.get(server)
|
|
|
|
if not dist_server:
|
|
|
|
raise CoreError(f"invalid distributed server: {server}")
|
2019-05-02 07:17:46 +01:00
|
|
|
# create node
|
2022-05-25 18:51:42 +01:00
|
|
|
node = self.create_node(_class, start, _id, name, dist_server, options)
|
|
|
|
# set node position
|
|
|
|
position = position or Position()
|
|
|
|
if position.has_geo():
|
|
|
|
self.set_node_geo(node, position.lon, position.lat, position.alt)
|
2021-08-28 00:58:44 +01:00
|
|
|
else:
|
2022-05-25 18:51:42 +01:00
|
|
|
self.set_node_pos(node, position.x, position.y)
|
|
|
|
# setup default wlan
|
2019-11-21 20:44:50 +00:00
|
|
|
if isinstance(node, WlanNode):
|
2022-05-25 18:51:42 +01:00
|
|
|
self.mobility.set_model_config(self.id, BasicRangeModel.name)
|
|
|
|
# boot core nodes after runtime
|
|
|
|
is_runtime = self.state == EventTypes.RUNTIME_STATE
|
|
|
|
if is_runtime and isinstance(node, CoreNode):
|
2020-12-16 18:19:17 +00:00
|
|
|
self.add_remove_control_iface(node, remove=False)
|
2021-05-27 05:01:24 +01:00
|
|
|
self.boot_node(node)
|
2020-02-28 05:39:18 +00:00
|
|
|
self.sdt.add_node(node)
|
2019-05-02 07:17:46 +01:00
|
|
|
return node
|
|
|
|
|
2021-08-28 00:58:44 +01:00
|
|
|
def set_node_pos(self, node: NodeBase, x: float, y: float) -> None:
|
|
|
|
node.setposition(x, y, None)
|
|
|
|
self.sdt.edit_node(
|
|
|
|
node, node.position.lon, node.position.lat, node.position.alt
|
|
|
|
)
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2021-08-28 00:58:44 +01:00
|
|
|
def set_node_geo(self, node: NodeBase, lon: float, lat: float, alt: float) -> None:
|
|
|
|
x, y, _ = self.location.getxyz(lat, lon, alt)
|
|
|
|
if math.isinf(x) or math.isinf(y):
|
|
|
|
raise CoreError(
|
|
|
|
f"invalid geo for current reference/scale: {lon},{lat},{alt}"
|
|
|
|
)
|
|
|
|
node.setposition(x, y, None)
|
|
|
|
node.position.set_geo(lon, lat, alt)
|
|
|
|
self.sdt.edit_node(node, lon, lat, alt)
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2021-03-19 23:54:24 +00:00
|
|
|
def open_xml(self, file_path: Path, start: bool = False) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
Import a session from the EmulationScript XML format.
|
|
|
|
|
2021-03-19 23:54:24 +00:00
|
|
|
:param file_path: xml file to load session from
|
2020-01-16 19:00:57 +00:00
|
|
|
:param start: instantiate session if true, false otherwise
|
2019-05-02 07:17:46 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("opening xml: %s", file_path)
|
2019-05-02 07:17:46 +01:00
|
|
|
# clear out existing session
|
|
|
|
self.clear()
|
2020-06-26 05:34:45 +01:00
|
|
|
# set state and read xml
|
|
|
|
state = EventTypes.CONFIGURATION_STATE if start else EventTypes.DEFINITION_STATE
|
2019-10-22 20:08:55 +01:00
|
|
|
self.set_state(state)
|
2021-03-19 23:54:24 +00:00
|
|
|
self.name = file_path.name
|
|
|
|
self.file_path = file_path
|
|
|
|
CoreXmlReader(self).read(file_path)
|
2019-05-02 07:17:46 +01:00
|
|
|
# start session if needed
|
|
|
|
if start:
|
2020-06-26 05:34:45 +01:00
|
|
|
self.set_state(EventTypes.INSTANTIATION_STATE)
|
2019-05-02 07:17:46 +01:00
|
|
|
self.instantiate()
|
|
|
|
|
2021-03-19 23:54:24 +00:00
|
|
|
def save_xml(self, file_path: Path) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
Export a session to the EmulationScript XML format.
|
|
|
|
|
2021-03-19 23:54:24 +00:00
|
|
|
:param file_path: file name to write session xml to
|
2019-05-02 07:17:46 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-03-19 23:54:24 +00:00
|
|
|
CoreXmlWriter(self).write(file_path)
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2020-03-07 06:35:23 +00:00
|
|
|
def add_hook(
|
2021-03-19 23:54:24 +00:00
|
|
|
self, state: EventTypes, file_name: str, data: str, src_name: str = None
|
2020-03-07 06:35:23 +00:00
|
|
|
) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
|
|
|
Store a hook from a received file message.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param state: when to run hook
|
|
|
|
:param file_name: file name for hook
|
2019-05-02 07:17:46 +01:00
|
|
|
:param data: hook data
|
2021-03-19 23:54:24 +00:00
|
|
|
:param src_name: source name
|
2019-05-02 07:17:46 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2021-03-19 23:54:24 +00:00
|
|
|
"setting state hook: %s - %s source(%s)", state, file_name, src_name
|
2020-03-07 06:35:23 +00:00
|
|
|
)
|
|
|
|
hook = file_name, data
|
2020-06-13 04:22:51 +01:00
|
|
|
state_hooks = self.hooks.setdefault(state, [])
|
2020-03-07 06:35:23 +00:00
|
|
|
state_hooks.append(hook)
|
|
|
|
|
|
|
|
# immediately run a hook if it is in the current state
|
|
|
|
if self.state == state:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("immediately running new state hook")
|
2020-03-07 06:35:23 +00:00
|
|
|
self.run_hook(hook)
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def clear(self) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
2019-10-15 22:13:42 +01:00
|
|
|
Clear all CORE session data. (nodes, hooks, etc)
|
2019-05-02 07:17:46 +01:00
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2019-10-24 21:05:02 +01:00
|
|
|
self.emane.shutdown()
|
2019-05-02 07:17:46 +01:00
|
|
|
self.delete_nodes()
|
2022-03-17 22:28:38 +00:00
|
|
|
self.link_manager.reset()
|
2019-10-17 19:10:59 +01:00
|
|
|
self.distributed.shutdown()
|
2020-06-13 04:22:51 +01:00
|
|
|
self.hooks.clear()
|
2019-05-02 07:17:46 +01:00
|
|
|
self.emane.reset()
|
2019-10-24 21:05:02 +01:00
|
|
|
self.emane.config_reset()
|
|
|
|
self.location.reset()
|
|
|
|
self.services.reset()
|
|
|
|
self.mobility.config_reset()
|
2020-04-15 23:41:37 +01:00
|
|
|
self.link_colors.clear()
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def set_location(self, lat: float, lon: float, alt: float, scale: float) -> None:
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
2019-10-23 05:27:31 +01:00
|
|
|
Set session geospatial location.
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param lat: latitude
|
|
|
|
:param lon: longitude
|
|
|
|
:param alt: altitude
|
|
|
|
:param scale: reference scale
|
2019-10-23 05:27:31 +01:00
|
|
|
:return: nothing
|
2019-05-02 07:17:46 +01:00
|
|
|
"""
|
2019-10-23 05:27:31 +01:00
|
|
|
self.location.setrefgeo(lat, lon, alt)
|
|
|
|
self.location.refscale = scale
|
2019-05-02 07:17:46 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def shutdown(self) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-04-30 07:31:47 +01:00
|
|
|
Shutdown all session nodes and remove the session directory.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-12-05 17:01:53 +00:00
|
|
|
if self.state == EventTypes.SHUTDOWN_STATE:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("session(%s) state(%s) already shutdown", self.id, self.state)
|
2021-03-12 17:51:55 +00:00
|
|
|
else:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("session(%s) state(%s) shutting down", self.id, self.state)
|
2021-03-12 17:51:55 +00:00
|
|
|
self.set_state(EventTypes.SHUTDOWN_STATE, send_event=True)
|
|
|
|
# clear out current core session
|
|
|
|
self.clear()
|
|
|
|
# shutdown sdt
|
|
|
|
self.sdt.shutdown()
|
2017-04-25 16:45:34 +01:00
|
|
|
# remove this sessions working directory
|
2022-04-04 23:13:31 +01:00
|
|
|
preserve = self.options.get_int("preservedir") == 1
|
2013-08-29 15:21:13 +01:00
|
|
|
if not preserve:
|
2021-03-19 23:56:54 +00:00
|
|
|
shutil.rmtree(self.directory, ignore_errors=True)
|
2017-05-04 21:49:14 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def broadcast_event(self, event_data: EventData) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handle event data that should be provided to event handler.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param event_data: event data to send out
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
for handler in self.event_handlers:
|
|
|
|
handler(event_data)
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def broadcast_exception(self, exception_data: ExceptionData) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handle exception data that should be provided to exception handlers.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param exception_data: exception data to send out
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
for handler in self.exception_handlers:
|
|
|
|
handler(exception_data)
|
|
|
|
|
2020-03-22 23:38:15 +00:00
|
|
|
def broadcast_node(
|
|
|
|
self,
|
|
|
|
node: NodeBase,
|
|
|
|
message_type: MessageFlags = MessageFlags.NONE,
|
|
|
|
source: str = None,
|
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handle node data that should be provided to node handlers.
|
|
|
|
|
2020-03-22 23:38:15 +00:00
|
|
|
:param node: node to broadcast
|
|
|
|
:param message_type: type of message to broadcast, None by default
|
|
|
|
:param source: source of broadcast, None by default
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2020-06-18 17:06:31 +01:00
|
|
|
node_data = NodeData(node=node, message_type=message_type, source=source)
|
2017-04-25 16:45:34 +01:00
|
|
|
for handler in self.node_handlers:
|
|
|
|
handler(node_data)
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def broadcast_file(self, file_data: FileData) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handle file data that should be provided to file handlers.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param file_data: file data to send out
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
for handler in self.file_handlers:
|
|
|
|
handler(file_data)
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def broadcast_config(self, config_data: ConfigData) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handle config data that should be provided to config handlers.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param config_data: config data to send out
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
for handler in self.config_handlers:
|
|
|
|
handler(config_data)
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def broadcast_link(self, link_data: LinkData) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handle link data that should be provided to link handlers.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param link_data: link data to send out
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
|
|
|
for handler in self.link_handlers:
|
|
|
|
handler(link_data)
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def set_state(self, state: EventTypes, send_event: bool = False) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Set the session's current state.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param state: state to set to
|
2017-04-25 16:45:34 +01:00
|
|
|
:param send_event: if true, generate core API event messages
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-03-07 06:35:23 +00:00
|
|
|
if self.state == state:
|
2017-05-04 23:24:45 +01:00
|
|
|
return
|
2020-03-07 06:35:23 +00:00
|
|
|
self.state = state
|
2020-06-13 04:22:51 +01:00
|
|
|
self.state_time = time.monotonic()
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("changing session(%s) to state %s", self.id, state.name)
|
2020-03-07 06:35:23 +00:00
|
|
|
self.run_hooks(state)
|
|
|
|
self.run_state_hooks(state)
|
2017-04-25 16:45:34 +01:00
|
|
|
if send_event:
|
2020-03-07 06:35:23 +00:00
|
|
|
event_data = EventData(event_type=state, time=str(time.monotonic()))
|
2017-04-25 16:45:34 +01:00
|
|
|
self.broadcast_event(event_data)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-03-07 06:35:23 +00:00
|
|
|
def run_hooks(self, state: EventTypes) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-01-11 06:37:19 +00:00
|
|
|
Run hook scripts upon changing states. If hooks is not specified, run all hooks
|
|
|
|
in the given state.
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param state: state to run hooks for
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
hooks = self.hooks.get(state, [])
|
|
|
|
for hook in hooks:
|
2017-04-25 16:45:34 +01:00
|
|
|
self.run_hook(hook)
|
2014-09-23 17:26:22 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def run_hook(self, hook: Tuple[str, str]) -> None:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Run a hook.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param hook: hook to run
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2017-04-25 16:45:34 +01:00
|
|
|
file_name, data = hook
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("running hook %s", file_name)
|
2021-03-19 23:56:54 +00:00
|
|
|
file_path = self.directory / file_name
|
|
|
|
log_path = self.directory / f"{file_name}.log"
|
2017-04-25 16:45:34 +01:00
|
|
|
try:
|
2021-03-19 23:54:24 +00:00
|
|
|
with file_path.open("w") as f:
|
2020-06-13 04:22:51 +01:00
|
|
|
f.write(data)
|
2021-03-19 23:54:24 +00:00
|
|
|
with log_path.open("w") as f:
|
2020-06-13 04:22:51 +01:00
|
|
|
args = ["/bin/sh", file_name]
|
|
|
|
subprocess.check_call(
|
|
|
|
args,
|
|
|
|
stdout=f,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
close_fds=True,
|
2021-03-19 23:56:54 +00:00
|
|
|
cwd=self.directory,
|
2020-06-13 04:22:51 +01:00
|
|
|
env=self.get_environment(),
|
|
|
|
)
|
|
|
|
except (IOError, subprocess.CalledProcessError):
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("error running hook: %s", file_path)
|
2015-02-05 00:15:43 +00:00
|
|
|
|
2020-03-07 06:35:23 +00:00
|
|
|
def run_state_hooks(self, state: EventTypes) -> None:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Run state hooks.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param state: state to run hooks for
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
for hook in self.state_hooks.get(state, []):
|
|
|
|
self.run_state_hook(state, hook)
|
|
|
|
|
|
|
|
def run_state_hook(self, state: EventTypes, hook: Callable[[EventTypes], None]):
|
|
|
|
try:
|
|
|
|
hook(state)
|
|
|
|
except Exception:
|
|
|
|
message = f"exception occurred when running {state.name} state hook: {hook}"
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception(message)
|
2020-06-13 04:22:51 +01:00
|
|
|
self.exception(ExceptionLevels.ERROR, "Session.run_state_hooks", message)
|
2015-02-05 00:15:43 +00:00
|
|
|
|
2020-03-07 06:35:23 +00:00
|
|
|
def add_state_hook(
|
|
|
|
self, state: EventTypes, hook: Callable[[EventTypes], None]
|
|
|
|
) -> None:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Add a state hook.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param state: state to add hook for
|
|
|
|
:param hook: hook callback for the state
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
hooks = self.state_hooks.setdefault(state, [])
|
2018-10-11 21:28:02 +01:00
|
|
|
if hook in hooks:
|
2019-09-11 21:12:42 +01:00
|
|
|
raise CoreError("attempting to add duplicate state hook")
|
2017-04-25 16:45:34 +01:00
|
|
|
hooks.append(hook)
|
|
|
|
if self.state == state:
|
2020-06-13 04:22:51 +01:00
|
|
|
self.run_state_hook(state, hook)
|
2015-02-05 00:15:43 +00:00
|
|
|
|
2020-06-13 04:22:51 +01:00
|
|
|
def del_state_hook(
|
|
|
|
self, state: EventTypes, hook: Callable[[EventTypes], None]
|
|
|
|
) -> None:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Delete a state hook.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param state: state to delete hook for
|
|
|
|
:param hook: hook to delete
|
2020-01-11 06:37:19 +00:00
|
|
|
:return: nothing
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
hooks = self.state_hooks.get(state, [])
|
|
|
|
if hook in hooks:
|
|
|
|
hooks.remove(hook)
|
2014-09-23 17:26:22 +01:00
|
|
|
|
2020-06-13 04:22:51 +01:00
|
|
|
def runtime_state_hook(self, _state: EventTypes) -> None:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Runtime state hook check.
|
|
|
|
|
2020-06-13 04:22:51 +01:00
|
|
|
:param _state: state to check
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
self.emane.poststartup()
|
|
|
|
# create session deployed xml
|
|
|
|
xml_writer = corexml.CoreXmlWriter(self)
|
|
|
|
corexmldeployment.CoreXmlDeployment(self, xml_writer.scenario)
|
2021-03-19 23:56:54 +00:00
|
|
|
xml_file_path = self.directory / "session-deployed.xml"
|
2021-03-19 23:54:24 +00:00
|
|
|
xml_writer.write(xml_file_path)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def get_environment(self, state: bool = True) -> Dict[str, str]:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Get an environment suitable for a subprocess.Popen call.
|
|
|
|
This is the current process environment with some session-specific
|
|
|
|
variables.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param state: flag to determine if session state should be included
|
2019-06-07 00:34:26 +01:00
|
|
|
:return: environment variables
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
env = os.environ.copy()
|
2020-08-04 00:04:07 +01:00
|
|
|
env["CORE_PYTHON"] = sys.executable
|
2019-10-18 18:33:31 +01:00
|
|
|
env["SESSION"] = str(self.id)
|
|
|
|
env["SESSION_SHORT"] = self.short_session_id()
|
2021-03-19 23:56:54 +00:00
|
|
|
env["SESSION_DIR"] = str(self.directory)
|
2019-10-18 18:33:31 +01:00
|
|
|
env["SESSION_NAME"] = str(self.name)
|
2021-03-19 23:54:24 +00:00
|
|
|
env["SESSION_FILENAME"] = str(self.file_path)
|
2019-10-18 18:33:31 +01:00
|
|
|
env["SESSION_USER"] = str(self.user)
|
2013-08-29 15:21:13 +01:00
|
|
|
if state:
|
2019-10-18 18:33:31 +01:00
|
|
|
env["SESSION_STATE"] = str(self.state)
|
2020-10-02 17:51:01 +01:00
|
|
|
# try reading and merging optional environments from:
|
2020-10-11 16:22:33 +01:00
|
|
|
# /etc/core/environment
|
2022-03-08 22:18:47 +00:00
|
|
|
# /home/user/.coregui/environment
|
2020-10-11 16:22:33 +01:00
|
|
|
# /tmp/pycore.<session id>/environment
|
2021-03-19 23:54:24 +00:00
|
|
|
core_env_path = constants.CORE_CONF_DIR / "environment"
|
2021-03-19 23:56:54 +00:00
|
|
|
session_env_path = self.directory / "environment"
|
2013-08-29 15:21:13 +01:00
|
|
|
if self.user:
|
2020-10-11 16:22:33 +01:00
|
|
|
user_home_path = Path(f"~{self.user}").expanduser()
|
2022-03-08 22:18:47 +00:00
|
|
|
user_env = user_home_path / ".coregui" / "environment"
|
|
|
|
paths = [core_env_path, user_env, session_env_path]
|
2020-10-11 16:22:33 +01:00
|
|
|
else:
|
|
|
|
paths = [core_env_path, session_env_path]
|
|
|
|
for path in paths:
|
|
|
|
if path.is_file():
|
|
|
|
try:
|
|
|
|
utils.load_config(path, env)
|
|
|
|
except IOError:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("error reading environment file: %s", path)
|
2013-08-29 15:21:13 +01:00
|
|
|
return env
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def set_user(self, user: str) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Set the username for this session. Update the permissions of the
|
|
|
|
session dir to allow the user write access.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param user: user to give write permissions to for the session directory
|
2017-04-25 16:45:34 +01:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.user = user
|
2022-03-08 23:50:19 +00:00
|
|
|
try:
|
|
|
|
uid = pwd.getpwnam(user).pw_uid
|
|
|
|
gid = self.directory.stat().st_gid
|
|
|
|
os.chown(self.directory, uid, gid)
|
|
|
|
except IOError:
|
|
|
|
logger.exception("failed to set permission on %s", self.directory)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-06-14 17:37:58 +01:00
|
|
|
def create_node(
|
2022-05-25 18:51:42 +01:00
|
|
|
self,
|
|
|
|
_class: Type[NT],
|
|
|
|
start: bool,
|
|
|
|
_id: int = None,
|
|
|
|
name: str = None,
|
|
|
|
server: str = None,
|
|
|
|
options: NodeOptions = None,
|
2020-06-14 17:37:58 +01:00
|
|
|
) -> NT:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-04-27 06:07:51 +01:00
|
|
|
Create an emulation node.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-05-21 06:14:03 +01:00
|
|
|
:param _class: node class to create
|
2020-06-14 17:37:58 +01:00
|
|
|
:param start: True to start node, False otherwise
|
2022-05-25 18:51:42 +01:00
|
|
|
:param _id: id for node, defaults to None for generated id
|
|
|
|
:param name: name to assign to node
|
|
|
|
:param server: distributed server for node, if desired
|
|
|
|
:param options: options to create node with
|
2019-04-30 07:31:47 +01:00
|
|
|
:return: the created node instance
|
2019-09-11 21:12:42 +01:00
|
|
|
:raises core.CoreError: when id of the node to create already exists
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
with self.nodes_lock:
|
2022-05-25 18:51:42 +01:00
|
|
|
node = _class(self, _id=_id, name=name, server=server, options=options)
|
2019-04-30 07:31:47 +01:00
|
|
|
if node.id in self.nodes:
|
2019-04-27 06:07:51 +01:00
|
|
|
node.shutdown()
|
2019-10-18 18:33:31 +01:00
|
|
|
raise CoreError(f"duplicate node id {node.id} for {node.name}")
|
2019-04-30 07:31:47 +01:00
|
|
|
self.nodes[node.id] = node
|
2022-05-25 18:51:42 +01:00
|
|
|
logger.info(
|
|
|
|
"created node(%s) id(%s) name(%s) start(%s)",
|
|
|
|
_class.__name__,
|
|
|
|
node.id,
|
|
|
|
node.name,
|
|
|
|
start,
|
|
|
|
)
|
2020-06-14 17:37:58 +01:00
|
|
|
if start:
|
|
|
|
node.startup()
|
2019-04-27 06:07:51 +01:00
|
|
|
return node
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-05-20 22:44:34 +01:00
|
|
|
def get_node(self, _id: int, _class: Type[NT]) -> NT:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-04-30 07:31:47 +01:00
|
|
|
Get a session node.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param _id: node id to retrieve
|
2020-05-20 22:44:34 +01:00
|
|
|
:param _class: expected node class
|
2019-04-30 07:31:47 +01:00
|
|
|
:return: node for the given id
|
2020-01-18 05:12:14 +00:00
|
|
|
:raises core.CoreError: when node does not exist
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
node = self.nodes.get(_id)
|
|
|
|
if node is None:
|
2019-10-18 18:33:31 +01:00
|
|
|
raise CoreError(f"unknown node id {_id}")
|
2020-05-20 22:44:34 +01:00
|
|
|
if not isinstance(node, _class):
|
|
|
|
actual = node.__class__.__name__
|
|
|
|
expected = _class.__name__
|
|
|
|
raise CoreError(f"node class({actual}) is not expected({expected})")
|
|
|
|
return node
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def delete_node(self, _id: int) -> bool:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-08-21 22:28:01 +01:00
|
|
|
Delete a node from the session and check if session should shutdown, if no nodes
|
|
|
|
are left.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param _id: id of node to delete
|
2019-04-30 07:31:47 +01:00
|
|
|
:return: True if node deleted, False otherwise
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2019-04-30 07:31:47 +01:00
|
|
|
# delete node and check for session shutdown if a node was removed
|
2019-10-25 05:17:15 +01:00
|
|
|
node = None
|
2020-06-13 04:22:51 +01:00
|
|
|
with self.nodes_lock:
|
2019-04-30 07:31:47 +01:00
|
|
|
if _id in self.nodes:
|
|
|
|
node = self.nodes.pop(_id)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("deleted node(%s)", node.name)
|
2019-10-25 05:17:15 +01:00
|
|
|
if node:
|
|
|
|
node.shutdown()
|
2020-02-28 05:39:18 +00:00
|
|
|
self.sdt.delete_node(_id)
|
2019-10-25 05:17:15 +01:00
|
|
|
return node is not None
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def delete_nodes(self) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-04-30 07:31:47 +01:00
|
|
|
Clear the nodes dictionary, and call shutdown for each node.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-12-17 05:32:47 +00:00
|
|
|
nodes_ids = []
|
2020-06-13 04:22:51 +01:00
|
|
|
with self.nodes_lock:
|
2019-10-29 17:25:39 +00:00
|
|
|
funcs = []
|
2019-04-30 07:31:47 +01:00
|
|
|
while self.nodes:
|
|
|
|
_, node = self.nodes.popitem()
|
2020-12-17 05:32:47 +00:00
|
|
|
nodes_ids.append(node.id)
|
2019-10-29 17:25:39 +00:00
|
|
|
funcs.append((node.shutdown, [], {}))
|
|
|
|
utils.threadpool(funcs)
|
2020-12-17 05:32:47 +00:00
|
|
|
for node_id in nodes_ids:
|
|
|
|
self.sdt.delete_node(node_id)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def exception(
|
2020-04-01 00:39:27 +01:00
|
|
|
self, level: ExceptionLevels, source: str, text: str, node_id: int = None
|
2020-01-11 06:37:19 +00:00
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-05-03 17:30:49 +01:00
|
|
|
Generate and broadcast an exception event.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param level: exception level
|
|
|
|
:param source: source name
|
|
|
|
:param text: exception message
|
2020-04-01 00:39:27 +01:00
|
|
|
:param node_id: node related to exception
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
exception_data = ExceptionData(
|
2019-04-30 07:31:47 +01:00
|
|
|
node=node_id,
|
2020-03-31 17:41:29 +01:00
|
|
|
session=self.id,
|
2017-04-25 16:45:34 +01:00
|
|
|
level=level,
|
|
|
|
source=source,
|
|
|
|
date=time.ctime(),
|
2019-09-10 23:10:24 +01:00
|
|
|
text=text,
|
2017-04-25 16:45:34 +01:00
|
|
|
)
|
|
|
|
self.broadcast_exception(exception_data)
|
|
|
|
|
2020-02-15 00:25:05 +00:00
|
|
|
def instantiate(self) -> List[Exception]:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
We have entered the instantiation state, invoke startup methods
|
|
|
|
of various managers and boot the nodes. Validate nodes and check
|
|
|
|
for transition to the runtime state.
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
:return: list of service boot errors during startup
|
|
|
|
"""
|
2022-03-04 17:19:56 +00:00
|
|
|
if self.state == EventTypes.RUNTIME_STATE:
|
|
|
|
logger.warning("ignoring instantiate, already in runtime state")
|
|
|
|
return []
|
2019-10-15 22:13:42 +01:00
|
|
|
# create control net interfaces and network tunnels
|
2019-06-19 18:58:49 +01:00
|
|
|
# which need to exist for emane to sync on location events
|
|
|
|
# in distributed scenarios
|
2020-01-11 06:37:19 +00:00
|
|
|
self.add_remove_control_net(0, remove=False)
|
2019-10-08 23:09:26 +01:00
|
|
|
# initialize distributed tunnels
|
2019-10-17 19:10:59 +01:00
|
|
|
self.distributed.start()
|
2020-01-11 06:37:19 +00:00
|
|
|
# instantiate will be invoked again upon emane configure
|
2020-07-02 23:37:51 +01:00
|
|
|
if self.emane.startup() == EmaneState.NOT_READY:
|
2020-01-11 06:37:19 +00:00
|
|
|
return []
|
2019-06-19 18:58:49 +01:00
|
|
|
# boot node services and then start mobility
|
2019-12-20 23:11:34 +00:00
|
|
|
exceptions = self.boot_nodes()
|
|
|
|
if not exceptions:
|
2022-03-29 20:16:07 +01:00
|
|
|
# complete wireless node
|
|
|
|
for node in self.nodes.values():
|
|
|
|
if isinstance(node, WirelessNode):
|
|
|
|
node.post_startup()
|
2019-12-20 23:11:34 +00:00
|
|
|
self.mobility.startup()
|
|
|
|
# notify listeners that instantiation is complete
|
2020-03-07 06:35:23 +00:00
|
|
|
event = EventData(event_type=EventTypes.INSTANTIATION_COMPLETE)
|
2019-12-20 23:11:34 +00:00
|
|
|
self.broadcast_event(event)
|
2022-03-04 17:19:56 +00:00
|
|
|
# startup event loop
|
|
|
|
self.event_loop.run()
|
|
|
|
self.set_state(EventTypes.RUNTIME_STATE, send_event=True)
|
2019-12-20 23:11:34 +00:00
|
|
|
return exceptions
|
2014-09-23 17:26:22 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def get_node_count(self) -> int:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Returns the number of CoreNodes and CoreNets, except for those
|
2013-11-25 19:54:02 +00:00
|
|
|
that are not considered in the GUI's node count.
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
:return: created node count
|
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
with self.nodes_lock:
|
2019-05-06 05:23:43 +01:00
|
|
|
count = 0
|
2020-06-13 04:22:51 +01:00
|
|
|
for node in self.nodes.values():
|
2019-09-26 21:00:12 +01:00
|
|
|
is_p2p_ctrlnet = isinstance(node, (PtpNet, CtrlNet))
|
|
|
|
is_tap = isinstance(node, GreTapBridge) and not isinstance(
|
|
|
|
node, TunnelNode
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2019-05-06 05:23:43 +01:00
|
|
|
if is_p2p_ctrlnet or is_tap:
|
|
|
|
continue
|
|
|
|
count += 1
|
2013-11-25 19:54:02 +00:00
|
|
|
return count
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def data_collect(self) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Tear down a running session. Stop the event loop and any running
|
|
|
|
nodes, and perform clean-up.
|
2020-01-11 06:37:19 +00:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-12-05 17:01:53 +00:00
|
|
|
if self.state.already_collected():
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2020-12-05 17:01:53 +00:00
|
|
|
"session(%s) state(%s) already data collected", self.id, self.state
|
|
|
|
)
|
|
|
|
return
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("session(%s) state(%s) data collection", self.id, self.state)
|
2020-12-05 17:01:53 +00:00
|
|
|
self.set_state(EventTypes.DATACOLLECT_STATE, send_event=True)
|
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
# stop event loop
|
|
|
|
self.event_loop.stop()
|
|
|
|
|
2021-03-19 23:54:24 +00:00
|
|
|
# stop mobility and node services
|
2020-06-13 04:22:51 +01:00
|
|
|
with self.nodes_lock:
|
2019-10-29 17:25:39 +00:00
|
|
|
funcs = []
|
2021-03-19 23:54:24 +00:00
|
|
|
for node in self.nodes.values():
|
|
|
|
if isinstance(node, CoreNodeBase) and node.up:
|
|
|
|
args = (node,)
|
|
|
|
funcs.append((self.services.stop_services, args, {}))
|
2019-10-29 17:25:39 +00:00
|
|
|
utils.threadpool(funcs)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
# shutdown emane
|
2013-08-29 15:21:13 +01:00
|
|
|
self.emane.shutdown()
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
# update control interface hosts
|
2020-06-16 17:30:16 +01:00
|
|
|
self.update_control_iface_hosts(remove=True)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
# remove all four possible control networks
|
2020-12-05 17:01:53 +00:00
|
|
|
for i in range(4):
|
|
|
|
self.add_remove_control_net(i, remove=True)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def short_session_id(self) -> str:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Return a shorter version of the session ID, appropriate for
|
|
|
|
interface names, where length may be limited.
|
2020-01-11 06:37:19 +00:00
|
|
|
|
|
|
|
:return: short session id
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2019-04-08 18:39:36 +01:00
|
|
|
ssid = (self.id >> 8) ^ (self.id & ((1 << 8) - 1))
|
2019-10-18 18:33:31 +01:00
|
|
|
return f"{ssid:x}"
|
2014-09-23 17:26:22 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def boot_node(self, node: CoreNode) -> None:
|
2019-10-29 17:25:39 +00:00
|
|
|
"""
|
|
|
|
Boot node by adding a control interface when necessary and starting
|
|
|
|
node services.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to boot
|
2019-10-29 17:25:39 +00:00
|
|
|
:return: nothing
|
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("booting node(%s): %s", node.name, [x.name for x in node.services])
|
2019-10-29 17:25:39 +00:00
|
|
|
self.services.boot_services(node)
|
2020-01-23 21:22:47 +00:00
|
|
|
node.start_config_services()
|
2019-10-29 17:25:39 +00:00
|
|
|
|
2020-01-13 18:06:18 +00:00
|
|
|
def boot_nodes(self) -> List[Exception]:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Invoke the boot() procedure for all nodes and send back node
|
|
|
|
messages to the GUI for node messages that had the status
|
|
|
|
request flag.
|
2019-12-20 23:11:34 +00:00
|
|
|
|
|
|
|
:return: service boot exceptions
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
with self.nodes_lock:
|
2019-10-29 17:25:39 +00:00
|
|
|
funcs = []
|
|
|
|
start = time.monotonic()
|
2020-12-16 18:19:17 +00:00
|
|
|
for node in self.nodes.values():
|
2022-05-25 18:51:42 +01:00
|
|
|
if isinstance(node, CoreNode):
|
2020-12-16 18:19:17 +00:00
|
|
|
self.add_remove_control_iface(node, remove=False)
|
|
|
|
funcs.append((self.boot_node, (node,), {}))
|
2019-10-29 17:25:39 +00:00
|
|
|
results, exceptions = utils.threadpool(funcs)
|
|
|
|
total = time.monotonic() - start
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("boot run time: %s", total)
|
2019-12-20 23:11:34 +00:00
|
|
|
if not exceptions:
|
2020-06-16 17:30:16 +01:00
|
|
|
self.update_control_iface_hosts()
|
2019-12-20 23:11:34 +00:00
|
|
|
return exceptions
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def get_control_net_prefixes(self) -> List[str]:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Retrieve control net prefixes.
|
|
|
|
|
|
|
|
:return: control net prefix list
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2022-04-04 23:13:31 +01:00
|
|
|
p = self.options.get("controlnet")
|
|
|
|
p0 = self.options.get("controlnet0")
|
|
|
|
p1 = self.options.get("controlnet1")
|
|
|
|
p2 = self.options.get("controlnet2")
|
|
|
|
p3 = self.options.get("controlnet3")
|
2015-05-22 01:53:43 +01:00
|
|
|
if not p0 and p:
|
|
|
|
p0 = p
|
2017-04-25 16:45:34 +01:00
|
|
|
return [p0, p1, p2, p3]
|
|
|
|
|
2020-06-16 17:30:16 +01:00
|
|
|
def get_control_net_server_ifaces(self) -> List[str]:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Retrieve control net server interfaces.
|
|
|
|
|
|
|
|
:return: list of control net server interfaces
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2022-04-04 23:13:31 +01:00
|
|
|
d0 = self.options.get("controlnetif0")
|
2015-05-22 01:53:43 +01:00
|
|
|
if d0:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.error("controlnet0 cannot be assigned with a host interface")
|
2022-04-04 23:13:31 +01:00
|
|
|
d1 = self.options.get("controlnetif1")
|
|
|
|
d2 = self.options.get("controlnetif2")
|
|
|
|
d3 = self.options.get("controlnetif3")
|
2017-04-25 16:45:34 +01:00
|
|
|
return [None, d1, d2, d3]
|
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def get_control_net_index(self, dev: str) -> int:
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Retrieve control net index.
|
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param dev: device to get control net index for
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: control net index, -1 otherwise
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2017-04-25 16:45:34 +01:00
|
|
|
if dev[0:4] == "ctrl" and int(dev[4]) in [0, 1, 2, 3]:
|
|
|
|
index = int(dev[4])
|
|
|
|
if index == 0:
|
|
|
|
return index
|
2017-05-04 22:43:57 +01:00
|
|
|
if index < 4 and self.get_control_net_prefixes()[index] is not None:
|
2017-04-25 16:45:34 +01:00
|
|
|
return index
|
2015-05-22 01:53:59 +01:00
|
|
|
return -1
|
2015-05-22 01:53:43 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def get_control_net(self, net_index: int) -> CtrlNet:
|
|
|
|
"""
|
|
|
|
Retrieve a control net based on index.
|
|
|
|
|
|
|
|
:param net_index: control net index
|
|
|
|
:return: control net
|
|
|
|
:raises CoreError: when control net is not found
|
|
|
|
"""
|
2020-05-20 22:44:34 +01:00
|
|
|
return self.get_node(CTRL_NET_ID + net_index, CtrlNet)
|
2015-05-22 01:53:43 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def add_remove_control_net(
|
|
|
|
self, net_index: int, remove: bool = False, conf_required: bool = True
|
|
|
|
) -> Optional[CtrlNet]:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Create a control network bridge as necessary.
|
2013-08-29 18:51:19 +01:00
|
|
|
When the remove flag is True, remove the bridge that connects control
|
2014-09-23 21:24:19 +01:00
|
|
|
interfaces. The conf_reqd flag, when False, causes a control network
|
|
|
|
bridge to be added even if one has not been configured.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param net_index: network index
|
|
|
|
:param remove: flag to check if it should be removed
|
|
|
|
:param conf_required: flag to check if conf is required
|
2019-04-30 07:31:47 +01:00
|
|
|
:return: control net node
|
2020-01-17 00:12:01 +00:00
|
|
|
"""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug(
|
2019-09-10 23:10:24 +01:00
|
|
|
"add/remove control net: index(%s) remove(%s) conf_required(%s)",
|
|
|
|
net_index,
|
|
|
|
remove,
|
|
|
|
conf_required,
|
|
|
|
)
|
2017-05-04 22:43:57 +01:00
|
|
|
prefix_spec_list = self.get_control_net_prefixes()
|
2017-04-25 16:45:34 +01:00
|
|
|
prefix_spec = prefix_spec_list[net_index]
|
|
|
|
if not prefix_spec:
|
|
|
|
if conf_required:
|
2017-05-03 17:30:49 +01:00
|
|
|
# no controlnet needed
|
|
|
|
return None
|
2014-09-23 17:26:22 +01:00
|
|
|
else:
|
2019-09-26 21:00:12 +01:00
|
|
|
prefix_spec = CtrlNet.DEFAULT_PREFIX_LIST[net_index]
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("prefix spec: %s", prefix_spec)
|
2020-06-16 17:30:16 +01:00
|
|
|
server_iface = self.get_control_net_server_ifaces()[net_index]
|
2014-09-23 17:26:22 +01:00
|
|
|
|
2013-08-29 18:51:19 +01:00
|
|
|
# return any existing controlnet bridge
|
|
|
|
try:
|
2019-04-30 07:31:47 +01:00
|
|
|
control_net = self.get_control_net(net_index)
|
2013-08-29 18:51:19 +01:00
|
|
|
if remove:
|
2019-04-30 07:31:47 +01:00
|
|
|
self.delete_node(control_net.id)
|
2013-08-29 18:51:19 +01:00
|
|
|
return None
|
2017-04-25 16:45:34 +01:00
|
|
|
return control_net
|
2019-09-12 23:48:09 +01:00
|
|
|
except CoreError:
|
2013-08-29 18:51:19 +01:00
|
|
|
if remove:
|
|
|
|
return None
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2013-08-29 18:51:19 +01:00
|
|
|
# build a new controlnet bridge
|
2019-10-21 17:36:07 +01:00
|
|
|
_id = CTRL_NET_ID + net_index
|
2015-05-22 01:53:43 +01:00
|
|
|
|
|
|
|
# use the updown script for control net 0 only.
|
2013-08-29 15:21:13 +01:00
|
|
|
updown_script = None
|
2017-04-25 16:45:34 +01:00
|
|
|
if net_index == 0:
|
2022-05-10 05:11:14 +01:00
|
|
|
updown_script = self.options.get("controlnet_updown_script") or None
|
2017-07-10 18:44:10 +01:00
|
|
|
if not updown_script:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.debug("controlnet updown script not configured")
|
2017-07-10 18:44:10 +01:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
prefixes = prefix_spec.split()
|
|
|
|
if len(prefixes) > 1:
|
|
|
|
# a list of per-host prefixes is provided
|
2019-10-26 06:06:30 +01:00
|
|
|
try:
|
|
|
|
# split first (master) entry into server and prefix
|
|
|
|
prefix = prefixes[0].split(":", 1)[1]
|
|
|
|
except IndexError:
|
|
|
|
# no server name. possibly only one server
|
|
|
|
prefix = prefixes[0]
|
2017-05-03 17:30:49 +01:00
|
|
|
else:
|
2015-05-22 01:53:43 +01:00
|
|
|
prefix = prefixes[0]
|
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2019-10-26 06:06:30 +01:00
|
|
|
"controlnet(%s) prefix(%s) updown(%s) serverintf(%s)",
|
2019-10-09 05:06:22 +01:00
|
|
|
_id,
|
|
|
|
prefix,
|
|
|
|
updown_script,
|
2020-06-16 17:30:16 +01:00
|
|
|
server_iface,
|
2019-10-09 05:06:22 +01:00
|
|
|
)
|
2022-05-25 18:51:42 +01:00
|
|
|
options = CtrlNet.create_options()
|
|
|
|
options.prefix = prefix
|
|
|
|
options.updown_script = updown_script
|
|
|
|
options.serverintf = server_iface
|
|
|
|
control_net = self.create_node(CtrlNet, False, _id, options=options)
|
2020-10-10 13:31:26 +01:00
|
|
|
control_net.brname = f"ctrl{net_index}.{self.short_session_id()}"
|
|
|
|
control_net.startup()
|
2017-04-25 16:45:34 +01:00
|
|
|
return control_net
|
|
|
|
|
2020-06-16 17:30:16 +01:00
|
|
|
def add_remove_control_iface(
|
2020-01-11 06:37:19 +00:00
|
|
|
self,
|
2022-05-25 18:51:42 +01:00
|
|
|
node: CoreNode,
|
2020-01-11 06:37:19 +00:00
|
|
|
net_index: int = 0,
|
|
|
|
remove: bool = False,
|
|
|
|
conf_required: bool = True,
|
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Add a control interface to a node when a 'controlnet' prefix is
|
|
|
|
listed in the config file or session options. Uses
|
|
|
|
addremovectrlnet() to build or remove the control bridge.
|
|
|
|
If conf_reqd is False, the control network may be built even
|
|
|
|
when the user has not configured one (e.g. for EMANE.)
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node: node to add or remove control interface
|
|
|
|
:param net_index: network index
|
|
|
|
:param remove: flag to check if it should be removed
|
|
|
|
:param conf_required: flag to check if conf is required
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
control_net = self.add_remove_control_net(net_index, remove, conf_required)
|
|
|
|
if not control_net:
|
2013-08-29 18:51:19 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
if not node:
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-05-03 17:30:49 +01:00
|
|
|
# ctrl# already exists
|
2020-06-16 17:30:16 +01:00
|
|
|
if node.ifaces.get(control_net.CTRLIF_IDX_BASE + net_index):
|
2017-05-03 17:30:49 +01:00
|
|
|
return
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2020-06-09 20:42:15 +01:00
|
|
|
ip4 = control_net.prefix[node.id]
|
|
|
|
ip4_mask = control_net.prefix.prefixlen
|
2020-06-16 17:30:16 +01:00
|
|
|
iface_data = InterfaceData(
|
2020-06-09 20:42:15 +01:00
|
|
|
id=control_net.CTRLIF_IDX_BASE + net_index,
|
|
|
|
name=f"ctrl{net_index}",
|
|
|
|
mac=utils.random_mac(),
|
|
|
|
ip4=ip4,
|
|
|
|
ip4_mask=ip4_mask,
|
2022-01-08 00:03:45 +00:00
|
|
|
mtu=DEFAULT_MTU,
|
2020-06-09 20:42:15 +01:00
|
|
|
)
|
2022-03-17 22:28:38 +00:00
|
|
|
iface = node.create_iface(iface_data)
|
|
|
|
control_net.attach(iface)
|
2020-06-16 17:30:16 +01:00
|
|
|
iface.control = True
|
2013-08-29 15:21:13 +01:00
|
|
|
except ValueError:
|
2019-10-18 18:33:31 +01:00
|
|
|
msg = f"Control interface not added to node {node.id}. "
|
|
|
|
msg += f"Invalid control network prefix ({control_net.prefix}). "
|
2013-08-29 15:21:13 +01:00
|
|
|
msg += "A longer prefix length may be required for this many nodes."
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception(msg)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2020-06-16 17:30:16 +01:00
|
|
|
def update_control_iface_hosts(
|
2020-01-11 06:37:19 +00:00
|
|
|
self, net_index: int = 0, remove: bool = False
|
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Add the IP addresses of control interfaces to the /etc/hosts file.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param net_index: network index to update
|
|
|
|
:param remove: flag to check if it should be removed
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2022-04-04 23:13:31 +01:00
|
|
|
if not self.options.get_bool("update_etc_hosts", False):
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2019-04-30 07:31:47 +01:00
|
|
|
control_net = self.get_control_net(net_index)
|
2019-09-12 23:48:09 +01:00
|
|
|
except CoreError:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.exception("error retrieving control net node")
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2019-10-18 18:33:31 +01:00
|
|
|
header = f"CORE session {self.id} host entries"
|
2013-08-29 15:21:13 +01:00
|
|
|
if remove:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("Removing /etc/hosts file entries.")
|
2018-03-02 17:15:52 +00:00
|
|
|
utils.file_demunge("/etc/hosts", header)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
entries = []
|
2020-06-16 17:30:16 +01:00
|
|
|
for iface in control_net.get_ifaces():
|
|
|
|
name = iface.node.name
|
2020-06-19 18:54:58 +01:00
|
|
|
for ip in iface.ips():
|
2020-06-19 16:50:36 +01:00
|
|
|
entries.append(f"{ip.ip} {name}")
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("Adding %d /etc/hosts file entries.", len(entries))
|
2018-03-02 17:15:52 +00:00
|
|
|
utils.file_munge("/etc/hosts", header, "\n".join(entries) + "\n")
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def runtime(self) -> float:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Return the current time we have been in the runtime state, or zero
|
|
|
|
if not in runtime.
|
|
|
|
"""
|
2020-03-22 21:59:42 +00:00
|
|
|
if self.state == EventTypes.RUNTIME_STATE:
|
2020-06-13 04:22:51 +01:00
|
|
|
return time.monotonic() - self.state_time
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
|
|
|
return 0.0
|
2014-09-23 17:26:22 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def add_event(
|
2020-06-14 05:48:51 +01:00
|
|
|
self, event_time: float, node_id: int = None, name: str = None, data: str = None
|
2020-01-11 06:37:19 +00:00
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Add an event to the event queue, with a start time relative to the
|
|
|
|
start of the runtime state.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param event_time: event time
|
2020-06-14 05:48:51 +01:00
|
|
|
:param node_id: node to add event for
|
2020-01-16 19:00:57 +00:00
|
|
|
:param name: name of event
|
2017-05-03 17:30:49 +01:00
|
|
|
:param data: data for event
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
current_time = self.runtime()
|
2019-05-02 07:17:46 +01:00
|
|
|
if current_time > 0:
|
|
|
|
if event_time <= current_time:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.warning(
|
2019-09-10 23:10:24 +01:00
|
|
|
"could not schedule past event for time %s (run time is now %s)",
|
|
|
|
event_time,
|
|
|
|
current_time,
|
|
|
|
)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
event_time = event_time - current_time
|
2019-09-10 23:10:24 +01:00
|
|
|
self.event_loop.add_event(
|
2020-06-14 05:48:51 +01:00
|
|
|
event_time, self.run_event, node_id=node_id, name=name, data=data
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2017-04-25 16:45:34 +01:00
|
|
|
if not name:
|
2013-08-29 15:21:13 +01:00
|
|
|
name = ""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info(
|
2019-09-10 23:10:24 +01:00
|
|
|
"scheduled event %s at time %s data=%s",
|
|
|
|
name,
|
|
|
|
event_time + current_time,
|
|
|
|
data,
|
|
|
|
)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-11 06:37:19 +00:00
|
|
|
def run_event(
|
|
|
|
self, node_id: int = None, name: str = None, data: str = None
|
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Run a scheduled event, executing commands in the data string.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param node_id: node id to run event
|
|
|
|
:param name: event name
|
|
|
|
:param data: event data
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2020-06-13 04:22:51 +01:00
|
|
|
if data is None:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.warning("no data for event node(%s) name(%s)", node_id, name)
|
2020-06-13 04:22:51 +01:00
|
|
|
return
|
2013-08-29 15:21:13 +01:00
|
|
|
now = self.runtime()
|
2017-04-25 16:45:34 +01:00
|
|
|
if not name:
|
2013-08-29 15:21:13 +01:00
|
|
|
name = ""
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("running event %s at time %s cmd=%s", name, now, data)
|
2017-04-25 16:45:34 +01:00
|
|
|
if not node_id:
|
2018-03-02 17:15:52 +00:00
|
|
|
utils.mute_detach(data)
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
2020-05-20 22:44:34 +01:00
|
|
|
node = self.get_node(node_id, CoreNodeBase)
|
2019-10-19 07:28:09 +01:00
|
|
|
node.cmd(data, wait=False)
|
2020-04-15 23:41:37 +01:00
|
|
|
|
|
|
|
def get_link_color(self, network_id: int) -> str:
|
|
|
|
"""
|
|
|
|
Assign a color for links associated with a network.
|
|
|
|
|
|
|
|
:param network_id: network to get a link color for
|
|
|
|
:return: link color
|
|
|
|
"""
|
|
|
|
color = self.link_colors.get(network_id)
|
|
|
|
if not color:
|
|
|
|
index = len(self.link_colors) % len(LINK_COLORS)
|
|
|
|
color = LINK_COLORS[index]
|
|
|
|
self.link_colors[network_id] = color
|
|
|
|
return color
|