From d5254e6a91d9573542cdb948819f147778683e4c Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Wed, 20 May 2020 14:44:34 -0700 Subject: [PATCH 1/2] changes to support better type checking for retrieving an arbitrary node from a session, get_node now requires an expected class that the node would be an instance of, if the returned node is not an instance a CoreError is thrown, this also helps editors pick up expected types to account for variable/function usage better as well --- daemon/core/api/grpc/server.py | 31 ++++---------- daemon/core/api/tlv/corehandlers.py | 13 +++--- daemon/core/emane/commeffect.py | 3 +- daemon/core/emane/emanemanager.py | 8 ++-- daemon/core/emane/emanemodel.py | 3 +- daemon/core/emulator/distributed.py | 2 +- daemon/core/emulator/session.py | 33 ++++++++------- daemon/core/location/mobility.py | 12 +++--- daemon/core/nodes/base.py | 22 ++++++++++ daemon/core/nodes/physical.py | 1 - daemon/core/plugins/sdt.py | 2 +- daemon/core/services/emaneservices.py | 7 +++- daemon/core/xml/corexml.py | 13 +++--- daemon/examples/python/emane80211.py | 5 ++- daemon/examples/python/switch.py | 5 ++- daemon/examples/python/wlan.py | 5 ++- daemon/tests/emane/test_emane.py | 12 +++--- daemon/tests/test_grpc.py | 5 ++- daemon/tests/test_gui.py | 30 +++++++------- daemon/tests/test_nodes.py | 3 +- daemon/tests/test_xml.py | 60 ++++++++++++++------------- 21 files changed, 149 insertions(+), 126 deletions(-) diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 77e6bf08..19779320 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -6,7 +6,7 @@ import tempfile import threading import time from concurrent import futures -from typing import Type, TypeVar +from typing import Type import grpc from grpc import ServicerContext @@ -109,7 +109,7 @@ from core.emulator.coreemu import CoreEmu from core.emulator.data import LinkData from core.emulator.emudata import LinkOptions, NodeOptions from core.emulator.enumerations import EventTypes, LinkTypes, MessageFlags -from core.emulator.session import Session +from core.emulator.session import NT, Session from core.errors import CoreCommandError, CoreError from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility from core.nodes.base import CoreNode, CoreNodeBase, NodeBase @@ -118,7 +118,6 @@ from core.services.coreservices import ServiceManager _ONE_DAY_IN_SECONDS = 60 * 60 * 24 _INTERFACE_REGEX = re.compile(r"veth(?P[0-9a-fA-F]+)") -T = TypeVar("T") class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): @@ -174,36 +173,22 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): return session def get_node( - self, - session: Session, - node_id: int, - context: ServicerContext, - node_class: Type[T], - ) -> T: + self, session: Session, node_id: int, context: ServicerContext, _class: Type[NT] + ) -> NT: """ Retrieve node given session and node id :param session: session that has the node :param node_id: node id :param context: request - :param node_class: type of node we are expecting + :param _class: type of node we are expecting :return: node object that satisfies. If node not found then raise an exception. :raises Exception: raises grpc exception when node does not exist """ try: - node = session.get_node(node_id) - if isinstance(node, node_class): - return node - else: - actual = node.__class__.__name__ - expected = node_class.__name__ - context.abort( - grpc.StatusCode.NOT_FOUND, - f"node({node_id}) class({actual}) " - f"was not expected class({expected})", - ) - except CoreError: - context.abort(grpc.StatusCode.NOT_FOUND, f"node {node_id} not found") + return session.get_node(node_id, _class) + except CoreError as e: + context.abort(grpc.StatusCode.NOT_FOUND, str(e)) def validate_service( self, name: str, context: ServicerContext diff --git a/daemon/core/api/tlv/corehandlers.py b/daemon/core/api/tlv/corehandlers.py index d7e41a6c..7f647873 100644 --- a/daemon/core/api/tlv/corehandlers.py +++ b/daemon/core/api/tlv/corehandlers.py @@ -41,6 +41,7 @@ from core.emulator.enumerations import ( ) from core.errors import CoreCommandError, CoreError from core.location.mobility import BasicRangeModel +from core.nodes.base import CoreNodeBase, NodeBase from core.nodes.network import WlanNode from core.services.coreservices import ServiceManager, ServiceShim @@ -836,7 +837,7 @@ class CoreHandler(socketserver.BaseRequestHandler): return () try: - node = self.session.get_node(node_num) + node = self.session.get_node(node_num, CoreNodeBase) # build common TLV items for reply tlv_data = b"" @@ -1228,7 +1229,7 @@ class CoreHandler(socketserver.BaseRequestHandler): if not node_id: return replies - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, CoreNodeBase) if node is None: logging.warning( "request to configure service for unknown node %s", node_id @@ -1373,7 +1374,7 @@ class CoreHandler(socketserver.BaseRequestHandler): self.session.mobility.set_model_config(node_id, object_name, parsed_config) if self.session.state == EventTypes.RUNTIME_STATE and parsed_config: try: - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, WlanNode) if object_name == BasicRangeModel.name: node.updatemodel(parsed_config) except CoreError: @@ -1553,7 +1554,7 @@ class CoreHandler(socketserver.BaseRequestHandler): logging.debug("handling event %s at %s", event_type.name, time.ctime()) if event_type.value <= EventTypes.SHUTDOWN_STATE.value: if node_id is not None: - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, NodeBase) # configure mobility models for WLAN added during runtime if event_type == EventTypes.INSTANTIATION_STATE and isinstance( @@ -1647,7 +1648,7 @@ class CoreHandler(socketserver.BaseRequestHandler): name = event_data.name try: - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, CoreNodeBase) except CoreError: logging.warning( "ignoring event for service '%s', unknown node '%s'", name, node_id @@ -1883,7 +1884,7 @@ class CoreHandler(socketserver.BaseRequestHandler): data_types = tuple( repeat(ConfigDataTypes.STRING.value, len(ServiceShim.keys)) ) - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, CoreNodeBase) values = ServiceShim.tovaluelist(node, service) config_data = ConfigData( message_type=0, diff --git a/daemon/core/emane/commeffect.py b/daemon/core/emane/commeffect.py index 635291e0..f98f2454 100644 --- a/daemon/core/emane/commeffect.py +++ b/daemon/core/emane/commeffect.py @@ -10,6 +10,7 @@ from lxml import etree from core.config import ConfigGroup, Configuration from core.emane import emanemanifest, emanemodel +from core.emane.nodes import EmaneNet from core.nodes.interface import CoreInterface from core.xml import emanexml @@ -137,7 +138,7 @@ class EmaneCommEffectModel(emanemodel.EmaneModel): # TODO: batch these into multiple events per transmission # TODO: may want to split out seconds portion of delay and jitter event = CommEffectEvent() - emane_node = self.session.get_node(self.id) + emane_node = self.session.get_node(self.id, EmaneNet) nemid = emane_node.getnemid(netif) nemid2 = emane_node.getnemid(netif2) mbw = bw diff --git a/daemon/core/emane/emanemanager.py b/daemon/core/emane/emanemanager.py index 8b4bade2..16680e0e 100644 --- a/daemon/core/emane/emanemanager.py +++ b/daemon/core/emane/emanemanager.py @@ -21,7 +21,7 @@ from core.emane.rfpipe import EmaneRfPipeModel from core.emane.tdma import EmaneTdmaModel from core.emulator.enumerations import ConfigDataTypes, RegisterTlvs from core.errors import CoreCommandError, CoreError -from core.nodes.base import CoreNode +from core.nodes.base import CoreNode, NodeBase from core.nodes.interface import CoreInterface from core.nodes.network import CtrlNet from core.xml import emanexml @@ -801,8 +801,8 @@ class EmaneManager(ModelManager): zbit_check = z.bit_length() > 16 or z < 0 if any([xbit_check, ybit_check, zbit_check]): logging.error( - "Unable to build node location message, received lat/long/alt exceeds coordinate " - "space: NEM %s (%d, %d, %d)", + "Unable to build node location message, received lat/long/alt " + "exceeds coordinate space: NEM %s (%d, %d, %d)", nemid, x, y, @@ -812,7 +812,7 @@ class EmaneManager(ModelManager): # generate a node message for this location update try: - node = self.session.get_node(n) + node = self.session.get_node(n, NodeBase) except CoreError: logging.exception( "location event NEM %s has no corresponding node %s", nemid, n diff --git a/daemon/core/emane/emanemodel.py b/daemon/core/emane/emanemodel.py index 001ea8b0..57a73012 100644 --- a/daemon/core/emane/emanemodel.py +++ b/daemon/core/emane/emanemodel.py @@ -7,6 +7,7 @@ from typing import Dict, List from core.config import ConfigGroup, Configuration from core.emane import emanemanifest +from core.emane.nodes import EmaneNet from core.emulator.enumerations import ConfigDataTypes from core.errors import CoreError from core.location.mobility import WirelessModel @@ -148,7 +149,7 @@ class EmaneModel(WirelessModel): :return: nothing """ try: - wlan = self.session.get_node(self.id) + wlan = self.session.get_node(self.id, EmaneNet) wlan.setnempositions(moved_netifs) except CoreError: logging.exception("error during update") diff --git a/daemon/core/emulator/distributed.py b/daemon/core/emulator/distributed.py index 30becfb5..4e7fcdde 100644 --- a/daemon/core/emulator/distributed.py +++ b/daemon/core/emulator/distributed.py @@ -244,7 +244,7 @@ class DistributedController: ) return key & 0xFFFFFFFF - def get_tunnel(self, n1_id: int, n2_id: int) -> Tuple[GreTap, GreTap]: + def get_tunnel(self, n1_id: int, n2_id: int) -> GreTap: """ Return the GreTap between two nodes if it exists. diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index 95aa7c0b..17c46749 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -12,7 +12,7 @@ import subprocess import tempfile import threading import time -from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type +from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, TypeVar from core import constants, utils from core.emane.emanemanager import EmaneManager @@ -77,6 +77,7 @@ NODES = { NODES_TYPE = {NODES[x]: x for x in NODES} CTRL_NET_ID = 9001 LINK_COLORS = ["green", "blue", "orange", "purple", "turquoise"] +NT = TypeVar("NT", bound=NodeBase) class Session: @@ -194,7 +195,7 @@ class Session: def _link_nodes( self, node_one_id: int, node_two_id: int ) -> Tuple[ - CoreNode, CoreNode, CoreNetworkBase, CoreNetworkBase, Tuple[GreTap, GreTap] + Optional[NodeBase], Optional[NodeBase], CoreNetworkBase, CoreNetworkBase, GreTap ]: """ Convenience method for retrieving nodes within link data. @@ -212,8 +213,8 @@ class Session: net_two = None # retrieve node one - node_one = self.get_node(node_one_id) - node_two = self.get_node(node_two_id) + node_one = self.get_node(node_one_id, NodeBase) + node_two = self.get_node(node_two_id, NodeBase) # both node ids are provided tunnel = self.distributed.get_tunnel(node_one_id, node_two_id) @@ -225,6 +226,7 @@ class Session: else: node_two = None # physical node connected via gre tap tunnel + # TODO: double check this cases type elif tunnel: if tunnel.remotenum == node_one_id: node_one = None @@ -777,7 +779,7 @@ class Session: :raises core.CoreError: when node to update does not exist """ # get node to update - node = self.get_node(node_id) + node = self.get_node(node_id, NodeBase) # set node position and broadcast it self.set_node_position(node, options) @@ -908,9 +910,7 @@ class Session: :param data: file data :return: nothing """ - - node = self.get_node(node_id) - + node = self.get_node(node_id, CoreNodeBase) if source_name is not None: node.addfile(source_name, file_name) elif data is not None: @@ -1381,17 +1381,23 @@ class Session: self.nodes[node.id] = node return node - def get_node(self, _id: int) -> NodeBase: + def get_node(self, _id: int, _class: Type[NT]) -> NT: """ Get a session node. :param _id: node id to retrieve + :param _class: expected node class :return: node for the given id :raises core.CoreError: when node does not exist """ if _id not in self.nodes: raise CoreError(f"unknown node id {_id}") - return self.nodes[_id] + node = self.nodes[_id] + if not isinstance(node, _class): + actual = node.__class__.__name__ + expected = _class.__name__ + raise CoreError(f"node class({actual}) is not expected({expected})") + return node def delete_node(self, _id: int) -> bool: """ @@ -1709,10 +1715,7 @@ class Session: :return: control net :raises CoreError: when control net is not found """ - node = self.get_node(CTRL_NET_ID + net_index) - if not isinstance(node, CtrlNet): - raise CoreError("node is not a valid CtrlNet: %s", node.name) - return node + return self.get_node(CTRL_NET_ID + net_index, CtrlNet) def add_remove_control_net( self, net_index: int, remove: bool = False, conf_required: bool = True @@ -1959,7 +1962,7 @@ class Session: if not node_id: utils.mute_detach(data) else: - node = self.get_node(node_id) + node = self.get_node(node_id, CoreNodeBase) node.cmd(data, wait=False) def get_link_color(self, network_id: int) -> str: diff --git a/daemon/core/location/mobility.py b/daemon/core/location/mobility.py index d2cb0c8a..f2a47c1f 100644 --- a/daemon/core/location/mobility.py +++ b/daemon/core/location/mobility.py @@ -24,6 +24,7 @@ from core.emulator.enumerations import ( from core.errors import CoreError from core.nodes.base import CoreNode, NodeBase from core.nodes.interface import CoreInterface +from core.nodes.network import WlanNode if TYPE_CHECKING: from core.emulator.session import Session @@ -75,7 +76,7 @@ class MobilityManager(ModelManager): ) try: - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, WlanNode) except CoreError: logging.warning( "skipping mobility configuration for unknown node: %s", node_id @@ -103,9 +104,8 @@ class MobilityManager(ModelManager): event_type = event_data.event_type node_id = event_data.node name = event_data.name - try: - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, WlanNode) except CoreError: logging.exception( "Ignoring event for model '%s', unknown node '%s'", name, node_id @@ -190,7 +190,7 @@ class MobilityManager(ModelManager): """ for node_id in self.nodes(): try: - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, WlanNode) except CoreError: continue if node.model: @@ -299,7 +299,7 @@ class BasicRangeModel(WirelessModel): """ super().__init__(session, _id) self.session = session - self.wlan = session.get_node(_id) + self.wlan = session.get_node(_id, WlanNode) self._netifs = {} self._netifslock = threading.Lock() self.range = 0 @@ -590,7 +590,7 @@ class WayPointMobility(WirelessModel): self.initial = {} self.lasttime = None self.endtime = None - self.wlan = session.get_node(_id) + self.wlan = session.get_node(_id, WlanNode) # these are really set in child class via confmatrix self.loop = False self.refresh_ms = 50 diff --git a/daemon/core/nodes/base.py b/daemon/core/nodes/base.py index 2749323a..61e9e8fb 100644 --- a/daemon/core/nodes/base.py +++ b/daemon/core/nodes/base.py @@ -433,6 +433,28 @@ class CoreNodeBase(NodeBase): common.append((netif1.net, netif1, netif2)) return common + def nodefile(self, filename: str, contents: str, mode: int = 0o644) -> None: + """ + Create a node file with a given mode. + + :param filename: name of file to create + :param contents: contents of file + :param mode: mode for file + :return: nothing + """ + raise NotImplementedError + + def addfile(self, srcname: str, filename: str) -> None: + """ + Add a file. + + :param srcname: source file name + :param filename: file name to add + :return: nothing + :raises CoreCommandError: when a non-zero exit status occurs + """ + raise NotImplementedError + def cmd(self, args: str, wait: bool = True, shell: bool = False) -> str: """ Runs a command within a node container. diff --git a/daemon/core/nodes/physical.py b/daemon/core/nodes/physical.py index e8c999c6..baef7922 100644 --- a/daemon/core/nodes/physical.py +++ b/daemon/core/nodes/physical.py @@ -296,7 +296,6 @@ class Rj45Node(CoreNodeBase, CoreInterface): self.localname = name self.old_up = False self.old_addrs = [] - if start: self.startup() diff --git a/daemon/core/plugins/sdt.py b/daemon/core/plugins/sdt.py index 93663052..06c23de5 100644 --- a/daemon/core/plugins/sdt.py +++ b/daemon/core/plugins/sdt.py @@ -344,7 +344,7 @@ class Sdt: """ result = False try: - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, NodeBase) result = isinstance(node, (WlanNode, EmaneNet)) except CoreError: pass diff --git a/daemon/core/services/emaneservices.py b/daemon/core/services/emaneservices.py index e145e842..9d09516e 100644 --- a/daemon/core/services/emaneservices.py +++ b/daemon/core/services/emaneservices.py @@ -1,4 +1,5 @@ from core.emane.nodes import EmaneNet +from core.errors import CoreError from core.services.coreservices import CoreService from core.xml import emanexml @@ -20,8 +21,8 @@ class EmaneTransportService(CoreService): if filename == cls.configs[0]: transport_commands = [] for interface in node.netifs(sort=True): - network_node = node.session.get_node(interface.net.id) - if isinstance(network_node, EmaneNet): + try: + network_node = node.session.get_node(interface.net.id, EmaneNet) config = node.session.emane.get_configs( network_node.id, network_node.model.name ) @@ -32,6 +33,8 @@ class EmaneTransportService(CoreService): % nem_id ) transport_commands.append(command) + except CoreError: + pass transport_commands = "\n".join(transport_commands) return """ emanegentransportxml -o ../ ../platform%s.xml diff --git a/daemon/core/xml/corexml.py b/daemon/core/xml/corexml.py index deedd139..3d174db0 100644 --- a/daemon/core/xml/corexml.py +++ b/daemon/core/xml/corexml.py @@ -10,7 +10,7 @@ from core.emulator.data import LinkData from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions from core.emulator.enumerations import EventTypes, NodeTypes from core.errors import CoreXmlError -from core.nodes.base import CoreNetworkBase, CoreNodeBase, NodeBase +from core.nodes.base import CoreNodeBase, NodeBase from core.nodes.docker import DockerNode from core.nodes.lxd import LxcNode from core.nodes.network import CtrlNet, WlanNode @@ -505,9 +505,9 @@ class CoreXmlWriter: ip6_mask: int, ) -> etree.Element: interface = etree.Element(element_name) - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, NodeBase) interface_name = None - if not isinstance(node, CoreNetworkBase): + if isinstance(node, CoreNodeBase): node_interface = node.netif(interface_id) interface_name = node_interface.name @@ -523,7 +523,6 @@ class CoreXmlWriter: add_attribute(interface, "ip4_mask", ip4_mask) add_attribute(interface, "ip6", ip6) add_attribute(interface, "ip6_mask", ip6_mask) - return interface def create_link_element(self, link_data: LinkData) -> etree.Element: @@ -560,8 +559,8 @@ class CoreXmlWriter: link_element.append(interface_two) # check for options, don't write for emane/wlan links - node_one = self.session.get_node(link_data.node1_id) - node_two = self.session.get_node(link_data.node2_id) + node_one = self.session.get_node(link_data.node1_id, NodeBase) + node_two = self.session.get_node(link_data.node2_id, NodeBase) is_node_one_wireless = isinstance(node_one, (WlanNode, EmaneNet)) is_node_two_wireless = isinstance(node_two, (WlanNode, EmaneNet)) if not any([is_node_one_wireless, is_node_two_wireless]): @@ -902,7 +901,7 @@ class CoreXmlReader: for configservice_element in configservice_configs.iterchildren(): name = configservice_element.get("name") node_id = get_int(configservice_element, "node") - node = self.session.get_node(node_id) + node = self.session.get_node(node_id, CoreNodeBase) service = node.config_services[name] configs_element = configservice_element.find("configs") diff --git a/daemon/examples/python/emane80211.py b/daemon/examples/python/emane80211.py index e9764a09..6d8655f3 100644 --- a/daemon/examples/python/emane80211.py +++ b/daemon/examples/python/emane80211.py @@ -11,6 +11,7 @@ from core.emane.ieee80211abg import EmaneIeee80211abgModel from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions from core.emulator.enumerations import EventTypes, NodeTypes +from core.nodes.base import CoreNode NODES = 2 EMANE_DELAY = 10 @@ -51,8 +52,8 @@ def main(): time.sleep(EMANE_DELAY) # get nodes to run example - first_node = session.get_node(1) - last_node = session.get_node(NODES) + first_node = session.get_node(1, CoreNode) + last_node = session.get_node(NODES, CoreNode) address = prefixes.ip4_address(first_node) logging.info("node %s pinging %s", last_node.name, address) output = last_node.cmd(f"ping -c 3 {address}") diff --git a/daemon/examples/python/switch.py b/daemon/examples/python/switch.py index b4903457..d16303e6 100644 --- a/daemon/examples/python/switch.py +++ b/daemon/examples/python/switch.py @@ -8,6 +8,7 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes from core.emulator.enumerations import EventTypes, NodeTypes +from core.nodes.base import CoreNode NODES = 2 @@ -36,8 +37,8 @@ def main(): session.instantiate() # get nodes to run example - first_node = session.get_node(1) - last_node = session.get_node(NODES) + first_node = session.get_node(1, CoreNode) + last_node = session.get_node(NODES, CoreNode) address = prefixes.ip4_address(first_node) logging.info("node %s pinging %s", last_node.name, address) output = last_node.cmd(f"ping -c 3 {address}") diff --git a/daemon/examples/python/wlan.py b/daemon/examples/python/wlan.py index e9ae47f4..886d3ca9 100644 --- a/daemon/examples/python/wlan.py +++ b/daemon/examples/python/wlan.py @@ -9,6 +9,7 @@ from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions from core.emulator.enumerations import EventTypes, NodeTypes from core.location.mobility import BasicRangeModel +from core.nodes.base import CoreNode NODES = 2 @@ -40,8 +41,8 @@ def main(): session.instantiate() # get nodes for example run - first_node = session.get_node(1) - last_node = session.get_node(NODES) + first_node = session.get_node(1, CoreNode) + last_node = session.get_node(NODES, CoreNode) address = prefixes.ip4_address(first_node) logging.info("node %s pinging %s", last_node.name, address) output = last_node.cmd(f"ping -c 3 {address}") diff --git a/daemon/tests/emane/test_emane.py b/daemon/tests/emane/test_emane.py index 4c507eee..ada8e903 100644 --- a/daemon/tests/emane/test_emane.py +++ b/daemon/tests/emane/test_emane.py @@ -9,11 +9,13 @@ import pytest from core.emane.bypass import EmaneBypassModel from core.emane.commeffect import EmaneCommEffectModel from core.emane.ieee80211abg import EmaneIeee80211abgModel +from core.emane.nodes import EmaneNet from core.emane.rfpipe import EmaneRfPipeModel from core.emane.tdma import EmaneTdmaModel from core.emulator.emudata import NodeOptions from core.emulator.enumerations import NodeTypes from core.errors import CoreCommandError, CoreError +from core.nodes.base import CoreNode _EMANE_MODELS = [ EmaneIeee80211abgModel, @@ -133,9 +135,9 @@ class TestEmane: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, CoreNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, CoreNode) # load saved xml session.open_xml(file_path, start=True) @@ -146,7 +148,7 @@ class TestEmane: ) # verify nodes and configuration were restored - assert session.get_node(n1_id) - assert session.get_node(n2_id) - assert session.get_node(emane_id) + assert session.get_node(n1_id, CoreNode) + assert session.get_node(n2_id, CoreNode) + assert session.get_node(emane_id, EmaneNet) assert value == config_value diff --git a/daemon/tests/test_grpc.py b/daemon/tests/test_grpc.py index 5d8bfa1d..2580020a 100644 --- a/daemon/tests/test_grpc.py +++ b/daemon/tests/test_grpc.py @@ -19,6 +19,7 @@ from core.emulator.emudata import NodeOptions from core.emulator.enumerations import EventTypes, ExceptionLevels, NodeTypes from core.errors import CoreError from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility +from core.nodes.base import CoreNode from core.xml.corexml import CoreXmlWriter @@ -355,7 +356,7 @@ class TestGrpc: # then assert response.node_id is not None - assert session.get_node(response.node_id) is not None + assert session.get_node(response.node_id, CoreNode) is not None def test_get_node(self, grpc_server): # given @@ -402,7 +403,7 @@ class TestGrpc: assert response.result is expected if expected is True: with pytest.raises(CoreError): - assert session.get_node(node.id) + assert session.get_node(node.id, CoreNode) def test_node_command(self, request, grpc_server): if request.config.getoption("mock"): diff --git a/daemon/tests/test_gui.py b/daemon/tests/test_gui.py index 481a0fa9..40bc3d0b 100644 --- a/daemon/tests/test_gui.py +++ b/daemon/tests/test_gui.py @@ -24,6 +24,8 @@ from core.emane.ieee80211abg import EmaneIeee80211abgModel from core.emulator.enumerations import EventTypes, MessageFlags, NodeTypes, RegisterTlvs from core.errors import CoreError from core.location.mobility import BasicRangeModel +from core.nodes.base import CoreNode, NodeBase +from core.nodes.network import SwitchNode def dict_to_str(values): @@ -57,8 +59,7 @@ class TestGui: ) coretlv.handle_message(message) - - assert coretlv.session.get_node(node_id) is not None + assert coretlv.session.get_node(node_id, NodeBase) is not None def test_node_update(self, coretlv): node_id = 1 @@ -76,7 +77,7 @@ class TestGui: coretlv.handle_message(message) - node = coretlv.session.get_node(node_id) + node = coretlv.session.get_node(node_id, NodeBase) assert node is not None assert node.position.x == x assert node.position.y == y @@ -91,7 +92,7 @@ class TestGui: coretlv.handle_message(message) with pytest.raises(CoreError): - coretlv.session.get_node(node_id) + coretlv.session.get_node(node_id, NodeBase) def test_link_add_node_to_net(self, coretlv): node_one = 1 @@ -113,7 +114,7 @@ class TestGui: coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 1 @@ -137,7 +138,7 @@ class TestGui: coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 1 @@ -189,7 +190,7 @@ class TestGui: ], ) coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 1 link = all_links[0] @@ -207,7 +208,7 @@ class TestGui: ) coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 1 link = all_links[0] @@ -275,7 +276,7 @@ class TestGui: ], ) coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 1 @@ -289,7 +290,7 @@ class TestGui: ) coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 0 @@ -311,7 +312,7 @@ class TestGui: ], ) coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 1 @@ -325,7 +326,7 @@ class TestGui: ) coretlv.handle_message(message) - switch_node = coretlv.session.get_node(switch) + switch_node = coretlv.session.get_node(switch, SwitchNode) all_links = switch_node.all_link_data() assert len(all_links) == 0 @@ -556,8 +557,7 @@ class TestGui: ) coretlv.handle_message(message) - - assert coretlv.session.get_node(node.id) + assert coretlv.session.get_node(node.id, NodeBase) @pytest.mark.parametrize( "state", @@ -619,7 +619,7 @@ class TestGui: coretlv.handle_message(message) - assert coretlv.coreemu.sessions[1].get_node(node.id) + assert coretlv.coreemu.sessions[1].get_node(node.id, CoreNode) def test_register_python(self, coretlv, tmpdir): xml_file = tmpdir.join("test.py") diff --git a/daemon/tests/test_nodes.py b/daemon/tests/test_nodes.py index 8c85e0ca..42202f93 100644 --- a/daemon/tests/test_nodes.py +++ b/daemon/tests/test_nodes.py @@ -3,6 +3,7 @@ import pytest from core.emulator.emudata import NodeOptions from core.emulator.enumerations import NodeTypes from core.errors import CoreError +from core.nodes.base import CoreNode MODELS = ["router", "host", "PC", "mdr"] NET_TYPES = [NodeTypes.SWITCH, NodeTypes.HUB, NodeTypes.WIRELESS_LAN] @@ -45,7 +46,7 @@ class TestNodes: # then with pytest.raises(CoreError): - session.get_node(node.id) + session.get_node(node.id, CoreNode) def test_node_sethwaddr(self, session): # given diff --git a/daemon/tests/test_xml.py b/daemon/tests/test_xml.py index 04f1192d..3d0a67d3 100644 --- a/daemon/tests/test_xml.py +++ b/daemon/tests/test_xml.py @@ -6,6 +6,8 @@ from core.emulator.emudata import LinkOptions, NodeOptions from core.emulator.enumerations import EventTypes, NodeTypes from core.errors import CoreError from core.location.mobility import BasicRangeModel +from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode, WlanNode from core.services.utility import SshService @@ -91,16 +93,16 @@ class TestXml: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, CoreNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, CoreNode) # load saved xml session.open_xml(file_path, start=True) # verify nodes have been recreated - assert session.get_node(n1_id) - assert session.get_node(n2_id) + assert session.get_node(n1_id, CoreNode) + assert session.get_node(n2_id, CoreNode) def test_xml_ptp_services(self, session, tmpdir, ip_prefixes): """ @@ -152,9 +154,9 @@ class TestXml: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, CoreNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, CoreNode) # load saved xml session.open_xml(file_path, start=True) @@ -163,8 +165,8 @@ class TestXml: service = session.services.get_service(node_one.id, SshService.name) # verify nodes have been recreated - assert session.get_node(n1_id) - assert session.get_node(n2_id) + assert session.get_node(n1_id, CoreNode) + assert session.get_node(n2_id, CoreNode) assert service.config_data.get(service_file) == file_data def test_xml_mobility(self, session, tmpdir, ip_prefixes): @@ -212,9 +214,9 @@ class TestXml: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, CoreNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, CoreNode) # load saved xml session.open_xml(file_path, start=True) @@ -223,9 +225,9 @@ class TestXml: value = str(session.mobility.get_config("test", wlan_id, BasicRangeModel.name)) # verify nodes and configuration were restored - assert session.get_node(n1_id) - assert session.get_node(n2_id) - assert session.get_node(wlan_id) + assert session.get_node(n1_id, CoreNode) + assert session.get_node(n2_id, CoreNode) + assert session.get_node(wlan_id, WlanNode) assert value == "1" def test_network_to_network(self, session, tmpdir): @@ -263,16 +265,16 @@ class TestXml: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, SwitchNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, SwitchNode) # load saved xml session.open_xml(file_path, start=True) # verify nodes have been recreated - switch_one = session.get_node(n1_id) - switch_two = session.get_node(n2_id) + switch_one = session.get_node(n1_id, SwitchNode) + switch_two = session.get_node(n2_id, SwitchNode) assert switch_one assert switch_two assert len(switch_one.all_link_data() + switch_two.all_link_data()) == 1 @@ -322,16 +324,16 @@ class TestXml: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, CoreNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, SwitchNode) # load saved xml session.open_xml(file_path, start=True) # verify nodes have been recreated - assert session.get_node(n1_id) - assert session.get_node(n2_id) + assert session.get_node(n1_id, CoreNode) + assert session.get_node(n2_id, SwitchNode) links = [] for node_id in session.nodes: node = session.nodes[node_id] @@ -389,16 +391,16 @@ class TestXml: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, CoreNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, CoreNode) # load saved xml session.open_xml(file_path, start=True) # verify nodes have been recreated - assert session.get_node(n1_id) - assert session.get_node(n2_id) + assert session.get_node(n1_id, CoreNode) + assert session.get_node(n2_id, CoreNode) links = [] for node_id in session.nodes: node = session.nodes[node_id] @@ -471,16 +473,16 @@ class TestXml: # verify nodes have been removed from session with pytest.raises(CoreError): - assert not session.get_node(n1_id) + assert not session.get_node(n1_id, CoreNode) with pytest.raises(CoreError): - assert not session.get_node(n2_id) + assert not session.get_node(n2_id, CoreNode) # load saved xml session.open_xml(file_path, start=True) # verify nodes have been recreated - assert session.get_node(n1_id) - assert session.get_node(n2_id) + assert session.get_node(n1_id, CoreNode) + assert session.get_node(n2_id, CoreNode) links = [] for node_id in session.nodes: node = session.nodes[node_id] From c07766e1ebd7e5073006c348010d24517c28cee9 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Wed, 20 May 2020 22:14:03 -0700 Subject: [PATCH 2/2] updated session.add_node to use better type hinting and force usage of classes directly, instead of indirectly through NodeTypes --- daemon/core/api/grpc/grpcutils.py | 3 +- daemon/core/api/grpc/server.py | 3 +- daemon/core/api/tlv/corehandlers.py | 7 +- daemon/core/emulator/session.py | 51 +++++-------- daemon/core/xml/corexml.py | 6 +- daemon/examples/configservices/testing.py | 10 +-- daemon/examples/docker/docker2core.py | 8 ++- daemon/examples/docker/docker2docker.py | 7 +- daemon/examples/docker/switch.py | 13 ++-- daemon/examples/lxd/lxd2core.py | 8 ++- daemon/examples/lxd/lxd2lxd.py | 7 +- daemon/examples/lxd/switch.py | 13 ++-- daemon/examples/python/distributed_emane.py | 10 +-- daemon/examples/python/distributed_lxd.py | 7 +- daemon/examples/python/distributed_ptp.py | 5 +- daemon/examples/python/distributed_switch.py | 10 +-- daemon/examples/python/emane80211.py | 7 +- daemon/examples/python/switch.py | 7 +- daemon/examples/python/switch_inject.py | 11 +-- daemon/examples/python/wlan.py | 7 +- daemon/tests/emane/test_emane.py | 13 ++-- daemon/tests/test_conf.py | 9 +-- daemon/tests/test_core.py | 36 +++++----- daemon/tests/test_distributed.py | 7 +- daemon/tests/test_grpc.py | 74 +++++++++---------- daemon/tests/test_gui.py | 75 ++++++++++---------- daemon/tests/test_links.py | 31 ++++---- daemon/tests/test_nodes.py | 20 +++--- daemon/tests/test_services.py | 29 ++++---- daemon/tests/test_xml.py | 38 +++++----- docs/scripting.md | 59 +++++++++------ 31 files changed, 315 insertions(+), 276 deletions(-) diff --git a/daemon/core/api/grpc/grpcutils.py b/daemon/core/api/grpc/grpcutils.py index 4736f017..6281ec67 100644 --- a/daemon/core/api/grpc/grpcutils.py +++ b/daemon/core/api/grpc/grpcutils.py @@ -123,7 +123,8 @@ def create_nodes( funcs = [] for node_proto in node_protos: _type, _id, options = add_node_data(node_proto) - args = (_type, _id, options) + _class = session.get_node_class(_type) + args = (_class, _id, options) funcs.append((session.add_node, args, {})) start = time.monotonic() results, exceptions = utils.threadpool(funcs) diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 19779320..5fae97dc 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -667,7 +667,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): logging.debug("add node: %s", request) session = self.get_session(request.session_id, context) _type, _id, options = grpcutils.add_node_data(request.node) - node = session.add_node(_type=_type, _id=_id, options=options) + _class = session.get_node_class(_type) + node = session.add_node(_class, _id, options) return core_pb2.AddNodeResponse(node_id=node.id) def GetNode( diff --git a/daemon/core/api/tlv/corehandlers.py b/daemon/core/api/tlv/corehandlers.py index 7f647873..02a6294c 100644 --- a/daemon/core/api/tlv/corehandlers.py +++ b/daemon/core/api/tlv/corehandlers.py @@ -41,7 +41,7 @@ from core.emulator.enumerations import ( ) from core.errors import CoreCommandError, CoreError from core.location.mobility import BasicRangeModel -from core.nodes.base import CoreNodeBase, NodeBase +from core.nodes.base import CoreNode, CoreNodeBase, NodeBase from core.nodes.network import WlanNode from core.services.coreservices import ServiceManager, ServiceShim @@ -682,10 +682,11 @@ class CoreHandler(socketserver.BaseRequestHandler): logging.warning("ignoring invalid message: add and delete flag both set") return () - node_type = None + _class = CoreNode node_type_value = message.get_tlv(NodeTlvs.TYPE.value) if node_type_value is not None: node_type = NodeTypes(node_type_value) + _class = self.session.get_node_class(node_type) node_id = message.get_tlv(NodeTlvs.NUMBER.value) @@ -720,7 +721,7 @@ class CoreHandler(socketserver.BaseRequestHandler): options.services = services.split("|") if message.flags & MessageFlags.ADD.value: - node = self.session.add_node(node_type, node_id, options) + node = self.session.add_node(_class, node_id, options) if node: if message.flags & MessageFlags.STRING.value: self.node_status_request[node.id] = True diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index 17c46749..6f112ccf 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -75,6 +75,7 @@ NODES = { NodeTypes.LXC: LxcNode, } NODES_TYPE = {NODES[x]: x for x in NODES} +CONTAINER_NODES = {DockerNode, LxcNode} CTRL_NET_ID = 9001 LINK_COLORS = ["green", "blue", "orange", "purple", "turquoise"] NT = TypeVar("NT", bound=NodeBase) @@ -348,7 +349,7 @@ class Session: node_two.name, ) start = self.state.should_start() - net_one = self.create_node(cls=PtpNet, start=start) + net_one = self.create_node(_class=PtpNet, start=start) # node to network if node_one and net_one: @@ -662,32 +663,21 @@ class Session: node_two.lock.release() def add_node( - self, - _type: NodeTypes = NodeTypes.DEFAULT, - _id: int = None, - options: NodeOptions = None, - _cls: Type[NodeBase] = None, - ) -> NodeBase: + self, _class: Type[NT], _id: int = None, options: NodeOptions = None + ) -> NT: """ Add a node to the session, based on the provided node data. - :param _type: type of node to create + :param _class: node class to create :param _id: id for node, defaults to None for generated id :param options: data to create node with - :param _cls: optional custom class to use for a created node :return: created node :raises core.CoreError: when an invalid node type is given """ - # validate node type, get class, or throw error - if _cls is None: - node_class = self.get_node_class(_type) - else: - node_class = _cls - # set node start based on current session state, override and check when rj45 start = self.state.should_start() enable_rj45 = self.options.get_config("enablerj45") == "1" - if _type == NodeTypes.RJ45 and not enable_rj45: + if _class == Rj45Node and not enable_rj45: start = False # determine node id @@ -703,7 +693,7 @@ class Session: options.set_position(0, 0) name = options.name if not name: - name = f"{node_class.__name__}{_id}" + name = f"{_class.__name__}{_id}" # verify distributed server server = self.distributed.servers.get(options.server) @@ -713,24 +703,15 @@ class Session: # create node logging.info( "creating node(%s) id(%s) name(%s) start(%s)", - node_class.__name__, + _class.__name__, _id, name, start, ) - if _type in [NodeTypes.DOCKER, NodeTypes.LXC]: - node = self.create_node( - cls=node_class, - _id=_id, - name=name, - start=start, - image=options.image, - server=server, - ) - else: - node = self.create_node( - cls=node_class, _id=_id, name=name, start=start, server=server - ) + kwargs = dict(_id=_id, name=name, start=start, server=server) + if _class in CONTAINER_NODES: + kwargs["image"] = options.image + node = self.create_node(_class, **kwargs) # set node attributes node.icon = options.icon @@ -1363,17 +1344,17 @@ class Session: break return node_id - def create_node(self, cls: Type[NodeBase], *args: Any, **kwargs: Any) -> NodeBase: + def create_node(self, _class: Type[NT], *args: Any, **kwargs: Any) -> NT: """ Create an emulation node. - :param cls: node class to create + :param _class: node class to create :param args: list of arguments for the class to create :param kwargs: dictionary of arguments for the class to create :return: the created node instance :raises core.CoreError: when id of the node to create already exists """ - node = cls(self, *args, **kwargs) + node = _class(self, *args, **kwargs) with self._nodes_lock: if node.id in self.nodes: node.shutdown() @@ -1791,7 +1772,7 @@ class Session: server_interface, ) control_net = self.create_node( - cls=CtrlNet, + _class=CtrlNet, _id=_id, prefix=prefix, assign_address=True, diff --git a/daemon/core/xml/corexml.py b/daemon/core/xml/corexml.py index 3d174db0..ddb51b28 100644 --- a/daemon/core/xml/corexml.py +++ b/daemon/core/xml/corexml.py @@ -840,6 +840,7 @@ class CoreXmlReader: node_type = NodeTypes.DOCKER elif clazz == "lxc": node_type = NodeTypes.LXC + _class = self.session.get_node_class(node_type) service_elements = device_element.find("services") if service_elements is not None: @@ -865,12 +866,13 @@ class CoreXmlReader: options.set_location(lat, lon, alt) logging.info("reading node id(%s) model(%s) name(%s)", node_id, model, name) - self.session.add_node(_type=node_type, _id=node_id, options=options) + self.session.add_node(_class, node_id, options) def read_network(self, network_element: etree.Element) -> None: node_id = get_int(network_element, "id") name = network_element.get("name") node_type = NodeTypes[network_element.get("type")] + _class = self.session.get_node_class(node_type) icon = network_element.get("icon") options = NodeOptions(name) options.icon = icon @@ -891,7 +893,7 @@ class CoreXmlReader: logging.info( "reading node id(%s) node_type(%s) name(%s)", node_id, node_type, name ) - self.session.add_node(_type=node_type, _id=node_id, options=options) + self.session.add_node(_class, node_id, options) def read_configservice_configs(self) -> None: configservice_configs = self.scenario.find("configservice_configurations") diff --git a/daemon/examples/configservices/testing.py b/daemon/examples/configservices/testing.py index 5b193aee..bc67ff46 100644 --- a/daemon/examples/configservices/testing.py +++ b/daemon/examples/configservices/testing.py @@ -2,7 +2,9 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) @@ -13,16 +15,16 @@ if __name__ == "__main__": coreemu = CoreEmu() session = coreemu.create_session() session.set_state(EventTypes.CONFIGURATION_STATE) - switch = session.add_node(_type=NodeTypes.SWITCH) + switch = session.add_node(SwitchNode) # node one options.config_services = ["DefaultRoute", "IPForward"] - node_one = session.add_node(options=options) + node_one = session.add_node(CoreNode, options=options) interface = prefixes.create_interface(node_one) session.add_link(node_one.id, switch.id, interface_one=interface) # node two - node_two = session.add_node(options=options) + node_two = session.add_node(CoreNode, options=options) interface = prefixes.create_interface(node_two) session.add_link(node_two.id, switch.id, interface_one=interface) diff --git a/daemon/examples/docker/docker2core.py b/daemon/examples/docker/docker2core.py index 86cf3dfe..1211a16f 100644 --- a/daemon/examples/docker/docker2core.py +++ b/daemon/examples/docker/docker2core.py @@ -2,7 +2,9 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode +from core.nodes.docker import DockerNode if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) @@ -15,11 +17,11 @@ if __name__ == "__main__": options = NodeOptions(model=None, image="ubuntu") # create node one - node_one = session.add_node(_type=NodeTypes.DOCKER, options=options) + node_one = session.add_node(DockerNode, options=options) interface_one = prefixes.create_interface(node_one) # create node two - node_two = session.add_node() + node_two = session.add_node(CoreNode) interface_two = prefixes.create_interface(node_two) # add link diff --git a/daemon/examples/docker/docker2docker.py b/daemon/examples/docker/docker2docker.py index 261a8f67..9e1ae11f 100644 --- a/daemon/examples/docker/docker2docker.py +++ b/daemon/examples/docker/docker2docker.py @@ -2,7 +2,8 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.docker import DockerNode if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) @@ -17,11 +18,11 @@ if __name__ == "__main__": options = NodeOptions(model=None, image="ubuntu") # create node one - node_one = session.add_node(_type=NodeTypes.DOCKER, options=options) + node_one = session.add_node(DockerNode, options=options) interface_one = prefixes.create_interface(node_one) # create node two - node_two = session.add_node(_type=NodeTypes.DOCKER, options=options) + node_two = session.add_node(DockerNode, options=options) interface_two = prefixes.create_interface(node_two) # add link diff --git a/daemon/examples/docker/switch.py b/daemon/examples/docker/switch.py index f66863e5..74d58fe0 100644 --- a/daemon/examples/docker/switch.py +++ b/daemon/examples/docker/switch.py @@ -2,7 +2,10 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode +from core.nodes.docker import DockerNode +from core.nodes.network import SwitchNode if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) @@ -16,18 +19,18 @@ if __name__ == "__main__": options = NodeOptions(model=None, image="ubuntu") # create switch - switch = session.add_node(_type=NodeTypes.SWITCH) + switch = session.add_node(SwitchNode) # node one - node_one = session.add_node(_type=NodeTypes.DOCKER, options=options) + node_one = session.add_node(DockerNode, options=options) interface_one = prefixes.create_interface(node_one) # node two - node_two = session.add_node(_type=NodeTypes.DOCKER, options=options) + node_two = session.add_node(DockerNode, options=options) interface_two = prefixes.create_interface(node_two) # node three - node_three = session.add_node() + node_three = session.add_node(CoreNode) interface_three = prefixes.create_interface(node_three) # add links diff --git a/daemon/examples/lxd/lxd2core.py b/daemon/examples/lxd/lxd2core.py index 06b2b6ba..1365bd4c 100644 --- a/daemon/examples/lxd/lxd2core.py +++ b/daemon/examples/lxd/lxd2core.py @@ -2,7 +2,9 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode +from core.nodes.lxd import LxcNode if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) @@ -15,11 +17,11 @@ if __name__ == "__main__": options = NodeOptions(image="ubuntu") # create node one - node_one = session.add_node(_type=NodeTypes.LXC, options=options) + node_one = session.add_node(LxcNode, options=options) interface_one = prefixes.create_interface(node_one) # create node two - node_two = session.add_node() + node_two = session.add_node(CoreNode) interface_two = prefixes.create_interface(node_two) # add link diff --git a/daemon/examples/lxd/lxd2lxd.py b/daemon/examples/lxd/lxd2lxd.py index 2449a223..53a360e8 100644 --- a/daemon/examples/lxd/lxd2lxd.py +++ b/daemon/examples/lxd/lxd2lxd.py @@ -2,7 +2,8 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.lxd import LxcNode if __name__ == "__main__": logging.basicConfig(level=logging.INFO) @@ -17,11 +18,11 @@ if __name__ == "__main__": options = NodeOptions(image="ubuntu:18.04") # create node one - node_one = session.add_node(_type=NodeTypes.LXC, options=options) + node_one = session.add_node(LxcNode, options=options) interface_one = prefixes.create_interface(node_one) # create node two - node_two = session.add_node(_type=NodeTypes.LXC, options=options) + node_two = session.add_node(LxcNode, options=options) interface_two = prefixes.create_interface(node_two) # add link diff --git a/daemon/examples/lxd/switch.py b/daemon/examples/lxd/switch.py index 7deaae5f..3b6226e4 100644 --- a/daemon/examples/lxd/switch.py +++ b/daemon/examples/lxd/switch.py @@ -2,7 +2,10 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode +from core.nodes.lxd import LxcNode +from core.nodes.network import SwitchNode if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) @@ -16,18 +19,18 @@ if __name__ == "__main__": options = NodeOptions(image="ubuntu") # create switch - switch = session.add_node(_type=NodeTypes.SWITCH) + switch = session.add_node(SwitchNode) # node one - node_one = session.add_node(_type=NodeTypes.LXC, options=options) + node_one = session.add_node(LxcNode, options=options) interface_one = prefixes.create_interface(node_one) # node two - node_two = session.add_node(_type=NodeTypes.LXC, options=options) + node_two = session.add_node(LxcNode, options=options) interface_two = prefixes.create_interface(node_two) # node three - node_three = session.add_node() + node_three = session.add_node(CoreNode) interface_three = prefixes.create_interface(node_three) # add links diff --git a/daemon/examples/python/distributed_emane.py b/daemon/examples/python/distributed_emane.py index 4b748803..3248a8e3 100644 --- a/daemon/examples/python/distributed_emane.py +++ b/daemon/examples/python/distributed_emane.py @@ -7,9 +7,11 @@ import argparse import logging from core.emane.ieee80211abg import EmaneIeee80211abgModel +from core.emane.nodes import EmaneNet from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode def parse(name): @@ -50,11 +52,11 @@ def main(args): # create local node, switch, and remote nodes options = NodeOptions(model="mdr") options.set_position(0, 0) - node_one = session.add_node(options=options) - emane_net = session.add_node(_type=NodeTypes.EMANE) + node_one = session.add_node(CoreNode, options=options) + emane_net = session.add_node(EmaneNet) session.emane.set_model(emane_net, EmaneIeee80211abgModel) options.server = server_name - node_two = session.add_node(options=options) + node_two = session.add_node(CoreNode, options=options) # create node interfaces and link interface_one = prefixes.create_interface(node_one) diff --git a/daemon/examples/python/distributed_lxd.py b/daemon/examples/python/distributed_lxd.py index 8d46d599..de919012 100644 --- a/daemon/examples/python/distributed_lxd.py +++ b/daemon/examples/python/distributed_lxd.py @@ -8,7 +8,8 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.lxd import LxcNode def parse(name): @@ -42,9 +43,9 @@ def main(args): # create local node, switch, and remote nodes options = NodeOptions(image="ubuntu:18.04") - node_one = session.add_node(_type=NodeTypes.LXC, options=options) + node_one = session.add_node(LxcNode, options=options) options.server = server_name - node_two = session.add_node(_type=NodeTypes.LXC, options=options) + node_two = session.add_node(LxcNode, options=options) # create node interfaces and link interface_one = prefixes.create_interface(node_one) diff --git a/daemon/examples/python/distributed_ptp.py b/daemon/examples/python/distributed_ptp.py index 85069603..26531399 100644 --- a/daemon/examples/python/distributed_ptp.py +++ b/daemon/examples/python/distributed_ptp.py @@ -9,6 +9,7 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode def parse(name): @@ -42,9 +43,9 @@ def main(args): # create local node, switch, and remote nodes options = NodeOptions() - node_one = session.add_node(options=options) + node_one = session.add_node(CoreNode, options=options) options.server = server_name - node_two = session.add_node(options=options) + node_two = session.add_node(CoreNode, options=options) # create node interfaces and link interface_one = prefixes.create_interface(node_one) diff --git a/daemon/examples/python/distributed_switch.py b/daemon/examples/python/distributed_switch.py index 57c6141b..c52c1cc1 100644 --- a/daemon/examples/python/distributed_switch.py +++ b/daemon/examples/python/distributed_switch.py @@ -8,7 +8,9 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode def parse(name): @@ -43,11 +45,11 @@ def main(args): session.set_state(EventTypes.CONFIGURATION_STATE) # create local node, switch, and remote nodes - node_one = session.add_node() - switch = session.add_node(_type=NodeTypes.SWITCH) + node_one = session.add_node(CoreNode) + switch = session.add_node(SwitchNode) options = NodeOptions() options.server = server_name - node_two = session.add_node(options=options) + node_two = session.add_node(CoreNode, options=options) # create node interfaces and link interface_one = prefixes.create_interface(node_one) diff --git a/daemon/examples/python/emane80211.py b/daemon/examples/python/emane80211.py index 6d8655f3..da93026b 100644 --- a/daemon/examples/python/emane80211.py +++ b/daemon/examples/python/emane80211.py @@ -8,9 +8,10 @@ import logging import time from core.emane.ieee80211abg import EmaneIeee80211abgModel +from core.emane.nodes import EmaneNet from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes from core.nodes.base import CoreNode NODES = 2 @@ -33,13 +34,13 @@ def main(): session.set_location(47.57917, -122.13232, 2.00000, 1.0) options = NodeOptions() options.set_position(80, 50) - emane_network = session.add_node(_type=NodeTypes.EMANE, options=options, _id=100) + emane_network = session.add_node(EmaneNet, options=options, _id=100) session.emane.set_model(emane_network, EmaneIeee80211abgModel) # create nodes options = NodeOptions(model="mdr") for i in range(NODES): - node = session.add_node(options=options) + node = session.add_node(CoreNode, options=options) node.setposition(x=150 * (i + 1), y=150) interface = prefixes.create_interface(node) session.add_link(node.id, emane_network.id, interface_one=interface) diff --git a/daemon/examples/python/switch.py b/daemon/examples/python/switch.py index d16303e6..9475fc47 100644 --- a/daemon/examples/python/switch.py +++ b/daemon/examples/python/switch.py @@ -7,8 +7,9 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode NODES = 2 @@ -25,11 +26,11 @@ def main(): session.set_state(EventTypes.CONFIGURATION_STATE) # create switch network node - switch = session.add_node(_type=NodeTypes.SWITCH, _id=100) + switch = session.add_node(SwitchNode, _id=100) # create nodes for _ in range(NODES): - node = session.add_node() + node = session.add_node(CoreNode) interface = prefixes.create_interface(node) session.add_link(node.id, switch.id, interface_one=interface) diff --git a/daemon/examples/python/switch_inject.py b/daemon/examples/python/switch_inject.py index e85880e6..8c929e91 100644 --- a/daemon/examples/python/switch_inject.py +++ b/daemon/examples/python/switch_inject.py @@ -7,8 +7,11 @@ same CoreEmu instance the GUI is using. import logging +from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes +from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode NODES = 2 @@ -18,18 +21,18 @@ def main(): prefixes = IpPrefixes("10.83.0.0/16") # create emulator instance for creating sessions and utility methods - coreemu = globals()["coreemu"] + coreemu: CoreEmu = globals()["coreemu"] session = coreemu.create_session() # must be in configuration state for nodes to start, when using "node_add" below session.set_state(EventTypes.CONFIGURATION_STATE) # create switch network node - switch = session.add_node(_type=NodeTypes.SWITCH) + switch = session.add_node(SwitchNode) # create nodes for _ in range(NODES): - node = session.add_node() + node = session.add_node(CoreNode) interface = prefixes.create_interface(node) session.add_link(node.id, switch.id, interface_one=interface) diff --git a/daemon/examples/python/wlan.py b/daemon/examples/python/wlan.py index 886d3ca9..b09ae5ce 100644 --- a/daemon/examples/python/wlan.py +++ b/daemon/examples/python/wlan.py @@ -7,9 +7,10 @@ import logging from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes from core.location.mobility import BasicRangeModel from core.nodes.base import CoreNode +from core.nodes.network import WlanNode NODES = 2 @@ -26,14 +27,14 @@ def main(): session.set_state(EventTypes.CONFIGURATION_STATE) # create wlan network node - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN, _id=100) + wlan = session.add_node(WlanNode, _id=100) session.mobility.set_model(wlan, BasicRangeModel) # create nodes, must set a position for wlan basic range model options = NodeOptions(model="mdr") options.set_position(0, 0) for _ in range(NODES): - node = session.add_node(options=options) + node = session.add_node(CoreNode, options=options) interface = prefixes.create_interface(node) session.add_link(node.id, wlan.id, interface_one=interface) diff --git a/daemon/tests/emane/test_emane.py b/daemon/tests/emane/test_emane.py index ada8e903..b3499337 100644 --- a/daemon/tests/emane/test_emane.py +++ b/daemon/tests/emane/test_emane.py @@ -13,7 +13,6 @@ from core.emane.nodes import EmaneNet from core.emane.rfpipe import EmaneRfPipeModel from core.emane.tdma import EmaneTdmaModel from core.emulator.emudata import NodeOptions -from core.emulator.enumerations import NodeTypes from core.errors import CoreCommandError, CoreError from core.nodes.base import CoreNode @@ -52,7 +51,7 @@ class TestEmane: session.set_location(47.57917, -122.13232, 2.00000, 1.0) options = NodeOptions() options.set_position(80, 50) - emane_network = session.add_node(_type=NodeTypes.EMANE, options=options) + emane_network = session.add_node(EmaneNet, options=options) session.emane.set_model(emane_network, model) # configure tdma @@ -66,9 +65,9 @@ class TestEmane: # create nodes options = NodeOptions(model="mdr") options.set_position(150, 150) - node_one = session.add_node(options=options) + node_one = session.add_node(CoreNode, options=options) options.set_position(300, 150) - node_two = session.add_node(options=options) + node_two = session.add_node(CoreNode, options=options) for i, node in enumerate([node_one, node_two]): node.setposition(x=150 * (i + 1), y=150) @@ -94,7 +93,7 @@ class TestEmane: session.set_location(47.57917, -122.13232, 2.00000, 1.0) options = NodeOptions() options.set_position(80, 50) - emane_network = session.add_node(_type=NodeTypes.EMANE, options=options) + emane_network = session.add_node(EmaneNet, options=options) config_key = "txpower" config_value = "10" session.emane.set_model( @@ -104,9 +103,9 @@ class TestEmane: # create nodes options = NodeOptions(model="mdr") options.set_position(150, 150) - node_one = session.add_node(options=options) + node_one = session.add_node(CoreNode, options=options) options.set_position(300, 150) - node_two = session.add_node(options=options) + node_two = session.add_node(CoreNode, options=options) for i, node in enumerate([node_one, node_two]): node.setposition(x=150 * (i + 1), y=150) diff --git a/daemon/tests/test_conf.py b/daemon/tests/test_conf.py index 10b36df7..55f6260a 100644 --- a/daemon/tests/test_conf.py +++ b/daemon/tests/test_conf.py @@ -7,8 +7,9 @@ from core.config import ( ModelManager, ) from core.emane.ieee80211abg import EmaneIeee80211abgModel -from core.emulator.enumerations import ConfigDataTypes, NodeTypes +from core.emulator.enumerations import ConfigDataTypes from core.location.mobility import BasicRangeModel +from core.nodes.network import WlanNode class TestConfigurableOptions(ConfigurableOptions): @@ -147,7 +148,7 @@ class TestConf: def test_model_set(self, session): # given - wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan_node = session.add_node(WlanNode) # when session.mobility.set_model(wlan_node, BasicRangeModel) @@ -157,7 +158,7 @@ class TestConf: def test_model_set_error(self, session): # given - wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan_node = session.add_node(WlanNode) # when / then with pytest.raises(ValueError): @@ -165,7 +166,7 @@ class TestConf: def test_get_models(self, session): # given - wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan_node = session.add_node(WlanNode) session.mobility.set_model(wlan_node, BasicRangeModel) # when diff --git a/daemon/tests/test_core.py b/daemon/tests/test_core.py index cc9ba2a4..e663b85f 100644 --- a/daemon/tests/test_core.py +++ b/daemon/tests/test_core.py @@ -8,13 +8,15 @@ import threading import pytest from core.emulator.emudata import NodeOptions -from core.emulator.enumerations import MessageFlags, NodeTypes +from core.emulator.enumerations import MessageFlags from core.errors import CoreCommandError from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility +from core.nodes.base import CoreNode +from core.nodes.network import HubNode, PtpNet, SwitchNode, WlanNode _PATH = os.path.abspath(os.path.dirname(__file__)) _MOBILITY_FILE = os.path.join(_PATH, "mobility.scen") -_WIRED = [NodeTypes.PEER_TO_PEER, NodeTypes.HUB, NodeTypes.SWITCH] +_WIRED = [PtpNet, HubNode, SwitchNode] def ping(from_node, to_node, ip_prefixes): @@ -39,11 +41,11 @@ class TestCore: """ # create net node - net_node = session.add_node(_type=net_type) + net_node = session.add_node(net_type) # create nodes - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) # link nodes to net node for node in [node_one, node_two]: @@ -66,11 +68,11 @@ class TestCore: :param ip_prefixes: generates ip addresses for nodes """ # create ptp - ptp_node = session.add_node(_type=NodeTypes.PEER_TO_PEER) + ptp_node = session.add_node(PtpNet) # create nodes - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) # link nodes to ptp net for node in [node_one, node_two]: @@ -99,11 +101,11 @@ class TestCore: """ # create ptp - ptp_node = session.add_node(_type=NodeTypes.PEER_TO_PEER) + ptp_node = session.add_node(PtpNet) # create nodes - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) # link nodes to ptp net for node in [node_one, node_two]: @@ -143,14 +145,14 @@ class TestCore: """ # create wlan - wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan_node = session.add_node(WlanNode) session.mobility.set_model(wlan_node, BasicRangeModel) # create nodes options = NodeOptions(model="mdr") options.set_position(0, 0) - node_one = session.add_node(options=options) - node_two = session.add_node(options=options) + node_one = session.add_node(CoreNode, options=options) + node_two = session.add_node(CoreNode, options=options) # link nodes for node in [node_one, node_two]: @@ -173,14 +175,14 @@ class TestCore: """ # create wlan - wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan_node = session.add_node(WlanNode) session.mobility.set_model(wlan_node, BasicRangeModel) # create nodes options = NodeOptions(model="mdr") options.set_position(0, 0) - node_one = session.add_node(options=options) - node_two = session.add_node(options=options) + node_one = session.add_node(CoreNode, options=options) + node_two = session.add_node(CoreNode, options=options) # link nodes for node in [node_one, node_two]: diff --git a/daemon/tests/test_distributed.py b/daemon/tests/test_distributed.py index 07f17ecb..2308db3d 100644 --- a/daemon/tests/test_distributed.py +++ b/daemon/tests/test_distributed.py @@ -1,5 +1,6 @@ from core.emulator.emudata import NodeOptions -from core.emulator.enumerations import NodeTypes +from core.nodes.base import CoreNode +from core.nodes.network import HubNode class TestDistributed: @@ -12,7 +13,7 @@ class TestDistributed: session.distributed.add_server(server_name, host) options = NodeOptions() options.server = server_name - node = session.add_node(options=options) + node = session.add_node(CoreNode, options=options) session.instantiate() # then @@ -30,7 +31,7 @@ class TestDistributed: session.distributed.add_server(server_name, host) options = NodeOptions() options.server = server_name - node = session.add_node(_type=NodeTypes.HUB, options=options) + node = session.add_node(HubNode, options=options) session.instantiate() # then diff --git a/daemon/tests/test_grpc.py b/daemon/tests/test_grpc.py index 2580020a..5f34e2e2 100644 --- a/daemon/tests/test_grpc.py +++ b/daemon/tests/test_grpc.py @@ -14,12 +14,14 @@ from core.api.grpc.wlan_pb2 import WlanConfig from core.api.tlv.dataconversion import ConfigShim from core.api.tlv.enumerations import ConfigFlags from core.emane.ieee80211abg import EmaneIeee80211abgModel +from core.emane.nodes import EmaneNet from core.emulator.data import EventData from core.emulator.emudata import NodeOptions from core.emulator.enumerations import EventTypes, ExceptionLevels, NodeTypes from core.errors import CoreError from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode, WlanNode from core.xml.corexml import CoreXmlWriter @@ -195,7 +197,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - session.add_node() + session.add_node(CoreNode) session.set_state(EventTypes.DEFINITION_STATE) # then @@ -362,7 +364,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) # then with client.context_connect(): @@ -375,7 +377,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) # then x, y = 10, 10 @@ -393,7 +395,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) # then with client.context_connect(): @@ -414,7 +416,7 @@ class TestGrpc: session = grpc_server.coreemu.create_session() session.set_state(EventTypes.CONFIGURATION_STATE) options = NodeOptions(model="Host") - node = session.add_node(options=options) + node = session.add_node(CoreNode, options=options) session.instantiate() output = "hello world" @@ -432,7 +434,7 @@ class TestGrpc: session = grpc_server.coreemu.create_session() session.set_state(EventTypes.CONFIGURATION_STATE) options = NodeOptions(model="Host") - node = session.add_node(options=options) + node = session.add_node(CoreNode, options=options) session.instantiate() # then @@ -509,8 +511,8 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - switch = session.add_node(_type=NodeTypes.SWITCH) - node = session.add_node() + switch = session.add_node(SwitchNode) + node = session.add_node(CoreNode) interface = ip_prefixes.create_interface(node) session.add_link(node.id, switch.id, interface) @@ -525,8 +527,8 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - switch = session.add_node(_type=NodeTypes.SWITCH) - node = session.add_node() + switch = session.add_node(SwitchNode) + node = session.add_node(CoreNode) interface = ip_prefixes.create_interface(node) session.add_link(node.id, switch.id, interface) @@ -539,8 +541,8 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - switch = session.add_node(_type=NodeTypes.SWITCH) - node = session.add_node() + switch = session.add_node(SwitchNode) + node = session.add_node(CoreNode) assert len(switch.all_link_data()) == 0 # then @@ -556,7 +558,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) # then interface = interface_helper.create_interface(node.id, 0) @@ -568,8 +570,8 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - switch = session.add_node(_type=NodeTypes.SWITCH) - node = session.add_node() + switch = session.add_node(SwitchNode) + node = session.add_node(CoreNode) interface = ip_prefixes.create_interface(node) session.add_link(node.id, switch.id, interface) options = core_pb2.LinkOptions(bandwidth=30000) @@ -591,9 +593,9 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node_one = session.add_node() + node_one = session.add_node(CoreNode) interface_one = ip_prefixes.create_interface(node_one) - node_two = session.add_node() + node_two = session.add_node(CoreNode) interface_two = ip_prefixes.create_interface(node_two) session.add_link(node_one.id, node_two.id, interface_one, interface_two) link_node = None @@ -618,7 +620,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = session.add_node(WlanNode) # then with client.context_connect(): @@ -632,7 +634,7 @@ class TestGrpc: client = CoreGrpcClient() session = grpc_server.coreemu.create_session() session.set_state(EventTypes.CONFIGURATION_STATE) - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = session.add_node(WlanNode) wlan.setmodel(BasicRangeModel, BasicRangeModel.default_values()) session.instantiate() range_key = "range" @@ -695,7 +697,7 @@ class TestGrpc: session.set_location(47.57917, -122.13232, 2.00000, 1.0) options = NodeOptions() options.emane = EmaneIeee80211abgModel.name - emane_network = session.add_node(_type=NodeTypes.EMANE, options=options) + emane_network = session.add_node(EmaneNet, options=options) session.emane.set_model(emane_network, EmaneIeee80211abgModel) config_key = "platform_id_start" config_value = "2" @@ -722,7 +724,7 @@ class TestGrpc: session.set_location(47.57917, -122.13232, 2.00000, 1.0) options = NodeOptions() options.emane = EmaneIeee80211abgModel.name - emane_network = session.add_node(_type=NodeTypes.EMANE, options=options) + emane_network = session.add_node(EmaneNet, options=options) session.emane.set_model(emane_network, EmaneIeee80211abgModel) config_key = "bandwidth" config_value = "900000" @@ -750,7 +752,7 @@ class TestGrpc: session.set_location(47.57917, -122.13232, 2.00000, 1.0) options = NodeOptions() options.emane = EmaneIeee80211abgModel.name - emane_network = session.add_node(_type=NodeTypes.EMANE, options=options) + emane_network = session.add_node(EmaneNet, options=options) session.emane.set_model(emane_network, EmaneIeee80211abgModel) # then @@ -778,7 +780,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = session.add_node(WlanNode) session.mobility.set_model_config(wlan.id, Ns2ScriptedMobility.name, {}) # then @@ -795,7 +797,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = session.add_node(WlanNode) session.mobility.set_model_config(wlan.id, Ns2ScriptedMobility.name, {}) # then @@ -809,7 +811,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = session.add_node(WlanNode) config_key = "refresh_ms" config_value = "60" @@ -828,7 +830,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = session.add_node(WlanNode) session.mobility.set_model_config(wlan.id, Ns2ScriptedMobility.name, {}) session.instantiate() @@ -881,7 +883,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) service_name = "DefaultRoute" session.services.set_service(node.id, service_name) @@ -899,7 +901,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) # then with client.context_connect(): @@ -912,7 +914,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) # then with client.context_connect(): @@ -927,7 +929,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) service_name = "DefaultRoute" validate = ["echo hello"] @@ -948,7 +950,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) service_name = "DefaultRoute" file_name = "defaultroute.sh" file_data = "echo hello" @@ -968,7 +970,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) service_name = "DefaultRoute" # then @@ -984,7 +986,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) queue = Queue() def handle_event(event_data): @@ -1005,8 +1007,8 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN) - node = session.add_node() + wlan = session.add_node(WlanNode) + node = session.add_node(CoreNode) interface = ip_prefixes.create_interface(node) session.add_link(node.id, wlan.id, interface) link_data = wlan.all_link_data()[0] @@ -1127,7 +1129,7 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - node = session.add_node() + node = session.add_node(CoreNode) queue = Queue() def handle_event(event_data): diff --git a/daemon/tests/test_gui.py b/daemon/tests/test_gui.py index 40bc3d0b..4a086e53 100644 --- a/daemon/tests/test_gui.py +++ b/daemon/tests/test_gui.py @@ -25,7 +25,7 @@ from core.emulator.enumerations import EventTypes, MessageFlags, NodeTypes, Regi from core.errors import CoreError from core.location.mobility import BasicRangeModel from core.nodes.base import CoreNode, NodeBase -from core.nodes.network import SwitchNode +from core.nodes.network import SwitchNode, WlanNode def dict_to_str(values): @@ -63,7 +63,7 @@ class TestGui: def test_node_update(self, coretlv): node_id = 1 - coretlv.session.add_node(_id=node_id) + coretlv.session.add_node(CoreNode, _id=node_id) x = 50 y = 100 message = coreapi.CoreNodeMessage.create( @@ -84,7 +84,7 @@ class TestGui: def test_node_delete(self, coretlv): node_id = 1 - coretlv.session.add_node(_id=node_id) + coretlv.session.add_node(CoreNode, _id=node_id) message = coreapi.CoreNodeMessage.create( MessageFlags.DELETE.value, [(NodeTlvs.NUMBER, node_id)] ) @@ -96,9 +96,9 @@ class TestGui: def test_link_add_node_to_net(self, coretlv): node_one = 1 - coretlv.session.add_node(_id=node_one) + coretlv.session.add_node(CoreNode, _id=node_one) switch = 2 - coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH) + coretlv.session.add_node(SwitchNode, _id=switch) ip_prefix = netaddr.IPNetwork("10.0.0.0/24") interface_one = str(ip_prefix[node_one]) message = coreapi.CoreLinkMessage.create( @@ -120,9 +120,9 @@ class TestGui: def test_link_add_net_to_node(self, coretlv): node_one = 1 - coretlv.session.add_node(_id=node_one) + coretlv.session.add_node(CoreNode, _id=node_one) switch = 2 - coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH) + coretlv.session.add_node(SwitchNode, _id=switch) ip_prefix = netaddr.IPNetwork("10.0.0.0/24") interface_one = str(ip_prefix[node_one]) message = coreapi.CoreLinkMessage.create( @@ -144,9 +144,9 @@ class TestGui: def test_link_add_node_to_node(self, coretlv): node_one = 1 - coretlv.session.add_node(_id=node_one) + coretlv.session.add_node(CoreNode, _id=node_one) node_two = 2 - coretlv.session.add_node(_id=node_two) + coretlv.session.add_node(CoreNode, _id=node_two) ip_prefix = netaddr.IPNetwork("10.0.0.0/24") interface_one = str(ip_prefix[node_one]) interface_two = str(ip_prefix[node_two]) @@ -174,9 +174,9 @@ class TestGui: def test_link_update(self, coretlv): node_one = 1 - coretlv.session.add_node(_id=node_one) + coretlv.session.add_node(CoreNode, _id=node_one) switch = 2 - coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH) + coretlv.session.add_node(SwitchNode, _id=switch) ip_prefix = netaddr.IPNetwork("10.0.0.0/24") interface_one = str(ip_prefix[node_one]) message = coreapi.CoreLinkMessage.create( @@ -216,9 +216,9 @@ class TestGui: def test_link_delete_node_to_node(self, coretlv): node_one = 1 - coretlv.session.add_node(_id=node_one) + coretlv.session.add_node(CoreNode, _id=node_one) node_two = 2 - coretlv.session.add_node(_id=node_two) + coretlv.session.add_node(CoreNode, _id=node_two) ip_prefix = netaddr.IPNetwork("10.0.0.0/24") interface_one = str(ip_prefix[node_one]) interface_two = str(ip_prefix[node_two]) @@ -260,9 +260,9 @@ class TestGui: def test_link_delete_node_to_net(self, coretlv): node_one = 1 - coretlv.session.add_node(_id=node_one) + coretlv.session.add_node(CoreNode, _id=node_one) switch = 2 - coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH) + coretlv.session.add_node(SwitchNode, _id=switch) ip_prefix = netaddr.IPNetwork("10.0.0.0/24") interface_one = str(ip_prefix[node_one]) message = coreapi.CoreLinkMessage.create( @@ -296,9 +296,9 @@ class TestGui: def test_link_delete_net_to_node(self, coretlv): node_one = 1 - coretlv.session.add_node(_id=node_one) + coretlv.session.add_node(CoreNode, _id=node_one) switch = 2 - coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH) + coretlv.session.add_node(SwitchNode, _id=switch) ip_prefix = netaddr.IPNetwork("10.0.0.0/24") interface_one = str(ip_prefix[node_one]) message = coreapi.CoreLinkMessage.create( @@ -396,7 +396,7 @@ class TestGui: assert file_data == data def test_file_service_file_set(self, coretlv): - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) service = "DefaultRoute" file_name = "defaultroute.sh" file_data = "echo hello" @@ -419,7 +419,7 @@ class TestGui: def test_file_node_file_copy(self, request, coretlv): file_name = "/var/log/test/node.log" - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) node.makenodedir() file_data = "echo hello" message = coreapi.CoreFileMessage.create( @@ -441,7 +441,7 @@ class TestGui: def test_exec_node_tty(self, coretlv): coretlv.dispatch_replies = mock.MagicMock() - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) message = coreapi.CoreExecMessage.create( MessageFlags.TTY.value, [ @@ -462,7 +462,7 @@ class TestGui: pytest.skip("mocking calls") coretlv.dispatch_replies = mock.MagicMock() - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) cmd = "echo hello" message = coreapi.CoreExecMessage.create( MessageFlags.TEXT.value | MessageFlags.LOCAL.value, @@ -481,7 +481,7 @@ class TestGui: def test_exec_node_command(self, coretlv): coretlv.dispatch_replies = mock.MagicMock() - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) cmd = "echo hello" message = coreapi.CoreExecMessage.create( MessageFlags.TEXT.value, @@ -516,7 +516,7 @@ class TestGui: def test_event_schedule(self, coretlv): coretlv.session.add_event = mock.MagicMock() - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) message = coreapi.CoreEventMessage.create( MessageFlags.ADD.value, [ @@ -535,7 +535,7 @@ class TestGui: def test_event_save_xml(self, coretlv, tmpdir): xml_file = tmpdir.join("coretlv.session.xml") file_path = xml_file.strpath - coretlv.session.add_node() + coretlv.session.add_node(CoreNode) message = coreapi.CoreEventMessage.create( 0, [(EventTlvs.TYPE, EventTypes.FILE_SAVE.value), (EventTlvs.NAME, file_path)], @@ -548,7 +548,7 @@ class TestGui: def test_event_open_xml(self, coretlv, tmpdir): xml_file = tmpdir.join("coretlv.session.xml") file_path = xml_file.strpath - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) coretlv.session.save_xml(file_path) coretlv.session.delete_node(node.id) message = coreapi.CoreEventMessage.create( @@ -571,7 +571,7 @@ class TestGui: ) def test_event_service(self, coretlv, state): coretlv.session.broadcast_event = mock.MagicMock() - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) message = coreapi.CoreEventMessage.create( 0, [ @@ -609,7 +609,7 @@ class TestGui: def test_register_xml(self, coretlv, tmpdir): xml_file = tmpdir.join("coretlv.session.xml") file_path = xml_file.strpath - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) coretlv.session.save_xml(file_path) coretlv.session.delete_node(node.id) message = coreapi.CoreRegMessage.create( @@ -625,9 +625,10 @@ class TestGui: xml_file = tmpdir.join("test.py") file_path = xml_file.strpath with open(file_path, "w") as f: + f.write("from core.nodes.base import CoreNode\n") f.write("coreemu = globals()['coreemu']\n") f.write(f"session = coreemu.sessions[{coretlv.session.id}]\n") - f.write("session.add_node()\n") + f.write("session.add_node(CoreNode)\n") message = coreapi.CoreRegMessage.create( 0, [(RegisterTlvs.EXECUTE_SERVER, file_path)] ) @@ -773,7 +774,7 @@ class TestGui: coretlv.handle_broadcast_config.assert_called_once() def test_config_services_request_specific(self, coretlv): - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) message = coreapi.CoreConfMessage.create( 0, [ @@ -790,7 +791,7 @@ class TestGui: coretlv.handle_broadcast_config.assert_called_once() def test_config_services_request_specific_file(self, coretlv): - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) message = coreapi.CoreConfMessage.create( 0, [ @@ -807,7 +808,7 @@ class TestGui: coretlv.session.broadcast_file.assert_called_once() def test_config_services_reset(self, coretlv): - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) service = "DefaultRoute" coretlv.session.services.set_service(node.id, service) message = coreapi.CoreConfMessage.create( @@ -824,7 +825,7 @@ class TestGui: assert coretlv.session.services.get_service(node.id, service) is None def test_config_services_set(self, coretlv): - node = coretlv.session.add_node() + node = coretlv.session.add_node(CoreNode) service = "DefaultRoute" values = {"meta": "metadata"} message = coreapi.CoreConfMessage.create( @@ -844,7 +845,7 @@ class TestGui: assert coretlv.session.services.get_service(node.id, service) is not None def test_config_mobility_reset(self, coretlv): - wlan = coretlv.session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = coretlv.session.add_node(WlanNode) message = coreapi.CoreConfMessage.create( 0, [ @@ -860,7 +861,7 @@ class TestGui: assert len(coretlv.session.mobility.node_configurations) == 0 def test_config_mobility_model_request(self, coretlv): - wlan = coretlv.session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = coretlv.session.add_node(WlanNode) message = coreapi.CoreConfMessage.create( 0, [ @@ -876,7 +877,7 @@ class TestGui: coretlv.handle_broadcast_config.assert_called_once() def test_config_mobility_model_update(self, coretlv): - wlan = coretlv.session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = coretlv.session.add_node(WlanNode) config_key = "range" config_value = "1000" values = {config_key: config_value} @@ -898,7 +899,7 @@ class TestGui: assert config[config_key] == config_value def test_config_emane_model_request(self, coretlv): - wlan = coretlv.session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = coretlv.session.add_node(WlanNode) message = coreapi.CoreConfMessage.create( 0, [ @@ -914,7 +915,7 @@ class TestGui: coretlv.handle_broadcast_config.assert_called_once() def test_config_emane_model_update(self, coretlv): - wlan = coretlv.session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan = coretlv.session.add_node(WlanNode) config_key = "distance" config_value = "50051" values = {config_key: config_value} diff --git a/daemon/tests/test_links.py b/daemon/tests/test_links.py index d32a1c5f..afbdaab1 100644 --- a/daemon/tests/test_links.py +++ b/daemon/tests/test_links.py @@ -1,11 +1,12 @@ from core.emulator.emudata import LinkOptions -from core.emulator.enumerations import NodeTypes +from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode def create_ptp_network(session, ip_prefixes): # create nodes - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) # link nodes to net node interface_one = ip_prefixes.create_interface(node_one) @@ -21,8 +22,8 @@ def create_ptp_network(session, ip_prefixes): class TestLinks: def test_ptp(self, session, ip_prefixes): # given - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) interface_one = ip_prefixes.create_interface(node_one) interface_two = ip_prefixes.create_interface(node_two) @@ -35,8 +36,8 @@ class TestLinks: def test_node_to_net(self, session, ip_prefixes): # given - node_one = session.add_node() - node_two = session.add_node(_type=NodeTypes.SWITCH) + node_one = session.add_node(CoreNode) + node_two = session.add_node(SwitchNode) interface_one = ip_prefixes.create_interface(node_one) # when @@ -48,8 +49,8 @@ class TestLinks: def test_net_to_node(self, session, ip_prefixes): # given - node_one = session.add_node(_type=NodeTypes.SWITCH) - node_two = session.add_node() + node_one = session.add_node(SwitchNode) + node_two = session.add_node(CoreNode) interface_two = ip_prefixes.create_interface(node_two) # when @@ -61,8 +62,8 @@ class TestLinks: def test_net_to_net(self, session): # given - node_one = session.add_node(_type=NodeTypes.SWITCH) - node_two = session.add_node(_type=NodeTypes.SWITCH) + node_one = session.add_node(SwitchNode) + node_two = session.add_node(SwitchNode) # when session.add_link(node_one.id, node_two.id) @@ -77,8 +78,8 @@ class TestLinks: per = 25 dup = 25 jitter = 10 - node_one = session.add_node() - node_two = session.add_node(_type=NodeTypes.SWITCH) + node_one = session.add_node(CoreNode) + node_two = session.add_node(SwitchNode) interface_one_data = ip_prefixes.create_interface(node_one) session.add_link(node_one.id, node_two.id, interface_one_data) interface_one = node_one.netif(interface_one_data.id) @@ -111,8 +112,8 @@ class TestLinks: def test_link_delete(self, session, ip_prefixes): # given - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) interface_one = ip_prefixes.create_interface(node_one) interface_two = ip_prefixes.create_interface(node_two) session.add_link(node_one.id, node_two.id, interface_one, interface_two) diff --git a/daemon/tests/test_nodes.py b/daemon/tests/test_nodes.py index 42202f93..f87e8e80 100644 --- a/daemon/tests/test_nodes.py +++ b/daemon/tests/test_nodes.py @@ -1,12 +1,12 @@ import pytest from core.emulator.emudata import NodeOptions -from core.emulator.enumerations import NodeTypes from core.errors import CoreError from core.nodes.base import CoreNode +from core.nodes.network import HubNode, SwitchNode, WlanNode MODELS = ["router", "host", "PC", "mdr"] -NET_TYPES = [NodeTypes.SWITCH, NodeTypes.HUB, NodeTypes.WIRELESS_LAN] +NET_TYPES = [SwitchNode, HubNode, WlanNode] class TestNodes: @@ -16,7 +16,7 @@ class TestNodes: options = NodeOptions(model=model) # when - node = session.add_node(options=options) + node = session.add_node(CoreNode, options=options) # then assert node @@ -25,7 +25,7 @@ class TestNodes: def test_node_update(self, session): # given - node = session.add_node() + node = session.add_node(CoreNode) position_value = 100 update_options = NodeOptions() update_options.set_position(x=position_value, y=position_value) @@ -39,7 +39,7 @@ class TestNodes: def test_node_delete(self, session): # given - node = session.add_node() + node = session.add_node(CoreNode) # when session.delete_node(node.id) @@ -50,7 +50,7 @@ class TestNodes: def test_node_sethwaddr(self, session): # given - node = session.add_node() + node = session.add_node(CoreNode) index = node.newnetif() interface = node.netif(index) mac = "aa:aa:aa:ff:ff:ff" @@ -63,7 +63,7 @@ class TestNodes: def test_node_sethwaddr_exception(self, session): # given - node = session.add_node() + node = session.add_node(CoreNode) index = node.newnetif() node.netif(index) mac = "aa:aa:aa:ff:ff:fff" @@ -74,7 +74,7 @@ class TestNodes: def test_node_addaddr(self, session): # given - node = session.add_node() + node = session.add_node(CoreNode) index = node.newnetif() interface = node.netif(index) addr = "192.168.0.1/24" @@ -87,7 +87,7 @@ class TestNodes: def test_node_addaddr_exception(self, session): # given - node = session.add_node() + node = session.add_node(CoreNode) index = node.newnetif() node.netif(index) addr = "256.168.0.1/24" @@ -101,7 +101,7 @@ class TestNodes: # given # when - node = session.add_node(_type=net_type) + node = session.add_node(net_type) # then assert node diff --git a/daemon/tests/test_services.py b/daemon/tests/test_services.py index 489a9ab7..c5a51461 100644 --- a/daemon/tests/test_services.py +++ b/daemon/tests/test_services.py @@ -4,6 +4,7 @@ import pytest from mock import MagicMock from core.errors import CoreCommandError +from core.nodes.base import CoreNode from core.services.coreservices import CoreService, ServiceDependencies, ServiceManager _PATH = os.path.abspath(os.path.dirname(__file__)) @@ -52,7 +53,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) file_name = "myservice.sh" - node = session.add_node() + node = session.add_node(CoreNode) # when session.services.set_service_file(node.id, SERVICE_ONE, file_name, "# test") @@ -66,7 +67,7 @@ class TestServices: def test_service_all_configs(self, session): # given ServiceManager.add_services(_SERVICES_PATH) - node = session.add_node() + node = session.add_node(CoreNode) # when session.services.set_service(node.id, SERVICE_ONE) @@ -80,7 +81,7 @@ class TestServices: def test_service_add_services(self, session): # given ServiceManager.add_services(_SERVICES_PATH) - node = session.add_node() + node = session.add_node(CoreNode) total_service = len(node.services) # when @@ -94,7 +95,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_ONE) - node = session.add_node() + node = session.add_node(CoreNode) file_name = my_service.configs[0] file_path = node.hostfilename(file_name) @@ -109,7 +110,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_ONE) - node = session.add_node() + node = session.add_node(CoreNode) session.services.create_service_files(node, my_service) # when @@ -122,7 +123,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_TWO) - node = session.add_node() + node = session.add_node(CoreNode) session.services.create_service_files(node, my_service) node.cmd = MagicMock(side_effect=CoreCommandError(-1, "invalid")) @@ -136,7 +137,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_ONE) - node = session.add_node() + node = session.add_node(CoreNode) session.services.create_service_files(node, my_service) # when @@ -149,7 +150,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_TWO) - node = session.add_node() + node = session.add_node(CoreNode) session.services.create_service_files(node, my_service) node.cmd = MagicMock(side_effect=CoreCommandError(-1, "invalid")) @@ -163,7 +164,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_ONE) - node = session.add_node() + node = session.add_node(CoreNode) session.services.create_service_files(node, my_service) # when @@ -176,7 +177,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_TWO) - node = session.add_node() + node = session.add_node(CoreNode) session.services.create_service_files(node, my_service) node.cmd = MagicMock(side_effect=CoreCommandError(-1, "invalid")) @@ -190,7 +191,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_ONE) - node = session.add_node() + node = session.add_node(CoreNode) # when session.services.set_service(node.id, my_service.name) @@ -204,8 +205,8 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_ONE) - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) file_name = my_service.configs[0] file_data_one = "# custom file one" file_data_two = "# custom file two" @@ -234,7 +235,7 @@ class TestServices: # given ServiceManager.add_services(_SERVICES_PATH) my_service = ServiceManager.get(SERVICE_ONE) - node = session.add_node() + node = session.add_node(CoreNode) # when no_service = session.services.get_service(node.id, SERVICE_ONE) diff --git a/daemon/tests/test_xml.py b/daemon/tests/test_xml.py index 3d0a67d3..897bb6fb 100644 --- a/daemon/tests/test_xml.py +++ b/daemon/tests/test_xml.py @@ -3,11 +3,11 @@ from xml.etree import ElementTree import pytest from core.emulator.emudata import LinkOptions, NodeOptions -from core.emulator.enumerations import EventTypes, NodeTypes +from core.emulator.enumerations import EventTypes from core.errors import CoreError from core.location.mobility import BasicRangeModel from core.nodes.base import CoreNode -from core.nodes.network import SwitchNode, WlanNode +from core.nodes.network import PtpNet, SwitchNode, WlanNode from core.services.utility import SshService @@ -61,11 +61,11 @@ class TestXml: :param ip_prefixes: generates ip addresses for nodes """ # create ptp - ptp_node = session.add_node(_type=NodeTypes.PEER_TO_PEER) + ptp_node = session.add_node(PtpNet) # create nodes - node_one = session.add_node() - node_two = session.add_node() + node_one = session.add_node(CoreNode) + node_two = session.add_node(CoreNode) # link nodes to ptp net for node in [node_one, node_two]: @@ -113,12 +113,12 @@ class TestXml: :param ip_prefixes: generates ip addresses for nodes """ # create ptp - ptp_node = session.add_node(_type=NodeTypes.PEER_TO_PEER) + ptp_node = session.add_node(PtpNet) # create nodes options = NodeOptions(model="host") - node_one = session.add_node(options=options) - node_two = session.add_node() + node_one = session.add_node(CoreNode, options=options) + node_two = session.add_node(CoreNode) # link nodes to ptp net for node in [node_one, node_two]: @@ -178,14 +178,14 @@ class TestXml: :param ip_prefixes: generates ip addresses for nodes """ # create wlan - wlan_node = session.add_node(_type=NodeTypes.WIRELESS_LAN) + wlan_node = session.add_node(WlanNode) session.mobility.set_model(wlan_node, BasicRangeModel, {"test": "1"}) # create nodes options = NodeOptions(model="mdr") options.set_position(0, 0) - node_one = session.add_node(options=options) - node_two = session.add_node(options=options) + node_one = session.add_node(CoreNode, options=options) + node_two = session.add_node(CoreNode, options=options) # link nodes for node in [node_one, node_two]: @@ -238,8 +238,8 @@ class TestXml: :param tmpdir: tmpdir to create data in """ # create nodes - switch_one = session.add_node(_type=NodeTypes.SWITCH) - switch_two = session.add_node(_type=NodeTypes.SWITCH) + switch_one = session.add_node(SwitchNode) + switch_two = session.add_node(SwitchNode) # link nodes session.add_link(switch_one.id, switch_two.id) @@ -288,9 +288,9 @@ class TestXml: :param ip_prefixes: generates ip addresses for nodes """ # create nodes - node_one = session.add_node() + node_one = session.add_node(CoreNode) interface_one = ip_prefixes.create_interface(node_one) - switch = session.add_node(_type=NodeTypes.SWITCH) + switch = session.add_node(SwitchNode) # create link link_options = LinkOptions() @@ -354,9 +354,9 @@ class TestXml: :param ip_prefixes: generates ip addresses for nodes """ # create nodes - node_one = session.add_node() + node_one = session.add_node(CoreNode) interface_one = ip_prefixes.create_interface(node_one) - node_two = session.add_node() + node_two = session.add_node(CoreNode) interface_two = ip_prefixes.create_interface(node_two) # create link @@ -421,9 +421,9 @@ class TestXml: :param ip_prefixes: generates ip addresses for nodes """ # create nodes - node_one = session.add_node() + node_one = session.add_node(CoreNode) interface_one = ip_prefixes.create_interface(node_one) - node_two = session.add_node() + node_two = session.add_node(CoreNode) interface_two = ip_prefixes.create_interface(node_two) # create link diff --git a/docs/scripting.md b/docs/scripting.md index aafbb7d3..7c8205c3 100644 --- a/docs/scripting.md +++ b/docs/scripting.md @@ -28,35 +28,54 @@ connections. Here are the basic elements of a CORE Python script: ```python +""" +This is a standalone script to run a small switch based scenario and will not +interact with the GUI. +""" + +import logging + from core.emulator.coreemu import CoreEmu from core.emulator.emudata import IpPrefixes from core.emulator.enumerations import EventTypes -from core.emulator.enumerations import NodeTypes +from core.nodes.base import CoreNode +from core.nodes.network import SwitchNode -# ip generator for example -prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16") +NODES = 2 -# create emulator instance for creating sessions and utility methods -coreemu = CoreEmu() -session = coreemu.create_session() -# must be in configuration state for nodes to start, when using "node_add" below -session.set_state(EventTypes.CONFIGURATION_STATE) +def main(): + # ip generator for example + prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16") -# create switch network node -switch = session.add_node(_type=NodeTypes.SWITCH) + # create emulator instance for creating sessions and utility methods + coreemu = CoreEmu() + session = coreemu.create_session() -# create nodes -for _ in range(2): - node = session.add_node() - interface = prefixes.create_interface(node) - session.add_link(node.id, switch.id, interface_one=interface) + # must be in configuration state for nodes to start, when using "node_add" below + session.set_state(EventTypes.CONFIGURATION_STATE) -# instantiate session -session.instantiate() + # create switch network node + switch = session.add_node(SwitchNode, _id=100) -# shutdown session -coreemu.shutdown() + # create nodes + for _ in range(NODES): + node = session.add_node(CoreNode) + interface = prefixes.create_interface(node) + session.add_link(node.id, switch.id, interface_one=interface) + + # instantiate session + session.instantiate() + + # run any desired logic here + + # shutdown session + coreemu.shutdown() + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + main() ``` The above script creates a CORE session having two nodes connected with a @@ -136,7 +155,7 @@ session = coreemu.create_session() session.set_location(47.57917, -122.13232, 2.00000, 1.0) options = NodeOptions() options.set_position(80, 50) -emane_network = session.add_node(_type=NodeTypes.EMANE, options=options) +emane_network = session.add_node(EmaneNet, options=options) # set custom emane model config config = {}