class NiCServer(nic_simulator_grpc_service_pb2_grpc.DualTorServiceServicer): """gRPC for a NiC.""" def __init__(self, nic_addr, ovs_bridge): self.nic_addr = nic_addr self.ovs_bridge = ovs_bridge self.server = None self.thread = None @validate_request_certificate(nic_simulator_grpc_service_pb2.AdminReply()) def QueryAdminPortState(self, request, context): logging.debug("QueryAdminPortState: request to server %s from client %s\n", self.nic_addr, context.peer()) response = nic_simulator_grpc_service_pb2.AdminReply( portid=[0, 1], state=self.ovs_bridge.query_forwarding_state() ) logging.debug("QueryAdminPortState: response to client %s from server %s:\n%s", context.peer(), self.nic_addr, response) return response @validate_request_certificate(nic_simulator_grpc_service_pb2.AdminReply()) def SetAdminPortState(self, request, context): logging.debug("SetAdminPortState: request to server %s from client %s\n", self.nic_addr, context.peer()) response = nic_simulator_grpc_service_pb2.AdminReply( portid=[0, 1], state=self.ovs_bridge.set_forwarding_state(request.state) ) logging.debug("SetAdminPortState: response to client %s from server %s:\n%s", context.peer(), self.nic_addr, response) return response @validate_request_certificate(nic_simulator_grpc_service_pb2.OperationReply()) def QueryOperationPortState(self, request, context): # TODO: Add QueryOperationPortState implementation return nic_simulator_grpc_service_pb2.OperationReply() def _run_server(self, binding_port): """Run the gRPC server.""" self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=THREAD_CONCURRENCY_PER_SERVER)) nic_simulator_grpc_service_pb2_grpc.add_DualTorServiceServicer_to_server( self, self.server ) self.server.add_insecure_port("%s:%s" % (self.nic_addr, binding_port)) self.server.start() self.server.wait_for_termination() def start(self, binding_port): """Start the gRPC server thread.""" self.thread = InterruptableThread(target=self._run_server, args=(binding_port,)) self.thread.start() def stop(self): """Stop the gRPC server thread.""" self.server._state.termination_event.set() def join(self, timeout=None, suppress_exception=False): """Wait the gRPC server thread termination.""" self.thread.join(timeout=timeout, suppress_exception=suppress_exception)
def SetAdminPortState(self, request, context): logging.debug("SetAdminPortState: request to server %s from client %s\n", self.nic_addr, context.peer()) response = nic_simulator_grpc_service_pb2.AdminReply( portid=[0, 1], state=self.ovs_bridge.set_forwarding_state(request.state) ) logging.debug("SetAdminPortState: response to client %s from server %s:\n%s", context.peer(), self.nic_addr, response) return response
def QueryAdminForwardingPortState(self, request, context): logging.debug("QueryAdminForwardingPortState: request to server %s from client %s\n", self.nic_addr, context.peer()) portids = request.portid response = nic_simulator_grpc_service_pb2.AdminReply( portid=portids, state=self.ovs_bridge.query_forwarding_state(portids) ) logging.debug("QueryAdminForwardingPortState: response to client %s from server %s:\n%s", context.peer(), self.nic_addr, response) return response