2020-01-20 22:04:31 +00:00
|
|
|
import logging
|
2021-02-19 07:07:55 +00:00
|
|
|
from typing import TYPE_CHECKING, List, Optional, Set
|
2020-06-20 06:08:24 +01:00
|
|
|
|
|
|
|
from PIL.ImageTk import PhotoImage
|
2020-01-14 19:59:44 +00:00
|
|
|
|
2020-08-27 19:02:02 +01:00
|
|
|
from core.api.grpc.wrappers import Node, NodeType
|
2021-02-19 05:04:16 +00:00
|
|
|
from core.gui import images
|
2020-05-08 08:16:25 +01:00
|
|
|
from core.gui.appconfig import CustomNode, GuiConfig
|
2021-02-19 07:07:55 +00:00
|
|
|
from core.gui.images import ImageEnum
|
|
|
|
|
2021-04-22 05:09:35 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-02-19 07:07:55 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from core.gui.app import Application
|
2019-11-16 07:31:41 +00:00
|
|
|
|
2021-02-18 18:47:20 +00:00
|
|
|
NODES: List["NodeDraw"] = []
|
|
|
|
NETWORK_NODES: List["NodeDraw"] = []
|
|
|
|
NODE_ICONS = {}
|
|
|
|
CONTAINER_NODES: Set[NodeType] = {NodeType.DEFAULT, NodeType.DOCKER, NodeType.LXC}
|
|
|
|
IMAGE_NODES: Set[NodeType] = {NodeType.DOCKER, NodeType.LXC}
|
2022-03-31 05:13:28 +01:00
|
|
|
WIRELESS_NODES: Set[NodeType] = {
|
|
|
|
NodeType.WIRELESS_LAN,
|
|
|
|
NodeType.EMANE,
|
|
|
|
NodeType.WIRELESS,
|
|
|
|
}
|
2021-02-18 18:47:20 +00:00
|
|
|
RJ45_NODES: Set[NodeType] = {NodeType.RJ45}
|
|
|
|
BRIDGE_NODES: Set[NodeType] = {NodeType.HUB, NodeType.SWITCH}
|
|
|
|
IGNORE_NODES: Set[NodeType] = {NodeType.CONTROL_NET}
|
|
|
|
MOBILITY_NODES: Set[NodeType] = {NodeType.WIRELESS_LAN, NodeType.EMANE}
|
2022-03-25 00:15:44 +00:00
|
|
|
NODE_MODELS: Set[str] = {"router", "PC", "mdr", "prouter"}
|
2021-02-18 18:47:20 +00:00
|
|
|
ROUTER_NODES: Set[str] = {"router", "mdr"}
|
|
|
|
ANTENNA_ICON: Optional[PhotoImage] = None
|
|
|
|
|
|
|
|
|
|
|
|
def setup() -> None:
|
|
|
|
global ANTENNA_ICON
|
|
|
|
nodes = [
|
|
|
|
(ImageEnum.PC, NodeType.DEFAULT, "PC", "PC"),
|
|
|
|
(ImageEnum.MDR, NodeType.DEFAULT, "MDR", "mdr"),
|
2021-05-12 06:59:21 +01:00
|
|
|
(ImageEnum.ROUTER, NodeType.DEFAULT, "Router", "router"),
|
2021-02-18 18:47:20 +00:00
|
|
|
(ImageEnum.PROUTER, NodeType.DEFAULT, "PRouter", "prouter"),
|
|
|
|
(ImageEnum.DOCKER, NodeType.DOCKER, "Docker", None),
|
|
|
|
(ImageEnum.LXC, NodeType.LXC, "LXC", None),
|
|
|
|
]
|
|
|
|
for image_enum, node_type, label, model in nodes:
|
|
|
|
node_draw = NodeDraw.from_setup(image_enum, node_type, label, model)
|
|
|
|
NODES.append(node_draw)
|
|
|
|
NODE_ICONS[(node_type, model)] = node_draw.image
|
|
|
|
network_nodes = [
|
|
|
|
(ImageEnum.HUB, NodeType.HUB, "Hub"),
|
|
|
|
(ImageEnum.SWITCH, NodeType.SWITCH, "Switch"),
|
|
|
|
(ImageEnum.WLAN, NodeType.WIRELESS_LAN, "WLAN"),
|
2022-03-31 05:13:28 +01:00
|
|
|
(ImageEnum.WIRELESS, NodeType.WIRELESS, "Wireless"),
|
2021-02-18 18:47:20 +00:00
|
|
|
(ImageEnum.EMANE, NodeType.EMANE, "EMANE"),
|
|
|
|
(ImageEnum.RJ45, NodeType.RJ45, "RJ45"),
|
|
|
|
(ImageEnum.TUNNEL, NodeType.TUNNEL, "Tunnel"),
|
|
|
|
]
|
|
|
|
for image_enum, node_type, label in network_nodes:
|
|
|
|
node_draw = NodeDraw.from_setup(image_enum, node_type, label)
|
|
|
|
NETWORK_NODES.append(node_draw)
|
|
|
|
NODE_ICONS[(node_type, None)] = node_draw.image
|
2021-02-19 05:04:16 +00:00
|
|
|
ANTENNA_ICON = images.from_enum(ImageEnum.ANTENNA, width=images.ANTENNA_SIZE)
|
2021-02-18 18:47:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_bridge(node: Node) -> bool:
|
|
|
|
return node.type in BRIDGE_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def is_mobility(node: Node) -> bool:
|
|
|
|
return node.type in MOBILITY_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def is_router(node: Node) -> bool:
|
|
|
|
return is_model(node) and node.model in ROUTER_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def should_ignore(node: Node) -> bool:
|
|
|
|
return node.type in IGNORE_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def is_container(node: Node) -> bool:
|
|
|
|
return node.type in CONTAINER_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def is_model(node: Node) -> bool:
|
|
|
|
return node.type == NodeType.DEFAULT
|
|
|
|
|
|
|
|
|
|
|
|
def has_image(node_type: NodeType) -> bool:
|
|
|
|
return node_type in IMAGE_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def is_wireless(node: Node) -> bool:
|
|
|
|
return node.type in WIRELESS_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def is_rj45(node: Node) -> bool:
|
|
|
|
return node.type in RJ45_NODES
|
|
|
|
|
|
|
|
|
|
|
|
def is_custom(node: Node) -> bool:
|
|
|
|
return is_model(node) and node.model not in NODE_MODELS
|
|
|
|
|
|
|
|
|
2022-03-18 22:53:13 +00:00
|
|
|
def is_iface_node(node: Node) -> bool:
|
|
|
|
return is_container(node) or is_bridge(node)
|
|
|
|
|
|
|
|
|
2021-02-18 18:47:20 +00:00
|
|
|
def get_custom_services(gui_config: GuiConfig, name: str) -> List[str]:
|
|
|
|
for custom_node in gui_config.nodes:
|
|
|
|
if custom_node.name == name:
|
|
|
|
return custom_node.services
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2021-02-19 07:07:55 +00:00
|
|
|
def _get_custom_file(config: GuiConfig, name: str) -> Optional[str]:
|
2021-02-18 18:47:20 +00:00
|
|
|
for custom_node in config.nodes:
|
|
|
|
if custom_node.name == name:
|
|
|
|
return custom_node.image
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-02-19 07:07:55 +00:00
|
|
|
def get_icon(node: Node, app: "Application") -> PhotoImage:
|
|
|
|
scale = app.app_scale
|
2021-02-18 18:47:20 +00:00
|
|
|
image = None
|
2022-09-10 18:06:40 +01:00
|
|
|
# node icon was overridden with a specific value
|
2021-02-18 18:47:20 +00:00
|
|
|
if node.icon:
|
|
|
|
try:
|
2021-02-19 05:04:16 +00:00
|
|
|
image = images.from_file(node.icon, width=images.NODE_SIZE, scale=scale)
|
2021-02-18 18:47:20 +00:00
|
|
|
except OSError:
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.error("invalid icon: %s", node.icon)
|
2021-02-19 17:31:58 +00:00
|
|
|
# custom node
|
|
|
|
elif is_custom(node):
|
|
|
|
image_file = _get_custom_file(app.guiconfig, node.model)
|
2021-04-22 05:09:35 +01:00
|
|
|
logger.info("custom node file: %s", image_file)
|
2021-02-19 17:31:58 +00:00
|
|
|
if image_file:
|
|
|
|
image = images.from_file(image_file, width=images.NODE_SIZE, scale=scale)
|
|
|
|
# built in node
|
2021-02-18 18:47:20 +00:00
|
|
|
else:
|
2021-02-19 07:07:55 +00:00
|
|
|
image = images.from_node(node, scale=scale)
|
|
|
|
# default image, if everything above fails
|
|
|
|
if not image:
|
|
|
|
image = images.from_enum(
|
|
|
|
ImageEnum.EDITNODE, width=images.NODE_SIZE, scale=scale
|
|
|
|
)
|
2021-02-18 18:47:20 +00:00
|
|
|
return image
|
|
|
|
|
2019-11-16 07:31:41 +00:00
|
|
|
|
|
|
|
class NodeDraw:
|
2020-06-20 06:08:24 +01:00
|
|
|
def __init__(self) -> None:
|
2020-01-14 19:59:44 +00:00
|
|
|
self.custom: bool = False
|
2020-06-22 19:04:33 +01:00
|
|
|
self.image: Optional[PhotoImage] = None
|
2020-01-14 22:30:08 +00:00
|
|
|
self.image_enum: Optional[ImageEnum] = None
|
2020-06-20 06:08:24 +01:00
|
|
|
self.image_file: Optional[str] = None
|
2020-07-25 18:30:14 +01:00
|
|
|
self.node_type: Optional[NodeType] = None
|
2020-01-14 22:30:08 +00:00
|
|
|
self.model: Optional[str] = None
|
2020-01-14 19:59:44 +00:00
|
|
|
self.services: Set[str] = set()
|
2020-06-20 06:08:24 +01:00
|
|
|
self.label: Optional[str] = None
|
2019-11-16 07:31:41 +00:00
|
|
|
|
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def from_setup(
|
|
|
|
cls,
|
|
|
|
image_enum: ImageEnum,
|
2020-06-20 06:08:24 +01:00
|
|
|
node_type: NodeType,
|
2020-01-14 22:02:37 +00:00
|
|
|
label: str,
|
|
|
|
model: str = None,
|
2020-06-20 06:08:24 +01:00
|
|
|
tooltip: str = None,
|
|
|
|
) -> "NodeDraw":
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw = NodeDraw()
|
|
|
|
node_draw.image_enum = image_enum
|
2021-02-19 05:04:16 +00:00
|
|
|
node_draw.image = images.from_enum(image_enum, width=images.NODE_SIZE)
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw.node_type = node_type
|
2019-12-11 19:42:05 +00:00
|
|
|
node_draw.label = label
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw.model = model
|
|
|
|
node_draw.tooltip = tooltip
|
|
|
|
return node_draw
|
|
|
|
|
|
|
|
@classmethod
|
2020-06-20 06:08:24 +01:00
|
|
|
def from_custom(cls, custom_node: CustomNode) -> "NodeDraw":
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw = NodeDraw()
|
|
|
|
node_draw.custom = True
|
2020-05-08 08:16:25 +01:00
|
|
|
node_draw.image_file = custom_node.image
|
2021-02-19 17:31:58 +00:00
|
|
|
node_draw.image = images.from_file(custom_node.image, width=images.NODE_SIZE)
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw.node_type = NodeType.DEFAULT
|
2021-04-14 05:53:50 +01:00
|
|
|
node_draw.services = set(custom_node.services)
|
2020-05-08 08:16:25 +01:00
|
|
|
node_draw.label = custom_node.name
|
|
|
|
node_draw.model = custom_node.name
|
|
|
|
node_draw.tooltip = custom_node.name
|
2019-11-16 07:31:41 +00:00
|
|
|
return node_draw
|