From f271b0289ef37fe5c7de25b4bc6174bab814f96f Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Tue, 27 Apr 2021 21:48:20 -0700 Subject: [PATCH] grpc: removed add hook and get gooks, can be achieved with get session and start session --- daemon/core/api/grpc/client.py | 32 ------------------------ daemon/core/api/grpc/clientw.py | 30 ----------------------- daemon/core/api/grpc/server.py | 32 ------------------------ daemon/proto/core/api/grpc/core.proto | 23 ------------------ daemon/tests/test_grpc.py | 35 --------------------------- daemon/tests/test_grpcw.py | 32 ------------------------ 6 files changed, 184 deletions(-) diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index 8a5a38a2..23c7cc0c 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -646,38 +646,6 @@ class CoreGrpcClient: ) return self.stub.DeleteLink(request) - def get_hooks(self, session_id: int) -> core_pb2.GetHooksResponse: - """ - Get all hook scripts. - - :param session_id: session id - :return: response with a list of hooks - :raises grpc.RpcError: when session doesn't exist - """ - request = core_pb2.GetHooksRequest(session_id=session_id) - return self.stub.GetHooks(request) - - def add_hook( - self, - session_id: int, - state: core_pb2.SessionState, - file_name: str, - file_data: str, - ) -> core_pb2.AddHookResponse: - """ - Add hook scripts. - - :param session_id: session id - :param state: state to trigger hook - :param file_name: name of file for hook script - :param file_data: hook script contents - :return: response with result of success or failure - :raises grpc.RpcError: when session doesn't exist - """ - hook = core_pb2.Hook(state=state, file=file_name, data=file_data) - request = core_pb2.AddHookRequest(session_id=session_id, hook=hook) - return self.stub.AddHook(request) - def get_mobility_configs(self, session_id: int) -> GetMobilityConfigsResponse: """ Get all mobility configurations. diff --git a/daemon/core/api/grpc/clientw.py b/daemon/core/api/grpc/clientw.py index 231a5051..b2396662 100644 --- a/daemon/core/api/grpc/clientw.py +++ b/daemon/core/api/grpc/clientw.py @@ -66,7 +66,6 @@ from core.api.grpc.wlan_pb2 import ( WlanConfig, WlanLinkRequest, ) -from core.api.grpc.wrappers import Hook from core.emulator.data import IpPrefixes from core.errors import CoreError @@ -764,35 +763,6 @@ class CoreGrpcClient: response = self.stub.DeleteLink(request) return response.result - def get_hooks(self, session_id: int) -> List[wrappers.Hook]: - """ - Get all hook scripts. - - :param session_id: session id - :return: list of hooks - :raises grpc.RpcError: when session doesn't exist - """ - request = core_pb2.GetHooksRequest(session_id=session_id) - response = self.stub.GetHooks(request) - hooks = [] - for hook_proto in response.hooks: - hook = wrappers.Hook.from_proto(hook_proto) - hooks.append(hook) - return hooks - - def add_hook(self, session_id: int, hook: Hook) -> bool: - """ - Add hook scripts. - - :param session_id: session id - :param hook: hook to add - :return: True for success, False otherwise - :raises grpc.RpcError: when session doesn't exist - """ - request = core_pb2.AddHookRequest(session_id=session_id, hook=hook.to_proto()) - response = self.stub.AddHook(request) - return response.result - def get_mobility_configs( self, session_id: int ) -> Dict[int, Dict[str, wrappers.ConfigOption]]: diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 0333a6ab..9aeefb8a 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -920,38 +920,6 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): session.broadcast_link(link_data) return core_pb2.DeleteLinkResponse(result=True) - def GetHooks( - self, request: core_pb2.GetHooksRequest, context: ServicerContext - ) -> core_pb2.GetHooksResponse: - """ - Retrieve all hooks from a session - - :param request: get-hook request - :param context: context object - :return: get-hooks response about all the hooks in all session states - """ - logger.debug("get hooks: %s", request) - session = self.get_session(request.session_id, context) - hooks = grpcutils.get_hooks(session) - return core_pb2.GetHooksResponse(hooks=hooks) - - def AddHook( - self, request: core_pb2.AddHookRequest, context: ServicerContext - ) -> core_pb2.AddHookResponse: - """ - Add hook to a session - - :param request: add-hook request - :param context: context object - :return: add-hook response - """ - logger.debug("add hook: %s", request) - session = self.get_session(request.session_id, context) - hook = request.hook - state = EventTypes(hook.state) - session.add_hook(state, hook.file, hook.data) - return core_pb2.AddHookResponse(result=True) - def GetMobilityConfigs( self, request: GetMobilityConfigsRequest, context: ServicerContext ) -> GetMobilityConfigsResponse: diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index c219b39a..640b3a4b 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -66,12 +66,6 @@ service CoreApi { rpc DeleteLink (DeleteLinkRequest) returns (DeleteLinkResponse) { } - // hook rpc - rpc GetHooks (GetHooksRequest) returns (GetHooksResponse) { - } - rpc AddHook (AddHookRequest) returns (AddHookResponse) { - } - // mobility rpc rpc GetMobilityConfigs (mobility.GetMobilityConfigsRequest) returns (mobility.GetMobilityConfigsResponse) { } @@ -492,23 +486,6 @@ message DeleteLinkResponse { bool result = 1; } -message GetHooksRequest { - int32 session_id = 1; -} - -message GetHooksResponse { - repeated Hook hooks = 1; -} - -message AddHookRequest { - int32 session_id = 1; - Hook hook = 2; -} - -message AddHookResponse { - bool result = 1; -} - message SaveXmlRequest { int32 session_id = 1; } diff --git a/daemon/tests/test_grpc.py b/daemon/tests/test_grpc.py index 3c931697..59012956 100644 --- a/daemon/tests/test_grpc.py +++ b/daemon/tests/test_grpc.py @@ -351,41 +351,6 @@ class TestGrpc: # then assert response.terminal is not None - def test_get_hooks(self, grpc_server: CoreGrpcServer): - # given - client = CoreGrpcClient() - session = grpc_server.coreemu.create_session() - file_name = "test" - file_data = "echo hello" - session.add_hook(EventTypes.RUNTIME_STATE, file_name, file_data) - - # then - with client.context_connect(): - response = client.get_hooks(session.id) - - # then - assert len(response.hooks) == 1 - hook = response.hooks[0] - assert hook.state == core_pb2.SessionState.RUNTIME - assert hook.file == file_name - assert hook.data == file_data - - def test_add_hook(self, grpc_server: CoreGrpcServer): - # given - client = CoreGrpcClient() - session = grpc_server.coreemu.create_session() - - # then - file_name = "test" - file_data = "echo hello" - with client.context_connect(): - response = client.add_hook( - session.id, core_pb2.SessionState.RUNTIME, file_name, file_data - ) - - # then - assert response.result is True - def test_save_xml(self, grpc_server: CoreGrpcServer, tmpdir: TemporaryFile): # given client = CoreGrpcClient() diff --git a/daemon/tests/test_grpcw.py b/daemon/tests/test_grpcw.py index f8776ae9..d85e6198 100644 --- a/daemon/tests/test_grpcw.py +++ b/daemon/tests/test_grpcw.py @@ -408,38 +408,6 @@ class TestGrpcw: # then assert terminal is not None - def test_get_hooks(self, grpc_server: CoreGrpcServer): - # given - client = CoreGrpcClient() - session = grpc_server.coreemu.create_session() - file_name = "test" - file_data = "echo hello" - session.add_hook(EventTypes.RUNTIME_STATE, file_name, file_data) - - # then - with client.context_connect(): - hooks = client.get_hooks(session.id) - - # then - assert len(hooks) == 1 - hook = hooks[0] - assert hook.state == SessionState.RUNTIME - assert hook.file == file_name - assert hook.data == file_data - - def test_add_hook(self, grpc_server: CoreGrpcServer): - # given - client = CoreGrpcClient() - session = grpc_server.coreemu.create_session() - hook = Hook(SessionState.RUNTIME, "test", "echo hello") - - # then - with client.context_connect(): - result = client.add_hook(session.id, hook) - - # then - assert result is True - def test_save_xml(self, grpc_server: CoreGrpcServer, tmpdir: TemporaryFile): # given client = CoreGrpcClient()