- for vm in sce_vnf['vms']:
- myVMDict = {}
- 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"
- myVMDict['name'] = myVMDict['name'][0:255] # limit name length
- #create image at vim in case it not exist
- image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
- image_id = create_or_use_image(mydb, {datacenter_id: vim}, image_dict, [], True)
- vm['vim_image_id'] = image_id
-
- # create flavor at vim in case it not exist
- flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
- if flavor_dict['extended']!=None:
- flavor_dict['extended'] = yaml.load(flavor_dict['extended'])
- flavor_id = create_or_use_flavor(mydb, {datacenter_id: vim}, flavor_dict, rollbackList, True)
-
- # Obtain information for additional disks
- extended_flavor_dict = mydb.get_rows(FROM='datacenters_flavors', SELECT=('extended',), WHERE={'vim_id': flavor_id})
- if not extended_flavor_dict:
- raise NfvoException("flavor '{}' not found".format(flavor_id), HTTP_Not_Found)
- return
-
- # extended_flavor_dict_yaml = yaml.load(extended_flavor_dict[0])
- myVMDict['disks'] = None
- extended_info = extended_flavor_dict[0]['extended']
- if extended_info != None:
- extended_flavor_dict_yaml = yaml.load(extended_info)
- if 'disks' in extended_flavor_dict_yaml:
- myVMDict['disks'] = extended_flavor_dict_yaml['disks']
-
- vm['vim_flavor_id'] = flavor_id
- myVMDict['imageRef'] = vm['vim_image_id']
- myVMDict['flavorRef'] = vm['vim_flavor_id']
- myVMDict['availability_zone'] = vm.get('availability_zone')
- myVMDict['networks'] = []
- task_depends_on = []
- # TODO ALF. connect_mgmt_interfaces. Connect management interfaces if this is true
- db_vm_ifaces = []
- for iface in vm['interfaces']:
- netDict = {}
- if iface['type']=="data":
- netDict['type'] = iface['model']
- elif "model" in iface and iface["model"]!=None:
- netDict['model']=iface['model']
- # TODO in future, remove this because mac_address will not be set, and the type of PV,VF
- # is obtained from iterface table model
- # discover type of interface looking at flavor
- for numa in flavor_dict.get('extended',{}).get('numas',[]):
- for flavor_iface in numa.get('interfaces',[]):
- if flavor_iface.get('name') == iface['internal_name']:
- if flavor_iface['dedicated'] == 'yes':
- netDict['type']="PF" #passthrough
- elif flavor_iface['dedicated'] == 'no':
- netDict['type']="VF" #siov
- elif flavor_iface['dedicated'] == 'yes:sriov':
- netDict['type']="VFnotShared" #sriov but only one sriov on the PF
- netDict["mac_address"] = flavor_iface.get("mac_address")
- break;
- netDict["use"]=iface['type']
- if netDict["use"]=="data" and not netDict.get("type"):
- #print "netDict", netDict
- #print "iface", iface
- e_text = "Cannot determine the interface type PF or VF of VNF '%s' VM '%s' iface '%s'" %(sce_vnf['name'], vm['name'], iface['internal_name'])
- if flavor_dict.get('extended')==None:
- raise NfvoException(e_text + "After database migration some information is not available. \
- Try to delete and create the scenarios and VNFs again", HTTP_Conflict)
- else:
- raise NfvoException(e_text, HTTP_Internal_Server_Error)
- if netDict["use"]=="mgmt" or netDict["use"]=="bridge":
- netDict["type"]="virtual"
- if "vpci" in iface and iface["vpci"] is not None:
- netDict['vpci'] = iface['vpci']
- if "mac" in iface and iface["mac"] is not None:
- netDict['mac_address'] = iface['mac']
- if "port-security" in iface and iface["port-security"] is not None:
- netDict['port_security'] = iface['port-security']
- if "floating-ip" in iface and iface["floating-ip"] is not None:
- netDict['floating_ip'] = iface['floating-ip']
- netDict['name'] = iface['internal_name']
- if iface['net_id'] is None:
- for vnf_iface in sce_vnf["interfaces"]:
- # print iface
- # print vnf_iface
- if vnf_iface['interface_id']==iface['uuid']:
- netDict['net_id'] = "TASK-{}".format(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id])
- instance_net_id = sce_net2instance[ vnf_iface['sce_net_id'] ][datacenter_id]
- task_depends_on.append(net2task_id['scenario'][ vnf_iface['sce_net_id'] ][datacenter_id])
- break
- else:
- netDict['net_id'] = "TASK-{}".format(net2task_id[ sce_vnf['uuid'] ][ iface['net_id'] ])
- instance_net_id = vnf_net2instance[ sce_vnf['uuid'] ][ iface['net_id'] ]
- task_depends_on.append(net2task_id[sce_vnf['uuid'] ][ iface['net_id']])
- # skip bridge ifaces not connected to any net
- if 'net_id' not in netDict or netDict['net_id']==None:
- continue
- myVMDict['networks'].append(netDict)
- db_vm_iface={
- # "uuid"
- # 'instance_vm_id': instance_vm_uuid,
- "instance_net_id": instance_net_id,
- 'interface_id': iface['uuid'],
- # 'vim_interface_id': ,
- 'type': 'external' if iface['external_name'] is not None else 'internal',
- 'ip_address': iface.get('ip_address'),
- 'floating_ip': int(iface.get('floating-ip', False)),
- 'port_security': int(iface.get('port-security', True))
- }
- db_vm_ifaces.append(db_vm_iface)
- # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
- # print myVMDict['name']
- # print "networks", yaml.safe_dump(myVMDict['networks'], indent=4, default_flow_style=False)
- # print "interfaces", yaml.safe_dump(vm['interfaces'], indent=4, default_flow_style=False)
- # print ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
- if vm.get("boot_data"):
- cloud_config_vm = unify_cloud_config(vm["boot_data"], cloud_config)
- else:
- cloud_config_vm = cloud_config
- if myVMDict.get('availability_zone'):
- av_index = vnf_availability_zones.index(myVMDict['availability_zone'])
- 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),
- 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]
- for net in myVMDict['networks']:
- if "vim_id" in net:
- for iface in vm['interfaces']:
- if net["name"]==iface["internal_name"]:
- iface["vim_id"]=net["vim_id"]
- break
- vm_uuid = str(uuid4())
- uuid_list.append(vm_uuid)
- db_vm = {
- "uuid": vm_uuid,
- 'instance_vnf_id': vnf_uuid,
- #TODO delete "vim_vm_id": vm_id,
- "vm_id": vm["uuid"],
- # "status":
- }
- db_instance_vms.append(db_vm)
-
- iface_index = 0
- for db_vm_iface in db_vm_ifaces:
- iface_uuid = str(uuid4())
- uuid_list.append(iface_uuid)
- db_vm_iface_instance = {
- "uuid": iface_uuid,
- "instance_vm_id": vm_uuid
- }
- db_vm_iface_instance.update(db_vm_iface)
- if db_vm_iface_instance.get("ip_address"): # increment ip_address
- ip = db_vm_iface_instance.get("ip_address")
- i = ip.rfind(".")
- if i > 0:
- try:
- i += 1
- ip = ip[i:] + str(int(ip[:i]) +1)
- db_vm_iface_instance["ip_address"] = ip
- except:
- db_vm_iface_instance["ip_address"] = None
- db_instance_interfaces.append(db_vm_iface_instance)
- myVMDict['networks'][iface_index]["uuid"] = iface_uuid
- iface_index += 1
-
- db_vim_action = {
- "instance_action_id": instance_action_id,
- "task_index": task_index,
- "datacenter_vim_id": myvim_thread_id,
- "action": "CREATE",
- "status": "SCHEDULED",
- "item": "instance_vms",
- "item_id": vm_uuid,
- "extra": yaml.safe_dump({"params": task_params, "depends_on": task_depends_on},
- default_flow_style=True, width=256)
- }
- task_index += 1
- db_vim_actions.append(db_vim_action)
-
- scenarioDict["datacenter2tenant"] = myvim_threads_id
-
- db_instance_action["number_tasks"] = task_index
- db_instance_scenario['datacenter_tenant_id'] = myvim_threads_id[default_datacenter_id]
- db_instance_scenario['datacenter_id'] = default_datacenter_id
- db_tables=[
- {"instance_scenarios": db_instance_scenario},
- {"instance_vnfs": db_instance_vnfs},
- {"instance_nets": db_instance_nets},
- {"ip_profiles": db_ip_profiles},
- {"instance_vms": db_instance_vms},
- {"instance_interfaces": db_instance_interfaces},
- {"instance_actions": db_instance_action},
- {"vim_actions": db_vim_actions}
- ]
-
- logger.debug("create_instance done DB tables: %s",
- yaml.safe_dump(db_tables, indent=4, default_flow_style=False) )
- mydb.new_rows(db_tables, uuid_list)
- for myvim_thread_id in myvim_threads_id.values():
- vim_threads["running"][myvim_thread_id].insert_task(db_vim_actions)
-
- returned_instance = mydb.get_instance_scenario(instance_uuid)
- returned_instance["action_id"] = instance_action_id
- return returned_instance
- except (NfvoException, vimconn.vimconnException, db_base_Exception) as e:
- message = rollback(mydb, myvims, rollbackList)
- if isinstance(e, db_base_Exception):
- error_text = "database Exception"
- elif isinstance(e, vimconn.vimconnException):
- error_text = "VIM Exception"
- else:
- error_text = "Exception"
- error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
- # logger.error("create_instance: %s", error_text)
- raise NfvoException(error_text, e.http_code)