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 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 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 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 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 process_event_action(self, action): """Process the request from the client. Currently, we will support the following event: start/check status/stop. :param action: the protobuf object, which contains the event information. :return: the function will return an message to remote, success or fail. """ ccap_id = action.ccap_core_id event_action = action.action self.logger.debug("CCAP core[%s] issued an event action:%s", ccap_id, action) if ccap_id not in self.ccap_cores: self.logger.warn("Cannot process the event action for id %s, reason: id is not registered" % ccap_id) self._send_event_notification(ccap_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_id) # return error self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, "Parameter is not set") return parameter = action.parameter # parameter is a string, we need to parse the string parameters = parameter.split(";") interface = parameters[0].strip() ccap_ip = parameters[1].strip() is_ipv6 = Convert.is_valid_ipv6_address(ccap_ip) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] # Get the interface IP address ip_addr = SysTools.get_ip_address(str(interface), family) if event_action == protoDef.msg_event.START or event_action == protoDef.msg_event.CHECKSTATUS: if ip_addr is None: self.logger.warn( "Cannot start/check l2tp status for id %s, " "reason:cannot find the ip addr for interface %s" % (ccap_id, interface)) self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, "cannot find the ip addr for interface %s" % interface) return # check if we have start the l2tp address for this IP address ret, reason = self.l2tp_dispatcher.register_local_address(ip_addr) if not ret: self.logger.warn("Cannot start/check l2tp status for id %s, " "reason:%s" % (ccap_id, reason)) self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, reason) return # check if we are in the requester list, if yes, we just # send a current status to it if (ip_addr, ccap_ip) not in self.l2tp_status: # create a interface in self interfaces self.l2tp_status[(ip_addr, ccap_ip)] = { "status": self.DOWN, "lastChangeTime": time(), "ccap_core_id": ccap_id, } self.l2tp_dispatcher.register_remote_address(ccap_ip) # check the l2tp status for k in L2tpConnection.ConnectionDb: connection = L2tpConnection.ConnectionDb[k] if connection.remoteAddr == ccap_ip and connection.localAddr == ip_addr: self.l2tp_status[(ip_addr, ccap_ip)]['status'] = self.UP break self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.OK, reason="Id has been issue this action, send current status to you", result=self.l2tp_status[(ip_addr, ccap_ip)]["status"]) return if event_action == protoDef.msg_event.STOP: for k in L2tpConnection.ConnectionDb: connection = L2tpConnection.ConnectionDb[k] """ we only get a single connection for the same remoteAddr, Do not check the local ip address in case ip_addr is none for now """ if connection.remoteAddr == ccap_ip: ip_addr = connection.localAddr connection.StopConnection() break if (ip_addr, ccap_ip) in self.l2tp_status: self.l2tp_status.pop((ip_addr, ccap_ip)) self.l2tp_dispatcher.unregister_remote_address(ccap_ip) if len(self.l2tp_status) == 0: ret, reason = self.l2tp_dispatcher.request_unregister( {"unregType": "localaddress", "value": ip_addr}) if not ret: self.logger.warn( "l2tp stop CCAP core[%s], unregister ip %s failed for %s", ccap_id, ip_addr, reason) self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.OK, reason="Successful stop event.") else: self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, reason="Cannot stop event since can not find it.") return