X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2Fopenvim.git;a=blobdiff_plain;f=ovim.py;h=a1eaffab87e20288e43182dbf12a2ed3c8ebe1ce;hp=02114a18d1592c8a0a7b31bc20f02c324ebb489c;hb=ba1862b460afc13008f96b13252f2abcb075f138;hpb=580435e27aa04a40b8893145d181855d37965027 diff --git a/ovim.py b/ovim.py old mode 100644 new mode 100755 index 02114a1..a1eaffa --- a/ovim.py +++ b/ovim.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- ## @@ -28,17 +29,21 @@ Two thread will be launched, with normal and administrative permissions. __author__ = "Alfonso Tierno, Leonardo Mirabal" __date__ = "$06-Feb-2017 12:07:15$" +__version__ = "0.5.10-r526" +version_date = "Apr 2017" +database_version = "0.17" #expected database schema version import threading import vim_db import logging -import threading import imp import host_thread as ht import dhcp_thread as dt import openflow_thread as oft from netaddr import IPNetwork from jsonschema import validate as js_v, exceptions as js_e +import openflow_conn +import argparse HTTP_Bad_Request = 400 HTTP_Unauthorized = 401 @@ -90,16 +95,17 @@ class ovim(): def __init__(self, configuration): self.config = configuration - self.logger = logging.getLogger(configuration["logger_name"]) + self.logger_name = configuration.get("logger_name", "openvim") + self.logger = logging.getLogger(self.logger_name) self.db = None - self.db = self._create_database_connection() + self.db = self._create_database_connection() self.db_lock = None self.db_of = None self.of_test_mode = False def _create_database_connection(self): db = vim_db.vim_db((self.config["network_vlan_range_start"], self.config["network_vlan_range_end"]), - self.config['log_level_db']); + self.logger_name + ".db", self.config.get('log_level_db')) if db.connect(self.config['db_host'], self.config['db_user'], self.config['db_passwd'], self.config['db_name']) == -1: # self.logger.error("Cannot connect to database %s at %s@%s", self.config['db_name'], self.config['db_user'], @@ -109,6 +115,18 @@ class ovim(): self.config['db_host']) ) return db + @staticmethod + def get_version(): + return __version__ + + @staticmethod + def get_version_date(): + return version_date + + @staticmethod + def get_database_version(): + return database_version + @staticmethod def _check_dhcp_data_integrity(network): """ @@ -149,16 +167,18 @@ class ovim(): Start ovim services :return: """ + global database_version # if self.running_info: # return #TODO service can be checked and rebuild broken threads r = self.db.get_db_version() if r[0] < 0: raise ovimException("DATABASE is not a VIM one or it is a '0.0' version. Try to upgrade to version '{}' with "\ - "'./database_utils/migrate_vim_db.sh'".format(self.config["database_version"]) ) - elif r[1] != self.config["database_version"]: + "'./database_utils/migrate_vim_db.sh'".format(database_version) ) + elif r[1] != database_version: raise ovimException("DATABASE wrong version '{}'. Try to upgrade/downgrade to version '{}' with "\ - "'./database_utils/migrate_vim_db.sh'".format(r[1], self.config["database_version"]) ) - + "'./database_utils/migrate_vim_db.sh'".format(r[1], database_version) ) + self.logger.critical("Starting ovim server version: '{} {}' database version '{}'".format( + self.get_version(), self.get_version_date(), self.get_database_version())) # create database connection for openflow threads self.db_of = self._create_database_connection() self.config["db"] = self.db_of @@ -180,7 +200,7 @@ class ovim(): # check if this bridge is already used (present at database) for a network) used_bridge_nets = [] for brnet in self.config['bridge_nets']: - r, nets = self.db_of.get_table(SELECT=('uuid',), FROM='nets', WHERE={'provider': "bridge:" + brnet[0]}) + r, nets = self.db.get_table(SELECT=('uuid',), FROM='nets', WHERE={'provider': "bridge:" + brnet[0]}) if r > 0: brnet[3] = nets[0]['uuid'] used_bridge_nets.append(brnet[0]) @@ -192,7 +212,7 @@ class ovim(): # get nets used by dhcp if self.config.get("dhcp_server"): for net in self.config["dhcp_server"].get("nets", ()): - r, nets = self.db_of.get_table(SELECT=('uuid',), FROM='nets', WHERE={'name': net}) + r, nets = self.db.get_table(SELECT=('uuid',), FROM='nets', WHERE={'name': net}) if r > 0: self.config['dhcp_nets'].append(nets[0]['uuid']) @@ -217,7 +237,7 @@ class ovim(): host_develop_bridge_iface = self.config.get('development_bridge', None) # get host list from data base before starting threads - r, hosts = self.db_of.get_table(SELECT=('name', 'ip_name', 'user', 'uuid'), FROM='hosts', WHERE={'status': 'ok'}) + r, hosts = self.db.get_table(SELECT=('name', 'ip_name', 'user', 'uuid'), FROM='hosts', WHERE={'status': 'ok'}) if r < 0: raise ovimException("Cannot get hosts from database {}".format(hosts)) @@ -316,7 +336,7 @@ class ovim(): def _load_of_module(self, db_config): """ import python module for each SDN controller supported - :param default: SDN dn information + :param db_config: SDN dn information :return: Module """ if not db_config: @@ -326,8 +346,9 @@ class ovim(): try: if self.of_test_mode: - return oft.of_test_connector({"name": db_config['type'], "dpid": db_config['dpid'], - "of_debug": self.config['log_level_of']}) + return openflow_conn.OfTestConnector({"name": db_config['type'], + "dpid": db_config['dpid'], + "of_debug": self.config['log_level_of']}) temp_dict = {} if db_config: @@ -335,6 +356,8 @@ class ovim(): temp_dict['of_port'] = db_config['port'] temp_dict['of_dpid'] = db_config['dpid'] temp_dict['of_controller'] = db_config['type'] + temp_dict['of_user'] = db_config.get('user') + temp_dict['of_password'] = db_config.get('password') temp_dict['of_debug'] = self.config['log_level_of'] @@ -374,10 +397,11 @@ class ovim(): """ # create openflow thread - if 'of_controller_nets_with_same_vlan' in self.config: - ofc_net_same_vlan = self.config['of_controller_nets_with_same_vlan'] - else: - ofc_net_same_vlan = False + #if 'of_controller_nets_with_same_vlan' in self.config: + # ofc_net_same_vlan = self.config['of_controller_nets_with_same_vlan'] + #else: + # ofc_net_same_vlan = False + ofc_net_same_vlan = False thread = oft.openflow_thread(ofc_uuid, of_conn, of_test=self.of_test_mode, db=self.db_of, db_lock=self.db_lock, pmp_with_same_vlan=ofc_net_same_vlan, debug=self.config['log_level_of']) @@ -661,17 +685,25 @@ class ovim(): # if result > 0 and nbports>0 and 'admin_state_up' in network # and network['admin_state_up'] != network_old[0]['admin_state_up']: if result > 0: - r, c = self.config['of_thread'].insert_task("update-net", network_id) - if r < 0: - raise ovimException("Error while launching openflow rules %s" % c, HTTP_Internal_Server_Error) + + try: + if nbports: + self.net_update_ofc_thread(network_id) + except ovimException as e: + raise ovimException("Error while launching openflow rules in network '{}' {}" + .format(network_id, str(e)), HTTP_Internal_Server_Error) + except Exception as e: + raise ovimException("Error while launching openflow rules in network '{}' {}" + .format(network_id, str(e)), HTTP_Internal_Server_Error) + if self.config.get("dhcp_server"): if network_id in self.config["dhcp_nets"]: self.config["dhcp_nets"].remove(network_id) - if network.get("name", network_old["name"]) in self.config["dhcp_server"].get("nets", ()): + if network.get("name", network_old[0]["name"]) in self.config["dhcp_server"].get("nets", ()): self.config["dhcp_nets"].append(network_id) else: - net_bind = network.get("bind", network_old["bind"]) - if net_bind and net_bind[:7] == "bridge:" and net_bind[7:] in self.config["dhcp_server"].get( + net_bind = network.get("bind_type", network_old[0]["bind_type"]) + if net_bind and net_bind and net_bind[:7] == "bridge:" and net_bind[7:] in self.config["dhcp_server"].get( "bridge_ifaces", ()): self.config["dhcp_nets"].append(network_id) return network_id @@ -712,9 +744,8 @@ class ovim(): where_ = {} else: where_ = {"net_id": network_id} - result, content = self.db.get_table( - SELECT=("name", "net_id", "priority", "vlan_id", "ingress_port", "src_mac", "dst_mac", "actions"), + SELECT=("name", "net_id", "ofc_id", "priority", "vlan_id", "ingress_port", "src_mac", "dst_mac", "actions"), WHERE=where_, FROM='of_flows') if result < 0: @@ -744,29 +775,56 @@ class ovim(): if net["type"] != "ptp" and net["type"] != "data": result -= 1 continue - r, c = self.config['of_thread'].insert_task("update-net", net['uuid']) - if r < 0: - raise ovimException(str(c), -r) + + try: + self.net_update_ofc_thread(net['uuid']) + except ovimException as e: + raise ovimException("Error updating network'{}' {}".format(net['uuid'], str(e)), + HTTP_Internal_Server_Error) + except Exception as e: + raise ovimException("Error updating network '{}' {}".format(net['uuid'], str(e)), + HTTP_Internal_Server_Error) + return result - def delete_openflow_rules(self): + def delete_openflow_rules(self, ofc_id=None): """ To make actions over the net. The action is to delete ALL openflow rules :return: return operation result """ - # ignore input data - r, c = self.config['of_thread'].insert_task("clear-all") - if r < 0: - raise ovimException(str(c), -r) + + if not ofc_id: + if 'Default' in self.config['ofcs_thread']: + r, c = self.config['ofcs_thread']['Default'].insert_task("clear-all") + else: + raise ovimException("Default Openflow controller not not running", HTTP_Not_Found) + + elif ofc_id in self.config['ofcs_thread']: + r, c = self.config['ofcs_thread'][ofc_id].insert_task("clear-all") + + # ignore input data + if r < 0: + raise ovimException(str(c), -r) + else: + raise ovimException("Openflow controller not found with ofc_id={}".format(ofc_id), HTTP_Not_Found) return r - def get_openflow_ports(self): + def get_openflow_ports(self, ofc_id=None): """ Obtain switch ports names of openflow controller :return: Return flow ports in DB """ - data = {'ports': self.config['of_thread'].OF_connector.pp2ofi} - return data + if not ofc_id: + if 'Default' in self.config['ofcs_thread']: + conn = self.config['ofcs_thread']['Default'].OF_connector + else: + raise ovimException("Default Openflow controller not not running", HTTP_Not_Found) + + if ofc_id in self.config['ofcs_thread']: + conn = self.config['ofcs_thread'][ofc_id].OF_connector + else: + raise ovimException("Openflow controller not found with ofc_id={}".format(ofc_id), HTTP_Not_Found) + return conn.pp2ofi def get_ports(self, columns=None, filter={}, limit=None): # result, content = my.db.get_ports(where_) @@ -789,14 +847,132 @@ class ovim(): result, uuid = self.db.new_row('ports', port_data, True, True) if result > 0: if 'net_id' in port_data: - r, c = self.config['of_thread'].insert_task("update-net", port_data['net_id']) - if r < 0: - self.logger.error("Cannot insert a task for updating network '$s' %s", port_data['net_id'], c) - #TODO put network in error status + try: + self.net_update_ofc_thread(port_data['net_id']) + except ovimException as e: + raise ovimException("Cannot insert a task for updating network '{}' {}" + .format(port_data['net_id'], str(e)), HTTP_Internal_Server_Error) + except Exception as e: + raise ovimException("Cannot insert a task for updating network '{}' {}" + .format(port_data['net_id'], str(e)), HTTP_Internal_Server_Error) + return uuid else: raise ovimException(str(uuid), -result) + def new_external_port(self, port_data): + """ + Create new external port and check port mapping correspondence + :param port_data: port_data = { + 'region': 'datacenter region', + 'compute_node': 'compute node id', + 'pci': 'pci port address', + 'vlan': 'net vlan', + 'net_id': 'net id', + 'tenant_id': 'tenant id', + 'mac': 'switch mac', + 'name': 'port name' + 'ip_address': 'ip address - optional'} + :return: + """ + + port_data['type'] = 'external' + + if port_data.get('net_id'): + # check that new net has the correct type + result, new_net = self.db.check_target_net(port_data['net_id'], None, 'external') + if result < 0: + raise ovimException(str(new_net), -result) + # insert in data base + db_filter = {} + + if port_data.get('region'): + db_filter['region'] = port_data['region'] + if port_data.get('pci'): + db_filter['pci'] = port_data['pci'] + if port_data.get('compute_node'): + db_filter['compute_node'] = port_data['compute_node'] + + columns = ['ofc_id', 'switch_dpid', 'switch_port', 'switch_mac', 'pci'] + port_mapping_data = self.get_of_port_mappings(columns, db_filter) + + if not len(port_mapping_data): + raise ovimException("No port mapping founded for '{}'".format(str(db_filter)), + HTTP_Not_Found) + elif len(port_mapping_data) > 1: + raise ovimException("Wrong port data was given, please check pci, region & compute id data", + HTTP_Conflict) + + port_data['ofc_id'] = port_mapping_data[0]['ofc_id'] + port_data['switch_dpid'] = port_mapping_data[0]['switch_dpid'] + port_data['switch_port'] = port_mapping_data[0]['switch_port'] + port_data['switch_mac'] = port_mapping_data[0]['switch_mac'] + + # remove from compute_node, region and pci of_port_data to adapt to 'ports' structure + if 'region' in port_data: + del port_data['region'] + if 'pci' in port_data: + del port_data['pci'] + if 'compute_node' in port_data: + del port_data['compute_node'] + + result, uuid = self.db.new_row('ports', port_data, True, True) + if result > 0: + try: + self.net_update_ofc_thread(port_data['net_id'], port_data['ofc_id']) + except ovimException as e: + raise ovimException("Cannot insert a task for updating network '{}' {}". + format(port_data['net_id'], str(e)), HTTP_Internal_Server_Error) + except Exception as e: + raise ovimException("Cannot insert a task for updating network '{}' {}" + .format(port_data['net_id'], e), HTTP_Internal_Server_Error) + return uuid + else: + raise ovimException(str(uuid), -result) + + def net_update_ofc_thread(self, net_id, ofc_id=None, switch_dpid=None): + """ + Insert a update net task by net id or ofc_id for each ofc thread + :param net_id: network id + :param ofc_id: openflow controller id + :param switch_dpid: switch dpid + :return: + """ + if not net_id: + raise ovimException("No net_id received", HTTP_Internal_Server_Error) + + r = -1 + c = 'No valid ofc_id or switch_dpid received' + + if not ofc_id: + ports = self.get_ports(filter={"net_id": net_id}) + for port in ports: + port_ofc_id = port.get('ofc_id', None) + if port_ofc_id: + ofc_id = port['ofc_id'] + switch_dpid = port['switch_dpid'] + break + #TODO if not ofc_id: look at database table ofcs + + + # If no ofc_id found it, default ofc_id is used. + if not ofc_id and not switch_dpid: + ofc_id = "Default" + + if ofc_id and ofc_id in self.config['ofcs_thread']: + r, c = self.config['ofcs_thread'][ofc_id].insert_task("update-net", net_id) + elif switch_dpid: + + ofcs_dpid_list = self.config['ofcs_thread_dpid'] + for ofc_t in ofcs_dpid_list: + if switch_dpid in ofc_t: + r, c = ofc_t[switch_dpid].insert_task("update-net", net_id) + + if r < 0: + message = "Cannot insert a task for updating network '{}', {}".format(net_id, c) + self.logger.error(message) + raise ovimException(message, HTTP_Internal_Server_Error) + def delete_port(self, port_id): # Look for the previous port data result, ports = self.db.get_table(WHERE={'uuid': port_id, "type": "external"}, FROM='ports') @@ -812,9 +988,16 @@ class ovim(): network = ports[0].get('net_id', None) if network: # change of net. - r, c = self.config['of_thread'].insert_task("update-net", network) - if r < 0: - self.logger.error("Cannot insert a task for updating network '$s' %s", network, c) + + try: + self.net_update_ofc_thread(network, ofc_id=ports[0]["ofc_id"], switch_dpid=ports[0]["switch_dpid"]) + except ovimException as e: + raise ovimException("Cannot insert a task for delete network '{}' {}".format(network, str(e)), + HTTP_Internal_Server_Error) + except Exception as e: + raise ovimException("Cannot insert a task for delete network '{}' {}".format(network, str(e)), + HTTP_Internal_Server_Error) + return content def edit_port(self, port_id, port_data, admin=True): @@ -861,14 +1044,20 @@ class ovim(): # insert in data base if result >= 0: result, content = self.db.update_rows('ports', port_data, WHERE={'uuid': port_id}, log=False) + port.update(port_data) # Insert task to complete actions if result > 0: for net_id in nets: - r, v = self.config['of_thread'].insert_task("update-net", net_id) - if r < 0: - self.logger.error("Error updating network '{}' {}".format(r,v)) - # TODO Do something if fails + try: + self.net_update_ofc_thread(net_id, port["ofc_id"], switch_dpid=port["switch_dpid"]) + except ovimException as e: + raise ovimException("Error updating network'{}' {}".format(net_id, str(e)), + HTTP_Internal_Server_Error) + except Exception as e: + raise ovimException("Error updating network '{}' {}".format(net_id, str(e)), + HTTP_Internal_Server_Error) + if host_id: r, v = self.config['host_threads'][host_id].insert_task("edit-iface", port_id, old_net, new_net) if r < 0: @@ -1073,6 +1262,63 @@ class ovim(): else: raise ovimException(str(content), -result) + def set_of_port_mapping(self, of_maps, ofc_id=None, switch_dpid=None, region=None): + """ + Create new port mapping entry + :param of_maps: List with port mapping information + # maps =[{"ofc_id": ,"region": datacenter region,"compute_node": compute uuid,"pci": pci adress, + "switch_dpid": swith dpid,"switch_port": port name,"switch_mac": mac}] + :param ofc_id: ofc id + :param switch_dpid: switch dpid + :param region: datacenter region id + :return: + """ + + for map in of_maps: + if ofc_id: + map['ofc_id'] = ofc_id + if switch_dpid: + map['switch_dpid'] = switch_dpid + if region: + map['region'] = region + + for of_map in of_maps: + result, uuid = self.db.new_row('of_port_mappings', of_map, True) + if result > 0: + of_map["uuid"] = uuid + else: + raise ovimException(str(uuid), -result) + return of_maps + + def clear_of_port_mapping(self, db_filter={}): + """ + Clear port mapping filtering using db_filter dict + :param db_filter: Parameter to filter during remove process + :return: + """ + result, content = self.db.delete_row_by_dict(FROM='of_port_mappings', WHERE=db_filter) + # delete_row_by_key + if result >= 0: + return content + else: + raise ovimException("Error deleting of_port_mappings with filter='{}'".format(str(db_filter)), + HTTP_Internal_Server_Error) + + def get_of_port_mappings(self, column=None, db_filter=None, db_limit=None): + """ + Retrive port mapping from DB + :param column: + :param db_filter: + :return: + """ + result, content = self.db.get_table(SELECT=column, WHERE=db_filter, FROM='of_port_mappings', LIMIT=db_limit) + + if result < 0: + self.logger.error("get_of_port_mappings Error %d %s", result, content) + raise ovimException(str(content), -result) + else: + return content + def get_dhcp_controller(self): """ Create an host_thread object for manage openvim controller and not create a thread for itself @@ -1090,8 +1336,8 @@ class ovim(): host_develop_mode = True if self.config['mode'] == 'development' else False dhcp_host = ht.host_thread(name='openvim_controller', user=ovs_controller_user, host=controller_ip, - db=self.config['db'], - db_lock=self.config['db_lock'], test=host_test_mode, + db=self.db_of, + db_lock=self.db_lock, test=host_test_mode, image_path=self.config['image_path'], version=self.config['version'], host_id='openvim_controller', develop_mode=host_develop_mode, develop_bridge_iface=bridge_ifaces) @@ -1122,4 +1368,15 @@ class ovim(): controller_host.create_dhcp_interfaces(vlan, first_ip, dhcp_netmask) controller_host.launch_dhcp_server(vlan, ip_range, dhcp_netmask, dhcp_path, gateway) +if __name__ == "__main__": + + parser = argparse.ArgumentParser() + parser.add_argument("-v","--version", help="show ovim library version", action="store_true") + parser.add_argument("--database-version", help="show required database version", action="store_true") + args = parser.parse_args() + if args.version: + print ('openvimd version {} {}'.format(ovim.get_version(), ovim.get_version_date())) + print ('(c) Copyright Telefonica') + elif args.database_version: + print ('required database version: {}'.format(ovim.get_database_version()))