From 18c9904d58652491cbb3d30b7fae6ca87f2ad930 Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Mon, 11 Nov 2019 10:19:30 -0800 Subject: [PATCH] modified grpc set node service and node service file to use messages for their config, updated start session to leverage these messages to set them when starting a session --- daemon/core/api/grpc/client.py | 25 ++++++++++------- daemon/core/api/grpc/grpcutils.py | 15 +++++++++++ daemon/core/api/grpc/server.py | 20 +++++++++----- daemon/proto/core/api/grpc/core.proto | 28 ++++++++++++------- daemon/tests/test_grpc.py | 39 ++++++++++++++++++--------- 5 files changed, 90 insertions(+), 37 deletions(-) diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index 01a7b1f6..ceec0448 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -159,6 +159,8 @@ class CoreGrpcClient: emane_model_configs=None, wlan_configs=None, mobility_configs=None, + service_configs=None, + service_file_configs=None, ): """ Start a session. @@ -169,9 +171,11 @@ class CoreGrpcClient: :param core_pb2.SessionLocation location: location to set :param list[core_pb2.Hook] hooks: session hooks to set :param dict emane_config: emane configuration to set - :param list emane_model_configs: emane model configurations to set - :param list wlan_configs: wlan configurations to set - :param list mobility_configs: mobility configurations to set + :param list emane_model_configs: node emane model configurations + :param list wlan_configs: node wlan configurations + :param list mobility_configs: node mobility configurations + :param list service_configs: node service configurations + :param list service_file_configs: node service file configurations :return: start session response :rtype: core_pb2.StartSessionResponse """ @@ -185,6 +189,8 @@ class CoreGrpcClient: emane_model_configs=emane_model_configs, wlan_configs=wlan_configs, mobility_configs=mobility_configs, + service_configs=service_configs, + service_file_configs=service_file_configs, ) return self.stub.StartSession(request) @@ -768,14 +774,14 @@ class CoreGrpcClient: :rtype: core_pb2.SetNodeServiceResponse :raises grpc.RpcError: when session or node doesn't exist """ - request = core_pb2.SetNodeServiceRequest( - session_id=session_id, + config = core_pb2.ServiceConfig( node_id=node_id, service=service, startup=startup, validate=validate, shutdown=shutdown, ) + request = core_pb2.SetNodeServiceRequest(session_id=session_id, config=config) return self.stub.SetNodeService(request) def set_node_service_file(self, session_id, node_id, service, file_name, data): @@ -791,12 +797,11 @@ class CoreGrpcClient: :rtype: core_pb2.SetNodeServiceFileResponse :raises grpc.RpcError: when session or node doesn't exist """ + config = core_pb2.ServiceFileConfig( + node_id=node_id, service=service, file=file_name, data=data + ) request = core_pb2.SetNodeServiceFileRequest( - session_id=session_id, - node_id=node_id, - service=service, - file=file_name, - data=data, + session_id=session_id, config=config ) return self.stub.SetNodeServiceFile(request) diff --git a/daemon/core/api/grpc/grpcutils.py b/daemon/core/api/grpc/grpcutils.py index 166807d0..c6b625fb 100644 --- a/daemon/core/api/grpc/grpcutils.py +++ b/daemon/core/api/grpc/grpcutils.py @@ -319,3 +319,18 @@ def session_location(session, location): session.location.refxyz = (location.x, location.y, location.z) session.location.setrefgeo(location.lat, location.lon, location.alt) session.location.refscale = location.scale + + +def service_configuration(session, config): + """ + Convenience method for setting a node service configuration. + + :param core.emulator.session.Session session: session for service configuration + :param core_pb2.ServiceConfig config: service configuration + :return: + """ + session.services.set_service(config.node_id, config.service) + service = session.services.get_service(config.node_id, config.service) + service.startup = tuple(config.startup) + service.validate = tuple(config.validate) + service.shutdown = tuple(config.shutdown) diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index e1e1f24d..c00cc3af 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -153,6 +153,16 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): config.node_id, Ns2ScriptedMobility.name, config.config ) + # service configs + for config in request.service_configs: + grpcutils.service_configuration(session, config) + + # service file configs + for config in request.service_file_configs: + session.services.set_service_file( + config.node_id, config.service, config.file, config.data + ) + # create links grpcutils.create_links(session, request.links) @@ -1172,11 +1182,8 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): """ logging.debug("set node service: %s", request) session = self.get_session(request.session_id, context) - session.services.set_service(request.node_id, request.service) - service = session.services.get_service(request.node_id, request.service) - service.startup = tuple(request.startup) - service.validate = tuple(request.validate) - service.shutdown = tuple(request.shutdown) + config = request.config + grpcutils.service_configuration(session, config) return core_pb2.SetNodeServiceResponse(result=True) def SetNodeServiceFile(self, request, context): @@ -1191,8 +1198,9 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): """ logging.debug("set node service file: %s", request) session = self.get_session(request.session_id, context) + config = request.config session.services.set_service_file( - request.node_id, request.service, request.file, request.data + config.node_id, config.service, config.file, config.data ) return core_pb2.SetNodeServiceFileResponse(result=True) diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index a53a11bb..ac7cc2ed 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -144,6 +144,8 @@ message StartSessionRequest { repeated WlanConfig wlan_configs = 7; repeated EmaneModelConfig emane_model_configs = 8; repeated MobilityConfig mobility_configs = 9; + repeated ServiceConfig service_configs = 10; + repeated ServiceFileConfig service_file_configs = 11; } message StartSessionResponse { @@ -554,11 +556,7 @@ message GetNodeServiceFileResponse { message SetNodeServiceRequest { int32 session_id = 1; - int32 node_id = 2; - string service = 3; - repeated string startup = 4; - repeated string validate = 5; - repeated string shutdown = 6; + ServiceConfig config = 2; } message SetNodeServiceResponse { @@ -567,10 +565,7 @@ message SetNodeServiceResponse { message SetNodeServiceFileRequest { int32 session_id = 1; - int32 node_id = 2; - string service = 3; - string file = 4; - string data = 5; + ServiceFileConfig config = 2; } message SetNodeServiceFileResponse { @@ -718,6 +713,21 @@ message EmaneModelConfig { map config = 4; } +message ServiceConfig { + int32 node_id = 1; + string service = 2; + repeated string startup = 3; + repeated string validate = 4; + repeated string shutdown = 5; +} + +message ServiceFileConfig { + int32 node_id = 1; + string service = 2; + string file = 3; + string data = 4; +} + message MessageType { enum Enum { NONE = 0; diff --git a/daemon/tests/test_grpc.py b/daemon/tests/test_grpc.py index 62ff3a22..5f934b64 100644 --- a/daemon/tests/test_grpc.py +++ b/daemon/tests/test_grpc.py @@ -27,7 +27,6 @@ class TestGrpc: # given client = CoreGrpcClient() session = grpc_server.coreemu.create_session() - nodes = [] position = core_pb2.Position(x=50, y=100) node_one = core_pb2.Node(id=1, position=position, model="PC") position = core_pb2.Position(x=100, y=100) @@ -36,8 +35,7 @@ class TestGrpc: wlan_node = core_pb2.Node( id=3, type=NodeTypes.WIRELESS_LAN.value, position=position ) - nodes.extend([node_one, node_two, wlan_node]) - links = [] + nodes = [node_one, node_two, wlan_node] interface_helper = InterfaceHelper(ip4_prefix="10.83.0.0/16") interface_one = interface_helper.create_interface(node_one.id, 0) interface_two = interface_helper.create_interface(node_two.id, 0) @@ -48,12 +46,11 @@ class TestGrpc: interface_one=interface_one, interface_two=interface_two, ) - links.append(link) - hooks = [] + links = [link] hook = core_pb2.Hook( state=core_pb2.SessionState.RUNTIME, file="echo.sh", data="echo hello" ) - hooks.append(hook) + hooks = [hook] location_x = 5 location_y = 10 location_z = 15 @@ -73,7 +70,6 @@ class TestGrpc: emane_config_key = "platform_id_start" emane_config_value = "2" emane_config = {emane_config_key: emane_config_value} - model_configs = [] model_node_id = 20 model_config_key = "bandwidth" model_config_value = "500000" @@ -83,21 +79,30 @@ class TestGrpc: model=EmaneIeee80211abgModel.name, config={model_config_key: model_config_value}, ) - model_configs.append(model_config) - wlan_configs = [] + model_configs = [model_config] wlan_config_key = "range" wlan_config_value = "333" wlan_config = core_pb2.WlanConfig( node_id=wlan_node.id, config={wlan_config_key: wlan_config_value} ) - wlan_configs.append(wlan_config) + wlan_configs = [wlan_config] mobility_config_key = "refresh_ms" mobility_config_value = "60" - mobility_configs = [] mobility_config = core_pb2.MobilityConfig( node_id=wlan_node.id, config={mobility_config_key: mobility_config_value} ) - mobility_configs.append(mobility_config) + mobility_configs = [mobility_config] + service_config = core_pb2.ServiceConfig( + node_id=node_one.id, service="DefaultRoute", validate=["echo hello"] + ) + service_configs = [service_config] + service_file_config = core_pb2.ServiceFileConfig( + node_id=node_one.id, + service="DefaultRoute", + file="defaultroute.sh", + data="echo hello", + ) + service_file_configs = [service_file_config] # when with patch.object(CoreXmlWriter, "write"): @@ -112,6 +117,8 @@ class TestGrpc: model_configs, wlan_configs, mobility_configs, + service_configs, + service_file_configs, ) # then @@ -139,6 +146,14 @@ class TestGrpc: model_node_id, EmaneIeee80211abgModel.name ) assert set_model_config[model_config_key] == model_config_value + service = session.services.get_service( + node_one.id, service_config.service, default_service=True + ) + assert service.validate == tuple(service_config.validate) + service_file = session.services.get_service_file( + node_one, service_file_config.service, service_file_config.file + ) + assert service_file.data == service_file_config.data @pytest.mark.parametrize("session_id", [None, 6013]) def test_create_session(self, grpc_server, session_id):