X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=httpserver.py;h=3e89073eadb6d43cfd0bb45f671a5e8ada447220;hb=fbfb797533c1ba8eb015a387596949b0146a28ec;hp=8f203dc82a11d5f91d25835943186a9b4e4ca9c0;hpb=65ba8f83bd586b24dc712cfe36e53aaa96ae535e;p=osm%2Fopenvim.git diff --git a/httpserver.py b/httpserver.py index 8f203dc..3e89073 100644 --- a/httpserver.py +++ b/httpserver.py @@ -47,7 +47,7 @@ from vim_schema import host_new_schema, host_edit_schema, tenant_new_schema, \ flavor_new_schema, flavor_update_schema, \ image_new_schema, image_update_schema, \ server_new_schema, server_action_schema, network_new_schema, network_update_schema, \ - port_new_schema, port_update_schema + port_new_schema, port_update_schema, openflow_controller_schema import ovim import logging @@ -153,6 +153,7 @@ http2db_flavor={'id':'uuid','imageRef':'image_id'} http2db_image={'id':'uuid', 'created':'created_at', 'updated':'modified_at', 'public': 'public'} http2db_server={'id':'uuid','hostId':'host_id','flavorRef':'flavor_id','imageRef':'image_id','created':'created_at'} http2db_network={'id':'uuid','provider:vlan':'vlan', 'provider:physical': 'provider'} +http2db_ofc = {'id': 'uuid'} http2db_port={'id':'uuid', 'network_id':'net_id', 'mac_address':'mac', 'device_owner':'type','device_id':'instance_id','binding:switch_port':'switch_port','binding:vlan':'vlan', 'bandwidth':'Mbps'} def remove_extra_items(data, schema): @@ -860,114 +861,140 @@ def http_delete_host_id(host_id): print "http_delete_host_id error",result, content bottle.abort(-result, content) return - - - # # TENANTS # + @bottle.route(url_base + '/tenants', method='GET') def http_get_tenants(): - my = config_dic['http_threads'][ threading.current_thread().name ] - select_,where_,limit_ = filter_query_string(bottle.request.query, http2db_tenant, - ('id','name','description','enabled') ) - result, content = my.db.get_table(FROM='tenants', SELECT=select_,WHERE=where_,LIMIT=limit_) - if result < 0: - print "http_get_tenants Error", content - bottle.abort(-result, content) - else: - change_keys_http2db(content, http2db_tenant, reverse=True) - convert_boolean(content, ('enabled',)) - data={'tenants' : content} - #data['tenants_links'] = dict([('tenant', row['id']) for row in content]) + """ + Retreive tenant list from DB + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + select_, where_, limit_ = filter_query_string(bottle.request.query, http2db_tenant, + ('id', 'name', 'description', 'enabled')) + tenants = my.ovim.get_tenants(select_, where_) + delete_nulls(tenants) + change_keys_http2db(tenants, http2db_tenant, reverse=True) + data = {'tenants': tenants} return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) + @bottle.route(url_base + '/tenants/', method='GET') def http_get_tenant_id(tenant_id): - my = config_dic['http_threads'][ threading.current_thread().name ] - result, content = my.db.get_table(FROM='tenants', SELECT=('uuid','name','description', 'enabled'),WHERE={'uuid': tenant_id} ) - if result < 0: - print "http_get_tenant_id error %d %s" % (result, content) - bottle.abort(-result, content) - elif result==0: - print "http_get_tenant_id tenant '%s' not found" % tenant_id - bottle.abort(HTTP_Not_Found, "tenant %s not found" % tenant_id) - else: - change_keys_http2db(content, http2db_tenant, reverse=True) - convert_boolean(content, ('enabled',)) - data={'tenant' : content[0]} - #data['tenants_links'] = dict([('tenant', row['id']) for row in content]) + """ + Get tenant from DB by id + :param tenant_id: tenant id + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + tenant = my.ovim.show_tenant_id(tenant_id) + delete_nulls(tenant) + change_keys_http2db(tenant, http2db_tenant, reverse=True) + data = {'tenant': tenant} return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) @bottle.route(url_base + '/tenants', method='POST') def http_post_tenants(): - '''insert a tenant into the database.''' - my = config_dic['http_threads'][ threading.current_thread().name ] - #parse input data - http_content = format_in( tenant_new_schema ) - r = remove_extra_items(http_content, tenant_new_schema) - if r is not None: print "http_post_tenants: Warning: remove extra items ", r - change_keys_http2db(http_content['tenant'], http2db_tenant) + """ + Insert a tenant into the database. + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + http_content = format_in(tenant_new_schema) + r = remove_extra_items(http_content, tenant_new_schema) + if r is not None: + my.logger.error("http_post_tenants: Warning: remove extra items " + str(r), exc_info=True) + # insert in data base + tenant_id = my.ovim.new_tentant(http_content['tenant']) + tenant = my.ovim.show_tenant_id(tenant_id) + change_keys_http2db(tenant, http2db_tenant, reverse=True) + delete_nulls(tenant) + data = {'tenant': tenant} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) - #insert in data base - result, content = my.db.new_tenant(http_content['tenant']) - - if result >= 0: - return http_get_tenant_id(content) - else: - bottle.abort(-result, content) - return @bottle.route(url_base + '/tenants/', method='PUT') def http_put_tenant_id(tenant_id): - '''update a tenant into the database.''' - my = config_dic['http_threads'][ threading.current_thread().name ] - #parse input data - http_content = format_in( tenant_edit_schema ) - r = remove_extra_items(http_content, tenant_edit_schema) - if r is not None: print "http_put_tenant_id: Warning: remove extra items ", r - change_keys_http2db(http_content['tenant'], http2db_tenant) + """ + Update a tenantinto DB. + :param tenant_id: tentant id + :return: + """ + + my = config_dic['http_threads'][threading.current_thread().name] + try: + # parse input data + http_content = format_in(tenant_edit_schema) + r = remove_extra_items(http_content, tenant_edit_schema) + if r is not None: + print "http_put_tenant_id: Warning: remove extra items ", r + change_keys_http2db(http_content['tenant'], http2db_tenant) + # insert in data base + my.ovim.edit_tenant(tenant_id, http_content['tenant']) + tenant = my.ovim.show_tenant_id(tenant_id) + change_keys_http2db(tenant, http2db_tenant, reverse=True) + data = {'tenant': tenant} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) - #insert in data base - result, content = my.db.update_rows('tenants', http_content['tenant'], WHERE={'uuid': tenant_id}, log=True ) - if result >= 0: - return http_get_tenant_id(tenant_id) - else: - bottle.abort(-result, content) - return @bottle.route(url_base + '/tenants/', method='DELETE') def http_delete_tenant_id(tenant_id): - my = config_dic['http_threads'][ threading.current_thread().name ] - #check permissions - r, tenants_flavors = my.db.get_table(FROM='tenants_flavors', SELECT=('flavor_id','tenant_id'), WHERE={'tenant_id': tenant_id}) - if r<=0: - tenants_flavors=() - r, tenants_images = my.db.get_table(FROM='tenants_images', SELECT=('image_id','tenant_id'), WHERE={'tenant_id': tenant_id}) - if r<=0: - tenants_images=() - result, content = my.db.delete_row('tenants', tenant_id) - if result == 0: - bottle.abort(HTTP_Not_Found, content) - elif result >0: - print "alf", tenants_flavors, tenants_images - for flavor in tenants_flavors: - my.db.delete_row_by_key("flavors", "uuid", flavor['flavor_id']) - for image in tenants_images: - my.db.delete_row_by_key("images", "uuid", image['image_id']) - data={'result' : content} - return format_out(data) - else: - print "http_delete_tenant_id error",result, content - bottle.abort(-result, content) - return + """ + Delete a tenant from the database. + :param tenant_id: tenant id + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + try: + content = my.ovim.delete_tentant(tenant_id) + data = {'result': content} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) # # FLAVORS # + @bottle.route(url_base + '//flavors', method='GET') def http_get_flavors(tenant_id): my = config_dic['http_threads'][ threading.current_thread().name ] @@ -1839,8 +1866,13 @@ def http_post_server_action(tenant_id, server_id): @bottle.route(url_base + '/networks', method='GET') def http_get_networks(): + """ + Get all networks available + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + try: - my = config_dic['http_threads'][threading.current_thread().name] # obtain data select_, where_, limit_ = filter_query_string(bottle.request.query, http2db_network, ('id', 'name', 'tenant_id', 'type', @@ -1848,6 +1880,7 @@ def http_get_networks(): 'admin_state_up', 'provider:physical')) if "tenant_id" in where_: del where_["tenant_id"] + content = my.ovim.get_networks(select_, where_, limit_) delete_nulls(content) @@ -1865,24 +1898,31 @@ def http_get_networks(): @bottle.route(url_base + '/networks/', method='GET') def http_get_network_id(network_id): - data = get_network_id(network_id) - return format_out(data) + """ + Get a network data by id + :param network_id: + :return: + """ + data = get_network_id(network_id) + return format_out(data) def get_network_id(network_id): + """ + Get network from DB by id + :param network_id: network Id + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + try: - my = config_dic['http_threads'][threading.current_thread().name] # obtain data where_ = bottle.request.query - where_['uuid'] = network_id - content = my.ovim.get_network_by_id(where_) + content = my.ovim.show_network(network_id, where_) - convert_boolean(content, ('shared', 'admin_state_up', 'enable_dhcp')) change_keys_http2db(content, http2db_network, reverse=True) - - # content[0]['ports'] = delete_nulls(content[0]['ports']) - delete_nulls(content[0]) - data = {'network': content[0]} + delete_nulls(content) + data = {'network': content} return data except ovim.ovimException as e: my.logger.error(str(e), exc_info=True) @@ -1898,8 +1938,9 @@ def http_post_networks(): Insert a network into the database. :return: """ + my = config_dic['http_threads'][threading.current_thread().name] + try: - my = config_dic['http_threads'][threading.current_thread().name] # parse input data http_content = format_in(network_new_schema ) r = remove_extra_items(http_content, network_new_schema) @@ -1907,8 +1948,8 @@ def http_post_networks(): print "http_post_networks: Warning: remove extra items ", r change_keys_http2db(http_content['network'], http2db_network) network = http_content['network'] - content = my.ovim.set_network(network) - return http_get_network_id(content) + content = my.ovim.new_network(network) + return format_out(get_network_id(content)) except ovim.ovimException as e: my.logger.error(str(e), exc_info=True) bottle.abort(e.http_code, str(e)) @@ -1919,130 +1960,176 @@ def http_post_networks(): @bottle.route(url_base + '/networks/', method='PUT') def http_put_network_id(network_id): - '''update a network_id into the database.''' - my = config_dic['http_threads'][ threading.current_thread().name ] - #parse input data - http_content = format_in( network_update_schema ) - r = remove_extra_items(http_content, network_update_schema) - change_keys_http2db(http_content['network'], http2db_network) - network=http_content['network'] - - #Look for the previous data - where_ = {'uuid': network_id} - result, network_old = my.db.get_table(FROM='nets', WHERE=where_) - if result < 0: - print "http_put_network_id error %d %s" % (result, network_old) - bottle.abort(-result, network_old) - return - elif result==0: - print "http_put_network_id network '%s' not found" % network_id - bottle.abort(HTTP_Not_Found, 'network %s not found' % network_id) - return - #get ports - nbports, content = my.db.get_table(FROM='ports', SELECT=('uuid as port_id',), - WHERE={'net_id': network_id}, LIMIT=100) - if result < 0: - print "http_put_network_id error %d %s" % (result, network_old) - bottle.abort(-result, content) - return - if nbports>0: - if 'type' in network and network['type'] != network_old[0]['type']: - bottle.abort(HTTP_Method_Not_Allowed, "Can not change type of network while having ports attached") - if 'vlan' in network and network['vlan'] != network_old[0]['vlan']: - bottle.abort(HTTP_Method_Not_Allowed, "Can not change vlan of network while having ports attached") - - #check valid params - net_provider = network.get('provider', network_old[0]['provider']) - net_type = network.get('type', network_old[0]['type']) - net_bind_net = network.get("bind_net") - net_bind_type= network.get("bind_type") - if net_bind_net != None: - #look for a valid net - if check_valid_uuid(net_bind_net): - net_bind_key = "uuid" - else: - net_bind_key = "name" - result, content = my.db.get_table(FROM='nets', WHERE={net_bind_key: net_bind_net} ) - if result<0: - bottle.abort(HTTP_Internal_Server_Error, 'getting nets from db ' + content) - return - elif result==0: - bottle.abort(HTTP_Bad_Request, "bind_net %s '%s'not found" % (net_bind_key, net_bind_net) ) - return - elif result>1: - bottle.abort(HTTP_Bad_Request, "more than one bind_net %s '%s' found, use uuid" % (net_bind_key, net_bind_net) ) - return - network["bind_net"] = content[0]["uuid"] - if net_bind_type != None: - if net_bind_type[0:5] != "vlan:": - bottle.abort(HTTP_Bad_Request, "bad format for 'bind_type', must be 'vlan:'") - return - if int(net_bind_type[5:]) > 4095 or int(net_bind_type[5:])<=0 : - bottle.abort(HTTP_Bad_Request, "bad format for 'bind_type', must be 'vlan:' with a tag between 1 and 4095") - return - if net_provider!=None: - if net_provider[:9]=="openflow:": - if net_type!="ptp" and net_type!="data": - bottle.abort(HTTP_Bad_Request, "Only 'ptp' or 'data' net types can be bound to 'openflow'") - else: - if net_type!="bridge_man" and net_type!="bridge_data": - bottle.abort(HTTP_Bad_Request, "Only 'bridge_man' or 'bridge_data' net types can be bound to 'bridge', 'macvtap' or 'default") + """ + Update a network_id into DB. + :param network_id: network id + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + # parse input data + http_content = format_in(network_update_schema) + change_keys_http2db(http_content['network'], http2db_network) + network = http_content['network'] + return my.ovim.edit_network(network_id, network) + + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) - #insert in data base - result, content = my.db.update_rows('nets', network, WHERE={'uuid': network_id}, log=True ) - if result >= 0: - if result>0: # and nbports>0 and 'admin_state_up' in network and network['admin_state_up'] != network_old[0]['admin_state_up']: - r,c = config_dic['of_thread'].insert_task("update-net", network_id) - if r < 0: - print "http_put_network_id error while launching openflow rules" - bottle.abort(HTTP_Internal_Server_Error, c) - if config_dic.get("dhcp_server"): - if network_id in config_dic["dhcp_nets"]: - config_dic["dhcp_nets"].remove(network_id) - print "dhcp_server: delete net", network_id - if network.get("name", network_old["name"]) in config_dic["dhcp_server"].get("nets", () ): - config_dic["dhcp_nets"].append(network_id) - print "dhcp_server: add new net", network_id - else: - net_bind = network.get("bind", network_old["bind"] ) - if net_bind and net_bind[:7]=="bridge:" and net_bind[7:] in config_dic["dhcp_server"].get("bridge_ifaces", () ): - config_dic["dhcp_nets"].append(network_id) - print "dhcp_server: add new net", network_id - return http_get_network_id(network_id) - else: - bottle.abort(-result, content) - return - @bottle.route(url_base + '/networks/', method='DELETE') def http_delete_network_id(network_id): - '''delete a network_id from the database.''' - my = config_dic['http_threads'][ threading.current_thread().name ] + """ + Delete a network_id from the database. + :param network_id: Network id + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] - #delete from the data base - result, content = my.db.delete_row('nets', network_id ) - - if result == 0: - bottle.abort(HTTP_Not_Found, content) - elif result >0: - for brnet in config_dic['bridge_nets']: - if brnet[3]==network_id: - brnet[3]=None - break - if config_dic.get("dhcp_server") and network_id in config_dic["dhcp_nets"]: - config_dic["dhcp_nets"].remove(network_id) - print "dhcp_server: delete net", network_id - data={'result' : content} + try: + # delete from the data base + content = my.ovim.delete_network(network_id) + data = {'result': content} return format_out(data) - else: - print "http_delete_network_id error",result, content - bottle.abort(-result, content) - return + + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) + # # OPENFLOW # +@bottle.route(url_base + '/openflow/controller', method='GET') +def http_get_openflow_controller(): + """ + Retrieve a openflow controllers list from DB. + :return: + """ + # TODO check if show a proper list + my = config_dic['http_threads'][threading.current_thread().name] + + try: + select_, where_, limit_ = filter_query_string(bottle.request.query, http2db_ofc, + ('id', 'name', 'dpid', 'ip', 'port', 'type', + 'version', 'user', 'password')) + + content = my.ovim.get_of_controllers(select_, where_) + delete_nulls(content) + change_keys_http2db(content, http2db_ofc, reverse=True) + data = {'ofcs': content} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) + + +@bottle.route(url_base + '/openflow/controller/', method='GET') +def http_get_openflow_controller_id(uuid): + """ + Get an openflow controller by dpid from DB.get_of_controllers + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + + content = my.ovim.show_of_controller(uuid) + delete_nulls(content) + change_keys_http2db(content, http2db_ofc, reverse=True) + data = {'ofc': content} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) + + +@bottle.route(url_base + '/openflow/controller/', method='POST') +def http_post_openflow_controller(): + """ + Create a new openflow controller into DB + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + http_content = format_in(openflow_controller_schema) + of_c = http_content['ofc'] + uuid = my.ovim.new_of_controller(of_c) + content = my.ovim.show_of_controller(uuid) + delete_nulls(content) + change_keys_http2db(content, http2db_ofc, reverse=True) + data = {'ofc': content} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) + + +@bottle.route(url_base + '/openflow/controller/', method='PUT') +def http_put_openflow_controller_by_id(of_controller_id): + """ + Create an openflow controller into DB + :param of_controller_id: openflow controller dpid + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + http_content = format_in(openflow_controller_schema) + of_c = http_content['ofc'] + + content = my.ovim.edit_of_controller(of_controller_id, of_c) + delete_nulls(content) + change_keys_http2db(content, http2db_ofc, reverse=True) + data = {'ofc': content} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) + + +@bottle.route(url_base + '/openflow/controller/', method='DELETE') +def http_delete_openflow_controller(of_controller_id): + """ + Delete an openflow controller from DB. + :param of_controller_id: openflow controller dpid + :return: + """ + my = config_dic['http_threads'][threading.current_thread().name] + + try: + content = my.ovim.delete_of_controller(of_controller_id) + data = {'result': content} + return format_out(data) + except ovim.ovimException as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(e.http_code, str(e)) + except Exception as e: + my.logger.error(str(e), exc_info=True) + bottle.abort(HTTP_Bad_Request, str(e)) + + @bottle.route(url_base + '/networks//openflow', method='GET') def http_get_openflow_id(network_id): """ @@ -2050,12 +2137,12 @@ def http_get_openflow_id(network_id): :param network_id: network id :return: """ + my = config_dic['http_threads'][threading.current_thread().name] # ignore input data if network_id == 'all': network_id = None try: - my = config_dic['http_threads'][threading.current_thread().name] content = my.ovim.get_openflow_rules(network_id) data = {'openflow-rules': content} except ovim.ovimException as e: @@ -2085,7 +2172,7 @@ def http_put_openflow_id(network_id): network_id = None try: - result = my.ovim.update_openflow_rules(network_id) + result = my.ovim.edit_openflow_rules(network_id) except ovim.ovimException as e: my.logger.error(str(e), exc_info=True) bottle.abort(e.http_code, str(e)) @@ -2108,7 +2195,7 @@ def http_clear_openflow_rules(): if not my.admin: bottle.abort(HTTP_Unauthorized, "Needed admin privileges") try: - my.ovim.clear_openflow_rules() + my.ovim.delete_openflow_rules() except ovim.ovimException as e: my.logger.error(str(e), exc_info=True) bottle.abort(e.http_code, str(e))