+ def new_of_controller(self, ofc_data):
+ """
+ Create a new openflow controller into DB
+ :param ofc_data: Dict openflow controller data
+ :return: openflow controller dpid
+ """
+
+ result, ofc_uuid = self.db.new_row('ofcs', ofc_data, True, True)
+ if result < 0:
+ raise ovimException("New ofc Error %s" % ofc_uuid, HTTP_Internal_Server_Error)
+
+ ofc_data['uuid'] = ofc_uuid
+ of_conn = self._load_of_module(ofc_data)
+ self._create_ofc_task(ofc_uuid, ofc_data['dpid'], of_conn)
+
+ return ofc_uuid
+
+ def edit_of_controller(self, of_id, ofc_data):
+ """
+ Edit an openflow controller entry from DB
+ :return:
+ """
+ if not ofc_data:
+ raise ovimException("No data received during uptade OF contorller", http_code=HTTP_Internal_Server_Error)
+
+ old_of_controller = self.show_of_controller(of_id)
+
+ if old_of_controller:
+ result, content = self.db.update_rows('ofcs', ofc_data, WHERE={'uuid': of_id}, log=False)
+ if result >= 0:
+ return ofc_data
+ else:
+ raise ovimException("Error uptating OF contorller with uuid {}".format(of_id),
+ http_code=-result)
+ else:
+ raise ovimException("Error uptating OF contorller with uuid {}".format(of_id),
+ http_code=HTTP_Internal_Server_Error)
+
+ def delete_of_controller(self, of_id):
+ """
+ Delete an openflow controller from DB.
+ :param of_id: openflow controller dpid
+ :return:
+ """
+
+ ofc = self.show_of_controller(of_id)
+
+ result, content = self.db.delete_row("ofcs", of_id)
+ if result < 0:
+ raise ovimException("Cannot delete ofc from database: {}".format(content), http_code=-result)
+ elif result == 0:
+ raise ovimException("ofc {} not found ".format(content), http_code=HTTP_Not_Found)
+
+ ofc_thread = self.config['ofcs_thread'][of_id]
+ del self.config['ofcs_thread'][of_id]
+ for ofc_th in self.config['ofcs_thread_dpid']:
+ if ofc['dpid'] in ofc_th:
+ self.config['ofcs_thread_dpid'].remove(ofc_th)
+
+ ofc_thread.insert_task("exit")
+ #ofc_thread.join()
+
+ return content
+
+ def show_of_controller(self, uuid):
+ """
+ Show an openflow controller by dpid from DB.
+ :param db_filter: List with where query parameters
+ :return:
+ """
+
+ result, content = self.db.get_table(FROM='ofcs', WHERE={"uuid": uuid}, LIMIT=100)
+
+ if result == 0:
+ raise ovimException("Openflow controller with uuid '{}' not found".format(uuid),
+ http_code=HTTP_Not_Found)
+ elif result < 0:
+ raise ovimException("Openflow controller with uuid '{}' error".format(uuid),
+ http_code=HTTP_Internal_Server_Error)
+ return content[0]
+
+ def get_of_controllers(self, columns=None, db_filter={}, limit=None):
+ """
+ Show an openflow controllers from DB.
+ :param columns: List with SELECT query parameters
+ :param db_filter: List with where query parameters
+ :param limit: result Limit
+ :return:
+ """
+ result, content = self.db.get_table(SELECT=columns, FROM='ofcs', WHERE=db_filter, LIMIT=limit)
+
+ if result < 0:
+ raise ovimException(str(content), -result)
+
+ return content
+
+ def get_tenants(self, columns=None, db_filter={}, limit=None):
+ """
+ Retrieve tenant list from DB
+ :param columns: List with SELECT query parameters
+ :param db_filter: List with where query parameters
+ :param limit: result limit
+ :return:
+ """
+ result, content = self.db.get_table(FROM='tenants', SELECT=columns, WHERE=db_filter, LIMIT=limit)
+ if result < 0:
+ raise ovimException('get_tenatns Error {}'.format(str(content)), -result)
+ else:
+ convert_boolean(content, ('enabled',))
+ return content
+
+ def show_tenant_id(self, tenant_id):
+ """
+ Get tenant from DB by id
+ :param tenant_id: tenant id
+ :return:
+ """
+ result, content = self.db.get_table(FROM='tenants', SELECT=('uuid', 'name', 'description', 'enabled'),
+ WHERE={"uuid": tenant_id})
+ if result < 0:
+ raise ovimException(str(content), -result)
+ elif result == 0:
+ raise ovimException("tenant with uuid='{}' not found".format(tenant_id), HTTP_Not_Found)
+ else:
+ convert_boolean(content, ('enabled',))
+ return content[0]
+
+ def new_tentant(self, tenant):
+ """
+ Create a tenant and store in DB
+ :param tenant: Dictionary with tenant data
+ :return: the uuid of created tenant. Raise exception upon error
+ """
+
+ # insert in data base
+ result, tenant_uuid = self.db.new_tenant(tenant)
+
+ if result >= 0:
+ return tenant_uuid
+ else:
+ raise ovimException(str(tenant_uuid), -result)
+
+ def delete_tentant(self, tenant_id):
+ """
+ Delete a tenant from the database.
+ :param tenant_id: Tenant id
+ :return: delete tenant id
+ """
+
+ # check permissions
+ r, tenants_flavors = self.db.get_table(FROM='tenants_flavors', SELECT=('flavor_id', 'tenant_id'),
+ WHERE={'tenant_id': tenant_id})
+ if r <= 0:
+ tenants_flavors = ()
+ r, tenants_images = self.db.get_table(FROM='tenants_images', SELECT=('image_id', 'tenant_id'),
+ WHERE={'tenant_id': tenant_id})
+ if r <= 0:
+ tenants_images = ()
+
+ result, content = self.db.delete_row('tenants', tenant_id)
+ if result == 0:
+ raise ovimException("tenant '%s' not found" % tenant_id, HTTP_Not_Found)
+ elif result > 0:
+ for flavor in tenants_flavors:
+ self.db.delete_row_by_key("flavors", "uuid", flavor['flavor_id'])
+ for image in tenants_images:
+ self.db.delete_row_by_key("images", "uuid", image['image_id'])
+ return content
+ else:
+ raise ovimException("Error deleting tenant '%s' " % tenant_id, HTTP_Internal_Server_Error)
+
+ def edit_tenant(self, tenant_id, tenant_data):
+ """
+ Update a tenant data identified by tenant id
+ :param tenant_id: tenant id
+ :param tenant_data: Dictionary with tenant data
+ :return:
+ """
+
+ # Look for the previous data
+ result, tenant_data_old = self.db.get_table(FROM='tenants', WHERE={'uuid': tenant_id})
+ if result < 0:
+ raise ovimException("Error updating tenant with uuid='{}': {}".format(tenant_id, tenant_data_old),
+ HTTP_Internal_Server_Error)
+ elif result == 0:
+ raise ovimException("tenant with uuid='{}' not found".format(tenant_id), HTTP_Not_Found)
+
+ # insert in data base
+ result, content = self.db.update_rows('tenants', tenant_data, WHERE={'uuid': tenant_id}, log=True)
+ if result >= 0:
+ return content
+ 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": <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
+