grpc: updated create session to return a session object with default values, avoids scripts needing to create and then join, can just create and leverage the returned object
This commit is contained in:
parent
598cb0f10d
commit
53ae6ac784
12 changed files with 136 additions and 87 deletions
|
@ -343,11 +343,7 @@ class CoreGrpcClient:
|
||||||
response = self.stub.StopSession(request)
|
response = self.stub.StopSession(request)
|
||||||
return response.result
|
return response.result
|
||||||
|
|
||||||
def add_session(self, session_id: int = None) -> wrappers.Session:
|
def create_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.
|
Create a session.
|
||||||
|
|
||||||
|
@ -357,7 +353,7 @@ class CoreGrpcClient:
|
||||||
"""
|
"""
|
||||||
request = core_pb2.CreateSessionRequest(session_id=session_id)
|
request = core_pb2.CreateSessionRequest(session_id=session_id)
|
||||||
response = self.stub.CreateSession(request)
|
response = self.stub.CreateSession(request)
|
||||||
return response.session_id
|
return wrappers.Session.from_proto(response.session)
|
||||||
|
|
||||||
def delete_session(self, session_id: int) -> bool:
|
def delete_session(self, session_id: int) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -7,7 +7,7 @@ import grpc
|
||||||
from grpc import ServicerContext
|
from grpc import ServicerContext
|
||||||
|
|
||||||
from core import utils
|
from core import utils
|
||||||
from core.api.grpc import common_pb2, core_pb2
|
from core.api.grpc import common_pb2, core_pb2, wrappers
|
||||||
from core.api.grpc.common_pb2 import MappedConfig
|
from core.api.grpc.common_pb2 import MappedConfig
|
||||||
from core.api.grpc.configservices_pb2 import ConfigServiceConfig
|
from core.api.grpc.configservices_pb2 import ConfigServiceConfig
|
||||||
from core.api.grpc.emane_pb2 import GetEmaneModelConfig
|
from core.api.grpc.emane_pb2 import GetEmaneModelConfig
|
||||||
|
@ -28,7 +28,7 @@ from core.nodes.base import CoreNode, CoreNodeBase, NodeBase
|
||||||
from core.nodes.docker import DockerNode
|
from core.nodes.docker import DockerNode
|
||||||
from core.nodes.interface import CoreInterface
|
from core.nodes.interface import CoreInterface
|
||||||
from core.nodes.lxd import LxcNode
|
from core.nodes.lxd import LxcNode
|
||||||
from core.nodes.network import WlanNode
|
from core.nodes.network import CtrlNet, PtpNet, WlanNode
|
||||||
from core.services.coreservices import CoreService
|
from core.services.coreservices import CoreService
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -657,3 +657,57 @@ def get_mobility_node(
|
||||||
return session.get_node(node_id, EmaneNet)
|
return session.get_node(node_id, EmaneNet)
|
||||||
except CoreError:
|
except CoreError:
|
||||||
context.abort(grpc.StatusCode.NOT_FOUND, "node id is not for wlan or emane")
|
context.abort(grpc.StatusCode.NOT_FOUND, "node id is not for wlan or emane")
|
||||||
|
|
||||||
|
|
||||||
|
def convert_session(session: Session) -> wrappers.Session:
|
||||||
|
links = []
|
||||||
|
nodes = []
|
||||||
|
for _id in session.nodes:
|
||||||
|
node = session.nodes[_id]
|
||||||
|
if not isinstance(node, (PtpNet, CtrlNet)):
|
||||||
|
node_proto = get_node_proto(session, node)
|
||||||
|
nodes.append(node_proto)
|
||||||
|
node_links = get_links(node)
|
||||||
|
links.extend(node_links)
|
||||||
|
default_services = get_default_services(session)
|
||||||
|
x, y, z = session.location.refxyz
|
||||||
|
lat, lon, alt = session.location.refgeo
|
||||||
|
location = core_pb2.SessionLocation(
|
||||||
|
x=x, y=y, z=z, lat=lat, lon=lon, alt=alt, scale=session.location.refscale
|
||||||
|
)
|
||||||
|
hooks = get_hooks(session)
|
||||||
|
emane_models = get_emane_models(session)
|
||||||
|
emane_config = get_emane_config(session)
|
||||||
|
emane_model_configs = get_emane_model_configs(session)
|
||||||
|
wlan_configs = get_wlan_configs(session)
|
||||||
|
mobility_configs = get_mobility_configs(session)
|
||||||
|
service_configs = get_node_service_configs(session)
|
||||||
|
config_service_configs = get_node_config_service_configs(session)
|
||||||
|
session_file = str(session.file_path) if session.file_path else None
|
||||||
|
options = get_config_options(session.options.get_configs(), session.options)
|
||||||
|
servers = [
|
||||||
|
core_pb2.Server(name=x.name, host=x.host)
|
||||||
|
for x in session.distributed.servers.values()
|
||||||
|
]
|
||||||
|
return core_pb2.Session(
|
||||||
|
id=session.id,
|
||||||
|
state=session.state.value,
|
||||||
|
nodes=nodes,
|
||||||
|
links=links,
|
||||||
|
dir=str(session.directory),
|
||||||
|
user=session.user,
|
||||||
|
default_services=default_services,
|
||||||
|
location=location,
|
||||||
|
hooks=hooks,
|
||||||
|
emane_models=emane_models,
|
||||||
|
emane_config=emane_config,
|
||||||
|
emane_model_configs=emane_model_configs,
|
||||||
|
wlan_configs=wlan_configs,
|
||||||
|
service_configs=service_configs,
|
||||||
|
config_service_configs=config_service_configs,
|
||||||
|
mobility_configs=mobility_configs,
|
||||||
|
metadata=session.metadata,
|
||||||
|
file=session_file,
|
||||||
|
options=options,
|
||||||
|
servers=servers,
|
||||||
|
)
|
||||||
|
|
|
@ -91,7 +91,7 @@ from core.emulator.session import NT, Session
|
||||||
from core.errors import CoreCommandError, CoreError
|
from core.errors import CoreCommandError, CoreError
|
||||||
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
|
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
|
||||||
from core.nodes.base import CoreNode, NodeBase
|
from core.nodes.base import CoreNode, NodeBase
|
||||||
from core.nodes.network import CtrlNet, PtpNet, WlanNode
|
from core.nodes.network import WlanNode
|
||||||
from core.services.coreservices import ServiceManager
|
from core.services.coreservices import ServiceManager
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -359,9 +359,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
||||||
session.set_state(EventTypes.DEFINITION_STATE)
|
session.set_state(EventTypes.DEFINITION_STATE)
|
||||||
session.location.setrefgeo(47.57917, -122.13232, 2.0)
|
session.location.setrefgeo(47.57917, -122.13232, 2.0)
|
||||||
session.location.refscale = 150.0
|
session.location.refscale = 150.0
|
||||||
return core_pb2.CreateSessionResponse(
|
session_proto = grpcutils.convert_session(session)
|
||||||
session_id=session.id, state=session.state.value
|
return core_pb2.CreateSessionResponse(session=session_proto)
|
||||||
)
|
|
||||||
|
|
||||||
def DeleteSession(
|
def DeleteSession(
|
||||||
self, request: core_pb2.DeleteSessionRequest, context: ServicerContext
|
self, request: core_pb2.DeleteSessionRequest, context: ServicerContext
|
||||||
|
@ -427,57 +426,58 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
|
||||||
"""
|
"""
|
||||||
logger.debug("get session: %s", request)
|
logger.debug("get session: %s", request)
|
||||||
session = self.get_session(request.session_id, context)
|
session = self.get_session(request.session_id, context)
|
||||||
links = []
|
# links = []
|
||||||
nodes = []
|
# nodes = []
|
||||||
for _id in session.nodes:
|
# for _id in session.nodes:
|
||||||
node = session.nodes[_id]
|
# node = session.nodes[_id]
|
||||||
if not isinstance(node, (PtpNet, CtrlNet)):
|
# if not isinstance(node, (PtpNet, CtrlNet)):
|
||||||
node_proto = grpcutils.get_node_proto(session, node)
|
# node_proto = grpcutils.get_node_proto(session, node)
|
||||||
nodes.append(node_proto)
|
# nodes.append(node_proto)
|
||||||
node_links = get_links(node)
|
# node_links = get_links(node)
|
||||||
links.extend(node_links)
|
# links.extend(node_links)
|
||||||
default_services = grpcutils.get_default_services(session)
|
# default_services = grpcutils.get_default_services(session)
|
||||||
x, y, z = session.location.refxyz
|
# x, y, z = session.location.refxyz
|
||||||
lat, lon, alt = session.location.refgeo
|
# lat, lon, alt = session.location.refgeo
|
||||||
location = core_pb2.SessionLocation(
|
# location = core_pb2.SessionLocation(
|
||||||
x=x, y=y, z=z, lat=lat, lon=lon, alt=alt, scale=session.location.refscale
|
# x=x, y=y, z=z, lat=lat, lon=lon, alt=alt, scale=session.location.refscale
|
||||||
)
|
# )
|
||||||
hooks = grpcutils.get_hooks(session)
|
# hooks = grpcutils.get_hooks(session)
|
||||||
emane_models = grpcutils.get_emane_models(session)
|
# emane_models = grpcutils.get_emane_models(session)
|
||||||
emane_config = grpcutils.get_emane_config(session)
|
# emane_config = grpcutils.get_emane_config(session)
|
||||||
emane_model_configs = grpcutils.get_emane_model_configs(session)
|
# emane_model_configs = grpcutils.get_emane_model_configs(session)
|
||||||
wlan_configs = grpcutils.get_wlan_configs(session)
|
# wlan_configs = grpcutils.get_wlan_configs(session)
|
||||||
mobility_configs = grpcutils.get_mobility_configs(session)
|
# mobility_configs = grpcutils.get_mobility_configs(session)
|
||||||
service_configs = grpcutils.get_node_service_configs(session)
|
# service_configs = grpcutils.get_node_service_configs(session)
|
||||||
config_service_configs = grpcutils.get_node_config_service_configs(session)
|
# config_service_configs = grpcutils.get_node_config_service_configs(session)
|
||||||
session_file = str(session.file_path) if session.file_path else None
|
# session_file = str(session.file_path) if session.file_path else None
|
||||||
options = get_config_options(session.options.get_configs(), session.options)
|
# options = get_config_options(session.options.get_configs(), session.options)
|
||||||
servers = [
|
# servers = [
|
||||||
core_pb2.Server(name=x.name, host=x.host)
|
# core_pb2.Server(name=x.name, host=x.host)
|
||||||
for x in session.distributed.servers.values()
|
# for x in session.distributed.servers.values()
|
||||||
]
|
# ]
|
||||||
session_proto = core_pb2.Session(
|
# session_proto = core_pb2.Session(
|
||||||
id=session.id,
|
# id=session.id,
|
||||||
state=session.state.value,
|
# state=session.state.value,
|
||||||
nodes=nodes,
|
# nodes=nodes,
|
||||||
links=links,
|
# links=links,
|
||||||
dir=str(session.directory),
|
# dir=str(session.directory),
|
||||||
user=session.user,
|
# user=session.user,
|
||||||
default_services=default_services,
|
# default_services=default_services,
|
||||||
location=location,
|
# location=location,
|
||||||
hooks=hooks,
|
# hooks=hooks,
|
||||||
emane_models=emane_models,
|
# emane_models=emane_models,
|
||||||
emane_config=emane_config,
|
# emane_config=emane_config,
|
||||||
emane_model_configs=emane_model_configs,
|
# emane_model_configs=emane_model_configs,
|
||||||
wlan_configs=wlan_configs,
|
# wlan_configs=wlan_configs,
|
||||||
service_configs=service_configs,
|
# service_configs=service_configs,
|
||||||
config_service_configs=config_service_configs,
|
# config_service_configs=config_service_configs,
|
||||||
mobility_configs=mobility_configs,
|
# mobility_configs=mobility_configs,
|
||||||
metadata=session.metadata,
|
# metadata=session.metadata,
|
||||||
file=session_file,
|
# file=session_file,
|
||||||
options=options,
|
# options=options,
|
||||||
servers=servers,
|
# servers=servers,
|
||||||
)
|
# )
|
||||||
|
session_proto = grpcutils.convert_session(session)
|
||||||
return core_pb2.GetSessionResponse(session=session_proto)
|
return core_pb2.GetSessionResponse(session=session_proto)
|
||||||
|
|
||||||
def SessionAlert(
|
def SessionAlert(
|
||||||
|
|
|
@ -357,9 +357,9 @@ class CoreClient:
|
||||||
Create a new session
|
Create a new session
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
session_id = self.client.create_session()
|
session = self.client.create_session()
|
||||||
logger.info("created session: %s", session_id)
|
logger.info("created session: %s", session.id)
|
||||||
self.join_session(session_id)
|
self.join_session(session.id)
|
||||||
location_config = self.app.guiconfig.location
|
location_config = self.app.guiconfig.location
|
||||||
self.session.location = SessionLocation(
|
self.session.location = SessionLocation(
|
||||||
x=location_config.x,
|
x=location_config.x,
|
||||||
|
|
|
@ -18,7 +18,7 @@ def main(args):
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# create session
|
# create session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# add distributed server
|
# add distributed server
|
||||||
server = Server(name="core2", host=args.server)
|
server = Server(name="core2", host=args.server)
|
||||||
|
|
|
@ -11,7 +11,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=200, y=200)
|
position = Position(x=200, y=200)
|
||||||
|
|
|
@ -9,7 +9,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=100, y=100)
|
position = Position(x=100, y=100)
|
||||||
|
|
|
@ -9,7 +9,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=200, y=200)
|
position = Position(x=200, y=200)
|
||||||
|
|
|
@ -9,7 +9,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=200, y=200)
|
position = Position(x=200, y=200)
|
||||||
|
|
|
@ -179,8 +179,7 @@ message CreateSessionRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
message CreateSessionResponse {
|
message CreateSessionResponse {
|
||||||
int32 session_id = 1;
|
Session session = 1;
|
||||||
SessionState.Enum state = 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message DeleteSessionRequest {
|
message DeleteSessionRequest {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import grpc
|
||||||
import pytest
|
import pytest
|
||||||
from mock import patch
|
from mock import patch
|
||||||
|
|
||||||
from core.api.grpc import core_pb2
|
from core.api.grpc import core_pb2, wrappers
|
||||||
from core.api.grpc.client import CoreGrpcClient, InterfaceHelper, MoveNodesStreamer
|
from core.api.grpc.client import CoreGrpcClient, InterfaceHelper, MoveNodesStreamer
|
||||||
from core.api.grpc.server import CoreGrpcServer
|
from core.api.grpc.server import CoreGrpcServer
|
||||||
from core.api.grpc.wrappers import (
|
from core.api.grpc.wrappers import (
|
||||||
|
@ -50,8 +50,7 @@ class TestGrpc:
|
||||||
# given
|
# given
|
||||||
client = CoreGrpcClient()
|
client = CoreGrpcClient()
|
||||||
with client.context_connect():
|
with client.context_connect():
|
||||||
session_id = client.create_session()
|
session = client.create_session()
|
||||||
session = client.get_session(session_id)
|
|
||||||
position = Position(x=50, y=100)
|
position = Position(x=50, y=100)
|
||||||
node1 = session.add_node(1, position=position)
|
node1 = session.add_node(1, position=position)
|
||||||
position = Position(x=100, y=100)
|
position = Position(x=100, y=100)
|
||||||
|
@ -181,14 +180,14 @@ class TestGrpc:
|
||||||
|
|
||||||
# when
|
# when
|
||||||
with client.context_connect():
|
with client.context_connect():
|
||||||
created_session_id = client.create_session(session_id)
|
created_session = client.create_session(session_id)
|
||||||
|
|
||||||
# then
|
# then
|
||||||
assert isinstance(created_session_id, int)
|
assert isinstance(created_session, wrappers.Session)
|
||||||
session = grpc_server.coreemu.sessions.get(created_session_id)
|
session = grpc_server.coreemu.sessions.get(created_session.id)
|
||||||
assert session is not None
|
assert session is not None
|
||||||
if session_id is not None:
|
if session_id is not None:
|
||||||
assert created_session_id == session_id
|
assert created_session.id == session_id
|
||||||
assert session.id == session_id
|
assert session.id == session_id
|
||||||
|
|
||||||
@pytest.mark.parametrize("session_id, expected", [(None, True), (6013, False)])
|
@pytest.mark.parametrize("session_id, expected", [(None, True), (6013, False)])
|
||||||
|
@ -335,6 +334,7 @@ class TestGrpc:
|
||||||
node = session.add_node(CoreNode, options=options)
|
node = session.add_node(CoreNode, options=options)
|
||||||
session.instantiate()
|
session.instantiate()
|
||||||
expected_output = "hello world"
|
expected_output = "hello world"
|
||||||
|
expected_status = 0
|
||||||
|
|
||||||
# then
|
# then
|
||||||
command = f"echo {expected_output}"
|
command = f"echo {expected_output}"
|
||||||
|
@ -342,7 +342,7 @@ class TestGrpc:
|
||||||
output = client.node_command(session.id, node.id, command)
|
output = client.node_command(session.id, node.id, command)
|
||||||
|
|
||||||
# then
|
# then
|
||||||
assert expected_output == output
|
assert (expected_status, expected_output) == output
|
||||||
|
|
||||||
def test_get_node_terminal(self, grpc_server: CoreGrpcServer):
|
def test_get_node_terminal(self, grpc_server: CoreGrpcServer):
|
||||||
# given
|
# given
|
||||||
|
|
12
docs/grpc.md
12
docs/grpc.md
|
@ -109,7 +109,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# provide no events to listen to all events
|
# provide no events to listen to all events
|
||||||
core.events(session.id, event_listener)
|
core.events(session.id, event_listener)
|
||||||
|
@ -141,7 +141,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=100, y=100)
|
position = Position(x=100, y=100)
|
||||||
|
@ -180,7 +180,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=100, y=100)
|
position = Position(x=100, y=100)
|
||||||
|
@ -211,7 +211,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=200, y=200)
|
position = Position(x=200, y=200)
|
||||||
|
@ -245,7 +245,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=200, y=200)
|
position = Position(x=200, y=200)
|
||||||
|
@ -310,7 +310,7 @@ core = client.CoreGrpcClient()
|
||||||
core.connect()
|
core.connect()
|
||||||
|
|
||||||
# add session
|
# add session
|
||||||
session = core.add_session()
|
session = core.create_session()
|
||||||
|
|
||||||
# create nodes
|
# create nodes
|
||||||
position = Position(x=200, y=200)
|
position = Position(x=200, y=200)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue