From 3782945e0a074cb28f82a512f7a9128797b298e3 Mon Sep 17 00:00:00 2001 From: mirabal Date: Thu, 9 Mar 2017 14:41:21 +0100 Subject: [PATCH] new_external_port and DB table adds - new_external_port() add to ovim, create a new external port and ensure the mapping correspondencer btw multimple sdn controllers - Add ofc_id, switch_mac columns to table "ports" & "resources_port" - Add foreign FK_port_ofc_id key btw "ports" and "ofcs" - Add foreign FK_resource_ofc_id key btw "resources_port" and "ofcs" Change-Id: I3a3603a17f415954fad7520573e4d6d434791f96 Signed-off-by: mirabal --- database_utils/migrate_vim_db.sh | 33 +++++++++++++++- httpserver.py | 1 + openvimd.py | 4 +- ovim.py | 66 ++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) diff --git a/database_utils/migrate_vim_db.sh b/database_utils/migrate_vim_db.sh index e465e68..8def5cb 100755 --- a/database_utils/migrate_vim_db.sh +++ b/database_utils/migrate_vim_db.sh @@ -180,7 +180,8 @@ DATABASE_TARGET_VER_NUM=0 [ $OPENVIM_VER_NUM -ge 5002 ] && DATABASE_TARGET_VER_NUM=10 #0.5.2 => 10 [ $OPENVIM_VER_NUM -ge 5004 ] && DATABASE_TARGET_VER_NUM=11 #0.5.4 => 11 [ $OPENVIM_VER_NUM -ge 5005 ] && DATABASE_TARGET_VER_NUM=12 #0.5.5 => 12 -[ $OPENVIM_VER_NUM -ge 5006 ] && DATABASE_TARGET_VER_NUM=13 #0.5.6 => 12 +[ $OPENVIM_VER_NUM -ge 5006 ] && DATABASE_TARGET_VER_NUM=13 #0.5.6 => 13 +[ $OPENVIM_VER_NUM -ge 5007 ] && DATABASE_TARGET_VER_NUM=14 #0.5.7 => 14 #TODO ... put next versions here function upgrade_to_1(){ @@ -525,6 +526,7 @@ function downgrade_from_12(){ echo "ALTER TABLE uuids CHANGE COLUMN used_at used_at ENUM('flavors', 'hosts', 'images', 'instances', 'nets', 'ports', 'tenants') NULL DEFAULT NULL COMMENT 'Table that uses this UUID' ;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '12';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } + function upgrade_to_13(){ echo " upgrade database from version 0.12 to version 0.13" echo " Create of_port_mapings table " @@ -553,6 +555,35 @@ function downgrade_from_13(){ echo "DROP TABLE of_port_mappings;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 echo "DELETE FROM schema_version WHERE version_int = '13';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 } + +function upgrade_to_14(){ + echo " upgrade database from version 0.13 to version 0.14" + echo " Add switch_mac, ofc_id colum to 'ports' and 'resources_port'" + echo "ALTER TABLE ports + ADD COLUMN switch_mac VARCHAR(18) NULL DEFAULT NULL AFTER switch_port, + ADD COLUMN ofc_id VARCHAR(36) NULL DEFAULT NULL AFTER switch_dpid, + ADD CONSTRAINT FK_port_ofc_id FOREIGN KEY (ofc_id) REFERENCES ofcs (uuid);"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 + echo "ALTER TABLE resources_port + ADD COLUMN switch_mac VARCHAR(18) NULL DEFAULT NULL AFTER switch_port, + ADD COLUMN ofc_id VARCHAR(36) NULL DEFAULT NULL AFTER switch_dpid, + ADD CONSTRAINT FK_resource_ofc_id FOREIGN KEY (ofc_id) REFERENCES ofcs (uuid);"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 + echo "INSERT INTO schema_version (version_int, version, openvim_ver, comments, date) VALUES (14, '0.14', '0.5.7', 'Add switch_mac, ofc_id colum to ports and resources_port tables', '2017-03-09');"| $DBCMD || ! echo "ERROR. Aborted!" || exit -1 +} + +function downgrade_from_14(){ + echo " downgrade database from version 0.14 to version 0.13" + echo " Delete switch_mac, ofc_id colum to 'ports'" + echo "ALTER TABLE ports + DROP COLUMN switch_mac, + DROP COLUMN ofc_id, + DROP FOREIGN KEY FK_port_ofc_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 + echo "ALTER TABLE resources_port + DROP COLUMN switch_mac, + DROP COLUMN ofc_id, + DROP FOREIGN KEY FK_resource_ofc_id;" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 + echo "DELETE FROM schema_version WHERE version_int = '14';" | $DBCMD || ! echo "ERROR. Aborted!" || exit -1 +} + #TODO ... put funtions here echo "db version = "${DATABASE_VER_NUM} diff --git a/httpserver.py b/httpserver.py index 7ef02f4..b15f7a1 100644 --- a/httpserver.py +++ b/httpserver.py @@ -2415,3 +2415,4 @@ def delete_of_port_mapping(region): except Exception as e: my.logger.error(str(e), exc_info=True) bottle.abort(HTTP_Bad_Request, str(e)) + diff --git a/openvimd.py b/openvimd.py index 5441f4d..8fe3843 100755 --- a/openvimd.py +++ b/openvimd.py @@ -30,9 +30,9 @@ and host controllers __author__ = "Alfonso Tierno" __date__ = "$10-jul-2014 12:07:15$" -__version__ = "0.5.6-r523" +__version__ = "0.5.7-r524" version_date = "Feb 2017" -database_version = "0.13" #expected database schema version +database_version = "0.14" #expected database schema version import httpserver import auxiliary_functions as af diff --git a/ovim.py b/ovim.py index 02f52e0..14bc50d 100644 --- a/ovim.py +++ b/ovim.py @@ -797,6 +797,72 @@ class ovim(): 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 region='{}', compute id='{}' and pci='{}'". + format(db_filter['region'], db_filter['compute_node'], db_filter['pci']), + 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 + del port_data['compute_node'] + del port_data['region'] + del port_data['pci'] + + result, uuid = self.db.new_row('ports', port_data, True, True) + if result > 0: + if 'net_id' in port_data and port_data['ofc_id'] in self.config['ofcs_thread']: + r, c = self.config['ofcs_thread'][port_data['ofc_id']].insert_task("update-net", port_data['net_id']) + if r < 0: + message = "Cannot insert a task for updating network '$s' %s", port_data['net_id'], c + self.logger.error(message) + raise ovimException(message, HTTP_Internal_Server_Error) + return uuid + else: + raise ovimException(str(uuid), -result) + 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') -- 2.17.1