def tpc_msg_cb(self, ipc_msg): try: tpc_msg = t_TpcMessage() tpc_msg.ParseFromString(ipc_msg) self.logger.debug("TPC status: %s", tpc_msg.t_Status.Name(tpc_msg.Status)) if tpc_msg.Status == tpc_msg.SUCCESS: self.logger.info("Valid timestamp received from TPC") if tpc_msg.HasField('Timestamp'): self.set_system_time(tpc_msg.Timestamp) self.fsm.time_cfged() self.processes['tpc'].cleanup() else: self.logger.error( "Mandatory timestamp missing, ignoring message") elif tpc_msg.Status == tpc_msg.FIRST_ATTEMPT_FAILED: self.logger.info("First attempt to get time failed - " "resetting time to 1.1.1970") self.set_system_time(0) elif tpc_msg.Status == tpc_msg.ALL_ATTEMPTS_FAILED: # don't have correct time -> restart device self.logger.error("Failed to get time of day - rebooting") self.reboot() else: self.logger.error("Unexpected status received from TPC") return except DecodeError, ex: self.logger.error("Failed to decode IPC message: %s", ex.message) return
def test_tpc_response_yes(self): # Set time to 2 min after epoch self.mgr.set_system_time(120) self.mgr.fsm.current = 'time_waiting' # Create some placeholder for TPC process (to be cleanup up in msg cb) self.mgr.processes['tpc'] = ProcessInfo(["test", "test"], "ipc://test") # Create simulated message to change system time to value higher than # initial value tpc_msg = t_TpcMessage() tpc_msg.Status = tpc_msg.SUCCESS tpc_msg.Timestamp = 500 msg_str = tpc_msg.SerializeToString() self.mgr.tpc_msg_cb(msg_str) # time should be >= initial value self.assertGreaterEqual(int(time.time()), 500)
def test_tpc_response_no(self): # After first failed attempt to get time, it should be cleared to # zero (1.1.1970), so we set time to some different value and check # Set time to 2 min after epoch self.mgr.set_system_time(120) # Create some placeholder for TPC process (to be cleanup up in msg cb) self.mgr.processes['tpc'] = ProcessInfo(["test", "test"], "ipc://test") # Create simulated message to reset system time tpc_msg = t_TpcMessage() tpc_msg.Status = tpc_msg.FIRST_ATTEMPT_FAILED msg_str = tpc_msg.SerializeToString() self.mgr.tpc_msg_cb(msg_str) # less than 30 sec (timeouts included) self.assertLess(int(time.time()), 30)
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 ipc_msg_cb(self, fd, eventmask): # Receive the msg from the remote if eventmask == 0 or self.process_transport.sock != fd: self.logger.warn("Got a fake process event, ignore it") return # FixMe: may need more action if eventmask & self.dispatcher.EV_FD_ERR: self.logger.warn("Got EV_FD_ERR event") return if self.process_transport.sock.getsockopt(zmq.EVENTS) != zmq.POLLIN: self.logger.debug("Got a fake event, the receive is not ready!") return try: data = self.process_transport.sock.recv(flags=zmq.NOBLOCK) tpc_msg = t_TpcMessage() tpc_msg.ParseFromString(data) self.logger.debug("TPC status: %s", tpc_msg.t_Status.Name(tpc_msg.Status)) status_changed = False valid_timeserver = tpc_msg.Validtimeserver if tpc_msg.Status == tpc_msg.SUCCESS: self.logger.info("Valid timestamp received from TPC") if tpc_msg.HasField('Timestamp'): if not self.tod_done: status_changed = True self.tod_done = True for time_server in self.tod: self.tod[time_server]['status'] = self.UP self.tod[time_server]["lastChangeTime"] = time() self.terminate_process(self.processes['tod']) self.processes['tod'] = None for idx in self.mgrs: event_request_rsp = protoDef.msg_event_notification() event_request_rsp.mgr_event.mgr_id = idx event_request_rsp.mgr_event.event_id = self.id event_request_rsp.mgr_event.data = 'success/' + str(tpc_msg.Timestamp) + \ '|' + valid_timeserver self.mgrs[idx]['transport'].sock.send( event_request_rsp.SerializeToString(), flags=zmq.NOBLOCK) self.logger.debug( "Send event notification to id %s, msg:%s" % (idx, event_request_rsp)) else: self.logger.warn( "Mandatory timestamp missing, ignoring message") self.notify.error( rpd_event_def.RPD_EVENT_TOD_INVALID_FMT[0], '') elif tpc_msg.Status == tpc_msg.FIRST_ATTEMPT_FAILED: self.logger.info("First attempt to get time failed - " "resetting time to 1.1.1970") # notify the mgr about TOD failure for idx in self.mgrs: event_request_rsp = protoDef.msg_event_notification() event_request_rsp.mgr_event.mgr_id = idx event_request_rsp.mgr_event.event_id = self.id event_request_rsp.mgr_event.data = 'tod_first_failed/0' + '|' + valid_timeserver self.mgrs[idx]['transport'].sock.send( event_request_rsp.SerializeToString(), flags=zmq.NOBLOCK) self.logger.debug( "Send event notification to id %s, msg:%s" % (idx, event_request_rsp)) elif tpc_msg.Status == tpc_msg.ALL_ATTEMPTS_FAILED: self.logger.info("All attempt to get time failed") # Failed to get time of day - rebooting if self.tod_done: status_changed = True self.tod_done = False for time_server in self.tod: self.tod[time_server]['status'] = self.DOWN self.tod[time_server]["lastChangeTime"] = time() # notify the mgr about TOD failure for idx in self.mgrs: event_request_rsp = protoDef.msg_event_notification() event_request_rsp.mgr_event.mgr_id = idx event_request_rsp.mgr_event.event_id = self.id event_request_rsp.mgr_event.data = 'tod_failed/' + '' + '|' + valid_timeserver self.mgrs[idx]['transport'].sock.send( event_request_rsp.SerializeToString(), flags=zmq.NOBLOCK) self.logger.debug( "Send event notification to id %s, msg:%s" % (idx, event_request_rsp)) else: self.notify.error(rpd_event_def.RPD_EVENT_TOD_INVALID_FMT[0], '') return # send the status change to the requester if not status_changed: return for time_server in self.tod: popup_list = list() for ccap_core_id in self.tod[time_server]["requester"]: if ccap_core_id not in self.ccap_cores: popup_list.append(ccap_core_id) continue self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.OK, "Status changed", result=self.tod[time_server]["status"]) for idx in popup_list: self.tod[time_server]['requester'].remove(idx) except zmq.Again as e: pass except Exception as e: self.logger.error("Cannot process the event, reason:%s" % str(e))