+ 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']
+ 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):
+ index_to_delete = []
+ users = 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]
+
+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 new_scenario_v03(mydb, tenant_id, scenario_dict):
+ scenario = scenario_dict["scenario"]
+ if tenant_id != "any":
+ check_tenant(mydb, tenant_id)
+ if "tenant_id" in scenario:
+ if scenario["tenant_id"] != tenant_id:
+ logger("Tenant '%s' not found", tenant_id)
+ raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(
+ scenario["tenant_id"], tenant_id), HTTP_Unauthorized)
+ else:
+ tenant_id=None
+
+#1: Check that VNF are present at database table vnfs and update content into scenario dict
+ for name,vnf in scenario["vnfs"].iteritems():
+ where={}
+ where_or={"tenant_id": tenant_id, 'public': "true"}
+ error_text = ""
+ error_pos = "'scenario':'vnfs':'" + name + "'"
+ if 'vnf_id' in vnf:
+ error_text += " 'vnf_id' " + vnf['vnf_id']
+ where['uuid'] = vnf['vnf_id']
+ if 'vnf_name' in vnf:
+ error_text += " 'vnf_name' " + vnf['vnf_name']
+ where['name'] = vnf['vnf_name']
+ if len(where) == 0:
+ raise NfvoException("Needed a 'vnf_id' or 'vnf_name' at " + error_pos, HTTP_Bad_Request)
+ vnf_db = mydb.get_rows(SELECT=('uuid','name','description'),
+ FROM='vnfs',
+ WHERE=where,
+ WHERE_OR=where_or,
+ WHERE_AND_OR="AND")
+ if len(vnf_db)==0:
+ raise NfvoException("Unknown" + error_text + " at " + error_pos, HTTP_Not_Found)
+ elif len(vnf_db)>1:
+ raise NfvoException("More than one" + error_text + " at " + error_pos + " Concrete with 'vnf_id'", HTTP_Conflict)
+ vnf['uuid']=vnf_db[0]['uuid']
+ vnf['description']=vnf_db[0]['description']
+ vnf['ifaces'] = {}
+ # get external interfaces
+ ext_ifaces = mydb.get_rows(SELECT=('external_name as name','i.uuid as iface_uuid', 'i.type as type'),
+ FROM='vnfs join vms on vnfs.uuid=vms.vnf_id join interfaces as i on vms.uuid=i.vm_id',
+ WHERE={'vnfs.uuid':vnf['uuid']}, WHERE_NOT={'external_name':None} )
+ for ext_iface in ext_ifaces:
+ vnf['ifaces'][ ext_iface['name'] ] = {'uuid':ext_iface['iface_uuid'], 'type':ext_iface['type']}
+
+ # TODO? get internal-connections from db.nets and their profiles, and update scenario[vnfs][internal-connections] accordingly
+
+#2: Insert net_key and ip_address at every vnf interface
+ for net_name,net in scenario["networks"].iteritems():
+ net_type_bridge=False
+ net_type_data=False
+ for iface_dict in net["interfaces"]:
+ logger.debug("Iface_dict %s", iface_dict)
+ vnf = iface_dict["vnf"]
+ iface = iface_dict["vnf_interface"]
+ if vnf not in scenario["vnfs"]:
+ error_text = "Error at 'networks':'%s':'interfaces' VNF '%s' not match any VNF at 'vnfs'" % (net_name, vnf)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Not_Found)
+ if iface not in scenario["vnfs"][vnf]['ifaces']:
+ error_text = "Error at 'networks':'%s':'interfaces':'%s' interface not match any VNF interface" % (net_name, iface)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ if "net_key" in scenario["vnfs"][vnf]['ifaces'][iface]:
+ error_text = "Error at 'networks':'%s':'interfaces':'%s' interface already connected at network '%s'" \
+ % (net_name, iface,scenario["vnfs"][vnf]['ifaces'][iface]['net_key'])
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ scenario["vnfs"][vnf]['ifaces'][ iface ]['net_key'] = net_name
+ scenario["vnfs"][vnf]['ifaces'][ iface ]['ip_address'] = iface_dict.get('ip_address',None)
+ iface_type = scenario["vnfs"][vnf]['ifaces'][iface]['type']
+ if iface_type=='mgmt' or iface_type=='bridge':
+ net_type_bridge = True
+ else:
+ net_type_data = True
+ if net_type_bridge and net_type_data:
+ error_text = "Error connection interfaces of bridge type and data type at 'networks':'%s':'interfaces'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ elif net_type_bridge:
+ type_='bridge'
+ else:
+ type_='data' if len(net["interfaces"])>2 else 'ptp'
+
+ if ("implementation" in net):
+ if (type_ == "bridge" and net["implementation"] == "underlay"):
+ error_text = "Error connecting interfaces of data type to a network declared as 'underlay' at 'network':'%s'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ elif (type_ <> "bridge" and net["implementation"] == "overlay"):
+ error_text = "Error connecting interfaces of data type to a network declared as 'overlay' at 'network':'%s'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ net.pop("implementation")
+ if ("type" in net):
+ if (type_ == "data" and net["type"] == "e-line"):
+ error_text = "Error connecting more than 2 interfaces of data type to a network declared as type 'e-line' at 'network':'%s'" % (net_name)
+ #logger.debug(error_text)
+ raise NfvoException(error_text, HTTP_Bad_Request)
+ elif (type_ == "ptp" and net["type"] == "e-lan"):
+ type_ = "data"
+
+ net['type'] = type_
+ net['name'] = net_name
+ net['external'] = net.get('external', False)
+
+#3: insert at database
+ scenario["nets"] = scenario["networks"]
+ scenario['tenant_id'] = tenant_id
+ scenario_id = mydb.new_scenario2(scenario)
+ return scenario_id
+
+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 = {}
+ datacenter2tenant = {}
+ datacenter = instance_dict.get("datacenter")
+ default_datacenter_id, vim = get_datacenter_by_name_uuid(mydb, tenant_id, datacenter)
+ myvims[default_datacenter_id] = vim
+ datacenter2tenant[default_datacenter_id] = vim['config']['datacenter_tenant_id']
+ #myvim_tenant = myvim['tenant_id']
+# default_datacenter_name = vim['name']
+ rollbackList=[]
+
+ #print "Checking that the scenario exists and getting the scenario dictionary"
+ scenarioDict = mydb.get_scenario(scenario, tenant_id, default_datacenter_id)
+
+ #logger.debug(">>>>>>> Dictionaries before merging")
+ #logger.debug(">>>>>>> InstanceDict:\n{}".format(yaml.safe_dump(instance_dict,default_flow_style=False, width=256)))
+ #logger.debug(">>>>>>> ScenarioDict:\n{}".format(yaml.safe_dump(scenarioDict,default_flow_style=False, width=256)))
+
+ scenarioDict['datacenter_id'] = default_datacenter_id
+
+ auxNetDict = {} #Auxiliar dictionary. First key:'scenario' or sce_vnf uuid. Second Key: uuid of the net/sce_net. Value: vim_net_id
+ auxNetDict['scenario'] = {}
+
+ logger.debug("Creating instance from scenario-dict:\n%s", yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False)) #TODO remove
+ instance_name = instance_dict["name"]
+ instance_description = instance_dict.get("description")
+ try: