def scan_available_interface(self, _): interface_up = list() stats = net_if_stats() for interface in stats.keys(): if interface != 'lo': if SysTools.is_if_oper_up(interface): interface_up.append(interface) # need to check redefined interface proto is provision or not for RPD interface_ret = self.filter_proto_interface("'provision'", interface_up) # prepare for startup if self.init_start: for interface in interface_ret: SysTools.set_protocol(interface) self.init_start = False 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 = json.dumps(interface_ret) try: self.mgrs[idx]['transport'].sock.send( event_request_rsp.SerializeToString(), flags=zmq.NOBLOCK) except zmq.ZMQError as ex: self.logger.error("failed to send to manager: %s" % str(ex))
def start_dhcpv4(self, interface): exec_timer = self.dispatcher.timer_register( self.DHCPV4_BACKOFF_TIMEOUT, self._dhcp_timeout_cb, arg=interface, timer_type=DpTimerManager.TIMER_ONESHOT) SysTools.set_protocol(interface, proto=SysTools.supported_proto[2]) self.processes[interface] = { "name": 'dhcpv4', "process": True, "timer": exec_timer, }
def cleanup_db(self, ccap_core_id): """cleanup the remain requester if exist.""" # clear the remain requester if exist for interface in self.dhcp.keys(): if ccap_core_id in self.dhcp[interface]["requester"]: self.logger.info("cleanup DHCP agent {}".format(ccap_core_id)) self.dhcp[interface]["requester"].remove(ccap_core_id) if len(self.dhcp[interface]["requester"]) == 0: self.dhcp.pop(interface) SysTools.set_protocol(interface) self.processes[interface]["process"] = False self.processes.pop(interface)
def _dhcp_no_lease(self, interface): """DHCP client failed to get required information. (backoff timer increased to maximum value without success) * If DHCPv6 failed -> try DHCPv4 * If DHCPv4 failed -> reboot :return: False to represent DHCP fail, True if do not want to report this fail """ if interface not in self.processes: self.logger.warn( "Process information about Interface[%s] doesn't exist", interface) return False stats = net_if_stats() if interface in stats and not SysTools.is_if_oper_up(interface): self.logger.info("Ignore this message caused by link down...") return False if interface in stats and self.dhcp[interface]['initiated']: self.logger.info("Ignore failure for %s previous up", self.processes[interface]['name']) return False name = self.processes[interface]['name'] if self.processes[interface]['process']: if name == "dhcpv6": # when dhcpv6 process failed, check if any duplicated ipv6 address event need to be reported self.check_and_report_ipv6_event(interface) self.logger.info("Starting DHCPv4 client ...") self.start_dhcpv4(interface) return False elif name == "dhcpv4": self.logger.warn("DHCPv4 failure...") SysTools.set_protocol(interface) self.processes[interface]['process'] = False return False else: raise ValueError("Unexpected process name {}".format(name)) return False
def interrupt_handler(self, signum, frame): for interface in self.processes.keys(): SysTools.set_protocol(interface) sys.exit(0)
def process_event_action(self, action): """Process the request from the client. :param action: :return: """ ccap_core_id = action.ccap_core_id event_action = action.action self.logger.debug("Receive an event action:%s", action) if ccap_core_id not in self.ccap_cores: self.logger.warn( "Cannot process the event action for id %s, reason: id is not registered" % ccap_core_id) self.cleanup_db(ccap_core_id) self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.FAIL, "CCAP core ID is not registered") return if not action.HasField("parameter"): self.logger.warn( "Cannot process the event action for id %s, reason:Parameter is not set" % ccap_core_id) # return error self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.FAIL, "Parameter is not set") return interface = action.parameter if event_action == protoDef.msg_event.START or event_action == protoDef.msg_event.CHECKSTATUS: # check if we are in the requester list, if yes, we just send a # current status to it if interface in self.dhcp: if ccap_core_id not in self.dhcp[interface]["requester"]: self.dhcp[interface]["requester"].append(ccap_core_id) if None is self.dhcp[interface]['initiated_by']: self.dhcp[interface]['initiated_by'] = ccap_core_id if not self.processes[interface]['process']: if self.simulate_mode: self.start_dhcpv4(interface) else: self.start_dhcpv6(interface) else: if self.simulate_mode: self.start_dhcpv4(interface) else: self.start_dhcpv6(interface) # create a interface in self interfaces self.dhcp[interface] = { "status": self.DOWN, "requester": [ ccap_core_id, ], "lastChangeTime": time(), "transport": self.process_transport, "initiated_by": ccap_core_id, "initiated": False, } self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.OK, "Id has been issue this action, send current status to you", result=self.dhcp[interface]["status"]) return if event_action == protoDef.msg_event.STOP: if interface in self.dhcp: if ccap_core_id in self.dhcp[interface]["requester"]: self.dhcp[interface]["requester"].remove(ccap_core_id) if len(self.dhcp[interface]["requester"] ) == 0 and self.dhcp[interface]["status"] == self.DOWN: self.dhcp.pop(interface) self.processes.pop(interface) SysTools.set_protocol(interface) self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.OK, reason="Successful stop event.") else: self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.FAIL, reason="Cannot stop event since can not find it.") return