+def new_vnf_v02(mydb, tenant_id, vnf_descriptor):
+ global global_config
+
+ # Step 1. Check the VNF descriptor
+ check_vnf_descriptor(vnf_descriptor)
+ # Step 2. Check tenant exist
+ if tenant_id != "any":
+ check_tenant(mydb, tenant_id)
+ if "tenant_id" in vnf_descriptor["vnf"]:
+ if vnf_descriptor["vnf"]["tenant_id"] != tenant_id:
+ raise NfvoException("VNF can not have a different tenant owner '{}', must be '{}'".format(vnf_descriptor["vnf"]["tenant_id"], tenant_id),
+ HTTP_Unauthorized)
+ else:
+ vnf_descriptor['vnf']['tenant_id'] = tenant_id
+ # Step 3. Get the URL of the VIM from the nfvo_tenant and the datacenter
+ vims = get_vim(mydb, tenant_id)
+ else:
+ vims={}
+
+ # Step 4. Review the descriptor and add missing fields
+ #print vnf_descriptor
+ #logger.debug("Refactoring VNF descriptor with fields: description, public (default: true)")
+ vnf_name = vnf_descriptor['vnf']['name']
+ vnf_descriptor['vnf']['description'] = vnf_descriptor['vnf'].get("description", vnf_name)
+ if "physical" in vnf_descriptor['vnf']:
+ del vnf_descriptor['vnf']['physical']
+ #print vnf_descriptor
+ # Step 5. Check internal connections
+ # TODO: to be moved to step 1????
+ internal_connections=vnf_descriptor['vnf'].get('internal_connections',[])
+ for ic in internal_connections:
+ if len(ic['elements'])>2 and ic['type']=='e-line':
+ raise NfvoException("Mismatch 'type':'e-line' with {} elements at 'vnf':'internal-conections'['name':'{}']. Change 'type' to 'e-lan'".format(len(ic), ic['name']),
+ HTTP_Bad_Request)
+
+ # Step 6. For each VNFC in the descriptor, flavors and images are created in the VIM
+ logger.debug('BEGIN creation of VNF "%s"' % vnf_name)
+ logger.debug("VNF %s: consisting of %d VNFC(s)" % (vnf_name,len(vnf_descriptor['vnf']['VNFC'])))
+
+ #For each VNFC, we add it to the VNFCDict and we create a flavor.
+ VNFCDict = {} # Dictionary, key: VNFC name, value: dict with the relevant information to create the VNF and VMs in the MANO database
+ rollback_list = [] # It will contain the new images created in mano. It is used for rollback
+ try:
+ logger.debug("Creating additional disk images and new flavors in the VIM for each VNFC")
+ for vnfc in vnf_descriptor['vnf']['VNFC']:
+ VNFCitem={}
+ VNFCitem["name"] = vnfc['name']
+ VNFCitem["description"] = vnfc.get("description", 'VM %s of the VNF %s' %(vnfc['name'],vnf_name))
+
+ #print "Flavor name: %s. Description: %s" % (VNFCitem["name"]+"-flv", VNFCitem["description"])
+
+ myflavorDict = {}
+ myflavorDict["name"] = vnfc['name']+"-flv" #Maybe we could rename the flavor by using the field "image name" if exists
+ myflavorDict["description"] = VNFCitem["description"]
+ myflavorDict["ram"] = vnfc.get("ram", 0)
+ myflavorDict["vcpus"] = vnfc.get("vcpus", 0)
+ myflavorDict["disk"] = vnfc.get("disk", 1)
+ myflavorDict["extended"] = {}
+
+ devices = vnfc.get("devices")
+ if devices != None:
+ myflavorDict["extended"]["devices"] = devices
+
+ # TODO:
+ # Mapping from processor models to rankings should be available somehow in the NFVO. They could be taken from VIM or directly from a new database table
+ # Another option is that the processor in the VNF descriptor specifies directly the ranking of the host
+
+ # Previous code has been commented
+ #if vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz" :
+ # myflavorDict["flavor"]['extended']['processor_ranking'] = 200
+ #elif vnfc['processor']['model'] == "Intel(R) Xeon(R) CPU E5-2697 v2 @ 2.70GHz" :
+ # myflavorDict["flavor"]['extended']['processor_ranking'] = 300
+ #else:
+ # result2, message = rollback(myvim, myvimURL, myvim_tenant, flavorList, imageList)
+ # if result2:
+ # print "Error creating flavor: unknown processor model. Rollback successful."
+ # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback successful."
+ # else:
+ # return -HTTP_Bad_Request, "Error creating flavor: unknown processor model. Rollback fail: you need to access VIM and delete the following %s" % message
+ myflavorDict['extended']['processor_ranking'] = 100 #Hardcoded value, while we decide when the mapping is done
+
+ if 'numas' in vnfc and len(vnfc['numas'])>0:
+ myflavorDict['extended']['numas'] = vnfc['numas']
+
+ #print myflavorDict
+
+ # Step 6.2 New flavors are created in the VIM
+ flavor_id = create_or_use_flavor(mydb, vims, myflavorDict, rollback_list)
+
+ #print "Flavor id for VNFC %s: %s" % (vnfc['name'],flavor_id)
+ VNFCitem["flavor_id"] = flavor_id
+ VNFCDict[vnfc['name']] = VNFCitem
+
+ logger.debug("Creating new images in the VIM for each VNFC")
+ # Step 6.3 New images are created in the VIM
+ #For each VNFC, we must create the appropriate image.
+ #This "for" loop might be integrated with the previous one
+ #In case this integration is made, the VNFCDict might become a VNFClist.
+ for vnfc in vnf_descriptor['vnf']['VNFC']:
+ #print "Image name: %s. Description: %s" % (vnfc['name']+"-img", VNFCDict[vnfc['name']]['description'])
+ image_dict={}
+ image_dict['name']=vnfc.get('image name',vnf_name+"-"+vnfc['name']+"-img")
+ image_dict['universal_name']=vnfc.get('image name')
+ image_dict['description']=vnfc.get('image name', VNFCDict[vnfc['name']]['description'])
+ image_dict['location']=vnfc.get('VNFC image')
+ image_dict['checksum']=vnfc.get('image checksum')
+ image_metadata_dict = vnfc.get('image metadata', None)
+ image_metadata_str = None
+ if image_metadata_dict is not None:
+ image_metadata_str = yaml.safe_dump(image_metadata_dict,default_flow_style=True,width=256)
+ image_dict['metadata']=image_metadata_str
+ #print "create_or_use_image", mydb, vims, image_dict, rollback_list
+ image_id = create_or_use_image(mydb, vims, image_dict, rollback_list)
+ #print "Image id for VNFC %s: %s" % (vnfc['name'],image_id)
+ VNFCDict[vnfc['name']]["image_id"] = image_id
+ VNFCDict[vnfc['name']]["image_path"] = vnfc.get('VNFC image')
+
+
+ # Step 7. Storing the VNF descriptor in the repository
+ if "descriptor" not in vnf_descriptor["vnf"]:
+ vnf_descriptor["vnf"]["descriptor"] = yaml.safe_dump(vnf_descriptor, indent=4, explicit_start=True, default_flow_style=False)
+
+ # Step 8. Adding the VNF to the NFVO DB
+ vnf_id = mydb.new_vnf_as_a_whole2(tenant_id,vnf_name,vnf_descriptor,VNFCDict)
+ return vnf_id
+ except (db_base_Exception, vimconn.vimconnException, KeyError) as e:
+ _, message = rollback(mydb, vims, rollback_list)
+ if isinstance(e, db_base_Exception):
+ error_text = "Exception at database"
+ elif isinstance(e, KeyError):
+ error_text = "KeyError exception "
+ e.http_code = HTTP_Internal_Server_Error
+ 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)
+