def test_register(self): # process_agent = ProcessAgent("ipc:///tmp/p1sock", "ipc:///tmp/p3sock") context = zmq.Context() sock = context.socket(zmq.REQ) sock.connect(ProcessAgent.SockPathMapping[ ProcessAgent.AGENTTYPE_INTERFACE_STATUS]['api']) # Fail case event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "abcd" reg.action = pb2.msg_manager_register.REG event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "abcd" reg.action = pb2.msg_manager_register.REG reg.path_info = ProcessAgent.SockPathMapping[ ProcessAgent.AGENTTYPE_INTERFACE_STATUS]['push'] event_request.reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp # unregister event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "abcd" reg.action = pb2.msg_manager_register.UNREG reg.path_info = ProcessAgent.SockPathMapping[ ProcessAgent.AGENTTYPE_INTERFACE_STATUS]['push'] event_request.reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp
def test_interface_status_event(self): context = zmq.Context() sock1 = context.socket(zmq.PUSH) sock1.connect(ProcessAgent.SockPathMapping[ ProcessAgent.AGENTTYPE_INTERFACE_STATUS]['pull']) sock = context.socket(zmq.REQ) sock.connect(ProcessAgent.SockPathMapping[ ProcessAgent.AGENTTYPE_INTERFACE_STATUS]['api']) sock2 = context.socket(zmq.PULL) sock2.bind("ipc:///tmp/sock4.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "abcd" reg.action = pb2.msg_manager_register.REG reg.path_info = "ipc:///tmp/sock4.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.event_id = 1 event_request.action.parameter = "eth0" event_request.action.action = pb2.msg_event.START sock1.send(event_request.SerializeToString()) data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # test stop event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.event_id = 1 event_request.action.parameter = "eth0" event_request.action.action = pb2.msg_event.STOP # sock1.send(event_request.SerializeToString()) while True: data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp
def test_ike_event(self): context = zmq.Context() sock1 = context.socket(zmq.PUSH) sock1.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_8021X]['pull']) sock = context.socket(zmq.REQ) sock.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_8021X]['api']) sock2 = context.socket(zmq.PULL) sock2.bind("ipc:///tmp/sock4.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "abcd" reg.action = pb2.msg_manager_register.REG # reg.path_info = "ipc:///tmp/sock4.sock" reg.path_info = ProcessAgent.SockPathMapping[ ProcessAgent.AGENTTYPE_8021X]['api'] event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp # core register register_request = pb2.api_request() reg = pb2.msg_core_register() reg.ccap_core_id = 'abcd' reg.mgr_id = 'abcd' reg.action = pb2.msg_core_register.REG register_request.core_reg.CopyFrom(reg) data = register_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.ccap_core_id = "abcd" event_request.action.event_id = ppb2.AGENTTYPE_8021X # msg = t_TodMessage() # msg.TimeServers.extend(['127.0.0.1', ]) # msg.TimeOffset = 0 # parameter = msg.SerializeToString() parameter = "enp0s3" event_request.action.parameter = parameter event_request.action.action = pb2.msg_event.START sock1.send(event_request.SerializeToString()) data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # test stop event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.event_id = ppb2.AGENTTYPE_8021X event_request.action.parameter = parameter event_request.action.action = pb2.msg_event.STOP # sock1.send(event_request.SerializeToString()) while True: data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp
def test_dhcp_start_checkStatus_stop(self): print 'test dhcp start and stop action' context = zmq.Context() sock_push = context.socket(zmq.PUSH) sock_push.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_DHCP]['pull']) sock_api = context.socket(zmq.REQ) sock_api.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_DHCP]['api']) sock_pull = context.socket(zmq.PULL) sock_pull.bind("ipc:///tmp/test_dhcp_agent.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.REG reg.path_info = "ipc:///tmp/test_dhcp_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # test the successfully register event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.REG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.ccap_core_id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_DHCP event_request.action.parameter = "lo" event_request.action.action = pb2.msg_event.START sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # start a second core request in the same interface event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core_2" reg.action = pb2.msg_core_register.REG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.ccap_core_id = "test_ccap_core_2" event_request.action.event_id = ProcessAgent.AGENTTYPE_DHCP event_request.action.parameter = "lo" event_request.action.action = pb2.msg_event.START sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # test dhcp client send message to the agent print 'DHCP client send the dhcp message to agent' dhcp_msg = t_DhcpMessage() dhcp_msg.InterfaceName = 'lo' dhcp_msg.Status = dhcp_msg.UPDATED dhcp_msg.Client = dhcp_msg.DHCPV4 dhcp_data = dhcp_msg.DHCPData dhcp_data.TimeServers.extend(['2.2.2.2', '1.1.1.1']) dhcp_data.LogServers.extend(['2.2.2.2', '1.1.1.1']) dhcp_data.CCAPCores.extend([ '2.2.2.2', ]) dhcp_data.TimeOffset = 0 self.dhcp_client_transport.sock.send(dhcp_msg.SerializeToString()) time.sleep(5) dhcp_msg.InterfaceName = 'eth0' dhcp_msg.Status = dhcp_msg.UPDATED self.dhcp_client_transport.sock.send(dhcp_msg.SerializeToString()) time.sleep(5) dhcp_msg.Status = dhcp_msg.FAILED self.dhcp_client_transport.sock.send(dhcp_msg.SerializeToString()) time.sleep(5) dhcp_msg.Status = dhcp_msg.INITIATED self.dhcp_client_transport.sock.send(dhcp_msg.SerializeToString()) time.sleep(5) # test stop event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_DHCP event_request.action.parameter = "lo" event_request.action.ccap_core_id = "test_ccap_core" event_request.action.action = pb2.msg_event.STOP sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # unregister the ccapcore event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.UNREG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # unregister the mgr event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.UNREG reg.path_info = "ipc:///tmp/test_dhcp_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK)
def test_rcp_event(self): context = zmq.Context() sock1 = context.socket(zmq.PUSH) sock1.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_GCP]['pull']) sock = context.socket(zmq.REQ) sock.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_GCP]['api']) sock2 = context.socket(zmq.PULL) sock2.bind("ipc:///tmp/sock4.sock") rcp_sock = context.socket(zmq.PAIR) rcp_sock.connect(RcpOverGcp.SOCK_ADDRESS) mgr_sock = context.socket(zmq.REP) mgr_sock.bind("ipc:///tmp/rpd_provision_manager_api.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "abcd" reg.action = pb2.msg_manager_register.REG reg.path_info = "ipc:///tmp/sock4.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp # core register register_request = pb2.api_request() reg = pb2.msg_core_register() reg.ccap_core_id = 'abcd' reg.mgr_id = 'abcd' reg.action = pb2.msg_core_register.REG register_request.core_reg.CopyFrom(reg) data = register_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.ccap_core_id = "abcd" event_request.action.event_id = pb2.AGENTTYPE_GCP event_request.action.parameter = '127.0.0.1' event_request.action.action = pb2.msg_event.START sock1.send(event_request.SerializeToString()) data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # test stop event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.ccap_core_id = "abcd" event_request.action.event_id = pb2.AGENTTYPE_GCP event_request.action.parameter = '127.0.0.1' event_request.action.action = pb2.msg_event.STOP # sock1.send(event_request.SerializeToString()) # send rcp message rcp_msg = t_RcpMessage() rcp_msg.RcpMessageType = t_RcpMessage.REDIRECT_NOTIFICATION rcp_msg.RedirectCCAPAddresses.extend(['1.1.1.1']) rcp_sock.send(rcp_msg.SerializeToString()) data = mgr_sock.recv() red_rsp = pb2.msg_magager_api() red_rsp.ParseFromString(data) print red_rsp while True: data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp
def test_dhcp_event(self): context = zmq.Context() sock1 = context.socket(zmq.PUSH) sock1.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_DHCP]['pull']) sock = context.socket(zmq.REQ) sock.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_DHCP]['api']) sock2 = context.socket(zmq.PULL) # sock2.bind("ipc:///tmp/sock4.sock") sock2.bind( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_DHCP]['push']) dhcp_sock = context.socket(zmq.PUSH) dhcp_sock.connect(DhcpAgent.SOCK_ADDRESS) # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "abcd" reg.action = pb2.msg_manager_register.REG reg.path_info = ProcessAgent.SockPathMapping[ ProcessAgent.AGENTTYPE_DHCP]['push'] event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print 'mgr reg:', reg_rsp # core register register_request = pb2.api_request() reg = pb2.msg_core_register() reg.ccap_core_id = 'abcd' reg.mgr_id = 'abcd' reg.action = pb2.msg_core_register.REG register_request.core_reg.CopyFrom(reg) data = register_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print 'core reg:', reg_rsp event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.ccap_core_id = "abcd" event_request.action.event_id = pb2.AGENTTYPE_DHCP event_request.action.parameter = 'eth1' event_request.action.action = pb2.msg_event.START sock1.send(event_request.SerializeToString()) data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # test stop event_request = pb2.msg_event_request() event_request.action.id = "abcd" event_request.action.ccap_core_id = "abcd" event_request.action.event_id = pb2.AGENTTYPE_DHCP event_request.action.parameter = 'eth1' event_request.action.action = pb2.msg_event.STOP # sock1.send(event_request.SerializeToString()) # data = sock2.recv() # red_rsp = pb2.msg_event_notification() # red_rsp.ParseFromString(data) # print red_rsp # send dhcp failed message hostip = '127.0.0.1' dhcp_msg = t_DhcpMessage() dhcp_msg.InterfaceName = "eth1" dhcp_msg.Status = dhcp_msg.FAILED # dhcp_sock.send(dhcp_msg.SerializeToString()) # send dhcp success message dhcp_msg.Status = dhcp_msg.UPDATED dhcp_data = dhcp_msg.DHCPData dhcp_data.TimeServers.extend([hostip, '1.1.1.1']) dhcp_data.LogServers.extend([hostip, '1.1.1.1']) dhcp_data.CCAPCores.extend([ hostip, ]) dhcp_data.TimeOffset = 0 # dhcp_sock.send(dhcp_msg.SerializeToString()) while True: data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp
def test_ptp_start_checkStatus_stop(self): context = zmq.Context() sock_push = context.socket(zmq.PUSH) sock_push.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_PTP]['pull']) sock_api = context.socket(zmq.REQ) sock_api.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_PTP]['api']) sock_pull = context.socket(zmq.PULL) sock_pull.bind("ipc:///tmp/test_ptp_agent.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.REG reg.path_info = "ipc:///tmp/test_ptp_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # test the successfully register event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.REG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.ccap_core_id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_PTP event_request.action.parameter = '' event_request.action.action = pb2.msg_event.START sock_push.send(event_request.SerializeToString()) if self.mastersim_pid is None: self.start_mastersim() # we want to receive 2 notifications, 1 for check status initial, 2 for # the status update timeout = time.time() + 60 i = 2 while i > 0 and time.time() < timeout: try: data = sock_pull.recv(flags=zmq.NOBLOCK) except Exception as e: print "Got exception: %s" % (str(e)) time.sleep(1) continue else: rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp i -= 1 self.assertEqual(i, 0) # test stop event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_PTP event_request.action.parameter = '' event_request.action.ccap_core_id = "test_ccap_core" event_request.action.action = pb2.msg_event.STOP sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # unregister the ccapcore event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.UNREG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # unregister the mgr event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.UNREG reg.path_info = "ipc:///tmp/test_ptp_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK)
def test_l2tp_start_checkStatus_stop(self): context = zmq.Context() sock_push = context.socket(zmq.PUSH) sock_push.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_L2TP]['pull']) sock_api = context.socket(zmq.REQ) sock_api.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_L2TP]['api']) sock_pull = context.socket(zmq.PULL) sock_pull.bind("ipc:///tmp/test_l2tp_agent.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.REG reg.path_info = "ipc:///tmp/test_l2tp_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # test the successfully register event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.REG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.ccap_core_id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_L2TP event_request.action.parameter = "lo;127.0.0.1" event_request.action.action = pb2.msg_event.START sock_push.send(event_request.SerializeToString()) self.start_mastersim() # we want to receive 2 notifications, 1 for check status initial, 2 for the status update i = 2 while i > 0: data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp i -= 1 # test stop event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_L2TP event_request.action.parameter = "lo;127.0.0.1" event_request.action.ccap_core_id = "test_ccap_core" event_request.action.action = pb2.msg_event.STOP sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print rsp # unregister the ccapcore event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.UNREG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # unregister the mgr event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.UNREG reg.path_info = "ipc:///tmp/test_l2tp_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK)
def test_tod_start_checkStatus_stop(self): context = zmq.Context() sock_push = context.socket(zmq.PUSH) sock_push.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_TOD]['pull']) sock_api = context.socket(zmq.REQ) sock_api.connect( ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_TOD]['api']) sock_pull = context.socket(zmq.PULL) sock_pull.bind("ipc:///tmp/test_tod_agent.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.REG reg.path_info = "ipc:///tmp/test_tod_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # test the successfully register event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.REG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print reg_rsp self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.ccap_core_id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_TOD event_request.action.parameter = "127.0.0.1/10|127.0.0.1" event_request.action.action = pb2.msg_event.START sock_push.send(event_request.SerializeToString()) # we want to receive 2 notifications, 1 for check status initial, 2 for the status update # but it's waste time if there is no time server, as tpc client will # retry data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) # same parameter resend # event_request.action.ccap_core_id = "test_ccap_core" sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) # test stop event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_TOD event_request.action.parameter = "127.0.0.1/10|127.0.0.1" event_request.action.ccap_core_id = "test_ccap_core" event_request.action.action = pb2.msg_event.STOP sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) # test wrong ccap core id event_request = pb2.msg_event_request() event_request.action.id = "test_wrong_ccap_id" event_request.action.event_id = ProcessAgent.AGENTTYPE_TOD event_request.action.parameter = "0.0.0.1/10|0.0.0.1" event_request.action.ccap_core_id = "test_wrong_ccap_id" event_request.action.action = pb2.msg_event.START sock_push.send(event_request.SerializeToString()) # test no parameter event_request = pb2.msg_event_request() event_request.action.id = "test_ccap_core" event_request.action.event_id = ProcessAgent.AGENTTYPE_TOD event_request.action.ccap_core_id = "test_ccap_core" event_request.action.action = pb2.msg_event.STOP sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) # test no timeoffset and no log server event_request.action.parameter = "10.0.0.1/|" sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print(rsp) # test illegal parameter event_request.action.parameter = "hahaha" sock_push.send(event_request.SerializeToString()) data = sock_pull.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print(rsp) # test ipc_msg_call back # simulate tpc send to tod_agent kill_cmd = "kill -9 `pgrep -f tpc.py`" call(kill_cmd, shell=True) tod_sock_push = context.socket(zmq.PUSH) tod_sock_push.connect(TimeOfDay.SOCK_ADDRESS) tpc_msg = t_TpcMessage() tpc_msg.Status = tpc_msg.INITIATED msg_str = tpc_msg.SerializeToString() tod_sock_push.send(msg_str) time.sleep(2) tpc_msg.Status = tpc_msg.SUCCESS msg_str = tpc_msg.SerializeToString() tod_sock_push.send(msg_str) time.sleep(2) tpc_msg.Timestamp = 500 msg_str = tpc_msg.SerializeToString() tod_sock_push.send(msg_str) time.sleep(2) tpc_msg.Timestamp = 500 msg_str = tpc_msg.SerializeToString() tod_sock_push.send(msg_str) time.sleep(2) tpc_msg.Status = tpc_msg.FIRST_ATTEMPT_FAILED msg_str = tpc_msg.SerializeToString() tod_sock_push.send(msg_str) time.sleep(2) tpc_msg.Status = tpc_msg.ALL_ATTEMPTS_FAILED msg_str = tpc_msg.SerializeToString() tod_sock_push.send(msg_str) time.sleep(2) tod_sock_push.close() # unregister the ccapcore event_request = pb2.api_request() reg = pb2.msg_core_register() reg.mgr_id = "test_mgr" # use a fake ccap id reg.ccap_core_id = "test_ccap_core" reg.action = pb2.msg_core_register.UNREG event_request.core_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print(reg_rsp) self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK) # unregister the mgr event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_mgr" # use a fake ccap id reg.action = pb2.msg_manager_register.UNREG reg.path_info = "ipc:///tmp/test_tod_agent.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock_api.send(data) data = sock_api.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print(reg_rsp) self.assertEqual(reg_rsp.reg_rsp.status, reg_rsp.reg_rsp.OK)
def test_rcp_event(self): context = zmq.Context() sock1 = context.socket(zmq.PUSH) sock1.connect(ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_GCP]['pull']) sock = context.socket(zmq.REQ) sock.connect(ProcessAgent.SockPathMapping[ProcessAgent.AGENTTYPE_GCP]['api']) sock2 = context.socket(zmq.PULL) sock2.bind("ipc:///tmp/sock4.sock") mgr_sock = context.socket(zmq.REP) mgr_sock.bind("ipc:///tmp/rpd_provision_manager_api.sock") # test the successfully register event_request = pb2.api_request() reg = pb2.msg_manager_register() reg.id = "test_rcp" reg.action = pb2.msg_manager_register.REG reg.path_info = "ipc:///tmp/sock4.sock" event_request.mgr_reg.CopyFrom(reg) data = event_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print "=" * 40 + "MGR REG" + "=" * 40 print reg_rsp # core register register_request = pb2.api_request() reg = pb2.msg_core_register() reg.ccap_core_id = 'test_rcp' reg.mgr_id = 'test_rcp' reg.action = pb2.msg_core_register.REG register_request.core_reg.CopyFrom(reg) data = register_request.SerializeToString() sock.send(data) data = sock.recv() reg_rsp = pb2.api_rsp() reg_rsp.ParseFromString(data) print "=" * 40 + "CORE REG" + "=" * 40 print reg_rsp event_request = pb2.msg_event_request() event_request.action.id = "test_rcp" event_request.action.ccap_core_id = "test_rcp" event_request.action.event_id = ProcessAgent.AGENTTYPE_GCP event_request.action.parameter = 'lo;127.0.0.1' event_request.action.action = pb2.msg_event.START sock1.send(event_request.SerializeToString()) data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print "=" * 40 + "START" + "=" * 40 print rsp result = False print "*" * 40 + 'start timer' + str(time.localtime(time.time())) + "*" * 40 self.timer = Timer(1, self.timer_cb) self.timer.start() while self.running: try: data = sock2.recv(flags=zmq.NOBLOCK) if len(data) > 0: rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print "=" * 40 + "NTF" + "=" * 40 print rsp if rsp.core_event.result == "DOWN": event_request = pb2.msg_event_request() event_request.action.id = "test_rcp" event_request.action.ccap_core_id = "test_rcp" event_request.action.event_id = ProcessAgent.AGENTTYPE_GCP event_request.action.parameter = 'lo;127.0.0.1' event_request.action.action = pb2.msg_event.STOP sock1.send(event_request.SerializeToString()) data = sock2.recv() rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print "=" * 40 + "STOP" + "=" * 40 print rsp result = False self.running = False elif rsp.core_event.result == "UP": result = True except zmq.Again: pass except Exception as e: print "exception:%s" % str(e) if self.timer: self.timer.cancel() self.assertEqual(result, True) time.sleep(5) try: data = sock2.recv(flags=zmq.NOBLOCK) if len(data) > 0: rsp = pb2.msg_event_notification() rsp.ParseFromString(data) print "=" * 40 + "TIMEOUT" + "=" * 40 print rsp self.assertEqual(rsp.core_event.result, "DOWN") except Exception as e: print "exception:%s" % str(e)
def _handle_mgr_register(self, reg): self.logger.info("%s Processing the mgr register request:%s" % (self.AgentName[self.id], reg)) id = reg.id action = reg.action if action == process_agent_pb2.msg_manager_register.REG: path = module_name = parameter = None if reg.HasField("path_info"): path = reg.path_info if reg.HasField("module_name"): module_name = reg.module_name if reg.HasField("parameter"): parameter = reg.parameter # check is the client has been registered if id in self.mgrs: self.logger.info( "The mgr[%s] has been registered, ignore this request." % id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.OK rsp.reason = "Mgr has been registered, ignore this register request." api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return if path is None: self.logger.error( "Cannot process the register request since the pull sock is none." ) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.FAIL rsp.reason = "Path is not included in request message" api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return # Create the pull, will not register to dispatcher, since we will # use the non-block send transport = Transport(path, Transport.PUSHSOCK, Transport.TRANSPORT_CLIENT) # Add the fsm to our internal database self.mgrs[id] = { "transport": transport, "name": module_name, "para": parameter, "path": path, } # send the feed back self.logger.info("Mgr[%s] registered successfully" % id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.OK rsp.reason = "Register successfully" api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return elif action == process_agent_pb2.msg_manager_register.UNREG: # check if the requester has been registered if id not in self.mgrs: self.logger.error( "Cannot process the mgr unregister request since cannot find the id[%s] in local db" % id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.FAIL rsp.reason = "Cannot process mgr unregister request since cannot find the id[%s] in local db" % id api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return # process the requester requester = self.mgrs[id] transport = requester["transport"] if transport and transport.sock: transport.sock.close() self.mgrs.pop(id) # send the feed back self.logger.info("Id[%s] unregistered successfully" % id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.OK rsp.reason = "Unregistered successfully" api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return else: pass
def _handle_core_register(self, reg): self.logger.info("%s Processing the core register request:%s" % (self.AgentName[self.id], reg)) id = reg.ccap_core_id action = reg.action mgr_id = reg.mgr_id if action == process_agent_pb2.msg_core_register.REG: # check is the client has been registered if id in self.ccap_cores: self.logger.info( "The ccap core[%s] has been registered, ignore this request." % id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.OK rsp.reason = "Core [%s] has been registered, ignore this register request." % id api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return if mgr_id not in self.mgrs: self.logger.info("Cannot find the mgr[%s], core id is [%s]" % (mgr_id, id)) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.FAIL rsp.reason = "Cannot find the mgr:%s." % mgr_id api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return # Add the fsm to our internal database self.ccap_cores[id] = { "mgr": mgr_id, } # send the feed back self.logger.info("CCap core [%s] registered successfully" % id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.OK rsp.reason = "Register successfully" api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return elif action == process_agent_pb2.msg_manager_register.UNREG: # check if the requester has been registered if id not in self.ccap_cores: self.logger.error( "Cannot process unregister request since we cannot find the id[%s] in local db", id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.FAIL rsp.reason = "Cannot process the unregister request since cannot find the id[%s] in local db" % id api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return # process the requester self.ccap_cores.pop(id) # send the feed back self.logger.info("CCAP core [%s] unregistered successfully" % id) rsp = process_agent_pb2.msg_register_rsp() rsp.id = id rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.OK rsp.reason = "Unregistered successfully" api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) self.send_reg_rsp(api_rsp) return else: pass
def api_event_callback(self, fd, eventmask): """Call back functions, the subclass should implement this function. :param fd: passed from the register. the arg should contains the transport information. :param eventmask: passed from the dispatcher, which indicates the event type. :return: None """ # Receive the msg from the remote if eventmask == 0 or self.api_transport.sock != fd: self.logger.warn("Got a fake process event[%x], ignore it." % eventmask) return if eventmask & self.dispatcher.EV_FD_ERR: self.logger.error("Got an error event, handle the failure.") # FixMe: may need more action to handle the failure return if self.api_transport.sock.getsockopt(zmq.EVENTS) != zmq.POLLIN: self.logger.debug("Got a fake event, the receive is not ready!") return try: data = self.api_transport.sock.recv(flags=zmq.NOBLOCK) msg = process_agent_pb2.api_request() msg.ParseFromString(data) self.logger.debug("Receive an api message from the FSM:%s" % str(msg)) # check the fields, we only processing the register fields fields = msg.ListFields() for field in fields: desc, value = field if desc.name == "mgr_reg": self._handle_mgr_register(value) elif desc.name == "core_reg": self._handle_core_register(value) else: self.logger.error( "Cannot handle the request since no handler for this, msg:%s.", msg) except zmq.Again: pass except Exception as e: self.logger.error("Cannot process the event, reason:%s" % str(e)) rsp = process_agent_pb2.msg_register_rsp() rsp.id = "" rsp.agent_id = self.id rsp.status = process_agent_pb2.msg_register_rsp.FAIL rsp.reason = "Exception happens" api_rsp = process_agent_pb2.api_rsp() api_rsp.reg_rsp.CopyFrom(rsp) data = api_rsp.SerializeToString() self.send_reg_rsp(data)