def test_searchBounds(self): th = None sock = None server = None target = ('127.0.0.1', 43991) msgs = [] expectmsg = pb.RtpgenIPCmsgV1() expectmsg.response_code = pb.RtpgenIPCmsgV1.ERROR_NOT_FOUND expectmsg.id_selector = 84 msgs.append(expectmsg) expectmsg = pb.RtpgenIPCmsgV1() expectmsg.response_code = pb.RtpgenIPCmsgV1.ERROR_NOT_FOUND expectmsg.portid = 31 msgs.append(expectmsg) for i in range(32): expectmsg = pb.RtpgenIPCmsgV1() if i in [0, 4, 9, 31]: expectmsg.response_code = pb.RtpgenIPCmsgV1.ERROR_NOT_FOUND else: expectmsg.response_code = pb.RtpgenIPCmsgV1.ERROR_FORBIDDEN expectmsg.portid = i msgs.append(expectmsg) try: server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1, ) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1, ) server.bind(target) th = threading.Thread(target=Test_SouthboundApiManager.stub_server, args=( server, expectmsg, len(msgs), msgs, )) th.start() self.ipc.createConnection(target) ports, sessionsize = self.ipc.searchBounds() self.assertEqual(ports, [0, 4, 9, 31]) self.assertEqual(sessionsize, 85) finally: if th: th.join() if sock: sock.close() if server: server.close()
def test_sendmsg(self): th = None sock = None server = None target = ('127.0.0.1', 43991) expectmsg = pb.RtpgenIPCmsgV1() expectmsg.response_code = pb.RtpgenIPCmsgV1.SUCCESS expectmsg.portid = 1 expectmsg.id_selector = 2 expectmsg.size = 3 expectmsg.rtp_config.ip_dst_addr = 4 expectmsg.rtp_config.ip_src_addr = 5 expectmsg.rtp_config.udp_dst_port = 6 expectmsg.rtp_config.udp_src_port = 7 expectmsg.rtp_config.rtp_timestamp = 8 expectmsg.rtp_config.rtp_sequence = 9 expectmsg.rtp_config.rtp_ssrc = 10 try: server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1, ) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1, ) server.bind(target) th = threading.Thread(target=Test_SouthboundApiManager.stub_server, args=( server, expectmsg, )) th.start() self.ipc.createConnection(target) res = self.ipc.sendmsg(self.ipc.getmsg(0, 1)) self.assertEqual(res.response_code, pb.RtpgenIPCmsgV1.SUCCESS) self.assertEqual(res.portid, 1) self.assertEqual(res.id_selector, 2) self.assertEqual(res.size, 3) self.assertEqual(res.rtp_config.ip_dst_addr, 4) self.assertEqual(res.rtp_config.ip_src_addr, 5) self.assertEqual(res.rtp_config.udp_dst_port, 6) self.assertEqual(res.rtp_config.udp_src_port, 7) self.assertEqual(res.rtp_config.rtp_timestamp, 8) self.assertEqual(res.rtp_config.rtp_sequence, 9) self.assertEqual(res.rtp_config.rtp_ssrc, 10) finally: if th: th.join() if sock: sock.close() if server: server.close()
def test_sendmsgReconnectFail(self): th = None sock = None server = None target = ('127.0.0.1', 43991) expectmsg = pb.RtpgenIPCmsgV1() expectmsg.response_code = pb.RtpgenIPCmsgV1.SUCCESS expectmsg.portid = 1 expectmsg.id_selector = 2 expectmsg.size = 3 expectmsg.rtp_config.ip_dst_addr = 4 expectmsg.rtp_config.ip_src_addr = 5 expectmsg.rtp_config.udp_dst_port = 6 expectmsg.rtp_config.udp_src_port = 7 expectmsg.rtp_config.rtp_timestamp = 8 expectmsg.rtp_config.rtp_sequence = 9 expectmsg.rtp_config.rtp_ssrc = 10 try: server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1, ) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1, ) server.bind(target) th = threading.Thread(target=Test_SouthboundApiManager.stub_server, args=( server, expectmsg, )) th.start() self.ipc.createConnection(target) server.shutdown(socket.SHUT_RDWR) server.close() th.join() self.assertRaises(ConnectionRefusedError, lambda: self.ipc.sendmsg(self.ipc.getmsg(0, 1))) finally: if sock: sock.close() if server: try: server.shutdown(socket.SHUT_RDWR) except OSError: pass server.close() if th: th.join()
def msgReadSuccess(self): msg=pb.RtpgenIPCmsgV1() msg.response_code=pb.RtpgenIPCmsgV1.SUCCESS msg.portid=1 msg.id_selector=2 msg.rtp_config.ip_dst_addr=4 msg.rtp_config.ip_src_addr=5 msg.rtp_config.udp_dst_port=6 msg.rtp_config.udp_src_port=7 msg.rtp_config.rtp_timestamp=8 msg.rtp_config.rtp_sequence=9 msg.rtp_config.rtp_ssrc=10 return msg
def writemsg(self, request_code, portid, selector, src=None, dst=None, timestamp=None, sequence=None, ssrc=None): """Generate a protobuf message for create or update in CRUD operation You can generate a protobuf message object for create or update operation. This choise is made possible by set Enum value in 'request_code'. 'portid' and 'selector' are mandatory values for identifying resources. Which 'src' to 'ssrc' are optional value, you set if needed update that value. Args: request_code(Enum): The type of request code(Defined by '.proto' file). portid(int): Port id for identifying resources. selector(int): Session id for identifying resources. src(tuple(str,int)): OPTIONAL. Source ip and port as updating data. dst(tuple(str,int)): OPTIONAL. Destination ip and port as updating data. timestamp(int): OPTIONAL. RTP timestamp as updating data. sequence(int): OPTIONAL. RTP sequence number as updating data. ssrc(int): OPTIONAL. RTP SSRC as updating data. Returns: Object: Generated request message object as protobuf. """ pack = pb.RtpgenIPCmsgV1() pack.request_code = request_code pack.id_selector = selector pack.portid = portid if (src != None): ip, port = src pack.rtp_config.ip_src_addr = SouthboundApiManager.ip_s2i(ip) pack.rtp_config.udp_src_port = port if (dst != None): ip, port = dst pack.rtp_config.ip_dst_addr = SouthboundApiManager.ip_s2i(ip) pack.rtp_config.udp_dst_port = port if (timestamp != None): pack.rtp_config.rtp_timestamp = timestamp if (sequence != None): pack.rtp_config.rtp_sequence = sequence if (ssrc != None): pack.rtp_config.rtp_ssrc = ssrc return pack
def sendmsg(self, msg): """Send a protobuf message thorough the southbound API You can send a protobuf message thorough the southbound API. The message is serialized by protobuf method and transported by socket liblary. 'None' will be returned if any exception raised or malformed packet returned. Args: msg(Object): A sending request protobuf message object. Returns: Object: A received response protobuf message object. """ res = None try: self.sock.send(msg.SerializeToString()) except (BrokenPipeError, ConnectionResetError) as e: refuseerror = None for retryCnt in range(3, 0, -1): try: self.connectionClose() self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.createConnection(self.target) raise ConnectionResetError except ConnectionRefusedError as e2: refuseerror = e2 continue except ConnectionResetError as e2: raise e2 else: raise refuseerror try: res = pb.RtpgenIPCmsgV1() res.ParseFromString(self.sock.recv(SouthboundApiManager.BUF_SIZE)) if not res.HasField("response_code"): res = None except: res = None return res
def delmsg(self, portid, selector): """Generate a protobuf message for delete in CRUD operation You can generate a protobuf message object for delete operation. 'portid' and 'selector' are mandatory values for identifying resources. Args: request_code(Enum): The type of request code(Defined by '.proto' file). portid(int): Port id for identifying resources. Returns: Object: Generated request message object as protobuf. """ pack = pb.RtpgenIPCmsgV1() pack.request_code = pb.RtpgenIPCmsgV1.DELETE pack.id_selector = selector pack.portid = portid return pack
def test_rscSync(self): th = None sock = None server = None target = ('127.0.0.1', 43991) msgs = [] portlist = [0, 4, 9, 31] activeSessions = [4, 8] for j in portlist: for i in range(10): expectmsg = pb.RtpgenIPCmsgV1() if i in activeSessions: expectmsg.response_code = pb.RtpgenIPCmsgV1.SUCCESS expectmsg.portid = j expectmsg.id_selector = i expectmsg.rtp_config.ip_dst_addr = SouthboundApiManager.ip_s2i( "192.168.0.1") expectmsg.rtp_config.ip_src_addr = SouthboundApiManager.ip_s2i( "172.16.0.155") expectmsg.rtp_config.udp_dst_port = 5000 + i expectmsg.rtp_config.udp_src_port = 8000 + i expectmsg.rtp_config.rtp_timestamp = 12345 expectmsg.rtp_config.rtp_sequence = 5678 expectmsg.rtp_config.rtp_ssrc = 12345 else: expectmsg.response_code = pb.RtpgenIPCmsgV1.ERROR_NOT_FOUND msgs.append(expectmsg) try: server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1, ) server.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1, ) server.bind(target) th = threading.Thread(target=Test_SouthboundApiManager.stub_server, args=( server, expectmsg, len(msgs), msgs, )) th.start() self.ipc.createConnection(target) rsclist = self.ipc.rscSync(portlist, 10) for portid in portlist: for sessionid in activeSessions: data = rsclist.pop(0) self.assertEqual(data["portid"], portid) self.assertEqual(data["sessionid"], sessionid) self.assertEqual(data["enabled"], True) self.assertEqual(data['src']['ip'], "172.16.0.155") self.assertEqual(data['dst']['ip'], "192.168.0.1") self.assertEqual(data['src']['port'], 8000 + sessionid) self.assertEqual(data['dst']['port'], 5000 + sessionid) self.assertEqual(data['start_timestamp'], 12345) finally: if th: th.join() if sock: sock.close() if server: server.close()
def msgWriteSuccess(self): msg=pb.RtpgenIPCmsgV1() msg.response_code=pb.RtpgenIPCmsgV1.SUCCESS return msg
def helperCreateSuccessMsg(self, portid, sessionid): expectmsg=pb.RtpgenIPCmsgV1() expectmsg.response_code=pb.RtpgenIPCmsgV1.SUCCESS expectmsg.portid=portid expectmsg.id_selector=sessionid return expectmsg