- #We should use the dictionary as input parameter for new_tenant_network
- result, network_id = myvim.new_tenant_network(myNetName, myNetType)
- if result < 0:
- error_text="Error creating network: %s." % network_id
- _, message = rollback(mydb, vims, rollbackList)
- error_text += message
- print "start_scenario: " + error_text
- return result, error_text
- print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
- net['vim_id'] = network_id
- if sce_vnf['uuid'] not in auxNetDict:
- auxNetDict[sce_vnf['uuid']] = {}
- auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
- rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
-
- print "auxNetDict:"
- print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
-
- print "3. Creating new vm instances in the VIM"
- #myvim.new_tenant_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
- i = 0
- for sce_vnf in scenarioDict['vnfs']:
- for vm in sce_vnf['vms']:
- i += 1
- myVMDict = {}
- #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
- myVMDict['name'] = "%s.%s.%d" % (instance_scenario_name,sce_vnf['name'],i)
- #myVMDict['description'] = vm['description']
- myVMDict['description'] = myVMDict['name'][0:99]
- if not startvms:
- myVMDict['start'] = "no"
- myVMDict['name'] = myVMDict['name'][0:255] #limit name length
- print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
-
- #create image at vim in case it not exist
- res, image_dict = mydb.get_table_by_uuid_name("images", vm['image_id'])
- if res<0:
- print "start_scenario error getting image", image_dict
- return res, image_dict
- res, image_id = create_or_use_image(mydb, vims, image_dict, [], True)
- if res < 0:
- print "start_scenario error adding image to VIM", image_dict
- return res, image_id
- vm['vim_image_id'] = image_id
-
- #create flavor at vim in case it not exist
- res, flavor_dict = mydb.get_table_by_uuid_name("flavors", vm['flavor_id'])
- if res<0:
- print "start_scenario error getting flavor", flavor_dict
- return res, flavor_dict
- if flavor_dict['extended']!=None:
- flavor_dict['extended']= yaml.load(flavor_dict['extended'])
- res, flavor_id = create_or_use_flavor(mydb, vims, flavor_dict, [], True)
- if res < 0:
- print "start_scenario error adding flavor to VIM", flavor_dict
- return res, flavor_id
- vm['vim_flavor_id'] = flavor_id
-
-
- myVMDict['imageRef'] = vm['vim_image_id']
- myVMDict['flavorRef'] = vm['vim_flavor_id']
- myVMDict['networks'] = []
- 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:
- return -HTTP_Conflict, e_text + "After database migration some information is not available. \
- Try to delete and create the scenarios and VNFs again"
- else:
- return -HTTP_Internal_Server_Error, e_text
- 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']
- 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'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
- break
- else:
- netDict['net_id'] = auxNetDict[ 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)
- 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 ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
- result, vm_id = myvim.new_tenant_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
- myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
- if result < 0:
- error_text = "Error creating vm instance: %s." % vm_id
- _, message = rollback(mydb, vims, rollbackList)
- error_text += message
- print "start_scenario: " + error_text
- return result, error_text
- print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
- vm['vim_id'] = vm_id
- rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
- #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
-
- print "==================Deployment done=========="
- print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
- #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
- result,c = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
- if result <0:
- error_text = c + "."
- _, message = rollback(mydb, vims, rollbackList)
- error_text += message
- print "start_scenario: " + error_text
- return result, error_text
-
- return mydb.get_instance_scenario(c)
-
-def create_instance(mydb, tenant_id, instance_dict):
- print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
- scenario = instance_dict["scenario"]
- datacenter_id = None
- datacenter_name=None
- datacenter = instance_dict.get("datacenter")
- if datacenter:
- if af.check_valid_uuid(datacenter):
- datacenter_id = datacenter
- else:
- datacenter_name = datacenter
- result, vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, vim_tenant=None)
- if result < 0:
- print "start_scenario error. Datacenter not found"
- return result, vims
- elif result == 0:
- return -HTTP_Not_Found, "datacenter '%s' not found" % str(datacenter)
- elif result > 1:
- print "start_scenario error. Several datacenters available, you must concrete"
- return -HTTP_Bad_Request, "Several datacenters available, you must concrete"
- myvim = vims.values()[0]
- #myvim_tenant = myvim['tenant_id']
- datacenter_id = myvim['id']
- datacenter_name = myvim['name']
- datacenter_tenant_id = myvim['config']['datacenter_tenant_id']
+ #We should use the dictionary as input parameter for new_network
+ #print myNetDict
+ if not sce_net["external"]:
+ network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
+ #print "New VIM network created for scenario %s. Network id: %s" % (scenarioDict['name'],network_id)
+ sce_net['vim_id'] = network_id
+ auxNetDict['scenario'][sce_net['uuid']] = network_id
+ rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
+ sce_net["created"] = True
+ else:
+ if sce_net['vim_id'] == None:
+ error_text = "Error, datacenter '%s' does not have external network '%s'." % (datacenter_name, sce_net['name'])
+ _, message = rollback(mydb, vims, rollbackList)
+ logger.error("nfvo.start_scenario: %s", error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ logger.debug("Using existent VIM network for scenario %s. Network id %s", scenarioDict['name'],sce_net['vim_id'])
+ auxNetDict['scenario'][sce_net['uuid']] = sce_net['vim_id']
+
+ logger.debug("start_scenario 2. Creating new nets (vnf internal nets) in the VIM")
+ #For each vnf net, we create it and we add it to instanceNetlist.
+ for sce_vnf in scenarioDict['vnfs']:
+ for net in sce_vnf['nets']:
+ #print "Net name: %s. Description: %s" % (net["name"], net["description"])
+
+ myNetName = "%s.%s" % (instance_scenario_name,net['name'])
+ myNetName = myNetName[0:255] #limit length
+ myNetType = net['type']
+ myNetDict = {}
+ myNetDict["name"] = myNetName
+ myNetDict["type"] = myNetType
+ myNetDict["tenant_id"] = myvim_tenant
+ myNetIPProfile = net.get('ip_profile', None)
+ #print myNetDict
+ #TODO:
+ #We should use the dictionary as input parameter for new_network
+ network_id = myvim.new_network(myNetName, myNetType, myNetIPProfile)
+ #print "VIM network id for scenario %s: %s" % (scenarioDict['name'],network_id)
+ net['vim_id'] = network_id
+ if sce_vnf['uuid'] not in auxNetDict:
+ auxNetDict[sce_vnf['uuid']] = {}
+ auxNetDict[sce_vnf['uuid']][net['uuid']] = network_id
+ rollbackList.append({'what':'network','where':'vim','vim_id':datacenter_id,'uuid':network_id})
+ net["created"] = True
+
+ #print "auxNetDict:"
+ #print yaml.safe_dump(auxNetDict, indent=4, default_flow_style=False)
+
+ logger.debug("start_scenario 3. Creating new vm instances in the VIM")
+ #myvim.new_vminstance(self,vimURI,tenant_id,name,description,image_id,flavor_id,net_dict)
+ i = 0
+ for sce_vnf in scenarioDict['vnfs']:
+ for vm in sce_vnf['vms']:
+ i += 1
+ myVMDict = {}
+ #myVMDict['name'] = "%s-%s-%s" % (scenarioDict['name'],sce_vnf['name'], vm['name'])
+ myVMDict['name'] = "{}.{}.{}".format(instance_scenario_name,sce_vnf['name'],chr(96+i))
+ #myVMDict['description'] = vm['description']
+ myVMDict['description'] = myVMDict['name'][0:99]
+ if not startvms:
+ myVMDict['start'] = "no"
+ myVMDict['name'] = myVMDict['name'][0:255] #limit name length
+ #print "VM name: %s. Description: %s" % (myVMDict['name'], myVMDict['name'])
+
+ #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, vims, 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, vims, flavor_dict, [], True)
+ vm['vim_flavor_id'] = flavor_id
+
+
+ myVMDict['imageRef'] = vm['vim_image_id']
+ myVMDict['flavorRef'] = vm['vim_flavor_id']
+ myVMDict['networks'] = []
+ 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'] = auxNetDict['scenario'][ vnf_iface['sce_net_id'] ]
+ break
+ else:
+ netDict['net_id'] = auxNetDict[ 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)
+ #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 ">>>>>>>>>>>>>>>>>>>>>>>>>>>"
+ vm_id = myvim.new_vminstance(myVMDict['name'],myVMDict['description'],myVMDict.get('start', None),
+ myVMDict['imageRef'],myVMDict['flavorRef'],myVMDict['networks'])
+ #print "VIM vm instance id (server id) for scenario %s: %s" % (scenarioDict['name'],vm_id)
+ vm['vim_id'] = vm_id
+ rollbackList.append({'what':'vm','where':'vim','vim_id':datacenter_id,'uuid':vm_id})
+ #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
+
+ logger.debug("start scenario Deployment done")
+ #print yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)
+ #r,c = mydb.new_instance_scenario_as_a_whole(nfvo_tenant,scenarioDict['name'],scenarioDict)
+ instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_scenario_name, instance_scenario_description, scenarioDict)
+ return mydb.get_instance_scenario(instance_id)
+
+ except (db_base_Exception, vimconn.vimconnException) as e:
+ _, message = rollback(mydb, vims, rollbackList)
+ if isinstance(e, db_base_Exception):
+ error_text = "Exception at database"
+ else:
+ error_text = "Exception at VIM"
+ error_text += " {} {}. {}".format(type(e).__name__, str(e), message)
+ #logger.error("start_scenario %s", error_text)
+ raise NfvoException(error_text, e.http_code)
+
+
+def unify_cloud_config(cloud_config_preserve, cloud_config):
+ ''' join the cloud config information into cloud_config_preserve.
+ In case of conflict cloud_config_preserve preserves
+ None is admited
+ '''
+ if not cloud_config_preserve and not cloud_config:
+ return None
+
+ new_cloud_config = {"key-pairs":[], "users":[]}
+ # key-pairs
+ if cloud_config_preserve:
+ for key in cloud_config_preserve.get("key-pairs", () ):
+ if key not in new_cloud_config["key-pairs"]:
+ new_cloud_config["key-pairs"].append(key)
+ if cloud_config:
+ for key in cloud_config.get("key-pairs", () ):
+ if key not in new_cloud_config["key-pairs"]:
+ new_cloud_config["key-pairs"].append(key)
+ if not new_cloud_config["key-pairs"]:
+ del new_cloud_config["key-pairs"]
+
+ # users
+ if cloud_config:
+ new_cloud_config["users"] += cloud_config.get("users", () )
+ if cloud_config_preserve:
+ new_cloud_config["users"] += cloud_config_preserve.get("users", () )
+ index_to_delete = []
+ users = new_cloud_config.get("users", [])
+ for index0 in range(0,len(users)):
+ if index0 in index_to_delete:
+ continue
+ for index1 in range(index0+1,len(users)):
+ if index1 in index_to_delete:
+ continue
+ if users[index0]["name"] == users[index1]["name"]:
+ index_to_delete.append(index1)
+ for key in users[index1].get("key-pairs",()):
+ if "key-pairs" not in users[index0]:
+ users[index0]["key-pairs"] = [key]
+ elif key not in users[index0]["key-pairs"]:
+ users[index0]["key-pairs"].append(key)
+ index_to_delete.sort(reverse=True)
+ for index in index_to_delete:
+ del users[index]
+ if not new_cloud_config["users"]:
+ del new_cloud_config["users"]
+
+ #boot-data-drive
+ if cloud_config and cloud_config.get("boot-data-drive") != None:
+ new_cloud_config["boot-data-drive"] = cloud_config["boot-data-drive"]
+ if cloud_config_preserve and cloud_config_preserve.get("boot-data-drive") != None:
+ new_cloud_config["boot-data-drive"] = cloud_config_preserve["boot-data-drive"]
+
+ # user-data
+ if cloud_config and cloud_config.get("user-data") != None:
+ new_cloud_config["user-data"] = cloud_config["user-data"]
+ if cloud_config_preserve and cloud_config_preserve.get("user-data") != None:
+ new_cloud_config["user-data"] = cloud_config_preserve["user-data"]
+
+ # config files
+ new_cloud_config["config-files"] = []
+ if cloud_config and cloud_config.get("config-files") != None:
+ new_cloud_config["config-files"] += cloud_config["config-files"]
+ if cloud_config_preserve:
+ for file in cloud_config_preserve.get("config-files", ()):
+ for index in range(0, len(new_cloud_config["config-files"])):
+ if new_cloud_config["config-files"][index]["dest"] == file["dest"]:
+ new_cloud_config["config-files"][index] = file
+ break
+ else:
+ new_cloud_config["config-files"].append(file)
+ if not new_cloud_config["config-files"]:
+ del new_cloud_config["config-files"]
+ return new_cloud_config
+
+
+def get_vim_thread(tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
+ datacenter_id = None
+ datacenter_name = None
+ thread = None
+ if datacenter_id_name:
+ if utils.check_valid_uuid(datacenter_id_name):
+ datacenter_id = datacenter_id_name
+ else:
+ datacenter_name = datacenter_id_name
+ if datacenter_id:
+ thread = vim_threads["running"].get(datacenter_id + "." + tenant_id)
+ else:
+ for k, v in vim_threads["running"].items():
+ datacenter_tenant = k.split(".")
+ if datacenter_tenant[0] == datacenter_id and datacenter_tenant[1] == tenant_id:
+ if thread:
+ raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
+ thread = v
+ elif not datacenter_id and datacenter_tenant[1] == tenant_id:
+ if thread.datacenter_name == datacenter_name:
+ if thread:
+ raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
+ thread = v
+ if not thread:
+ raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
+ return thread
+
+
+def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
+ datacenter_id = None
+ datacenter_name = None
+ if datacenter_id_name:
+ if utils.check_valid_uuid(datacenter_id_name):
+ datacenter_id = datacenter_id_name
+ else:
+ datacenter_name = datacenter_id_name
+ vims = get_vim(mydb, tenant_id, datacenter_id, datacenter_name, **extra_filter)
+ if len(vims) == 0:
+ raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
+ elif len(vims)>1:
+ #print "nfvo.datacenter_action() error. Several datacenters found"
+ raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
+ return vims.keys()[0], vims.values()[0]
+
+
+def update(d, u):
+ '''Takes dict d and updates it with the values in dict u.'''
+ '''It merges all depth levels'''
+ for k, v in u.iteritems():
+ if isinstance(v, collections.Mapping):
+ r = update(d.get(k, {}), v)
+ d[k] = r
+ else:
+ d[k] = u[k]
+ return d
+
+
+def create_instance(mydb, tenant_id, instance_dict):
+ # print "Checking that nfvo_tenant_id exists and getting the VIM URI and the VIM tenant_id"
+ # logger.debug("Creating instance...")
+ scenario = instance_dict["scenario"]
+
+ #find main datacenter
+ myvims = {}
+ myvim_threads = {}
+ datacenter2tenant = {}
+ datacenter = instance_dict.get("datacenter")
+ default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
+ myvims[default_datacenter_id] = vim
+ myvim_threads[default_datacenter_id] = get_vim_thread(tenant_id, default_datacenter_id)
+ datacenter2tenant[default_datacenter_id] = vim['config']['datacenter_tenant_id']
+ #myvim_tenant = myvim['tenant_id']
+# default_datacenter_name = vim['name']