updated session.add_node to use better type hinting and force usage of classes directly, instead of indirectly through NodeTypes

This commit is contained in:
Blake Harnden 2020-05-20 22:14:03 -07:00
parent d5254e6a91
commit c07766e1eb
31 changed files with 315 additions and 276 deletions

View file

@ -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)

View file

@ -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(

View file

@ -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

View file

@ -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,

View file

@ -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")

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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]:

View file

@ -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

View file

@ -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):

View file

@ -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}

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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 = {}