X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_ro%2Fnfvo.py;h=76e7244b386bb27a6b035642368b5b78917efb45;hb=55d234c0d00b9936a83b632513379b9e13999126;hp=282b7f380ac6abc4943b0e09bf07f4949fa0be7a;hpb=1c848c01d41949c0498a342c38327b8e9dae03bd;p=osm%2FRO.git diff --git a/osm_ro/nfvo.py b/osm_ro/nfvo.py index 282b7f38..76e7244b 100644 --- a/osm_ro/nfvo.py +++ b/osm_ro/nfvo.py @@ -52,7 +52,8 @@ from Crypto.PublicKey import RSA import osm_im.vnfd as vnfd_catalog import osm_im.nsd as nsd_catalog from pyangbind.lib.serialise import pybindJSONDecoder -from itertools import chain +from copy import deepcopy + global global_config global vimconn_imported @@ -306,7 +307,7 @@ def get_imagelist(mydb, vnf_id, nfvo_tenant=None): def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, datacenter_tenant_id=None, - vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None): + vim_tenant=None, vim_tenant_name=None, vim_user=None, vim_passwd=None, ignore_errors=False): '''Obtain a dictionary of VIM (datacenter) classes with some of the input parameters return dictionary with {datacenter_id: vim_class, ... }. vim_class contain: 'nfvo_tenant_id','datacenter_id','vim_tenant_id','vim_url','vim_url_admin','datacenter_name','type','user','passwd' @@ -350,6 +351,10 @@ def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, da except (IOError, ImportError) as e: # if module_info and module_info[0]: # file.close(module_info[0]) + if ignore_errors: + logger.error("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( + vim["type"], module, type(e).__name__, str(e))) + continue raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format( vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request) @@ -372,6 +377,9 @@ def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, da config=extra, persistent_info=persistent_info ) except Exception as e: + if ignore_errors: + logger.error("Error at VIM {}; {}: {}".format(vim["type"], type(e).__name__, str(e))) + continue http_code = HTTP_Internal_Server_Error if isinstance(e, vimconn.vimconnException): http_code = e.http_code @@ -1141,8 +1149,8 @@ def new_vnfd_v3(mydb, tenant_id, vnf_descriptor): vnf=vnfd_id, vdu=vdu_id, iface=iface["name"], cp=iface.get("internal-connection-point-ref"), msg=str(e)), HTTP_Bad_Request) - if iface.get("position") is not None: - db_interface["created_at"] = int(iface.get("position")) - 1000 + if iface.get("position"): + db_interface["created_at"] = int(iface.get("position")) * 50 if iface.get("mac-address"): db_interface["mac"] = str(iface.get("mac-address")) db_interfaces.append(db_interface) @@ -1309,7 +1317,7 @@ def new_vnf(mydb, tenant_id, vnf_descriptor): vnf_descriptor['vnf']['tenant_id'] = tenant_id # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter if global_config["auto_push_VNF_to_VIMs"]: - vims = get_vim(mydb, tenant_id) + vims = get_vim(mydb, tenant_id, ignore_errors=True) # Step 4. Review the descriptor and add missing fields #print vnf_descriptor @@ -1446,7 +1454,7 @@ def new_vnf_v02(mydb, tenant_id, vnf_descriptor): vnf_descriptor['vnf']['tenant_id'] = tenant_id # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter if global_config["auto_push_VNF_to_VIMs"]: - vims = get_vim(mydb, tenant_id) + vims = get_vim(mydb, tenant_id, ignore_errors=True) # Step 4. Review the descriptor and add missing fields #print vnf_descriptor @@ -1630,7 +1638,7 @@ def delete_vnf(mydb,tenant_id,vnf_id,datacenter=None,vim_tenant=None): if tenant_id != "any": check_tenant(mydb, tenant_id) # Get the URL of the VIM from the nfvo_tenant and the datacenter - vims = get_vim(mydb, tenant_id) + vims = get_vim(mydb, tenant_id, ignore_errors=True) else: vims={} @@ -3147,7 +3155,7 @@ def create_instance(mydb, tenant_id, instance_dict): "created": create_network, 'datacenter_id': datacenter_id, 'datacenter_tenant_id': myvim_thread_id, - 'status': 'BUILD' if create_network else "ACTIVE" + 'status': 'BUILD' # if create_network else "ACTIVE" } db_instance_nets.append(db_net) db_vim_action = { @@ -3200,8 +3208,8 @@ def create_instance(mydb, tenant_id, instance_dict): "net2task_id": net2task_id, "sce_net2instance": sce_net2instance, } - sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) - for sce_vnf in sce_vnf_list: + # sce_vnf_list = sorted(scenarioDict['vnfs'], key=lambda k: k['name']) + for sce_vnf in scenarioDict['vnfs']: # sce_vnf_list: instantiate_vnf(mydb, sce_vnf, vnf_params, vnf_params_out, rollbackList) task_index = vnf_params_out["task_index"] uuid_list = vnf_params_out["uuid_list"] @@ -3533,7 +3541,8 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): for vm in sce_vnf['vms']: myVMDict = {} - myVMDict['name'] = "{}.{}.{}".format(instance_name[:64], sce_vnf['name'][:64], vm["name"][:64]) + sce_vnf_name = sce_vnf['member_vnf_index'] if sce_vnf['member_vnf_index'] else sce_vnf['name'] + myVMDict['name'] = "{}-{}-{}".format(instance_name[:64], sce_vnf_name[:64], vm["name"][:64]) myVMDict['description'] = myVMDict['name'][0:99] # if not startvms: # myVMDict['start'] = "no" @@ -3671,10 +3680,8 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): else: av_index = None for vm_index in range(0, vm.get('count', 1)): - vm_index_name = "" - if vm.get('count', 1) > 1: - vm_index_name += "." + chr(97 + vm_index) - task_params = (myVMDict['name'] + vm_index_name, myVMDict['description'], myVMDict.get('start', None), + vm_name = myVMDict['name'] + "-" + str(vm_index+1) + task_params = (vm_name, myVMDict['description'], myVMDict.get('start', None), myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'], cloud_config_vm, myVMDict['disks'], av_index, vnf_availability_zones) # put interface uuid back to scenario[vnfs][vms[[interfaces] @@ -3691,6 +3698,7 @@ def instantiate_vnf(mydb, sce_vnf, params, params_out, rollbackList): 'instance_vnf_id': vnf_uuid, # TODO delete "vim_vm_id": vm_id, "vm_id": vm["uuid"], + "vim_name": vm_name, # "status": } db_instance_vms.append(db_vm) @@ -4017,6 +4025,26 @@ def delete_instance(mydb, tenant_id, instance_id): else: return "action_id={} instance {} deleted".format(instance_action_id, message) +def get_instance_id(mydb, tenant_id, instance_id): + global ovim + #check valid tenant_id + check_tenant(mydb, tenant_id) + #obtain data + + instance_dict = mydb.get_instance_scenario(instance_id, tenant_id, verbose=True) + for net in instance_dict["nets"]: + if net.get("sdn_net_id"): + net_sdn = ovim.show_network(net["sdn_net_id"]) + net["sdn_info"] = { + "admin_state_up": net_sdn.get("admin_state_up"), + "flows": net_sdn.get("flows"), + "last_error": net_sdn.get("last_error"), + "ports": net_sdn.get("ports"), + "type": net_sdn.get("type"), + "status": net_sdn.get("status"), + "vlan": net_sdn.get("vlan"), + } + return instance_dict def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenant=None): '''Refreshes a scenario instance. It modifies instanceDict''' @@ -4192,21 +4220,196 @@ def instance_action(mydb,nfvo_tenant,instance_id, action_dict): if len(vims) == 0: raise NfvoException("datacenter '{}' not found".format(str(instanceDict['datacenter_id'])), HTTP_Not_Found) myvim = vims.values()[0] + vm_result = {} + vm_error = 0 + vm_ok = 0 - if action_dict.get("create-vdu"): - for vdu in action_dict["create-vdu"]: + myvim_threads_id = {} + if action_dict.get("vdu-scaling"): + db_instance_vms = [] + db_vim_actions = [] + db_instance_interfaces = [] + instance_action_id = get_task_id() + db_instance_action = { + "uuid": instance_action_id, # same uuid for the instance and the action on create + "tenant_id": nfvo_tenant, + "instance_id": instance_id, + "description": "SCALE", + } + vm_result["instance_action_id"] = instance_action_id + task_index = 0 + for vdu in action_dict["vdu-scaling"]: vdu_id = vdu.get("vdu-id") + osm_vdu_id = vdu.get("osm_vdu_id") + member_vnf_index = vdu.get("member-vnf-index") vdu_count = vdu.get("count", 1) - # get from database TODO - # insert tasks TODO - pass + if vdu_id: + target_vm = mydb.get_rows( + FROM="instance_vms as vms join instance_vnfs as vnfs on vms.instance_vnf_id=vnfs.uuid", + WHERE={"vms.uuid": vdu_id}, + ORDER_BY="vms.created_at" + ) + if not target_vm: + raise NfvoException("Cannot find the vdu with id {}".format(vdu_id), HTTP_Not_Found) + else: + if not osm_vdu_id and not member_vnf_index: + raise NfvoException("Invalid imput vdu parameters. Must supply either 'vdu-id' of 'osm_vdu_id','member-vnf-index'") + target_vm = mydb.get_rows( + # SELECT=("ivms.uuid", "ivnfs.datacenter_id", "ivnfs.datacenter_tenant_id"), + FROM="instance_vms as ivms join instance_vnfs as ivnfs on ivms.instance_vnf_id=ivnfs.uuid"\ + " join sce_vnfs as svnfs on ivnfs.sce_vnf_id=svnfs.uuid"\ + " join vms on ivms.vm_id=vms.uuid", + WHERE={"vms.osm_id": osm_vdu_id, "svnfs.member_vnf_index": member_vnf_index}, + ORDER_BY="ivms.created_at" + ) + if not target_vm: + raise NfvoException("Cannot find the vdu with osm_vdu_id {} and member-vnf-index {}".format(osm_vdu_id, member_vnf_index), HTTP_Not_Found) + vdu_id = target_vm[-1]["uuid"] + vm_result[vdu_id] = {"created": [], "deleted": [], "description": "scheduled"} + target_vm = target_vm[-1] + datacenter = target_vm["datacenter_id"] + myvim_threads_id[datacenter], _ = get_vim_thread(mydb, nfvo_tenant, datacenter) + if vdu["type"] == "delete": + # look for nm + vm_interfaces = None + for sce_vnf in instanceDict['vnfs']: + for vm in sce_vnf['vms']: + if vm["uuid"] == vdu_id: + vm_interfaces = vm["interfaces"] + break + + db_vim_action = { + "instance_action_id": instance_action_id, + "task_index": task_index, + "datacenter_vim_id": target_vm["datacenter_tenant_id"], + "action": "DELETE", + "status": "SCHEDULED", + "item": "instance_vms", + "item_id": target_vm["uuid"], + "extra": yaml.safe_dump({"params": vm_interfaces}, + default_flow_style=True, width=256) + } + task_index += 1 + db_vim_actions.append(db_vim_action) + vm_result[vdu_id]["deleted"].append(vdu_id) + # delete from database + db_instance_vms.append({"TO-DELETE": vdu_id}) + + else: # vdu["type"] == "create": + iface2iface = {} + where = {"item": "instance_vms", "item_id": target_vm["uuid"], "action": "CREATE"} + + vim_action_to_clone = mydb.get_rows(FROM="vim_actions", WHERE=where) + if not vim_action_to_clone: + raise NfvoException("Cannot find the vim_action at database with {}".format(where), HTTP_Internal_Server_Error) + vim_action_to_clone = vim_action_to_clone[0] + extra = yaml.safe_load(vim_action_to_clone["extra"]) + + # generate a new depends_on. Convert format TASK-Y into new format TASK-ACTION-XXXX.XXXX.Y + # TODO do the same for flavor and image when available + task_depends_on = [] + task_params = extra["params"] + task_params_networks = deepcopy(task_params[5]) + for iface in task_params[5]: + if iface["net_id"].startswith("TASK-"): + if "." not in iface["net_id"]: + task_depends_on.append("{}.{}".format(vim_action_to_clone["instance_action_id"], + iface["net_id"][5:])) + iface["net_id"] = "TASK-{}.{}".format(vim_action_to_clone["instance_action_id"], + iface["net_id"][5:]) + else: + task_depends_on.append(iface["net_id"][5:]) + if "mac_address" in iface: + del iface["mac_address"] + + vm_ifaces_to_clone = mydb.get_rows(FROM="instance_interfaces", WHERE={"instance_vm_id": target_vm["uuid"]}) + for index in range(0, vdu_count): + vm_uuid = str(uuid4()) + vm_name = target_vm.get('vim_name') + try: + suffix = vm_name.rfind("-") + vm_name = vm_name[:suffix+1] + str(1 + int(vm_name[suffix+1:])) + except Exception: + pass + db_instance_vm = { + "uuid": vm_uuid, + 'instance_vnf_id': target_vm['instance_vnf_id'], + 'vm_id': target_vm['vm_id'], + 'vim_name': vm_name + } + db_instance_vms.append(db_instance_vm) + + for vm_iface in vm_ifaces_to_clone: + iface_uuid = str(uuid4()) + iface2iface[vm_iface["uuid"]] = iface_uuid + db_vm_iface = { + "uuid": iface_uuid, + 'instance_vm_id': vm_uuid, + "instance_net_id": vm_iface["instance_net_id"], + 'interface_id': vm_iface['interface_id'], + 'type': vm_iface['type'], + 'floating_ip': vm_iface['floating_ip'], + 'port_security': vm_iface['port_security'] + } + db_instance_interfaces.append(db_vm_iface) + task_params_copy = deepcopy(task_params) + for iface in task_params_copy[5]: + iface["uuid"] = iface2iface[iface["uuid"]] + # increment ip_address + if "ip_address" in iface: + ip = iface.get("ip_address") + i = ip.rfind(".") + if i > 0: + try: + i += 1 + ip = ip[i:] + str(int(ip[:i]) + 1) + iface["ip_address"] = ip + except: + iface["ip_address"] = None + if vm_name: + task_params_copy[0] = vm_name + db_vim_action = { + "instance_action_id": instance_action_id, + "task_index": task_index, + "datacenter_vim_id": vim_action_to_clone["datacenter_vim_id"], + "action": "CREATE", + "status": "SCHEDULED", + "item": "instance_vms", + "item_id": vm_uuid, + # ALF + # ALF + # TODO examinar parametros, quitar MAC o incrementar. Incrementar IP y colocar las dependencias con ACTION-asdfasd. + # ALF + # ALF + "extra": yaml.safe_dump({"params": task_params_copy, "depends_on": task_depends_on}, default_flow_style=True, width=256) + } + task_index += 1 + db_vim_actions.append(db_vim_action) + vm_result[vdu_id]["created"].append(vm_uuid) + + db_instance_action["number_tasks"] = task_index + db_tables = [ + {"instance_vms": db_instance_vms}, + {"instance_interfaces": db_instance_interfaces}, + {"instance_actions": db_instance_action}, + # TODO revise sfps + # {"instance_sfis": db_instance_sfis}, + # {"instance_sfs": db_instance_sfs}, + # {"instance_classifications": db_instance_classifications}, + # {"instance_sfps": db_instance_sfps}, + {"vim_actions": db_vim_actions} + ] + logger.debug("create_vdu done DB tables: %s", + yaml.safe_dump(db_tables, indent=4, default_flow_style=False)) + mydb.new_rows(db_tables, []) + for myvim_thread in myvim_threads_id.values(): + vim_threads["running"][myvim_thread].insert_task(db_vim_actions) + + return vm_result input_vnfs = action_dict.pop("vnfs", []) input_vms = action_dict.pop("vms", []) action_over_all = True if not input_vnfs and not input_vms else False - vm_result = {} - vm_error = 0 - vm_ok = 0 for sce_vnf in instanceDict['vnfs']: for vm in sce_vnf['vms']: if not action_over_all and sce_vnf['uuid'] not in input_vnfs and sce_vnf['vnf_name'] not in input_vnfs and \ @@ -4300,7 +4503,7 @@ def instance_action_get(mydb, nfvo_tenant, instance_id, action_id): raise NfvoException("Not found any action with this criteria", HTTP_Not_Found) vim_actions = mydb.get_rows(FROM="vim_actions", WHERE={"instance_action_id": action_id}) rows[0]["vim_actions"] = vim_actions - return {"ations": rows} + return {"actions": rows} def create_or_use_console_proxy_thread(console_server, console_port): @@ -5071,13 +5274,15 @@ def datacenter_sdn_port_mapping_set(mydb, tenant_id, datacenter_id, sdn_port_map element = dict() element["compute_node"] = compute_node["compute_node"] for port in compute_node["ports"]: - element["pci"] = port.get("pci") + pci = port.get("pci") element["switch_port"] = port.get("switch_port") element["switch_mac"] = port.get("switch_mac") - if not element["pci"] or not (element["switch_port"] or element["switch_mac"]): + if not pci or not (element["switch_port"] or element["switch_mac"]): raise NfvoException ("The mapping must contain the 'pci' and at least one of the elements 'switch_port'" " or 'switch_mac'", HTTP_Bad_Request) - maps.append(dict(element)) + for pci_expanded in utils.expand_brackets(pci): + element["pci"] = pci_expanded + maps.append(dict(element)) return ovim.set_of_port_mapping(maps, ofc_id=sdn_controller_id, switch_dpid=switch_dpid, region=datacenter_id)