X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_ro%2Fnfvo.py;h=8f105c32e97e83029253e192fac07a1fd8cc6553;hb=56d73d256a8067bae2bfa9dd74b7743ced2df7fa;hp=624be51bb97a06135db9f1aedb83ef2834946550;hpb=d573573c267f0db9ee21c454ed2a5a086967c81e;p=osm%2FRO.git diff --git a/osm_ro/nfvo.py b/osm_ro/nfvo.py index 624be51b..8f105c32 100644 --- a/osm_ro/nfvo.py +++ b/osm_ro/nfvo.py @@ -27,8 +27,8 @@ NFVO engine, implementing all the methods for the creation, deletion and managem __author__="Alfonso Tierno, Gerardo Garcia, Pablo Montes" __date__ ="$16-sep-2014 22:05:01$" -import imp -#import json +# import imp +# import json import yaml import utils import vim_thread @@ -44,6 +44,7 @@ import nfvo_db from threading import Lock from time import time from lib_osm_openvim import ovim as ovim_module +from lib_osm_openvim.ovim import ovimException global global_config global vimconn_imported @@ -128,14 +129,16 @@ def start_service(mydb): #TODO: log_level_of should not be needed. To be modified in ovim 'log_level_of': 'DEBUG' } - ovim = ovim_module.ovim(ovim_configuration) - ovim.start_service() - - from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid' - select_ = ('type','d.config as config','d.uuid as datacenter_id', 'vim_url', 'vim_url_admin', 'd.name as datacenter_name', - 'dt.uuid as datacenter_tenant_id','dt.vim_tenant_name as vim_tenant_name','dt.vim_tenant_id as vim_tenant_id', - 'user','passwd', 'dt.config as dt_config', 'nfvo_tenant_id') try: + ovim = ovim_module.ovim(ovim_configuration) + ovim.start_service() + + from_= 'tenants_datacenters as td join datacenters as d on td.datacenter_id=d.uuid join '\ + 'datacenter_tenants as dt on td.datacenter_tenant_id=dt.uuid' + select_ = ('type', 'd.config as config', 'd.uuid as datacenter_id', 'vim_url', 'vim_url_admin', + 'd.name as datacenter_name', 'dt.uuid as datacenter_tenant_id', + 'dt.vim_tenant_name as vim_tenant_name', 'dt.vim_tenant_id as vim_tenant_id', + 'user', 'passwd', 'dt.config as dt_config', 'nfvo_tenant_id') vims = mydb.get_rows(FROM=from_, SELECT=select_) for vim in vims: extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'), @@ -148,13 +151,15 @@ def start_service(mydb): module_info=None try: module = "vimconn_" + vim["type"] - module_info = imp.find_module(module) - vim_conn = imp.load_module(vim["type"], *module_info) + pkg = __import__("osm_ro." + module) + vim_conn = getattr(pkg, module) + # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) + # vim_conn = imp.load_module(vim["type"], *module_info) vimconn_imported[vim["type"]] = vim_conn except (IOError, ImportError) as e: - if module_info and module_info[0]: - file.close(module_info[0]) - raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( + # if module_info and module_info[0]: + # file.close(module_info[0]) + raise NfvoException("Unknown vim type '{}'. Cannot open file '{}.py'; {}: {}".format( vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) thread_id = vim['datacenter_tenant_id'] @@ -170,14 +175,25 @@ def start_service(mydb): config=extra, persistent_info=vim_persistent_info[thread_id] ) except Exception as e: - raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error) - thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], vim['vim_tenant_id']) + raise NfvoException("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, e), + HTTP_Internal_Server_Error) + thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], + vim['vim_tenant_id']) new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'], vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim) new_thread.start() vim_threads["running"][thread_id] = new_thread except db_base_Exception as e: raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code) + except ovim_module.ovimException as e: + message = str(e) + if message[:22] == "DATABASE wrong version": + message = "DATABASE wrong version of lib_osm_openvim {msg} -d{dbname} -u{dbuser} -p{dbpass} {ver}' "\ + "at host {dbhost}".format( + msg=message[22:-3], dbname=global_config["db_ovim_name"], + dbuser=global_config["db_ovim_user"], dbpass=global_config["db_ovim_passwd"], + ver=message[-3:-1], dbhost=global_config["db_ovim_host"]) + raise NfvoException(message, HTTP_Bad_Request) def stop_service(): @@ -192,6 +208,10 @@ def stop_service(): for thread in global_config["console_thread"]: thread.terminate = True +def get_version(): + return ("openmanod version {} {}\n(c) Copyright Telefonica".format(global_config["version"], + global_config["version_date"] )) + def get_flavorlist(mydb, vnf_id, nfvo_tenant=None): '''Obtain flavorList @@ -270,12 +290,14 @@ def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, da module_info=None try: module = "vimconn_" + vim["type"] - module_info = imp.find_module(module) - vim_conn = imp.load_module(vim["type"], *module_info) + pkg = __import__("osm_ro." + module) + vim_conn = getattr(pkg, module) + # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) + # vim_conn = imp.load_module(vim["type"], *module_info) vimconn_imported[vim["type"]] = vim_conn except (IOError, ImportError) as e: - if module_info and module_info[0]: - file.close(module_info[0]) + # if module_info and module_info[0]: + # file.close(module_info[0]) raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) @@ -312,7 +334,9 @@ def rollback(mydb, vims, rollback_list): if item["where"]=="vim": if item["vim_id"] not in vims: continue - vim=vims[ item["vim_id"] ] + if is_task_id(item["uuid"]): + continue + vim = vims[item["vim_id"]] try: if item["what"]=="image": vim.delete_image(item["uuid"]) @@ -459,7 +483,7 @@ def check_vnf_descriptor(vnf_descriptor, vnf_descriptor_version=1): HTTP_Bad_Request) -def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error = None): +def create_or_use_image(mydb, vims, image_dict, rollback_list, only_create_at_vim=False, return_on_error=None): #look if image exist if only_create_at_vim: image_mano_id = image_dict['uuid'] @@ -1901,6 +1925,7 @@ def get_vim_thread(mydb, tenant_id, datacenter_id_name=None, datacenter_tenant_i except db_base_Exception as e: raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code) + def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter): datacenter_id = None datacenter_name = None @@ -2019,20 +2044,23 @@ def create_instance(mydb, tenant_id, instance_dict): for scenario_net in scenarioDict['nets']: if net_name == scenario_net["name"]: if 'ip-profile' in net_instance_desc: - ipprofile = net_instance_desc['ip-profile'] - ipprofile['subnet_address'] = ipprofile.pop('subnet-address',None) - ipprofile['ip_version'] = ipprofile.pop('ip-version','IPv4') - ipprofile['gateway_address'] = ipprofile.pop('gateway-address',None) - ipprofile['dns_address'] = ipprofile.pop('dns-address',None) - if 'dhcp' in ipprofile: - ipprofile['dhcp_start_address'] = ipprofile['dhcp'].get('start-address',None) - ipprofile['dhcp_enabled'] = ipprofile['dhcp'].get('enabled',True) - ipprofile['dhcp_count'] = ipprofile['dhcp'].get('count',None) - del ipprofile['dhcp'] + # translate from input format to database format + ipprofile_in = net_instance_desc['ip-profile'] + ipprofile_db = {} + ipprofile_db['subnet_address'] = ipprofile_in.get('subnet-address') + ipprofile_db['ip_version'] = ipprofile_in.get('ip-version', 'IPv4') + ipprofile_db['gateway_address'] = ipprofile_in.get('gateway-address') + ipprofile_db['dns_address'] = ipprofile_in.get('dns-address') + if isinstance(ipprofile_db['dns_address'], (list, tuple)): + ipprofile_db['dns_address'] = ";".join(ipprofile_db['dns_address']) + if 'dhcp' in ipprofile_in: + ipprofile_db['dhcp_start_address'] = ipprofile_in['dhcp'].get('start-address') + ipprofile_db['dhcp_enabled'] = ipprofile_in['dhcp'].get('enabled', True) + ipprofile_db['dhcp_count'] = ipprofile_in['dhcp'].get('count' ) if 'ip_profile' not in scenario_net: - scenario_net['ip_profile'] = ipprofile + scenario_net['ip_profile'] = ipprofile_db else: - update(scenario_net['ip_profile'],ipprofile) + update(scenario_net['ip_profile'], ipprofile_db) for interface in net_instance_desc.get('interfaces', () ): if 'ip_address' in interface: for vnf in scenarioDict['vnfs']: @@ -2165,7 +2193,9 @@ def create_instance(mydb, tenant_id, instance_dict): # 3. Creating new vm instances in the VIM #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict) - for sce_vnf in scenarioDict['vnfs']: + sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) + #for sce_vnf in scenarioDict['vnfs']: + for sce_vnf in sce_vnf_list: if sce_vnf.get("datacenter"): vim = myvims[ sce_vnf["datacenter"] ] myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ] @@ -2751,10 +2781,12 @@ def new_datacenter(mydb, datacenter_descriptor): module_info = None try: module = "vimconn_" + datacenter_type - module_info = imp.find_module(module) + pkg = __import__("osm_ro." + module) + vim_conn = getattr(pkg, module) + # module_info = imp.find_module(module, [__file__[:__file__.rfind("/")]]) except (IOError, ImportError): - if module_info and module_info[0]: - file.close(module_info[0]) + # if module_info and module_info[0]: + # file.close(module_info[0]) raise NfvoException("Incorrect datacenter type '{}'. Plugin '{}'.py not installed".format(datacenter_type, module), HTTP_Bad_Request) datacenter_id = mydb.new_row("datacenters", datacenter_descriptor, add_uuid=True) @@ -2762,20 +2794,24 @@ def new_datacenter(mydb, datacenter_descriptor): def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): - #obtain data, check that only one exist + # obtain data, check that only one exist datacenter = mydb.get_table_by_uuid_name('datacenters', datacenter_id_name) - #edit data + + # edit data datacenter_id = datacenter['uuid'] where={'uuid': datacenter['uuid']} + remove_port_mapping = False if "config" in datacenter_descriptor: - if datacenter_descriptor['config']!=None: + if datacenter_descriptor['config'] != None: try: new_config_dict = datacenter_descriptor["config"] #delete null fields to_delete=[] for k in new_config_dict: - if new_config_dict[k]==None: + if new_config_dict[k] == None: to_delete.append(k) + if k == 'sdn-controller': + remove_port_mapping = True config_text = datacenter.get("config") if not config_text: @@ -2787,7 +2823,16 @@ def edit_datacenter(mydb, datacenter_id_name, datacenter_descriptor): del config_dict[k] except Exception as e: raise NfvoException("Bad format at datacenter:config " + str(e), HTTP_Bad_Request) - datacenter_descriptor["config"]= yaml.safe_dump(config_dict,default_flow_style=True,width=256) if len(config_dict)>0 else None + if config_dict: + datacenter_descriptor["config"] = yaml.safe_dump(config_dict, default_flow_style=True, width=256) + else: + datacenter_descriptor["config"] = None + if remove_port_mapping: + try: + datacenter_sdn_port_mapping_delete(mydb, None, datacenter_id) + except ovimException as e: + logger.error("Error deleting datacenter-port-mapping " + str(e)) + mydb.update_rows('datacenters', datacenter_descriptor, where) return datacenter_id @@ -2796,12 +2841,16 @@ def delete_datacenter(mydb, datacenter): #get nfvo_tenant info datacenter_dict = mydb.get_table_by_uuid_name('datacenters', datacenter, 'datacenter') mydb.delete_row_by_id("datacenters", datacenter_dict['uuid']) + try: + datacenter_sdn_port_mapping_delete(mydb, None, datacenter_dict['uuid']) + except ovimException as e: + logger.error("Error deleting datacenter-port-mapping " + str(e)) return datacenter_dict['uuid'] + " " + datacenter_dict['name'] def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None): #get datacenter info - datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter) + datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, None, datacenter, vim_user=vim_username, vim_passwd=vim_password) datacenter_name = myvim["name"] create_vim_tenant = True if not vim_tenant_id and not vim_tenant_name else False @@ -2855,18 +2904,22 @@ def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id= datacenter_tenants_dict["uuid"] = id_ #fill tenants_datacenters table - tenants_datacenter_dict["datacenter_tenant_id"]=datacenter_tenants_dict["uuid"] + datacenter_tenant_id = datacenter_tenants_dict["uuid"] + tenants_datacenter_dict["datacenter_tenant_id"] = datacenter_tenant_id mydb.new_row('tenants_datacenters', tenants_datacenter_dict) # create thread datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_dict['uuid'], datacenter_id) # reload data thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid']) - new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, db=db, db_lock=db_lock, ovim=ovim) + new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, datacenter_tenant_id, + db=db, db_lock=db_lock, ovim=ovim) new_thread.start() thread_id = datacenter_tenants_dict["uuid"] vim_threads["running"][thread_id] = new_thread return datacenter_id -def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None, vim_username=None, vim_password=None, config=None): + +def edit_datacenter_to_tenant(mydb, nfvo_tenant, datacenter_id, vim_tenant_id=None, vim_tenant_name=None, + vim_username=None, vim_password=None, config=None): #Obtain the data of this datacenter_tenant_id vim_data = mydb.get_rows( SELECT=("datacenter_tenants.vim_tenant_name", "datacenter_tenants.vim_tenant_id", "datacenter_tenants.user", @@ -3048,6 +3101,137 @@ def datacenter_new_netmap(mydb, tenant_id, datacenter, action_dict=None): net_list.append(net_nfvo) return net_list +def get_sdn_net_id(mydb, tenant_id, datacenter, network_id): + # obtain all network data + try: + if utils.check_valid_uuid(network_id): + filter_dict = {"id": network_id} + else: + filter_dict = {"name": network_id} + + datacenter_id, myvim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter) + network = myvim.get_network_list(filter_dict=filter_dict) + except vimconn.vimconnException as e: + print "vim_action Not possible to get_%s_list from VIM: %s " % (item, str(e)) + raise NfvoException("Not possible to get_{}_list from VIM: {}".format(item, str(e)), e.http_code) + + # ensure the network is defined + if len(network) == 0: + raise NfvoException("Network {} is not present in the system".format(network_id), + HTTP_Bad_Request) + + # ensure there is only one network with the provided name + if len(network) > 1: + raise NfvoException("Multiple networks present in vim identified by {}".format(network_id), HTTP_Bad_Request) + + # ensure it is a dataplane network + if network[0]['type'] != 'data': + return None + + # ensure we use the id + network_id = network[0]['id'] + + # search in dabase mano_db in table instance nets for the sdn_net_id that corresponds to the vim_net_id==network_id + # and with instance_scenario_id==NULL + #search_dict = {'vim_net_id': network_id, 'instance_scenario_id': None} + search_dict = {'vim_net_id': network_id} + + try: + #sdn_network_id = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict)[0]['sdn_net_id'] + result = mydb.get_rows(SELECT=('sdn_net_id',), FROM='instance_nets', WHERE=search_dict) + except db_base_Exception as e: + raise NfvoException("db_base_Exception obtaining SDN network to associated to vim network {}".format( + network_id) + str(e), HTTP_Internal_Server_Error) + + sdn_net_counter = 0 + for net in result: + if net['sdn_net_id'] != None: + sdn_net_counter+=1 + sdn_net_id = net['sdn_net_id'] + + if sdn_net_counter == 0: + return None + elif sdn_net_counter == 1: + return sdn_net_id + else: + raise NfvoException("More than one SDN network is associated to vim network {}".format( + network_id), HTTP_Internal_Server_Error) + +def get_sdn_controller_id(mydb, datacenter): + # Obtain sdn controller id + config = mydb.get_rows(SELECT=('config',), FROM='datacenters', WHERE={'uuid': datacenter})[0].get('config', '{}') + if not config: + return None + + return yaml.load(config).get('sdn-controller') + +def vim_net_sdn_attach(mydb, tenant_id, datacenter, network_id, descriptor): + try: + sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) + if not sdn_network_id: + raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), HTTP_Internal_Server_Error) + + #Obtain sdn controller id + controller_id = get_sdn_controller_id(mydb, datacenter) + if not controller_id: + raise NfvoException("No SDN controller is set for datacenter {}".format(datacenter), HTTP_Internal_Server_Error) + + #Obtain sdn controller info + sdn_controller = ovim.show_of_controller(controller_id) + + port_data = { + 'name': 'external_port', + 'net_id': sdn_network_id, + 'ofc_id': controller_id, + 'switch_dpid': sdn_controller['dpid'], + 'switch_port': descriptor['port'] + } + + if 'vlan' in descriptor: + port_data['vlan'] = descriptor['vlan'] + if 'mac' in descriptor: + port_data['mac'] = descriptor['mac'] + + result = ovim.new_port(port_data) + except ovimException as e: + raise NfvoException("ovimException attaching SDN network {} to vim network {}".format( + sdn_network_id, network_id) + str(e), HTTP_Internal_Server_Error) + except db_base_Exception as e: + raise NfvoException("db_base_Exception attaching SDN network to vim network {}".format( + network_id) + str(e), HTTP_Internal_Server_Error) + + return 'Port uuid: '+ result + +def vim_net_sdn_detach(mydb, tenant_id, datacenter, network_id, port_id=None): + if port_id: + filter = {'uuid': port_id} + else: + sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, network_id) + if not sdn_network_id: + raise NfvoException("No SDN network is associated to vim-network {}".format(network_id), + HTTP_Internal_Server_Error) + #in case no port_id is specified only ports marked as 'external_port' will be detached + filter = {'name': 'external_port', 'net_id': sdn_network_id} + + try: + port_list = ovim.get_ports(columns={'uuid'}, filter=filter) + except ovimException as e: + raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), + HTTP_Internal_Server_Error) + + if len(port_list) == 0: + raise NfvoException("No ports attached to the network {} were found with the requested criteria".format(network_id), + HTTP_Bad_Request) + + port_uuid_list = [] + for port in port_list: + try: + port_uuid_list.append(port['uuid']) + ovim.delete_port(port['uuid']) + except ovimException as e: + raise NfvoException("ovimException deleting port {} for net {}. ".format(port['uuid'], network_id) + str(e), HTTP_Internal_Server_Error) + + return 'Detached ports uuid: {}'.format(','.join(port_uuid_list)) def vim_action_get(mydb, tenant_id, datacenter, item, name): #get datacenter info @@ -3062,9 +3246,32 @@ def vim_action_get(mydb, tenant_id, datacenter, item, name): if item=="networks": #filter_dict['tenant_id'] = myvim['tenant_id'] content = myvim.get_network_list(filter_dict=filter_dict) + + if len(content) == 0: + raise NfvoException("Network {} is not present in the system. ".format(name), + HTTP_Bad_Request) + + #Update the networks with the attached ports + for net in content: + sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, net['id']) + if sdn_network_id != None: + try: + #port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan'}, filter={'name': 'external_port', 'net_id': sdn_network_id}) + port_list = ovim.get_ports(columns={'uuid', 'switch_port', 'vlan','name'}, filter={'net_id': sdn_network_id}) + except ovimException as e: + raise NfvoException("ovimException obtaining external ports for net {}. ".format(network_id) + str(e), HTTP_Internal_Server_Error) + #Remove field name and if port name is external_port save it as 'type' + for port in port_list: + if port['name'] == 'external_port': + port['type'] = "External" + del port['name'] + net['sdn_network_id'] = sdn_network_id + net['sdn_attached_ports'] = port_list + elif item=="tenants": content = myvim.get_tenant_list(filter_dict=filter_dict) elif item == "images": + content = myvim.get_image_list(filter_dict=filter_dict) else: raise NfvoException(item + "?", HTTP_Method_Not_Allowed) @@ -3101,6 +3308,36 @@ def vim_action_delete(mydb, tenant_id, datacenter, item, name): try: if item=="networks": + # If there is a SDN network associated to the vim-network, proceed to clear the relationship and delete it + sdn_network_id = get_sdn_net_id(mydb, tenant_id, datacenter, item_id) + if sdn_network_id != None: + #Delete any port attachment to this network + try: + port_list = ovim.get_ports(columns={'uuid'}, filter={'net_id': sdn_network_id}) + except ovimException as e: + raise NfvoException( + "ovimException obtaining external ports for net {}. ".format(network_id) + str(e), + HTTP_Internal_Server_Error) + + # By calling one by one all ports to be detached we ensure that not only the external_ports get detached + for port in port_list: + vim_net_sdn_detach(mydb, tenant_id, datacenter, item_id, port['uuid']) + + #Delete from 'instance_nets' the correspondence between the vim-net-id and the sdn-net-id + try: + mydb.delete_row(FROM='instance_nets', WHERE={'instance_scenario_id': None, 'sdn_net_id': sdn_network_id, 'vim_net_id': item_id}) + except db_base_Exception as e: + raise NfvoException("Error deleting correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) + + str(e), HTTP_Internal_Server_Error) + + #Delete the SDN network + try: + ovim.delete_network(sdn_network_id) + except ovimException as e: + logger.error("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), exc_info=True) + raise NfvoException("ovimException deleting SDN network={} ".format(sdn_network_id) + str(e), + HTTP_Internal_Server_Error) + content = myvim.delete_network(item_id) elif item=="tenants": content = myvim.delete_tenant(item_id) @@ -3130,6 +3367,32 @@ def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): net_ipprofile = net.pop("ip_profile", None) net_vlan = net.pop("vlan", None) content = myvim.new_network(net_name, net_type, net_ipprofile, shared=net_public, vlan=net_vlan) #, **net) + + #If the datacenter has a SDN controller defined and the network is of dataplane type, then create the sdn network + if get_sdn_controller_id(mydb, datacenter) != None and (net_type == 'data' or net_type == 'ptp'): + try: + sdn_network = {} + sdn_network['vlan'] = net_vlan + sdn_network['type'] = net_type + sdn_network['name'] = net_name + ovim_content = ovim.new_network(sdn_network) + except ovimException as e: + self.logger.error("ovimException creating SDN network={} ".format( + sdn_network) + str(e), exc_info=True) + raise NfvoException("ovimException creating SDN network={} ".format(sdn_network) + str(e), + HTTP_Internal_Server_Error) + + # Save entry in in dabase mano_db in table instance_nets to stablish a dictionary vim_net_id <->sdn_net_id + # use instance_scenario_id=None to distinguish from real instaces of nets + correspondence = {'instance_scenario_id': None, 'sdn_net_id': ovim_content, 'vim_net_id': content} + #obtain datacenter_tenant_id + correspondence['datacenter_tenant_id'] = mydb.get_rows(SELECT=('uuid',), FROM='datacenter_tenants', WHERE={'datacenter_id': datacenter})[0]['uuid'] + + try: + mydb.new_row('instance_nets', correspondence, add_uuid=True) + except db_base_Exception as e: + raise NfvoException("Error saving correspondence for VIM/SDN dataplane networks{}: ".format(correspondence) + + str(e), HTTP_Internal_Server_Error) elif item=="tenants": tenant = descriptor["tenant"] content = myvim.new_tenant(tenant["name"], tenant.get("description")) @@ -3223,9 +3486,11 @@ def datacenter_sdn_port_mapping_list(mydb, tenant_id, datacenter_id): result["sdn-controller"] = controller_id result["dpid"] = sdn_controller["dpid"] - if result["sdn-controller"] == None or result["dpid"] == None: - raise NfvoException("Not all SDN controller information for datacenter {} could be found: {}".format(datacenter_id, result), - HTTP_Internal_Server_Error) + if result["sdn-controller"] == None: + raise NfvoException("SDN controller is not defined for datacenter {}".format(datacenter_id), HTTP_Bad_Request) + if result["dpid"] == None: + raise NfvoException("It was not possible to determine DPID for SDN controller {}".format(result["sdn-controller"]), + HTTP_Internal_Server_Error) if len(maps) == 0: return result