2020-01-14 22:30:08 +00:00
|
|
|
from typing import TYPE_CHECKING, Optional, Set
|
2020-01-14 19:59:44 +00:00
|
|
|
|
2019-11-16 07:31:41 +00:00
|
|
|
from core.api.grpc.core_pb2 import NodeType
|
2019-12-19 17:30:21 +00:00
|
|
|
from core.gui.images import ImageEnum, Images
|
2019-11-16 07:31:41 +00:00
|
|
|
|
2020-01-14 22:02:37 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from core.api.grpc import core_pb2
|
|
|
|
|
2019-11-22 06:55:37 +00:00
|
|
|
ICON_SIZE = 48
|
2019-12-06 00:37:48 +00:00
|
|
|
ANTENNA_SIZE = 32
|
2019-11-16 07:31:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NodeDraw:
|
|
|
|
def __init__(self):
|
2020-01-14 19:59:44 +00:00
|
|
|
self.custom: bool = False
|
2019-11-16 07:31:41 +00:00
|
|
|
self.image = None
|
2020-01-14 22:30:08 +00:00
|
|
|
self.image_enum: Optional[ImageEnum] = None
|
2019-11-16 07:31:41 +00:00
|
|
|
self.image_file = None
|
2020-01-14 22:02:37 +00:00
|
|
|
self.node_type: core_pb2.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()
|
2019-11-16 07:31:41 +00:00
|
|
|
|
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def from_setup(
|
|
|
|
cls,
|
|
|
|
image_enum: ImageEnum,
|
|
|
|
node_type: "core_pb2.NodeType",
|
|
|
|
label: str,
|
|
|
|
model: str = None,
|
|
|
|
tooltip=None,
|
|
|
|
):
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw = NodeDraw()
|
|
|
|
node_draw.image_enum = image_enum
|
|
|
|
node_draw.image = Images.get(image_enum, ICON_SIZE)
|
|
|
|
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-01-14 22:30:08 +00:00
|
|
|
def from_custom(cls, name: str, image_file: str, services: Set[str]):
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw = NodeDraw()
|
|
|
|
node_draw.custom = True
|
|
|
|
node_draw.image_file = image_file
|
|
|
|
node_draw.image = Images.get_custom(image_file, ICON_SIZE)
|
|
|
|
node_draw.node_type = NodeType.DEFAULT
|
|
|
|
node_draw.services = services
|
2019-12-11 19:42:05 +00:00
|
|
|
node_draw.label = name
|
2019-11-16 07:31:41 +00:00
|
|
|
node_draw.model = name
|
|
|
|
node_draw.tooltip = name
|
|
|
|
return node_draw
|
|
|
|
|
|
|
|
|
|
|
|
class NodeUtils:
|
|
|
|
NODES = []
|
|
|
|
NETWORK_NODES = []
|
|
|
|
NODE_ICONS = {}
|
2019-11-20 19:20:08 +00:00
|
|
|
CONTAINER_NODES = {NodeType.DEFAULT, NodeType.DOCKER, NodeType.LXC}
|
|
|
|
IMAGE_NODES = {NodeType.DOCKER, NodeType.LXC}
|
2019-11-22 00:41:38 +00:00
|
|
|
WIRELESS_NODES = {NodeType.WIRELESS_LAN, NodeType.EMANE}
|
2020-01-08 17:32:39 +00:00
|
|
|
RJ45_NODES = {NodeType.RJ45}
|
2019-11-25 23:40:09 +00:00
|
|
|
IGNORE_NODES = {NodeType.CONTROL_NET, NodeType.PEER_TO_PEER}
|
2019-11-20 19:20:08 +00:00
|
|
|
NODE_MODELS = {"router", "host", "PC", "mdr", "prouter"}
|
2019-12-06 00:37:48 +00:00
|
|
|
ANTENNA_ICON = None
|
2019-11-16 07:45:01 +00:00
|
|
|
|
2019-11-25 23:40:09 +00:00
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def is_ignore_node(cls, node_type: NodeType) -> bool:
|
2019-11-25 23:40:09 +00:00
|
|
|
return node_type in cls.IGNORE_NODES
|
|
|
|
|
2019-11-16 07:45:01 +00:00
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def is_container_node(cls, node_type: NodeType) -> bool:
|
2019-11-20 19:20:08 +00:00
|
|
|
return node_type in cls.CONTAINER_NODES
|
|
|
|
|
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def is_model_node(cls, node_type: NodeType) -> bool:
|
2019-11-20 19:20:08 +00:00
|
|
|
return node_type == NodeType.DEFAULT
|
|
|
|
|
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def is_image_node(cls, node_type: NodeType) -> bool:
|
2019-11-20 19:20:08 +00:00
|
|
|
return node_type in cls.IMAGE_NODES
|
2019-11-16 07:31:41 +00:00
|
|
|
|
2019-11-22 00:41:38 +00:00
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def is_wireless_node(cls, node_type: NodeType) -> bool:
|
2019-11-22 00:41:38 +00:00
|
|
|
return node_type in cls.WIRELESS_NODES
|
|
|
|
|
2020-01-08 17:32:39 +00:00
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def is_rj45_node(cls, node_type: NodeType) -> bool:
|
2020-01-08 17:32:39 +00:00
|
|
|
return node_type in cls.RJ45_NODES
|
|
|
|
|
2019-11-16 07:31:41 +00:00
|
|
|
@classmethod
|
2020-01-14 22:02:37 +00:00
|
|
|
def node_icon(cls, node_type: NodeType, model: str) -> bool:
|
2019-11-22 00:41:38 +00:00
|
|
|
if model == "":
|
|
|
|
model = None
|
2019-11-16 07:31:41 +00:00
|
|
|
return cls.NODE_ICONS[(node_type, model)]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup(cls):
|
|
|
|
nodes = [
|
2019-12-11 19:42:05 +00:00
|
|
|
(ImageEnum.ROUTER, NodeType.DEFAULT, "Router", "router"),
|
|
|
|
(ImageEnum.HOST, NodeType.DEFAULT, "Host", "host"),
|
|
|
|
(ImageEnum.PC, NodeType.DEFAULT, "PC", "PC"),
|
|
|
|
(ImageEnum.MDR, NodeType.DEFAULT, "MDR", "mdr"),
|
|
|
|
(ImageEnum.PROUTER, NodeType.DEFAULT, "PRouter", "prouter"),
|
|
|
|
(ImageEnum.DOCKER, NodeType.DOCKER, "Docker", None),
|
|
|
|
(ImageEnum.LXC, NodeType.LXC, "LXC", None),
|
2019-11-16 07:31:41 +00:00
|
|
|
]
|
2019-12-11 19:42:05 +00:00
|
|
|
for image_enum, node_type, label, model in nodes:
|
|
|
|
node_draw = NodeDraw.from_setup(image_enum, node_type, label, model)
|
2019-11-16 07:31:41 +00:00
|
|
|
cls.NODES.append(node_draw)
|
|
|
|
cls.NODE_ICONS[(node_type, model)] = node_draw.image
|
|
|
|
|
|
|
|
network_nodes = [
|
2019-12-11 19:42:05 +00:00
|
|
|
(ImageEnum.HUB, NodeType.HUB, "Hub"),
|
|
|
|
(ImageEnum.SWITCH, NodeType.SWITCH, "Switch"),
|
|
|
|
(ImageEnum.WLAN, NodeType.WIRELESS_LAN, "WLAN"),
|
2019-11-16 07:31:41 +00:00
|
|
|
(ImageEnum.EMANE, NodeType.EMANE, "EMANE"),
|
2019-12-11 19:42:05 +00:00
|
|
|
(ImageEnum.RJ45, NodeType.RJ45, "RJ45"),
|
|
|
|
(ImageEnum.TUNNEL, NodeType.TUNNEL, "Tunnel"),
|
2019-11-16 07:31:41 +00:00
|
|
|
]
|
2019-12-11 19:42:05 +00:00
|
|
|
for image_enum, node_type, label in network_nodes:
|
|
|
|
node_draw = NodeDraw.from_setup(image_enum, node_type, label)
|
2019-11-16 07:31:41 +00:00
|
|
|
cls.NETWORK_NODES.append(node_draw)
|
|
|
|
cls.NODE_ICONS[(node_type, None)] = node_draw.image
|
2019-12-06 00:37:48 +00:00
|
|
|
cls.ANTENNA_ICON = Images.get(ImageEnum.ANTENNA, ANTENNA_SIZE)
|