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:
Blake Harnden 2021-05-06 15:06:16 -07:00
parent 598cb0f10d
commit 53ae6ac784
12 changed files with 136 additions and 87 deletions

View file

@ -343,11 +343,7 @@ 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:
def create_session(self, session_id: int = None) -> wrappers.Session:
"""
Create a session.
@ -357,7 +353,7 @@ class CoreGrpcClient:
"""
request = core_pb2.CreateSessionRequest(session_id=session_id)
response = self.stub.CreateSession(request)
return response.session_id
return wrappers.Session.from_proto(response.session)
def delete_session(self, session_id: int) -> bool:
"""

View file

@ -7,7 +7,7 @@ import grpc
from grpc import ServicerContext
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.configservices_pb2 import ConfigServiceConfig
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.interface import CoreInterface
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
logger = logging.getLogger(__name__)
@ -657,3 +657,57 @@ def get_mobility_node(
return session.get_node(node_id, EmaneNet)
except CoreError:
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,
)

View file

@ -91,7 +91,7 @@ from core.emulator.session import NT, Session
from core.errors import CoreCommandError, CoreError
from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility
from core.nodes.base import CoreNode, NodeBase
from core.nodes.network import CtrlNet, PtpNet, WlanNode
from core.nodes.network import WlanNode
from core.services.coreservices import ServiceManager
logger = logging.getLogger(__name__)
@ -359,9 +359,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
session.set_state(EventTypes.DEFINITION_STATE)
session.location.setrefgeo(47.57917, -122.13232, 2.0)
session.location.refscale = 150.0
return core_pb2.CreateSessionResponse(
session_id=session.id, state=session.state.value
)
session_proto = grpcutils.convert_session(session)
return core_pb2.CreateSessionResponse(session=session_proto)
def DeleteSession(
self, request: core_pb2.DeleteSessionRequest, context: ServicerContext
@ -427,57 +426,58 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
"""
logger.debug("get session: %s", request)
session = self.get_session(request.session_id, context)
links = []
nodes = []
for _id in session.nodes:
node = session.nodes[_id]
if not isinstance(node, (PtpNet, CtrlNet)):
node_proto = grpcutils.get_node_proto(session, node)
nodes.append(node_proto)
node_links = get_links(node)
links.extend(node_links)
default_services = grpcutils.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 = grpcutils.get_hooks(session)
emane_models = grpcutils.get_emane_models(session)
emane_config = grpcutils.get_emane_config(session)
emane_model_configs = grpcutils.get_emane_model_configs(session)
wlan_configs = grpcutils.get_wlan_configs(session)
mobility_configs = grpcutils.get_mobility_configs(session)
service_configs = grpcutils.get_node_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
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()
]
session_proto = 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,
)
# links = []
# nodes = []
# for _id in session.nodes:
# node = session.nodes[_id]
# if not isinstance(node, (PtpNet, CtrlNet)):
# node_proto = grpcutils.get_node_proto(session, node)
# nodes.append(node_proto)
# node_links = get_links(node)
# links.extend(node_links)
# default_services = grpcutils.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 = grpcutils.get_hooks(session)
# emane_models = grpcutils.get_emane_models(session)
# emane_config = grpcutils.get_emane_config(session)
# emane_model_configs = grpcutils.get_emane_model_configs(session)
# wlan_configs = grpcutils.get_wlan_configs(session)
# mobility_configs = grpcutils.get_mobility_configs(session)
# service_configs = grpcutils.get_node_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
# 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()
# ]
# session_proto = 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,
# )
session_proto = grpcutils.convert_session(session)
return core_pb2.GetSessionResponse(session=session_proto)
def SessionAlert(

View file

@ -357,9 +357,9 @@ class CoreClient:
Create a new session
"""
try:
session_id = self.client.create_session()
logger.info("created session: %s", session_id)
self.join_session(session_id)
session = self.client.create_session()
logger.info("created session: %s", session.id)
self.join_session(session.id)
location_config = self.app.guiconfig.location
self.session.location = SessionLocation(
x=location_config.x,

View file

@ -18,7 +18,7 @@ def main(args):
core.connect()
# create session
session = core.add_session()
session = core.create_session()
# add distributed server
server = Server(name="core2", host=args.server)

View file

@ -11,7 +11,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=200, y=200)

View file

@ -9,7 +9,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=100, y=100)

View file

@ -9,7 +9,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=200, y=200)

View file

@ -9,7 +9,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=200, y=200)

View file

@ -179,8 +179,7 @@ message CreateSessionRequest {
}
message CreateSessionResponse {
int32 session_id = 1;
SessionState.Enum state = 2;
Session session = 1;
}
message DeleteSessionRequest {

View file

@ -8,7 +8,7 @@ import grpc
import pytest
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.server import CoreGrpcServer
from core.api.grpc.wrappers import (
@ -50,8 +50,7 @@ class TestGrpc:
# given
client = CoreGrpcClient()
with client.context_connect():
session_id = client.create_session()
session = client.get_session(session_id)
session = client.create_session()
position = Position(x=50, y=100)
node1 = session.add_node(1, position=position)
position = Position(x=100, y=100)
@ -181,14 +180,14 @@ class TestGrpc:
# when
with client.context_connect():
created_session_id = client.create_session(session_id)
created_session = client.create_session(session_id)
# then
assert isinstance(created_session_id, int)
session = grpc_server.coreemu.sessions.get(created_session_id)
assert isinstance(created_session, wrappers.Session)
session = grpc_server.coreemu.sessions.get(created_session.id)
assert session 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
@pytest.mark.parametrize("session_id, expected", [(None, True), (6013, False)])
@ -335,6 +334,7 @@ class TestGrpc:
node = session.add_node(CoreNode, options=options)
session.instantiate()
expected_output = "hello world"
expected_status = 0
# then
command = f"echo {expected_output}"
@ -342,7 +342,7 @@ class TestGrpc:
output = client.node_command(session.id, node.id, command)
# then
assert expected_output == output
assert (expected_status, expected_output) == output
def test_get_node_terminal(self, grpc_server: CoreGrpcServer):
# given

View file

@ -109,7 +109,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# provide no events to listen to all events
core.events(session.id, event_listener)
@ -141,7 +141,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=100, y=100)
@ -180,7 +180,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=100, y=100)
@ -211,7 +211,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=200, y=200)
@ -245,7 +245,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=200, y=200)
@ -310,7 +310,7 @@ core = client.CoreGrpcClient()
core.connect()
# add session
session = core.add_session()
session = core.create_session()
# create nodes
position = Position(x=200, y=200)