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 _check_interface_status_callback(self, _): self.logger.debug("Check the interface status...") if len(self.interfaces) == 0: return stats = net_if_stats() for interface in self.interfaces: if interface not in stats: self.logger.error( "Cannot find the interface[%s] in current system configuration." % interface) continue current_stat = self.UP if SysTools.is_if_oper_up(interface) else self.DOWN self.logger.debug("Check the interface[%s] status, original %s now %s ...", interface, self.interfaces[interface]['status'], current_stat) status_changed = False # CHeck if there is an status change: if SysTools.is_if_oper_up(interface): if None is not self.interfaces[interface]["flappingTimer"]: self.dispatcher.timer_unregister(self.interfaces[interface]["flappingTimer"]) self.interfaces[interface]["flappingTimer"] = None self.logger.info( "Cancel interface[%s] flappingTimer." % interface) if self.interfaces[interface]["status"] != self.UP: self.interfaces[interface]["status"] = self.UP status_changed = True elif (not SysTools.is_if_oper_up(interface)) and self.interfaces[interface]["status"] != self.DOWN: self.interfaces[interface]["status"] = self.DOWN status_changed = True else: pass # send the status change to the requester if not status_changed: continue self.logger.debug("Interface %s status changes to %s", interface, self.interfaces[interface]["status"]) popup_list = list() for ccap_id in self.interfaces[interface]["requester"]: if ccap_id not in self.ccap_cores: popup_list.append(ccap_id) continue try: self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.OK, "Status changed", result=self.interfaces[interface]["status"]) except zmq.ZMQError as ex: self.logger.error("failed to send to manager: %s" % str(ex)) for ccap_id in popup_list: self.interfaces[interface]['requester'].remove(ccap_id)
def recvRpdResetCtrl(self, cfgMsg): rcp_msg = self.DeSerializeConfigMsgPayload(cfgMsg) if rcp_msg is None: return { "Status": HalCommon_pb2.FAILED, "ErrorDescription": "DeSerialize ConfigMsgPayload fail" } self.logger.debug("\nProvMgr receive RpdResetCtrl:" + str(rcp_msg)) operation = rcp_msg.RpdDataMessage.RpdDataOperation recv_rcp_msg = rcp_msg.RpdDataMessage.RpdData if recv_rcp_msg.HasField("RpdCtrl") and recv_rcp_msg.RpdCtrl.HasField( "ResetCtrl"): if operation not in [ t_RpdDataMessage.RPD_CFG_WRITE, t_RpdDataMessage.RPD_CFG_READ ]: return { "Status": HalCommon_pb2.FAILED, "ErrorDescription": "Operation %d for RpdResetCtrl is not supported" % operation } rcp_rpd_resetctrl = recv_rcp_msg.RpdCtrl.ResetCtrl if operation == t_RpdDataMessage.RPD_CFG_WRITE: reset = rcp_rpd_resetctrl.Reset reset_type = rcp_tlv_def.RESET_TYPE[reset] with open(self.RESET_CTRL_FILENAME, 'w') as f: f.write(str(reset) + ":" + str(reset_type) + "\n") for ccap_core in CCAPCore.ccap_core_db.values(): ccap_core.del_ccap_core() SysTools.reboot(reset_type) if operation == t_RpdDataMessage.RPD_CFG_READ: try: with open(self.RESET_CTRL_FILENAME, 'r') as fr: reset_rd = fr.read() rcp_rpd_resetctrl.Reset = int(reset_rd.strip(":")[0]) except IOError: # file don't exist,skip check pass cfgMsg.CfgMsgPayload = rcp_msg.SerializeToString() return { "Status": HalCommon_pb2.SUCCESS, "ErrorDescription": "ProMgr handle RpdResetCtrl success for %d" % operation } else: return { "Status": HalCommon_pb2.SUCCESS_IGNORE_RESULT, "ErrorDescription": "ProvMgr Do not Have RpdCtrl Filed." }
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 getLocalIp(core_ip): intf = 'eth0' family = socket.AF_INET if core_ip is not None: is_ipv6 = Convert.is_valid_ipv6_address(core_ip) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] if core_ip is None or core_ip == "127.0.0.1" or core_ip == "::1": intf = 'eth0' else: intf = SysTools.get_interface() local_ip = SysTools.get_ip_address(intf, family) if local_ip is None: return globalSettings.L2tpv3GlobalSettings.LocalIPAddress return local_ip
def _pack_mcast_group_source_req(self): try: grp_addrinfo = socket.getaddrinfo(self.grp_ip, None)[0] family = grp_addrinfo[0] src_addrinfo = socket.getaddrinfo(self.src_ip, None)[0] ifindex = SysTools.if_nametoindex(self.interface) if ifindex is None: raise McastException("ifindex is None for %s" % self.interface) """ mcast_req as : uint32_t interface; struct sockaddr_storage group_req; (128 bytes); struct sockaddr_storage source_req; (128 bytes); for the sockaddr_storage: if this is INET6 family first 28 bytes are struct sockaddr_in6 type: sa_family_t sin6_family (2byte); in_port_t sin6_port (2byte); uint32_t sin6_flowinfo (4byte); struct in6_addr sin6_addr (16 bytes); uint32_t sin6_scope_id; (4 byte) if this is the INET family first 16 bytes are struct sockaddr_in type: sa_family_t sa_family (2byte) in_port_t sin_port (2byte) struct in_addr sin_addr (4byte) padding (8 bytes) char sa_data[14] """ if family == socket.AF_INET: group_ip_bin = socket.inet_aton(self.grp_ip) group_req = struct.pack( 'HH', family, self.port) + group_ip_bin + struct.pack( '120s', "") source_ip_bin = socket.inet_aton(self.src_ip) source_req = struct.pack( 'HH', family, 0) + source_ip_bin + struct.pack('120s', "") proto = socket.IPPROTO_IP else: group_ipv6_bin = socket.inet_pton(family, grp_addrinfo[4][0]) group_req = struct.pack('HHI', family, self.port, 0) + group_ipv6_bin + struct.pack( 'I100s', 0, "") source_ipv6_bin = socket.inet_pton(family, src_addrinfo[4][0]) source_req = struct.pack('HHI', family, 0, 0) + source_ipv6_bin + struct.pack( 'I100s', 0, "") proto = socket.IPPROTO_IPV6 """ for alignment, 64bit total size is 264, 32bit system, total size is 260 """ arch = platform.architecture() if arch[0] == "32bit": mcast_req = struct.pack('I', ifindex) + group_req + source_req else: mcast_req = struct.pack('II', ifindex, 0) + group_req + source_req return (mcast_req, proto) except Exception as e: self.logger.warning( "exception happen when prepare mcast_grp_src_req grp_ip[%s],src_ip[%s],local[%s]: %s", self.grp_ip, self.src_ip, self.local_ip, str(e)) return (None, None)
def __init__(self, agent_type): """Init fake agent. :param agent_type: :return: """ self.input_parameter = {} self.agent_status = self.DOWN super(FakeAgent, self).__init__(agent_type) if not os.path.exists(self.FakeAgent_Descriptor[self.id]): SysTools.touch(self.FakeAgent_Descriptor[self.id]) self.register_poll_timer(self.FAKE_AGENT_PERIOD_STATUS_CHECK, self.fake_agent_period_status_check)
def add_ccap_cores(self, ccap_cores, port_master=GCPSessionDescriptor.DEFAULT_PORT_MASTER): """Create GCP descriptors based on addresses of CCAP cores received from DHCP server to orchestrator. :param ccap_cores: list of "interface;core ip" :type ccap_cores: list(string) :return: """ descriptors = [] for core_addr in ccap_cores: interface, core = core_addr.split(';') if not Convert.is_valid_ip_address(core): self.logger.warn("Malformed IP address received: %s", core) continue is_ipv6 = Convert.is_valid_ipv6_address(core) family = (AF_INET, AF_INET6)[is_ipv6] addr_local = SysTools.get_ip_address(str(interface), family) # TODO pass also local address to use specific interface descriptors.append( GCPSlaveDescriptor(core, port_master=port_master, addr_local=addr_local, interface_local=interface, addr_family=family)) self.orchestrator.add_sessions(descriptors)
def get_dhcp_info(self, args=None): """ :return: dhcp info """ dhcp_info = {'Interface': [], 'Details': []} try: nic_info = net_if_addrs() for intf in self.mgr.dhcp_parameter: netmask = '0.0.0.0' is_ipv6 = Convert.is_valid_ipv6_address(self.mgr.dhcp_parameter[intf]['CCAPCores'][0]) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] for item in nic_info[intf]: if item.family == family: netmask = item.netmask break local_ip = SysTools.get_ip_address(str(intf), family=family) value = {"Interface": intf, 'IP-Address': local_ip, "Subnet-Mask": netmask} dhcp_info['Interface'].append(value) addr_type = "IPv4" if is_ipv6: if self.mgr.dhcp_parameter[intf]['Slaac']: addr_type = "IPv6<Stateless>" else: addr_type = "IPv6<Stateful>" intf_dhcp_parameter = self.mgr.dhcp_parameter[intf] intf_dhcp_parameter["AddrType"] = addr_type dhcp_info['Details'].append((intf, intf_dhcp_parameter)) return True, dhcp_info except Exception as e: return False, str(e)
def get_gcp_info(self, args=None): """ :return: gcp info """ gcp_info = [] try: agent_id = ProcessAgent.AGENTTYPE_GCP for ccap_core_id in CCAPCore.ccap_core_db: ccap_core = CCAPCore.ccap_core_db[ccap_core_id] status = ccap_core.agent_status[agent_id] para = ccap_core.parameters principal = ccap_core.is_principal is_ipv6 = Convert.is_valid_ipv6_address(ccap_core.ccap_core_network_address) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] if agent_id in para and ';' in para[agent_id]: intf, core_ip = para[agent_id].split(';') local_ip = SysTools.get_ip_address(str(intf), family=family) value = { "Core-ID": ccap_core_id, "Core-IP": core_ip, "Local-IP": local_ip, "Principal": 'Yes' if principal == CoreDescription.CORE_ROLE_PRINCIPAL else 'No', 'Status': status} gcp_info.append(value) return True, gcp_info except Exception as e: return False, str(e)
def config_rsyslog_remote(self, address): # write to remote server, check whether it's been configured already. conf = open('/etc/rsyslog.conf', 'r') server_configed = conf.read() address_configed = server_configed.find(address) conf.close() if address_configed > 0: self.logger.info( "Log Server IP address provided for remote logging already configed" ) return conf = open('/etc/rsyslog.conf', 'a+') # write to remote logserver, TCP to logserver conf.write("\n") remote_channel = "*." + self.rsyslog_loglevel + " @" + address conf.write(remote_channel) conf.write("\n") conf.close() if SysTools.is_vrpd(): hostmac = SysTools.get_mac_address("eth0") else: hostmac = SysTools.get_sys_mac_address() hostname = 'RPD' + hostmac.replace(':', '') set_host_cmd = 'echo ' + hostname + '>/proc/sys/kernel/hostname' os.system(set_host_cmd) new_hostline = hostname + " localhost" # write to /etc/hosts new_host = open('/tmp/new_hosts', 'w') old_host = open('/etc/hosts', 'r') line = old_host.read() found = line.find('localhost') configed = line.find('RPD') if found > 0 and configed < 0: new_host.write(line.replace('localhost', str(new_hostline))) else: new_host.write(line) old_host.close() new_host.flush() new_host.close() os.remove('/etc/hosts') shutil.move('/tmp/new_hosts', '/etc/hosts') return
def test_logmem(self): ret = True try: SysTools.logmem("dummy tag") SysTools.logmem("") SysTools.logmem(1) SysTools.logmem(None) except Exception: ret = False self.assertTrue(ret)
def set_enetif(self, enetif): enetif.ifIndex = self.interface_dict['eth0'] enetif.ifName = "eth0" enetif.ifAlias = "eth0" enetif.ifPhysAddress = SysTools.get_mac_address("eth0") enetif.ifType = 6 enetif.ifMTU = 1500 enetif.ifAdminStatus = 1 enetif.ifOperStatus = 1
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 __init__(self, simulator=False): """ :param simulator: Simulator is used to run the program with interface simulator mode """ # initiate agent dict which need to start, and corresponding instance created self.agent_dict = self.build_agent_dict(simulator) self.simulator_flag = simulator if simulator: self.manager_cmd_line = "python -m rpd.provision.manager.src.manager_process -s".split( ) self.hal_cmd_line = None self.fake_driver_cmd_line = None self.ptp_driver_cmd_line = None self.ssd_driver_cmd_line = None self.res_hal_cmd_line = None else: self.manager_cmd_line = "python -m rpd.provision.manager.src.manager_process".split( ) self.hal_cmd_line = "python -m rpd.hal.src.HalMain --conf=/etc/config/hal.conf".split( ) self.res_hal_cmd_line = "python -m rpd.resource.src.RpdResHalClient".split( ) if SysTools.is_vrpd(): self.fake_driver_cmd_line = "python -m rpd.hal.lib.drivers.HalDriver0".split( ) self.ptp_driver_cmd_line = "python -m rpd.hal.lib.drivers.HalPtpDriver".split( ) self.ssd_driver_cmd_line = None else: # pragma: no cover self.fake_driver_cmd_line = "python -m rpdHalDrv.HalDriverClient".split( ) self.ptp_driver_cmd_line = "python -m rpdPtpHalDrv.PtpHalDriverClient".split( ) self.ssd_driver_cmd_line = "python -m rpd.ssd.HalSsdDriver".split( ) self.fault_manager_cmd_line = "python -m rpd.common.rpd_fault_manager".split( ) self.agent_obj = {} self.manager_process = None self.hal_process = None self.fake_driver_process = None self.ptp_driver_process = None self.monitor_driver_process = None self.res_driver_process = None self.ssd_driver_process = None
def update(self, core_instance, event): """update the statistics info. :param core_instance: CCAP core instance :param event: fsm event instance :return: """ self.RPD_MAC = SysTools.get_mac_address(core_instance.interface) ccap_core_id = core_instance.ccap_core_id added_by = core_instance.added_by item = "{}/{}".format(core_instance.interface, core_instance.ccap_core_network_address) # limit the core ip count to 20 each node if len(self.statistics) >= self.RECORD_CCAP_CORE_CNT_MAX: self.logger.warning( "Ccap core already exceed the max count %d, ignore %s ", self.RECORD_CCAP_CORE_CNT_MAX, item) return if item not in self.statistics: self.statistics[item] = OrderedDict() if ccap_core_id not in self.statistics[item]: self.statistics[item][ccap_core_id] = OrderedDict() # limit the core id count to 5 under each core ip while len(self.statistics[item] ) >= self.RECORD_CORE_ID_PER_CCAP_CORE_MAX: self.statistics[item].popitem(last=False) # limit the event count to 100 each core id while len(self.statistics[item] [ccap_core_id]) >= self.RECORD_EVENT_PER_CORE_ID_MAX: self.statistics[item][ccap_core_id].popitem(last=False) current = time() self.statistics[item][ccap_core_id].update({ self.fmt_timestamp(current): { "interface": core_instance.interface, "core-ip": core_instance.ccap_core_network_address, "mac": self.RPD_MAC, "src": event.src, "dst": event.dst, "event": event.event, "Added-By": added_by } })
def test_generate_event_66070408(self): rootca = self.rootpath + '/rpd/ssd/testing/CABLELABS_ROOT_CA_PEM.CRT' driver = HalClientTest("SSD_Driver", "This is SSD Driver", "0.1.0", (MsgTypeSsd, ), None, rootca=rootca) SysTools.is_last_reset_by_power_off = SysToolsTest.is_last_reset_by_power_off self.assertTrue(SysTools.is_last_reset_by_power_off()) driver.ssdInProgressFile = self.rootpath + '/rpd/ssd/testing/test_ssdInProgressFile.json' self.assertTrue(os.path.exists(driver.ssdInProgressFile)) driver.start(simulate_mode=True) driver.connection_cleanup()
def gen_dhcp_mapping_info(self, interface): """Generate dhcp mapping info if not exist. :param interface: :return: """ rpd_ident = ['cfg', 'RpdCapabilities', 'RpdIdentification'] self.dhcp_args_mapping = { '0x02': rpd_ident + ['DeviceDescription'], '0x04': rpd_ident + ['SerialNumber'], '0x05': self.hw_version_path, '0x06': rpd_ident + ['CurrentSwVersion'], '0x07': rpd_ident + ['BootRomVersion'], '0x08': "".join(SysTools.get_mac_address(interface).split(':') [0:3]), # vendor ID '0x09': rpd_ident + ['ModelNumber'], '0x0A': rpd_ident + ['VendorName'] } # Fill device information to DB, if not loaded mac_addr_str = rpd_ident + ['DeviceMacAddress'] if self.db_adapter.get_leaf(mac_addr_str) is None: self.db_adapter.set_leaf(mac_addr_str, SysTools.get_mac_address(interface), True) hostname_str = rpd_ident + ['DeviceAlias'] if self.db_adapter.get_leaf(hostname_str) is None: self.db_adapter.set_leaf(hostname_str, SysTools.get_host_name(), True) if self.db_adapter.get_leaf(self.hw_version_path) is None: self.db_adapter.set_leaf(self.hw_version_path, self.hw_version.encode('hex'), True)
def filter_proto_interface(self, proto, interface_list): """Filter the needed interface with specified proto. :param proto: proto needed :param interface_list: list of interface """ if not SysTools.is_system_openwrt(): return interface_list uci_interfaces = set() try: remove_interface = [] output = check_output(['uci', 'show', 'network']) network_list = output.strip().split('\n') for config in network_list: cfg, option = config.split('=') net_prex = cfg.split(".") if net_prex[-1] == "proto": ifname = '.'.join(net_prex[:-1]) + '.ifname' interface = check_output(['uci', 'get', ifname]).split('\n')[0] uci_interfaces.add(interface) if net_prex[-1] == "proto" and str(option) != proto: ifname = '.'.join(net_prex[:-1]) + '.ifname' interface = check_output(['uci', 'get', ifname]).split('\n')[0] if interface in interface_list: remove_interface.append(interface) if net_prex[-1] == "proto" and str(option).find("dhcpv6") >= 0: ifname = '.'.join(net_prex[:-1]) + '.ifname' interface = check_output(['uci', 'get', ifname]).split('\n')[0] if interface in interface_list and interface in remove_interface: remove_interface.remove(interface) # remove proto mismatched interface # only return the interface which is configured with # proto provision or dhcpv6 and status must be up for interface in remove_interface: if interface in self.interfaces: continue interface_list.remove(interface) except Exception as error: InterfaceStatus.logger.error("Got exception: %s", str(error)) self.logger.debug("scan interfaces: up interface list %s, uci interface list %s", interface_list, uci_interfaces) interface_list = uci_interfaces.intersection(set(interface_list)) return list(interface_list)
def test_generate_event_66070405(self): SysTools.check_ping_result = SysToolsTest.check_ping_result self.assertTrue(SysTools.check_ping_result('127.0.0.1')) rootca = self.rootpath + '/rpd/ssd/testing/CABLELABS_ROOT_CA_PEM.CRT' driver = HalClientTest("SSD_Driver", "This is SSD Driver", "0.1.0", (MsgTypeSsd, ), None, rootca=rootca) driver.ssdParam['SsdServerAddress'] = '127.0.0.1' driver.ssdParam[ 'SsdFilename'] = self.rootpath + '/rpd/ssd/testing/codefile' driver.ssdParam['SsdServerPort'] = driver.TRANSPORT_TFTP driver.download_process(driver.TRANSPORT_TFTP, driver.ssdParam['SsdServerAddress'], driver.ssdParam['SsdFilename']) driver.ssdParam['SsdServerAddress'] = 'fe80::204:9fff:fe03:115/64' driver.connection_cleanup()
def get_interface_info(self, args=None): """ :return: interface info """ ret_info = [] try: agent_id = ProcessAgent.AGENTTYPE_INTERFACE_STATUS for ccap_core_id in CCAPCore.ccap_core_db: ccap_core = CCAPCore.ccap_core_db[ccap_core_id] status = ccap_core.agent_status[agent_id] intf = ccap_core.parameters[agent_id] is_ipv6 = Convert.is_valid_ipv6_address(ccap_core.ccap_core_network_address) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] local_ip = SysTools.get_ip_address(str(intf), family=family) value = {"Registered-Cores": ccap_core_id, "Interface": intf, "IP": local_ip, 'Status': status} ret_info.append(value) return True, ret_info except Exception as e: return False, str(e)
def rpd_dependency(self): rpd_rsyslogd = 0 rpd_redis_server = 0 rpd_platform = 0 while rpd_rsyslogd == 0 or rpd_redis_server == 0: for proc in psutil.process_iter(): # check rsyslogd try: if proc.name() == "rsyslogd": rpd_rsyslogd = 1 if proc.name() == "redis-server": rpd_redis_server = 1 except psutil.NoSuchProcess: # pragma: no cover pass time.sleep(3) while rpd_platform == 0 and not SysTools.is_vrpd(): # pragma: no cover if os.path.exists("/tmp/platform_ok"): rpd_platform = 1 time.sleep(3) print "rpd dependencies software is up" return
def __init__(self): if SysTools.is_system_openwrt(): self.path_bin = '/usr/lib/python2.7/site-packages/rpd/' else: # self.path_bin = './' self.path_bin = os.path.split(os.path.realpath(__file__))[0]
# limitations under the License. # import unittest import os import time from subprocess import Popen from signal import (signal, SIGUSR1, SIGUSR2, SIGTERM, SIGINT, SIGQUIT) from rpd.common.utils import SysTools from rpd.gpb.dhcp_pb2 import t_DhcpMessage, t_DhcpData from rpd.gpb.tpc_pb2 import t_TpcMessage # These imports are intended for the RPD, so we will enclose it by the # if statement if SysTools.is_system_openwrt(): from rpd.manager import Manager, ProcessInfo from glibc import (SIG_BLOCK, SIG_UNBLOCK, sigset_t, signalfd_siginfo, sigemptyset, sigaddset, sigprocmask) @unittest.skipUnless(SysTools.is_system_openwrt(), "Don't change system time on local machine") class TestManager(unittest.TestCase): @classmethod def setUpClass(cls): # Save original value for REBOOT_HOLD variable and block it for testing cls.reboot_hold_backup = os.getenv("PC_REBOOT_HOLD", 'false') os.environ['PC_REBOOT_HOLD'] = 'true' @classmethod
def test_sys_tools(self): SysTools.touch('/tmp/test_utils') SysTools.REBOOT_SKIP_FILES = ('/tmp/test_utils') self.assertFalse(SysTools.is_system_openwrt()) self.assertEqual(SysTools.get_mac_address('if_test'), "00:00:00:00:00:00") self.assertEqual(SysTools.get_sys_mac_address(), "00:00:00:00:00:00") with open('/tmp/test_mac', 'w') as f: f.write('01:02:03:04:05:06') f.close() SysTools.SYS_MAC_FILE = '/tmp/test_mac' self.assertEqual(SysTools.get_sys_mac_address(), "01:02:03:04:05:06") SysTools.SYS_MAC_FILE = '/etc/gshadow' self.assertEqual(SysTools.get_sys_mac_address(), "00:00:00:00:00:00") print(SysTools.get_host_name()) print(SysTools.get_ip_address('if_test')) if not SysTools.reboot(): SysTools.sys_failure_reboot('skip files exist') SysTools.external_reboot(('test', 'test')) if os.path.exists('/tmp/test_utils'): os.remove('/tmp/test_utils') # d = Dispatcher() # timestamp = time.time() + 1000 # SysTools.set_system_time(d, timestamp) # SysTools.touch('/etc/openwrt_release') # SysTools.set_system_time(d, timestamp) # time.sleep(2) # self.assertTrue(timestamp > time.time()) #should no permission to change system time # del d # if os.path.exists('/etc/openwrt_release'): # os.remove('/etc/openwrt_release') self.assertIsNotNone(SysTools.sys_up_time()) ret = SysTools.if_indextoname(1) print "ifname of index 1:", ret self.assertIsNotNone(ret) ret = SysTools.if_indextoname("test") self.assertIsNone(ret) ret = SysTools.if_indextoname(600) self.assertIsNone(ret) ret = SysTools.if_nametoindex("lo") print "ifindex of lo:", ret self.assertIsNotNone(ret) ret = SysTools.if_nametoindex(5) self.assertIsNone(ret) ret = SysTools.if_nametoindex("dafsd") self.assertIsNone(ret) ret = SysTools.is_if_oper_up('eth0') self.assertTrue(ret)
def dummy_rpd_cap(self, cap): cap.NumBdirPorts = 3 cap.NumDsRfPorts = 1 cap.NumUsRfPorts = 2 cap.NumTenGeNsPorts = 2 cap.NumOneGeNsPorts = 1 cap.NumDsScQamChannels = 158 cap.NumDsOfdmChannels = 1 cap.NumUsScQamChannels = 12 cap.NumUsOfdmaChannels = 4 cap.NumDsOob55d1Channels = 1 cap.NumUsOob55d1Channels = 3 cap.NumOob55d2Modules = 0 cap.NumUsOob55d2Demodulators = 0 cap.NumNdfChannels = 1 cap.NumNdrChannels = 1 cap.SupportsUdpEncap = 0 cap.NumDsPspFlows = 8 cap.NumUsPspFlows = 4 cap.RpdIdentification.VendorName = "Cisco" cap.RpdIdentification.VendorId = 9 cap.RpdIdentification.ModelNumber = "0" cap.RpdIdentification.DeviceMacAddress = SysTools.get_mac_address( "eth0") cap.RpdIdentification.CurrentSwVersion = "dummy_cur_sw_ver" cap.RpdIdentification.BootRomVersion = "dummy_boot_rom_version" cap.RpdIdentification.DeviceDescription = "RPD" cap.RpdIdentification.DeviceAlias = "RPD" cap.RpdIdentification.SerialNumber = "NA" cap.RpdIdentification.UsBurstReceiverVendorId = 4413 cap.RpdIdentification.UsBurstReceiverModelNumber = "NA" cap.RpdIdentification.UsBurstReceiverDriverVersion = "NA" cap.RpdIdentification.UsBurstReceiverSerialNumber = "00000000" cap.RpdIdentification.RpdRcpProtocolVersion = "1.0" cap.RpdIdentification.RpdRcpSchemaVersion = "1.0.8" cap.RpdIdentification.HwRevision = "NA" cap.RpdIdentification.AssetId = "NA" cap.RpdIdentification.VspSelector = "" cap.RpdIdentification.CurrentSwImageLastUpdate = Convert.pack_timestamp_to_string( 0) cap.RpdIdentification.CurrentSwImageName = "" cap.RpdIdentification.CurrentSwImageServer = "0.0.0.0" cap.PilotToneCapabilities.NumCwToneGens = 4 cap.PilotToneCapabilities.LowestCwToneFreq = 50000000 cap.PilotToneCapabilities.HighestCwToneFreq = 1218000000 cap.PilotToneCapabilities.MaxPowerDedCwTone = 100 cap.PilotToneCapabilities.QamAsPilot = True cap.PilotToneCapabilities.MinPowerDedCwTone = -330 cap.PilotToneCapabilities.MaxPowerQamCwTone = 90 cap.PilotToneCapabilities.MinPowerQamCwTone = -30 cap.DeviceLocation.DeviceLocationDescription = "NA" cap.DeviceLocation.GeoLocationLatitude = "+000000.0" cap.DeviceLocation.GeoLocationLongitude = "+0000000.0" cap.NumAsyncVideoChannels = 160 cap.SupportsFlowTags = True cap.SupportsFrequencyTilt = True cap.TiltRange = 0 cap.BufferDepthMonitorAlertSupport = 0 cap.BufferDepthConfigurationSupport = 0 cap.RpdUcdProcessingTime = 50 cap.RpdUcdChangeNullGrantTime = 50 cap.SupportMultiSectionTimingMerReporting = 0 cap.RdtiCapabilities.NumPtpPortsPerEnetPort = 11 cap.MaxDsPspSegCount = 10 cap.DirectDsFlowQueueMapping = 1 cap.DsSchedulerPhbIdList = "0 10 12 14 18 20 22 26 28 30 34 36 38 46" cap.RpdPendingEvRepQueueSize = 1000 cap.RpdLocalEventLogSize = 1000 cap.SupportsOpticalNodeRf = False cap.MaxDsFrequency = 1218000000 cap.MinDsFrequency = 5700000 cap.MaxBasePower = 0 cap.MinTiltValue = 0 cap.MinPowerAdjustScQam = 0 cap.MaxPowerAdjustScQam = 0 cap.MinPowerAdjustOfdm = 0 cap.MaxPowerAdjustOfdm = 0 cap.OfdmConfigurationCapabilities.RequiresOfdmaImDurationConfig = True
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.error( "Cannot process the event action for id %s, reason: id is not registered" % 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.error( "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 parameter = 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 parameter in self.interfaces: if ccap_core_id not in self.interfaces[parameter]["requester"]: self.interfaces[parameter]["requester"].append(ccap_core_id) else: # create a interface in self interfaces self.interfaces[parameter] = { "status": self.DOWN, "requester": [ccap_core_id, ], "flappingTimer": None, } # check interface status when first time created stats = net_if_stats() if parameter in stats and SysTools.is_if_oper_up(parameter): self.interfaces[parameter]['status'] = self.UP 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.interfaces[parameter]["status"]) return if event_action == protoDef.msg_event.STOP: if parameter in self.interfaces: if ccap_core_id in self.interfaces[parameter]["requester"]: self.interfaces[parameter]["requester"].remove(ccap_core_id) if len(self.interfaces[parameter]["requester"]) == 0 and \ self.interfaces[parameter]["status"] == self.DOWN: self.interfaces.pop(parameter) 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
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
def interrupt_handler(self, signum, frame): for interface in self.processes.keys(): SysTools.set_protocol(interface) sys.exit(0)