def do_automatic_reset(self, cmdline): #delete floating IPs self.delete_floating_ips() #delete VMs self.delete_instances() #clear Database self.clearDB() #GET physical network data from SDN controller self.get_physical_hosts() #read VNC platform DB from utils import config_json_read data = config_json_read(host_path) num_physical = len(data['Physical_hosts']) #set IP allocation pool on OpenStack neutron self.set_allocation_pool(data) #create servers on Openstack nova self.create_server(num_physical) #Update latest VM info in database self.update_VM_info() print "Successfully Reset" print "Saved following in Database" from print_results import print_physical_hosts, print_Full_VMs, print_Partial_VMs print_physical_hosts(host_path) print_Full_VMs(host_path) print_Partial_VMs(host_path)
def get_physical_hosts(self): from utils import get_hosts host_data = get_hosts(self.controller_IP, "8080", "") if host_data: count = 1 print "Receiving physical network data from SDN controller ..." for host in host_data['hostConfig']: IP = host['networkAddress'] MAC = host['dataLayerAddress'] Service = 'Streaming_' + str(count) Status = 'Active' physical = { 'MAC': MAC, 'IP': IP, 'Service': Service, 'Status': Status } self.physical_hosts.append(physical) print "Get physical host:" + str(physical) count = count + 1 else: print "No physical network info. Check SDN controller" from utils import config_json_read data = config_json_read(host_path) data['Physical_hosts'] = self.physical_hosts #write hosts information in host.json file from utils import config_json_write config_json_write(host_path, data) print "Received host information"
def get_physical_hosts(self): from utils import get_hosts host_data = get_hosts(self.controller_IP, "8080","") if host_data: count = 1 print "Receiving physical network data from SDN controller ..." for host in host_data['hostConfig']: IP = host['networkAddress'] MAC = host['dataLayerAddress'] Service = 'Streaming_'+str(count) Status = 'Active' physical ={'MAC':MAC, 'IP':IP, 'Service':Service, 'Status':Status} self.physical_hosts.append(physical) print "Get physical host:"+str(physical) count = count + 1 else: print "No physical network info. Check SDN controller" from utils import config_json_read data = config_json_read(host_path) data['Physical_hosts']=self.physical_hosts #write hosts information in host.json file from utils import config_json_write config_json_write(host_path,data) print "Received host information"
def do_alias(self, line): from utils import refresh_alias_json_db from utils import config_json_read from utils import config_json_write from global_value import CONF_PATH #read and mode and write json db file_path = CONF_PATH+"/"+self.controller_uid+".json" try : read_json = config_json_read(file_path) for dpid_list in read_json['controllers']['switchs']: if dpid_list['dpid'] == self.dpid: #line command check dpid_list['sw_alias'] = line print "change switch alias" break print self.hp_sw_alias_db config_json_write(file_path, read_json) refresh_alias_json_db(self.ip, self.port, self.token, None, None) self.sw_name = line except Exception: traceback.print_exc() return None #check line self.prompt = "Controller("+line+"): " print "change datapath name",line
def return_sw_dpid(file_path, switch_alias): from utils import config_json_read sw_dpid = str() switches = config_json_read(file_path) for dpid in switches.keys(): if switches[dpid] == switch_alias: sw_dpid = dpid break return sw_dpid
def clearDB(self): from utils import config_json_read data = config_json_read(host_path) data['F_VMs'] = '' data['P_VMs'] = '' data['Physical_hosts'] = '' from utils import config_json_write config_json_write(host_path, data) print "Cleared all data in database"
def print_physical_hosts(file_path): from utils import config_json_read data = config_json_read(file_path) print "--- Physical host info ---" for host in data['Physical_hosts']: print("-"*45) print "| IP address : %-25s |" %host['IP'] print "| MAC address : %-25s |" %host['MAC'] print "| Service : %-25s |" %host['Service'] print "| Status : %-25s |" %host['Status'] print("-"*45)
def print_Partial_VMs(file_path): from utils import config_json_read data = config_json_read(file_path) print "--- Partial virtual machine info ---" for host in data['P_VMs']: print("-"*45) print "| IP address : %-25s |" %host['IP'] print "| MAC address : %-25s |" %host['MAC'] print "| Service : %-25s |" %host['Service'] print "| Status : %-25s |" %host['Status'] print("-"*45)
def print_specific_switch(data, file_path): from utils import config_json_read switches = config_json_read(file_path) print("-"*47) print "| switch name : %-25s |" %switches[data['id']] print "| switch dpid : %-25s |" %data['id'] print "| role : %-25s |" %data['role'] print "| mfr : %-25s |" %data['mfr'] print "| switch_type : %-25s |" %data['hw'] print "| switch_version : %-25s |" %data['sw'] print "| protocol : %-25s |" %data['annotations']['protocol'] print "| channel id : %-25s |" %data['annotations']['channelId'] print("-"*47)
def print_all_switches(data, x, file_path): from utils import config_json_read switches = config_json_read(file_path) if data['devices'][x]['role'] == 'MASTER': print("-"*47) print "| switch name : %-25s |" %switches[data['devices'][x]['id']] print "| switch dpid : %-25s |" %data['devices'][x]['id'] print "| role : %-25s |" %data['devices'][x]['role'] print "| mfr : %-25s |" %data['devices'][x]['mfr'] print "| switch_type : %-25s |" %data['devices'][x]['hw'] print "| switch_version : %-25s |" %data['devices'][x]['sw'] print "| protocol : %-25s |" %data['devices'][x]['annotations']['protocol'] print "| channel id : %-25s |" %data['devices'][x]['annotations']['channelId'] print("-"*47)
def update_VM_info(self): print "Updating latest VM data in database ..." for port in self.neut_client.list_ports()['ports']: mac_address = port['mac_address'] fixed_ip = port['fixed_ips'][0]['ip_address'] self.virtual_hosts[fixed_ip] = mac_address ips = self.nova_client.floating_ips.list() length = len(ips) f_count = 1 p_count = 1 n = 1 for x in range(length): fixed_ip = ips[x].fixed_ip net = fixed_ip.split(".") subnet = net[2] if subnet == "20": floating_ip = ips[x].ip mac_floating = self.virtual_hosts[floating_ip] service = 'Streaming_'+str(p_count) status = 'Active' vm = {'IP':floating_ip, 'MAC':mac_floating, 'Service':service, 'Status':status, "Fixed_IP":fixed_ip} self.P_VM.append(vm) if n == 2: p_count = p_count + 1 n = 1 else: n = n + 1 elif subnet == "10": floating_ip = ips[x].ip mac_floating = self.virtual_hosts[floating_ip] service = 'Streaming_'+str(f_count) status = 'Active' vm = {'IP':floating_ip, 'MAC':mac_floating, 'Service':service, 'Status':status, "Fixed_IP":fixed_ip} self.F_VM.append(vm) f_count = f_count + 1 from utils import config_json_read data = config_json_read(host_path) data['F_VMs'] = self.F_VM data['P_VMs'] = self.P_VM from utils import config_json_write config_json_write(host_path, data) print "Successfully updated."
def controller_db_init(ip, port, token): file_path = global_value.CONF_PATH + "/" + ip + ".json" # logger.debug(file_path) import os.path controller_db = dict() if os.path.isfile(file_path): controller_db = utils.config_json_read(file_path) if controller_db == None or len(controller_db) == 0: open_flie = open(file_path, "a+") controller_db = {"ip": ip, "con_alias": ip, "sw_alias": {}} controller_db["sw_alias"] = set_sw_alias(ip, port, token, controller_db.get("sw_alias")) utils.config_json_write(file_path, controller_db) return controller_db
def search_controller(self, ip, port, c_name): from utils import config_json_read from utils import config_json_write file_path = CONF_PATH + REG_CONF_FILE try: read_json = config_json_read(file_path) for index in range(len(read_json["controllers"])): if read_json["controllers"][index]["port"] == port and read_json["controllers"][index]["ip"] == ip: read_json["controllers"][index]["name"] = c_name print read_json config_json_write(file_path, read_json) except Exception: traceback.print_exc() return None
def update_controller_db(file_path, ip, port, token): from utils import config_json_read, config_json_write from onos_api.onos_controller_api import onos_api_switches latest_switches = {} latest_sw_data = onos_api_switches(ip, port, token)["devices"] switches = config_json_read(file_path) for switch in latest_sw_data: if switch["role"] == "MASTER": try: sw_alias = switches[switch["id"]] except KeyError: sw_alias = switch["id"] finally: latest_switches[switch["id"]] = sw_alias config_json_write(file_path, latest_switches)
def do_alias(self, cmd_line): from utils import config_json_read, config_json_write from global_value import CONF_PATH, REG_CONF_FILE reg_con_path = CONF_PATH+REG_CONF_FILE if cmd_line: try: read_con_json = config_json_read(reg_con_path) for controller in read_con_json['controllers']: if controller['ip'] == self.ip: controller['name'] = cmd_line config_json_write(reg_con_path, read_con_json) self.prompt = cmd_line self.prompt = "Controller("+cmd_line+"): " except Exception: traceback.print_exc() return None
def do_alias(self, cmd_line): from utils import config_json_read, config_json_write from onos_api.onos_utils import return_sw_names if cmd_line: switch_names = return_sw_names(self.file_path) if cmd_line in switch_names: print"Duplicated name, please input another name!!!" else: try: switches = config_json_read(self.file_path) switches[self.sw_dpid] = cmd_line config_json_write(self.file_path, switches) self.prompt = "#switch("+cmd_line+"): " except Exception: traceback.print_exc() return None else: print "Please input switch alias"
def set_alias(self, input_alias): file_path = global_value.CONF_PATH +"/" + self.ip + ".json" controller_db = utils.config_json_read(file_path) sw_alias_db = controller_db["sw_alias"] temp_dpid = sw_alias_db.get(input_alias) if temp_dpid: yn = "you want replace \"" + input_alias + "\":\"" + temp_dpid + "\" -> \"" + input_alias + "\":\"" + self.dpid + "\" ? (y/n) # " if not yn.lower() == "y" : print "cancle replace" return None sw_alias_db[temp_dpid] = temp_dpid sw_alias_db.pop(self.alias) sw_alias_db[input_alias] = self.dpid utils.config_json_write(file_path, controller_db) self.prompt = "Switch(" + input_alias + ") # "
def do_alias(self, cmd_line): from utils import refresh_alias_json_db from utils import config_json_write from utils import config_json_read from global_value import CONF_PATH reg_cont = Registered_controller() file_path = CONF_PATH+"/"+self.controller_uid+".json" try : read_json = config_json_read(file_path) read_json['controllers']['con_alias'] = cmd_line config_json_write(file_path, read_json) refresh_alias_json_db(self.ip, self.port, self.token, None, None) self.prompt = cmd_line self.prompt = "Controller("+cmd_line+"): " reg_cont.search_controller(self.ip, self.port, cmd_line) except Exception: traceback.print_exc() return None
def delete_instances(self): from utils import config_json_read data = config_json_read(host_path) instances = self.nova_client.servers.list() num_instances = len(instances) if num_instances == 0: print "No VM to delete" else: print "Deleting all VMs ..." f_num = len(data['F_VMs']) p_num = len(data['P_VMs']) if f_num > 0: for x in range(f_num): try: instance = self.nova_client.servers.find(name='F-VM' + str(x)) instance.delete() finally: print("Deleted F-VM" + str(x)) else: print "No full VMs" if p_num > 0: for n in range(p_num): try: instance = self.nova_client.servers.find(name='P-VM' + str(n)) instance.delete() finally: print("Deleted P-VM" + str(n)) else: print "No partial VMs" print "Deleted all VMs"
def delete_instances(self): from utils import config_json_read data = config_json_read(host_path) instances = self.nova_client.servers.list() num_instances = len(instances) if num_instances == 0: print "No VM to delete" else: print "Deleting all VMs ..." f_num = len(data['F_VMs']) p_num = len(data['P_VMs']) if f_num > 0: for x in range(f_num): try: instance = self.nova_client.servers.find(name='F-VM'+str(x)) instance.delete() finally: print("Deleted F-VM"+str(x)) else: print "No full VMs" if p_num > 0: for n in range(p_num): try: instance = self.nova_client.servers.find(name='P-VM'+str(n)) instance.delete() finally: print("Deleted P-VM"+str(n)) else: print "No partial VMs" print "Deleted all VMs"
def return_sw_names(file_path): from utils import config_json_read switches = config_json_read(file_path) return switches.values()
def update_VM_info(self): print "Updating latest VM data in database ..." for port in self.neut_client.list_ports()['ports']: mac_address = port['mac_address'] fixed_ip = port['fixed_ips'][0]['ip_address'] self.virtual_hosts[fixed_ip] = mac_address ips = self.nova_client.floating_ips.list() length = len(ips) f_count = 1 p_count = 1 n = 1 for x in range(length): fixed_ip = ips[x].fixed_ip net = fixed_ip.split(".") subnet = net[2] if subnet == "20": floating_ip = ips[x].ip mac_floating = self.virtual_hosts[floating_ip] service = 'Streaming_' + str(p_count) status = 'Active' vm = { 'IP': floating_ip, 'MAC': mac_floating, 'Service': service, 'Status': status, "Fixed_IP": fixed_ip } self.P_VM.append(vm) if n == 2: p_count = p_count + 1 n = 1 else: n = n + 1 elif subnet == "10": floating_ip = ips[x].ip mac_floating = self.virtual_hosts[floating_ip] service = 'Streaming_' + str(f_count) status = 'Active' vm = { 'IP': floating_ip, 'MAC': mac_floating, 'Service': service, 'Status': status, "Fixed_IP": fixed_ip } self.F_VM.append(vm) f_count = f_count + 1 from utils import config_json_read data = config_json_read(host_path) data['F_VMs'] = self.F_VM data['P_VMs'] = self.P_VM from utils import config_json_write config_json_write(host_path, data) print "Successfully updated."