X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=httpserver.py;h=4841a98bcc0c04de42f309203897fc65eaa4566a;hb=ae3fa6137b603b4d6b44e8c7897427a7e535b2a7;hp=a4e7fb9bf2bbb9fd19d2b9d852369859d98c8475;hpb=4dba7abed342277b08ebe1e903a9a87284c433ab;p=osm%2FRO.git diff --git a/httpserver.py b/httpserver.py index a4e7fb9b..4841a98b 100644 --- a/httpserver.py +++ b/httpserver.py @@ -42,7 +42,9 @@ from openmano_schemas import vnfd_schema_v01, vnfd_schema_v02, \ scenario_action_schema, instance_scenario_action_schema, instance_scenario_create_schema_v01, \ tenant_schema, tenant_edit_schema,\ datacenter_schema, datacenter_edit_schema, datacenter_action_schema, datacenter_associate_schema,\ - object_schema, netmap_new_schema, netmap_edit_schema + object_schema, netmap_new_schema, netmap_edit_schema, sdn_controller_schema, sdn_controller_edit_schema, \ + sdn_port_mapping_schema + import nfvo import utils from db_base import db_base_Exception @@ -143,7 +145,7 @@ def run_bottle(db, host_='localhost', port_=9090): @bottle.route(url_base + '/', method='GET') def http_get(): - print + #print return 'works' #TODO: to be completed # @@ -172,7 +174,7 @@ def change_keys_http2db(data, http_db, reverse=False): def format_out(data): '''return string of dictionary data according to requested json, yaml, xml. By default json''' - logger.debug(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True) ) + logger.debug("OUT: " + yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True) ) if 'application/yaml' in bottle.request.headers.get('Accept'): bottle.response.content_type='application/yaml' return yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True) #, canonical=True, default_style='"' @@ -207,14 +209,14 @@ def format_in(default_schema, version_fields=None, version_dict_schema=None): elif 'application/xml' in format_type: bottle.abort(501, "Content-Type: application/xml not supported yet.") else: - print 'Content-Type ' + str(format_type) + ' not supported.' + logger.warning('Content-Type ' + str(format_type) + ' not supported.') bottle.abort(HTTP_Not_Acceptable, 'Content-Type ' + str(format_type) + ' not supported.') return #if client_data == None: # bottle.abort(HTTP_Bad_Request, "Content error, empty") # return - #logger.debug('client-data: %s', client_data) + logger.debug('IN: %s', yaml.safe_dump(client_data, explicit_start=True, indent=4, default_flow_style=False, tags=False, encoding='utf-8', allow_unicode=True) ) #look for the client provider version error_text = "Invalid content " client_version = None @@ -241,10 +243,10 @@ def format_in(default_schema, version_fields=None, version_dict_schema=None): return client_data, used_schema except (ValueError, yaml.YAMLError) as exc: error_text += str(exc) - print error_text + logger.error(error_text) bottle.abort(HTTP_Bad_Request, error_text) except js_e.ValidationError as exc: - print "validate_in error, jsonschema exception ", exc.message, "at", exc.path + logger.error("validate_in error, jsonschema exception at '%s' '%s' ", str(exc.path), str(exc.message)) error_pos = "" if len(exc.path)>0: error_pos=" at " + ":".join(map(json.dumps, exc.path)) bottle.abort(HTTP_Bad_Request, error_text + exc.message + error_pos) @@ -321,6 +323,10 @@ def http_get_tenants(): except db_base_Exception as e: logger.error("http_get_tenants error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '/tenants/', method='GET') def http_get_tenant_id(tenant_id): @@ -336,6 +342,10 @@ def http_get_tenant_id(tenant_id): except db_base_Exception as e: logger.error("http_get_tenant_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '/tenants', method='POST') def http_post_tenants(): @@ -344,13 +354,18 @@ def http_post_tenants(): logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) http_content,_ = format_in( tenant_schema ) r = utils.remove_extra_items(http_content, tenant_schema) - if r is not None: print "http_post_tenants: Warning: remove extra items ", r + if r: + logger.debug("Remove received extra items %s", str(r)) try: data = nfvo.new_tenant(mydb, http_content['tenant']) return http_get_tenant_id(data) except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_tenants error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '/tenants/', method='PUT') def http_edit_tenant_id(tenant_id): @@ -359,7 +374,8 @@ def http_edit_tenant_id(tenant_id): logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) http_content,_ = format_in( tenant_edit_schema ) r = utils.remove_extra_items(http_content, tenant_edit_schema) - if r is not None: print "http_edit_tenant_id: Warning: remove extra items ", r + if r: + logger.debug("Remove received extra items %s", str(r)) #obtain data, check that only one exist try: @@ -372,6 +388,10 @@ def http_edit_tenant_id(tenant_id): except db_base_Exception as e: logger.error("http_edit_tenant_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '/tenants/', method='DELETE') def http_delete_tenant_id(tenant_id): @@ -383,7 +403,10 @@ def http_delete_tenant_id(tenant_id): except db_base_Exception as e: logger.error("http_delete_tenant_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) - + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//datacenters', method='GET') def http_get_datacenters(tenant_id): @@ -412,6 +435,10 @@ def http_get_datacenters(tenant_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_datacenters error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//datacenters/', method='GET') def http_get_datacenter_id(tenant_id, datacenter_id): @@ -425,7 +452,7 @@ def http_get_datacenter_id(tenant_id, datacenter_id): what = 'uuid' if utils.check_valid_uuid(datacenter_id) else 'name' where_={} where_[what] = datacenter_id - select_=['uuid', 'name','vim_url', 'vim_url_admin', 'type', 'config', 'description', 'd.created_at as created_at'] + select_=['uuid', 'name','vim_url', 'vim_url_admin', 'type', 'd.config as config', 'description', 'd.created_at as created_at'] if tenant_id != 'any': select_.append("datacenter_tenant_id") where_['td.nfvo_tenant_id']= tenant_id @@ -445,19 +472,28 @@ def http_get_datacenter_id(tenant_id, datacenter_id): if tenant_id != 'any': #get vim tenant info vim_tenants = mydb.get_rows( - SELECT=("vim_tenant_name", "vim_tenant_id", "user"), + SELECT=("vim_tenant_name", "vim_tenant_id", "user", "passwd", "config"), FROM="datacenter_tenants", WHERE={"uuid": datacenters[0]["datacenter_tenant_id"]}, ORDER_BY=("created", ) ) del datacenter["datacenter_tenant_id"] datacenter["vim_tenants"] = vim_tenants - + for vim_tenant in vim_tenants: + if vim_tenant["passwd"]: + vim_tenant["passwd"] = "******" + if vim_tenant['config'] != None: + try: + config_dict = yaml.load(vim_tenant['config']) + vim_tenant['config'] = config_dict + except Exception as e: + logger.error("Exception '%s' while trying to load config information", str(e)) + if datacenter['config'] != None: try: config_dict = yaml.load(datacenter['config']) datacenter['config'] = config_dict - except Exception, e: - print "Exception '%s' while trying to load config information" % str(e) + except Exception as e: + logger.error("Exception '%s' while trying to load config information", str(e)) #change_keys_http2db(content, http2db_datacenter, reverse=True) convert_datetime2str(datacenter) data={'datacenter' : datacenter} @@ -465,21 +501,30 @@ def http_get_datacenter_id(tenant_id, datacenter_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '/datacenters', method='POST') def http_post_datacenters(): - '''insert a tenant into the catalogue. ''' + '''insert a datacenter into the catalogue. ''' #parse input data logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) http_content,_ = format_in( datacenter_schema ) r = utils.remove_extra_items(http_content, datacenter_schema) - if r is not None: print "http_post_datacenters: Warning: remove extra items ", r + if r: + logger.debug("Remove received extra items %s", str(r)) try: data = nfvo.new_datacenter(mydb, http_content['datacenter']) return http_get_datacenter_id('any', data) except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_datacenters error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '/datacenters/', method='PUT') def http_edit_datacenter_id(datacenter_id_name): @@ -488,7 +533,8 @@ def http_edit_datacenter_id(datacenter_id_name): #parse input data http_content,_ = format_in( datacenter_edit_schema ) r = utils.remove_extra_items(http_content, datacenter_edit_schema) - if r is not None: print "http_edit_datacenter_id: Warning: remove extra items ", r + if r: + logger.debug("Remove received extra items %s", str(r)) try: datacenter_id = nfvo.edit_datacenter(mydb, datacenter_id_name, http_content['datacenter']) @@ -496,6 +542,142 @@ def http_edit_datacenter_id(datacenter_id_name): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_edit_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//sdn_controllers', method='POST') +def http_post_sdn_controller(tenant_id): + '''insert a sdn controller into the catalogue. ''' + #parse input data + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + http_content,_ = format_in( sdn_controller_schema ) + try: + logger.debug("tenant_id: "+tenant_id) + #logger.debug("content: {}".format(http_content['sdn_controller'])) + + data = nfvo.sdn_controller_create(mydb, tenant_id, http_content['sdn_controller']) + return format_out({"sdn_controller": nfvo.sdn_controller_list(mydb, tenant_id, data)}) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_post_sdn_controller error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//sdn_controllers/', method='PUT') +def http_put_sdn_controller_update(tenant_id, controller_id): + '''Update sdn controller''' + #parse input data + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + http_content,_ = format_in( sdn_controller_edit_schema ) +# r = utils.remove_extra_items(http_content, datacenter_schema) +# if r: +# logger.debug("Remove received extra items %s", str(r)) + try: + #logger.debug("tenant_id: "+tenant_id) + logger.debug("content: {}".format(http_content['sdn_controller'])) + + data = nfvo.sdn_controller_update(mydb, tenant_id, controller_id, http_content['sdn_controller']) + return format_out({"sdn_controller": nfvo.sdn_controller_list(mydb, tenant_id, controller_id)}) + + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_post_sdn_controller error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//sdn_controllers', method='GET') +def http_get_sdn_controller(tenant_id): + '''get sdn controllers list, can use both uuid or name''' + try: + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + + data = {'sdn_controllers': nfvo.sdn_controller_list(mydb, tenant_id)} + return format_out(data) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_get_sdn_controller error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//sdn_controllers/', method='GET') +def http_get_sdn_controller_id(tenant_id, controller_id): + '''get sdn controller details, can use both uuid or name''' + try: + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + data = nfvo.sdn_controller_list(mydb, tenant_id, controller_id) + return format_out({"sdn_controllers": data}) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_get_sdn_controller_id error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//sdn_controllers/', method='DELETE') +def http_delete_sdn_controller_id(tenant_id, controller_id): + '''delete sdn controller, can use both uuid or name''' + try: + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + data = nfvo.sdn_controller_delete(mydb, tenant_id, controller_id) + return format_out(data) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_delete_sdn_controller_id error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//datacenters//sdn_mapping', method='POST') +def http_post_datacenter_sdn_port_mapping(tenant_id, datacenter_id): + '''Set the sdn port mapping for a datacenter. ''' + #parse input data + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + http_content, _ = format_in(sdn_port_mapping_schema) +# r = utils.remove_extra_items(http_content, datacenter_schema) +# if r: +# logger.debug("Remove received extra items %s", str(r)) + try: + data = nfvo.datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, http_content['sdn_port_mapping']) + return format_out({"sdn_port_mapping": data}) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_post_datacenter_sdn_port_mapping error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//datacenters//sdn_mapping', method='GET') +def http_get_datacenter_sdn_port_mapping(tenant_id, datacenter_id): + '''get datacenter sdn mapping details, can use both uuid or name''' + try: + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + + data = nfvo.datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id) + return format_out({"sdn_port_mapping": data}) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_get_datacenter_sdn_port_mapping error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//datacenters//sdn_mapping', method='DELETE') +def http_delete_datacenter_sdn_port_mapping(tenant_id, datacenter_id): + '''clean datacenter sdn mapping, can use both uuid or name''' + try: + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + data = nfvo.datacenter_sdn_port_mapping_delete(mydb, tenant_id, datacenter_id) + return format_out({"result": data}) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_delete_datacenter_sdn_port_mapping error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '//datacenters//networks', method='GET') #deprecated @bottle.route(url_base + '//datacenters//netmaps', method='GET') @@ -528,6 +710,10 @@ def http_getnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id=None): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_getnetwork_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//datacenters//netmaps', method='DELETE') @bottle.route(url_base + '//datacenters//netmaps/', method='DELETE') @@ -554,6 +740,9 @@ def http_delnetmap_datacenter_id(tenant_id, datacenter_id, netmap_id=None): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_delnetmap_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '//datacenters//netmaps/upload', method='POST') @@ -568,6 +757,10 @@ def http_uploadnetmap_datacenter_id(tenant_id, datacenter_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_uploadnetmap_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//datacenters//netmaps', method='POST') def http_postnetmap_datacenter_id(tenant_id, datacenter_id): @@ -576,8 +769,8 @@ def http_postnetmap_datacenter_id(tenant_id, datacenter_id): #parse input data http_content,_ = format_in( netmap_new_schema ) r = utils.remove_extra_items(http_content, netmap_new_schema) - if r is not None: print "http_postnetmap_datacenter_id: Warning: remove extra items ", r - + if r: + logger.debug("Remove received extra items %s", str(r)) try: #obtain data, check that only one exist netmaps = nfvo.datacenter_new_netmap(mydb, tenant_id, datacenter_id, http_content) @@ -588,6 +781,10 @@ def http_postnetmap_datacenter_id(tenant_id, datacenter_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_postnetmap_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//datacenters//netmaps/', method='PUT') def http_putnettmap_datacenter_id(tenant_id, datacenter_id, netmap_id): @@ -596,7 +793,8 @@ def http_putnettmap_datacenter_id(tenant_id, datacenter_id, netmap_id): #parse input data http_content,_ = format_in( netmap_edit_schema ) r = utils.remove_extra_items(http_content, netmap_edit_schema) - if r is not None: print "http_putnettmap_datacenter_id: Warning: remove extra items ", r + if r: + logger.debug("Remove received extra items %s", str(r)) #obtain data, check that only one exist try: @@ -605,6 +803,9 @@ def http_putnettmap_datacenter_id(tenant_id, datacenter_id, netmap_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_putnettmap_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '//datacenters//action', method='POST') @@ -614,8 +815,8 @@ def http_action_datacenter_id(tenant_id, datacenter_id): #parse input data http_content,_ = format_in( datacenter_action_schema ) r = utils.remove_extra_items(http_content, datacenter_action_schema) - if r is not None: print "http_action_datacenter_id: Warning: remove extra items ", r - + if r: + logger.debug("Remove received extra items %s", str(r)) try: #obtain data, check that only one exist result = nfvo.datacenter_action(mydb, tenant_id, datacenter_id, http_content) @@ -626,6 +827,9 @@ def http_action_datacenter_id(tenant_id, datacenter_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_action_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '/datacenters/', method='DELETE') @@ -639,6 +843,10 @@ def http_delete_datacenter_id( datacenter_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_delete_datacenter_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//datacenters/', method='POST') def http_associate_datacenters(tenant_id, datacenter_id): @@ -647,18 +855,48 @@ def http_associate_datacenters(tenant_id, datacenter_id): #parse input data http_content,_ = format_in( datacenter_associate_schema ) r = utils.remove_extra_items(http_content, datacenter_associate_schema) - if r != None: print "http_associate_datacenters: Warning: remove extra items ", r + if r: + logger.debug("Remove received extra items %s", str(r)) try: id_ = nfvo.associate_datacenter_to_tenant(mydb, tenant_id, datacenter_id, http_content['datacenter'].get('vim_tenant'), http_content['datacenter'].get('vim_tenant_name'), http_content['datacenter'].get('vim_username'), - http_content['datacenter'].get('vim_password') - ) + http_content['datacenter'].get('vim_password'), + http_content['datacenter'].get('config') + ) return http_get_datacenter_id(tenant_id, id_) except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_associate_datacenters error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + +@bottle.route(url_base + '//datacenters/', method='PUT') +def http_associate_datacenters_edit(tenant_id, datacenter_id): + '''associate an existing datacenter to a this tenant. ''' + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + #parse input data + http_content,_ = format_in( datacenter_associate_schema ) + r = utils.remove_extra_items(http_content, datacenter_associate_schema) + if r: + logger.debug("Remove received extra items %s", str(r)) + try: + id_ = nfvo.edit_datacenter_to_tenant(mydb, tenant_id, datacenter_id, + http_content['datacenter'].get('vim_tenant'), + http_content['datacenter'].get('vim_tenant_name'), + http_content['datacenter'].get('vim_username'), + http_content['datacenter'].get('vim_password'), + http_content['datacenter'].get('config') + ) + return http_get_datacenter_id(tenant_id, id_) + except (nfvo.NfvoException, db_base_Exception) as e: + logger.error("http_associate_datacenters_edit error {}: {}".format(e.http_code, str(e))) + bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '//datacenters/', method='DELETE') def http_deassociate_datacenters(tenant_id, datacenter_id): @@ -670,6 +908,10 @@ def http_deassociate_datacenters(tenant_id, datacenter_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_deassociate_datacenters error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//vim//', method='GET') @bottle.route(url_base + '//vim///', method='GET') @@ -681,6 +923,10 @@ def http_get_vim_items(tenant_id, datacenter_id, item, name=None): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_vim_items error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//vim///', method='DELETE') def http_del_vim_items(tenant_id, datacenter_id, item, name): @@ -691,6 +937,11 @@ def http_del_vim_items(tenant_id, datacenter_id, item, name): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_del_vim_items error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + + @bottle.route(url_base + '//vim//', method='POST') def http_post_vim_items(tenant_id, datacenter_id, item): logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) @@ -701,6 +952,10 @@ def http_post_vim_items(tenant_id, datacenter_id, item): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_vim_items error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//vnfs', method='GET') def http_get_vnfs(tenant_id): @@ -724,6 +979,10 @@ def http_get_vnfs(tenant_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_vnfs error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//vnfs/', method='GET') def http_get_vnf_id(tenant_id,vnf_id): @@ -737,6 +996,10 @@ def http_get_vnf_id(tenant_id,vnf_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_vnf_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//vnfs', method='POST') def http_post_vnfs(tenant_id): @@ -746,7 +1009,8 @@ def http_post_vnfs(tenant_id): logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) http_content, used_schema = format_in( vnfd_schema_v01, ("schema_version",), {"0.2": vnfd_schema_v02}) r = utils.remove_extra_items(http_content, used_schema) - if r is not None: print "http_post_vnfs: Warning: remove extra items ", r + if r: + logger.debug("Remove received extra items %s", str(r)) try: if used_schema == vnfd_schema_v01: vnf_id = nfvo.new_vnf(mydb,tenant_id,http_content) @@ -759,6 +1023,10 @@ def http_post_vnfs(tenant_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_vnfs error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//vnfs/', method='DELETE') def http_delete_vnf_id(tenant_id,vnf_id): @@ -772,6 +1040,10 @@ def http_delete_vnf_id(tenant_id,vnf_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_delete_vnf_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + #@bottle.route(url_base + '//hosts/topology', method='GET') #@bottle.route(url_base + '//physicalview/Madrid-Alcantara', method='GET') @@ -788,15 +1060,18 @@ def http_get_hosts(tenant_id, datacenter): result, data = nfvo.get_hosts_info(mydb, tenant_id) #, datacenter) if result < 0: - print "http_get_hosts error %d %s" % (-result, data) + #print "http_get_hosts error %d %s" % (-result, data) bottle.abort(-result, data) else: convert_datetime2str(data) - print json.dumps(data, indent=4) + #print json.dumps(data, indent=4) return format_out(data) except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_hosts error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '/', method='OPTIONS') @@ -829,6 +1104,10 @@ def http_post_deploy(tenant_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_deploy error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//topology/verify', method='POST') def http_post_verify(tenant_id): @@ -854,9 +1133,9 @@ def http_post_scenarios(tenant_id): if used_schema == nsd_schema_v01: scenario_id = nfvo.new_scenario(mydb, tenant_id, http_content) elif used_schema == nsd_schema_v02: - scenario_id = nfvo.new_scenario_v02(mydb, tenant_id, http_content) + scenario_id = nfvo.new_scenario_v02(mydb, tenant_id, http_content, "0.2") elif used_schema == nsd_schema_v03: - scenario_id = nfvo.new_scenario_v03(mydb, tenant_id, http_content) + scenario_id = nfvo.new_scenario_v02(mydb, tenant_id, http_content, "0.3") else: logger.warning('Unexpected schema_version: %s', http_content.get("schema_version")) bottle.abort(HTTP_Bad_Request, "Invalid schema version") @@ -866,18 +1145,23 @@ def http_post_scenarios(tenant_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_scenarios error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//scenarios//action', method='POST') def http_post_scenario_action(tenant_id, scenario_id): '''take an action over a scenario''' logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) - #check valid tenant_id + # parse input data + http_content, _ = format_in(scenario_action_schema) + r = utils.remove_extra_items(http_content, scenario_action_schema) + if r: + logger.debug("Remove received extra items %s", str(r)) try: - nfvo.check_tenant(mydb, tenant_id) - #parse input data - http_content,_ = format_in( scenario_action_schema ) - r = utils.remove_extra_items(http_content, scenario_action_schema) - if r is not None: print "http_post_scenario_action: Warning: remove extra items ", r + # check valid tenant_id + nfvo.check_tenant(mydb, tenant_id) if "start" in http_content: data = nfvo.start_scenario(mydb, tenant_id, scenario_id, http_content['start']['instance_name'], \ http_content['start'].get('description',http_content['start']['instance_name']), @@ -903,6 +1187,10 @@ def http_post_scenario_action(tenant_id, scenario_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_scenario_action error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//scenarios', method='GET') def http_get_scenarios(tenant_id): @@ -927,6 +1215,10 @@ def http_get_scenarios(tenant_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_scenarios error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//scenarios/', method='GET') def http_get_scenario_id(tenant_id, scenario_id): @@ -944,14 +1236,19 @@ def http_get_scenario_id(tenant_id, scenario_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_scenarios error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//scenarios/', method='DELETE') def http_delete_scenario_id(tenant_id, scenario_id): '''delete a scenario from database, can use both uuid or name''' + logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) try: #check valid tenant_id if tenant_id != "any": - nfvo.check_tenant(mydb, tenant_id) + nfvo.check_tenant(mydb, tenant_id) #obtain data data = mydb.delete_scenario(scenario_id, tenant_id) #print json.dumps(data, indent=4) @@ -959,6 +1256,9 @@ def http_delete_scenario_id(tenant_id, scenario_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_delete_scenario_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '//scenarios/', method='PUT') @@ -977,25 +1277,31 @@ def http_put_scenario_id(tenant_id, scenario_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_put_scenario_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.route(url_base + '//instances', method='POST') def http_post_instances(tenant_id): - '''take an action over a scenario''' + '''create an instance-scenario''' logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + # parse input data + http_content, used_schema = format_in(instance_scenario_create_schema_v01) + r = utils.remove_extra_items(http_content, used_schema) + if r is not None: + logger.warning("http_post_instances: Warning: remove extra items %s", str(r)) try: #check valid tenant_id if tenant_id != "any": nfvo.check_tenant(mydb, tenant_id) - #parse input data - http_content,used_schema = format_in( instance_scenario_create_schema_v01) - r = utils.remove_extra_items(http_content, used_schema) - if r is not None: - logger.warning("http_post_instances: Warning: remove extra items %s", str(r)) data = nfvo.create_instance(mydb, tenant_id, http_content["instance"]) return format_out(data) except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_instances error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) # # INSTANCES @@ -1019,6 +1325,10 @@ def http_get_instances(tenant_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_instances error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//instances/', method='GET') def http_get_instance_id(tenant_id, instance_id): @@ -1044,6 +1354,10 @@ def http_get_instance_id(tenant_id, instance_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_get_instance_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//instances/', method='DELETE') def http_delete_instance_id(tenant_id, instance_id): @@ -1061,20 +1375,25 @@ def http_delete_instance_id(tenant_id, instance_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_delete_instance_id error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) + @bottle.route(url_base + '//instances//action', method='POST') def http_post_instance_scenario_action(tenant_id, instance_id): '''take an action over a scenario instance''' logger.debug('FROM %s %s %s', bottle.request.remote_addr, bottle.request.method, bottle.request.url) + # parse input data + http_content, _ = format_in(instance_scenario_action_schema) + r = utils.remove_extra_items(http_content, instance_scenario_action_schema) + if r: + logger.debug("Remove received extra items %s", str(r)) try: #check valid tenant_id if tenant_id != "any": nfvo.check_tenant(mydb, tenant_id) - #parse input data - http_content,_ = format_in( instance_scenario_action_schema ) - r = utils.remove_extra_items(http_content, instance_scenario_action_schema) - if r is not None: print "http_post_instance_scenario_action: Warning: remove extra items ", r #print "http_post_instance_scenario_action input: ", http_content #obtain data instance = mydb.get_instance_scenario(instance_id, tenant_id) @@ -1085,6 +1404,9 @@ def http_post_instance_scenario_action(tenant_id, instance_id): except (nfvo.NfvoException, db_base_Exception) as e: logger.error("http_post_instance_scenario_action error {}: {}".format(e.http_code, str(e))) bottle.abort(e.http_code, str(e)) + except Exception as e: + logger.error("Unexpected exception: ", exc_info=True) + bottle.abort(HTTP_Internal_Server_Error, type(e).__name__ + ": " + str(e)) @bottle.error(400)