diff --git a/daemon/core/api/grpc/clientw.py b/daemon/core/api/grpc/clientw.py index fdcc16d6..23a66f1f 100644 --- a/daemon/core/api/grpc/clientw.py +++ b/daemon/core/api/grpc/clientw.py @@ -352,6 +352,10 @@ class CoreGrpcClient: response = self.stub.StopSession(request) return response.result + def add_session(self, session_id: int = None) -> wrappers.Session: + session_id = self.create_session(session_id) + return self.get_session(session_id) + def create_session(self, session_id: int = None) -> int: """ Create a session. diff --git a/daemon/core/api/grpc/wrappers.py b/daemon/core/api/grpc/wrappers.py index 6b7ace26..41f7d7b8 100644 --- a/daemon/core/api/grpc/wrappers.py +++ b/daemon/core/api/grpc/wrappers.py @@ -395,11 +395,11 @@ class ExceptionEvent: @dataclass class ConfigOption: - label: str name: str value: str - type: ConfigOptionType - group: str + label: str = None + type: ConfigOptionType = None + group: str = None select: List[str] = None @classmethod @@ -737,6 +737,25 @@ class Node: canvas=self.canvas, ) + def set_wlan(self, config: Dict[str, str]) -> None: + for key, value in config.items(): + option = ConfigOption(name=key, value=value) + self.wlan_config[key] = option + + def set_mobility(self, config: Dict[str, str]) -> None: + for key, value in config.items(): + option = ConfigOption(name=key, value=value) + self.mobility_config[key] = option + + def set_emane_model( + self, model: str, config: Dict[str, str], iface_id: int = None + ) -> None: + key = (model, iface_id) + config_options = self.emane_model_configs.setdefault(key, {}) + for key, value in config.items(): + option = ConfigOption(name=key, value=value) + config_options[key] = option + @dataclass class Session: @@ -757,9 +776,6 @@ class Session: file: Path = None options: Dict[str, ConfigOption] = field(default_factory=dict) - def set_node(self, node: Node) -> None: - self.nodes[node.id] = node - @classmethod def from_proto(cls, proto: core_pb2.Session) -> "Session": nodes: Dict[int, Node] = {x.id: Node.from_proto(x) for x in proto.nodes} @@ -813,6 +829,62 @@ class Session: options=options, ) + def add_node( + self, + _id: int, + *, + name: str = None, + _type: NodeType = NodeType.DEFAULT, + model: str = "PC", + position: Position = None, + geo: Geo = None, + emane: str = None, + image: str = None, + server: str = None, + ) -> Node: + node = Node( + id=_id, + name=name, + type=_type, + model=model, + position=position, + geo=geo, + emane=emane, + image=image, + server=server, + ) + self.nodes[node.id] = node + return node + + def add_link( + self, + *, + node1: Node, + node2: Node, + iface1: Interface = None, + iface2: Interface = None, + options: LinkOptions = None, + ) -> Link: + link = Link( + node1_id=node1.id, + node2_id=node2.id, + iface1=iface1, + iface2=iface2, + options=options, + ) + self.links.append(link) + return link + + def set_emane(self, config: Dict[str, str]) -> None: + for key, value in config.items(): + option = ConfigOption(name=key, value=value) + self.emane_config[key] = option + + def set_options(self, config: Dict[str, str]) -> None: + for key, value in config.items(): + option = ConfigOption(name=key, value=value) + self.options[key] = option + @dataclass class LinkEvent: diff --git a/daemon/examples/grpc/distributed_switch.py b/daemon/examples/grpc/distributed_switch.py index e8ddfb4c..f9534b41 100644 --- a/daemon/examples/grpc/distributed_switch.py +++ b/daemon/examples/grpc/distributed_switch.py @@ -1,8 +1,8 @@ import argparse import logging -from core.api.grpc import client -from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState +from core.api.grpc import clientw +from core.api.grpc.wrappers import NodeType, Position def log_event(event): @@ -10,62 +10,41 @@ def log_event(event): def main(args): - core = client.CoreGrpcClient() + # helper to create interfaces + interface_helper = clientw.InterfaceHelper(ip4_prefix="10.83.0.0/16") - with core.context_connect(): - # create session - response = core.create_session() - session_id = response.session_id - logging.info("created session: %s", response) + # create grpc client and connect + core = clientw.CoreGrpcClient() + core.connect() - # add distributed server - server_name = "core2" - response = core.add_session_server(session_id, server_name, args.server) - logging.info("added session server: %s", response) + # create session + session = core.add_session() + logging.info("created session: %s", session.id) - # handle events session may broadcast - core.events(session_id, log_event) + # add distributed server + server_name = "core2" + result = core.add_session_server(session.id, server_name, args.server) + logging.info("added session server: %s", result) - # change session state - response = core.set_session_state(session_id, SessionState.CONFIGURATION) - logging.info("set session state: %s", response) + # handle events session may broadcast + core.events(session.id, log_event) - # create switch node - switch = Node(type=NodeType.SWITCH) - response = core.add_node(session_id, switch) - logging.info("created switch: %s", response) - switch_id = response.node_id + # create switch node + position = Position(x=150, y=100) + switch = session.add_node(1, _type=NodeType.SWITCH, position=position) + position = Position(x=100, y=50) + node1 = session.add_node(2, position=position) + position = Position(x=200, y=50) + node2 = session.add_node(3, position=position, server=server_name) - # helper to create interfaces - interface_helper = client.InterfaceHelper(ip4_prefix="10.83.0.0/16") + # create links + iface1 = interface_helper.create_iface(node1.id, 0) + session.add_link(node1=node1, node2=switch, iface1=iface1) + iface1 = interface_helper.create_iface(node2.id, 0) + session.add_link(node1=node2, node2=switch, iface1=iface1) - # create node one - position = Position(x=100, y=50) - node = Node(position=position) - response = core.add_node(session_id, node) - logging.info("created node one: %s", response) - node1_id = response.node_id - - # create link - interface1 = interface_helper.create_iface(node1_id, 0) - response = core.add_link(session_id, node1_id, switch_id, interface1) - logging.info("created link from node one to switch: %s", response) - - # create node two - position = Position(x=200, y=50) - node = Node(position=position, server=server_name) - response = core.add_node(session_id, node) - logging.info("created node two: %s", response) - node2_id = response.node_id - - # create link - interface1 = interface_helper.create_iface(node2_id, 0) - response = core.add_link(session_id, node2_id, switch_id, interface1) - logging.info("created link from node two to switch: %s", response) - - # change session state - response = core.set_session_state(session_id, SessionState.INSTANTIATION) - logging.info("set session state: %s", response) + # start session + core.start_session(session) if __name__ == "__main__": diff --git a/daemon/examples/grpc/emane80211.py b/daemon/examples/grpc/emane80211.py index c9ced47e..c6fba6c8 100644 --- a/daemon/examples/grpc/emane80211.py +++ b/daemon/examples/grpc/emane80211.py @@ -1,54 +1,37 @@ # required imports -from core.api.grpc import client -from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState +from core.api.grpc import clientw +from core.api.grpc.wrappers import NodeType, Position from core.emane.ieee80211abg import EmaneIeee80211abgModel # interface helper -iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") +iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") # create grpc client and connect -core = client.CoreGrpcClient() +core = clientw.CoreGrpcClient() core.connect() -# create session and get id -response = core.create_session() -session_id = response.session_id +# add session +session = core.add_session() -# change session state to configuration so that nodes get started when added -core.set_session_state(session_id, SessionState.CONFIGURATION) - -# create emane node +# create nodes position = Position(x=200, y=200) -emane = Node(type=NodeType.EMANE, position=position, emane=EmaneIeee80211abgModel.name) -response = core.add_node(session_id, emane) -emane_id = response.node_id - -# create node one -position = Position(x=100, y=100) -n1 = Node(type=NodeType.DEFAULT, position=position, model="mdr") -response = core.add_node(session_id, n1) -n1_id = response.node_id - -# create node two -position = Position(x=300, y=100) -n2 = Node(type=NodeType.DEFAULT, position=position, model="mdr") -response = core.add_node(session_id, n2) -n2_id = response.node_id - -# configure general emane settings -core.set_emane_config(session_id, {"eventservicettl": "2"}) - -# configure emane model settings -# using a dict mapping currently support values as strings -core.set_emane_model_config( - session_id, emane_id, EmaneIeee80211abgModel.name, {"unicastrate": "3"} +emane = session.add_node( + 1, _type=NodeType.EMANE, position=position, emane=EmaneIeee80211abgModel.name ) +position = Position(x=100, y=100) +node1 = session.add_node(2, model="mdr", position=position) +position = Position(x=300, y=100) +node2 = session.add_node(3, model="mdr", position=position) -# links nodes to emane -iface1 = iface_helper.create_iface(n1_id, 0) -core.add_link(session_id, n1_id, emane_id, iface1) -iface1 = iface_helper.create_iface(n2_id, 0) -core.add_link(session_id, n2_id, emane_id, iface1) +# create links +iface1 = iface_helper.create_iface(node1.id, 0) +session.add_link(node1=node1, node2=emane, iface1=iface1) +iface1 = iface_helper.create_iface(node2.id, 0) +session.add_link(node1=node2, node2=emane, iface1=iface1) -# change session state -core.set_session_state(session_id, SessionState.INSTANTIATION) +# setup emane configurations using a dict mapping currently support values as strings +session.set_emane({"eventservicettl": "2"}) +emane.set_emane_model(EmaneIeee80211abgModel.name, {"unicastrate": "3"}) + +# start session +core.start_session(session) diff --git a/daemon/examples/grpc/peertopeer.py b/daemon/examples/grpc/peertopeer.py index a5695b4b..8c1b47a1 100644 --- a/daemon/examples/grpc/peertopeer.py +++ b/daemon/examples/grpc/peertopeer.py @@ -1,36 +1,26 @@ -from core.api.grpc import client -from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState +from core.api.grpc import clientw +from core.api.grpc.wrappers import Position # interface helper -iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") +iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") # create grpc client and connect -core = client.CoreGrpcClient() +core = clientw.CoreGrpcClient() core.connect() -# create session and get id -response = core.create_session() -session_id = response.session_id +# add session +session = core.add_session() -# change session state to configuration so that nodes get started when added -core.set_session_state(session_id, SessionState.CONFIGURATION) - -# create node one +# create nodes position = Position(x=100, y=100) -n1 = Node(type=NodeType.DEFAULT, position=position, model="PC") -response = core.add_node(session_id, n1) -n1_id = response.node_id - -# create node two +node1 = session.add_node(1, position=position) position = Position(x=300, y=100) -n2 = Node(type=NodeType.DEFAULT, position=position, model="PC") -response = core.add_node(session_id, n2) -n2_id = response.node_id +node2 = session.add_node(2, position=position) -# links nodes together -iface1 = iface_helper.create_iface(n1_id, 0) -iface2 = iface_helper.create_iface(n2_id, 0) -core.add_link(session_id, n1_id, n2_id, iface1, iface2) +# create link +iface1 = iface_helper.create_iface(node1.id, 0) +iface2 = iface_helper.create_iface(node2.id, 0) +session.add_link(node1=node1, node2=node2, iface1=iface1, iface2=iface2) -# change session state -core.set_session_state(session_id, SessionState.INSTANTIATION) +# start session +core.start_session(session) diff --git a/daemon/examples/grpc/switch.py b/daemon/examples/grpc/switch.py index f79f8544..0a5be8a1 100644 --- a/daemon/examples/grpc/switch.py +++ b/daemon/examples/grpc/switch.py @@ -1,44 +1,29 @@ -# required imports -from core.api.grpc import client -from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState +from core.api.grpc import clientw +from core.api.grpc.wrappers import NodeType, Position # interface helper -iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") +iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") # create grpc client and connect -core = client.CoreGrpcClient() +core = clientw.CoreGrpcClient() core.connect() -# create session and get id -response = core.create_session() -session_id = response.session_id +# add session +session = core.add_session() -# change session state to configuration so that nodes get started when added -core.set_session_state(session_id, SessionState.CONFIGURATION) - -# create switch node +# create nodes position = Position(x=200, y=200) -switch = Node(type=NodeType.SWITCH, position=position) -response = core.add_node(session_id, switch) -switch_id = response.node_id - -# create node one +switch = session.add_node(1, _type=NodeType.SWITCH, position=position) position = Position(x=100, y=100) -n1 = Node(type=NodeType.DEFAULT, position=position, model="PC") -response = core.add_node(session_id, n1) -n1_id = response.node_id - -# create node two +node1 = session.add_node(2, position=position) position = Position(x=300, y=100) -n2 = Node(type=NodeType.DEFAULT, position=position, model="PC") -response = core.add_node(session_id, n2) -n2_id = response.node_id +node2 = session.add_node(3, position=position) -# links nodes to switch -iface1 = iface_helper.create_iface(n1_id, 0) -core.add_link(session_id, n1_id, switch_id, iface1) -iface1 = iface_helper.create_iface(n2_id, 0) -core.add_link(session_id, n2_id, switch_id, iface1) +# create links +iface1 = iface_helper.create_iface(node1.id, 0) +session.add_link(node1=node1, node2=switch, iface1=iface1) +iface1 = iface_helper.create_iface(node2.id, 0) +session.add_link(node1=node2, node2=switch, iface1=iface1) -# change session state -core.set_session_state(session_id, SessionState.INSTANTIATION) +# start session +core.start_session(session) diff --git a/daemon/examples/grpc/wlan.py b/daemon/examples/grpc/wlan.py index fa8ef9f6..86a3856b 100644 --- a/daemon/examples/grpc/wlan.py +++ b/daemon/examples/grpc/wlan.py @@ -1,58 +1,41 @@ -# required imports -from core.api.grpc import client -from core.api.grpc.core_pb2 import Node, NodeType, Position, SessionState +from core.api.grpc import clientw +from core.api.grpc.wrappers import NodeType, Position # interface helper -iface_helper = client.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") +iface_helper = clientw.InterfaceHelper(ip4_prefix="10.0.0.0/24", ip6_prefix="2001::/64") # create grpc client and connect -core = client.CoreGrpcClient() +core = clientw.CoreGrpcClient() core.connect() -# create session and get id -response = core.create_session() -session_id = response.session_id +# add session +session = core.add_session() -# change session state to configuration so that nodes get started when added -core.set_session_state(session_id, SessionState.CONFIGURATION) - -# create wlan node +# create nodes position = Position(x=200, y=200) -wlan = Node(type=NodeType.WIRELESS_LAN, position=position) -response = core.add_node(session_id, wlan) -wlan_id = response.node_id - -# create node one +wlan = session.add_node(1, _type=NodeType.WIRELESS_LAN, position=position) position = Position(x=100, y=100) -n1 = Node(type=NodeType.DEFAULT, position=position, model="mdr") -response = core.add_node(session_id, n1) -n1_id = response.node_id - -# create node two +node1 = session.add_node(2, model="mdr", position=position) position = Position(x=300, y=100) -n2 = Node(type=NodeType.DEFAULT, position=position, model="mdr") -response = core.add_node(session_id, n2) -n2_id = response.node_id +node2 = session.add_node(3, model="mdr", position=position) -# configure wlan using a dict mapping currently +# create links +iface1 = iface_helper.create_iface(node1.id, 0) +session.add_link(node1=node1, node2=wlan, iface1=iface1) +iface1 = iface_helper.create_iface(node2.id, 0) +session.add_link(node1=node2, node2=wlan, iface1=iface1) + +# set wlan config using a dict mapping currently # support values as strings -core.set_wlan_config( - session_id, - wlan_id, +wlan.set_wlan( { "range": "280", "bandwidth": "55000000", "delay": "6000", "jitter": "5", "error": "5", - }, + } ) -# links nodes to wlan -iface1 = iface_helper.create_iface(n1_id, 0) -core.add_link(session_id, n1_id, wlan_id, iface1) -iface1 = iface_helper.create_iface(n2_id, 0) -core.add_link(session_id, n2_id, wlan_id, iface1) - -# change session state -core.set_session_state(session_id, SessionState.INSTANTIATION) +# start session +core.start_session(session) diff --git a/daemon/tests/test_grpcw.py b/daemon/tests/test_grpcw.py index f8f0764e..f512dba1 100644 --- a/daemon/tests/test_grpcw.py +++ b/daemon/tests/test_grpcw.py @@ -55,18 +55,11 @@ class TestGrpcw: session_id = client.create_session() session = client.get_session(session_id) position = Position(x=50, y=100) - node1 = Node( - id=1, name="n1", position=position, type=NodeType.DEFAULT, model="PC" - ) + node1 = session.add_node(1, position=position) position = Position(x=100, y=100) - node2 = Node( - id=2, name="n2", position=position, type=NodeType.DEFAULT, model="PC" - ) + node2 = session.add_node(2, position=position) position = Position(x=200, y=200) - wlan_node = Node(id=3, name="n3", type=NodeType.WIRELESS_LAN, position=position) - session.set_node(node1) - session.set_node(node2) - session.set_node(wlan_node) + wlan_node = session.add_node(3, _type=NodeType.WIRELESS_LAN, position=position) iface_helper = InterfaceHelper(ip4_prefix="10.83.0.0/16") iface1_id = 0 iface1 = iface_helper.create_iface(node1.id, iface1_id) @@ -96,38 +89,17 @@ class TestGrpcw: # setup global emane config emane_config_key = "platform_id_start" emane_config_value = "2" - option = ConfigOption( - label=emane_config_key, - name=emane_config_key, - value=emane_config_value, - type=ConfigOptionType.INT64, - group="Default", - ) - session.emane_config[emane_config_key] = option + session.set_emane({emane_config_key: emane_config_value}) # setup wlan config wlan_config_key = "range" wlan_config_value = "333" - option = ConfigOption( - label=wlan_config_key, - name=wlan_config_key, - value=wlan_config_value, - type=ConfigOptionType.INT64, - group="Default", - ) - wlan_node.wlan_config[wlan_config_key] = option + wlan_node.set_wlan({wlan_config_key: wlan_config_value}) # setup mobility config mobility_config_key = "refresh_ms" mobility_config_value = "60" - option = ConfigOption( - label=mobility_config_key, - name=mobility_config_key, - value=mobility_config_value, - type=ConfigOptionType.INT64, - group="Default", - ) - wlan_node.mobility_config[mobility_config_key] = option + wlan_node.set_mobility({mobility_config_key: mobility_config_value}) # setup service config service_name = "DefaultRoute" @@ -153,14 +125,7 @@ class TestGrpcw: # setup session option option_key = "controlnet" option_value = "172.16.0.0/24" - option = ConfigOption( - label=option_key, - name=option_key, - value=option_value, - type=ConfigOptionType.STRING, - group="Default", - ) - session.options[option_key] = option + session.set_options({option_key: option_value}) # when with patch.object(CoreXmlWriter, "write"):