VIM refreshing of VM status at vim_thread 75/1375/8
authortierno <alfonso.tiernosepulveda@telefonica.com>
Mon, 27 Mar 2017 10:50:34 +0000 (12:50 +0200)
committertierno <alfonso.tiernosepulveda@telefonica.com>
Wed, 29 Mar 2017 13:06:33 +0000 (15:06 +0200)
Change-Id: Ife84566f66de9318a725d1ed8cf34e54a72e9e67
Signed-off-by: tierno <alfonso.tiernosepulveda@telefonica.com>
nfvo.py
nfvo_db.py
vim_thread.py
vimconn.py
vimconn_openstack.py

diff --git a/nfvo.py b/nfvo.py
index 3708edd..724766b 100644 (file)
--- a/nfvo.py
+++ b/nfvo.py
@@ -59,7 +59,7 @@ vim_threads = {"running":{}, "deleting": {}, "names": []}      # threads running
 vim_persistent_info = {}
 logger = logging.getLogger('openmano.nfvo')
 task_lock = Lock()
-task_dict = {}
+global_instance_tasks = {}
 last_task_id = 0.0
 db=None
 db_lock=Lock()
@@ -79,13 +79,11 @@ def get_task_id():
     return "TASK.{:.6f}".format(task_id)
 
 
-def new_task(name, params, store=True, depends=None):
+def new_task(name, params, depends=None):
     task_id = get_task_id()
     task = {"status": "enqueued", "id": task_id, "name": name, "params": params}
     if depends:
         task["depends"] = depends
-    if store:
-        task_dict[task_id] = task
     return task
 
 
@@ -143,7 +141,8 @@ def start_service(mydb):
     try:
         vims = mydb.get_rows(FROM=from_, SELECT=select_)
         for vim in vims:
-            extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id')}
+            extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
+                   'datacenter_id': vim.get('datacenter_id')}
             if vim["config"]:
                 extra.update(yaml.load(vim["config"]))
             if vim.get('dt_config'):
@@ -161,7 +160,7 @@ def start_service(mydb):
                     raise NfvoException("Unknown vim type '{}'. Can not open file '{}.py'; {}: {}".format(
                         vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
 
-            thread_id = vim["datacenter_id"] + "." + vim['nfvo_tenant_id']
+            thread_id = vim['datacenter_tenant_id']
             vim_persistent_info[thread_id] = {}
             try:
                 #if not tenant:
@@ -177,18 +176,19 @@ def start_service(mydb):
                 raise NfvoException("Error at VIM  {}; {}: {}".format(vim["type"], type(e).__name__, str(e)), HTTP_Internal_Server_Error)
             thread_name = get_non_used_vim_name(vim['datacenter_name'], vim['vim_tenant_id'], vim['vim_tenant_name'], vim['vim_tenant_id'])
             new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, vim['datacenter_name'],
-                                               vim.get('datacenter_tenant_id'), db=db, db_lock=db_lock, ovim=ovim)
+                                               vim['datacenter_tenant_id'], db=db, db_lock=db_lock, ovim=ovim)
             new_thread.start()
             vim_threads["running"][thread_id] = new_thread
     except db_base_Exception as e:
         raise NfvoException(str(e) + " at nfvo.get_vim", e.http_code)
 
+
 def stop_service():
     global ovim, global_config
     if ovim:
         ovim.stop_service()
     for thread_id,thread in vim_threads["running"].items():
-        thread.insert_task(new_task("exit", None, store=False))
+        thread.insert_task(new_task("exit", None))
         vim_threads["deleting"][thread_id] = thread
     vim_threads["running"] = {}
     if global_config and global_config.get("console_thread"):
@@ -263,7 +263,8 @@ def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, da
         vims = mydb.get_rows(FROM=from_, SELECT=select_, WHERE=WHERE_dict )
         vim_dict={}
         for vim in vims:
-            extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id')}
+            extra={'datacenter_tenant_id': vim.get('datacenter_tenant_id'),
+                   'datacenter_id': vim.get('datacenter_id')}
             if vim["config"]:
                 extra.update(yaml.load(vim["config"]))
             if vim.get('dt_config'):
@@ -282,8 +283,8 @@ def get_vim(mydb, nfvo_tenant=None, datacenter_id=None, datacenter_name=None, da
                                             vim["type"], module, type(e).__name__, str(e)), HTTP_Bad_Request)
 
             try:
-                if 'nfvo_tenant_id' in vim:
-                    thread_id = vim["datacenter_id"] + "." + vim['nfvo_tenant_id']
+                if 'datacenter_tenant_id' in vim:
+                    thread_id = vim["datacenter_tenant_id"]
                     if thread_id not in vim_persistent_info:
                         vim_persistent_info[thread_id] = {}
                     persistent_info = vim_persistent_info[thread_id]
@@ -1868,33 +1869,40 @@ def unify_cloud_config(cloud_config_preserve, cloud_config):
     return new_cloud_config
 
 
-def get_vim_thread(tenant_id, datacenter_id_name=None, datacenter_tenant_id=None):
+def get_vim_thread(mydb, 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
+    try:
+        if datacenter_tenant_id:
+            thread_id = datacenter_tenant_id
+            thread = vim_threads["running"].get(datacenter_tenant_id)
         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
-
+            where_={"td.nfvo_tenant_id": tenant_id}
+            if datacenter_id_name:
+                if utils.check_valid_uuid(datacenter_id_name):
+                    datacenter_id = datacenter_id_name
+                    where_["dt.datacenter_id"] = datacenter_id
+                else:
+                    datacenter_name = datacenter_id_name
+                    where_["d.name"] = datacenter_name
+            if datacenter_tenant_id:
+                where_["dt.uuid"] = datacenter_tenant_id
+            datacenters = mydb.get_rows(
+                SELECT=("dt.uuid as datacenter_tenant_id",),
+                FROM="datacenter_tenants as dt join tenants_datacenters as td on dt.uuid=td.datacenter_tenant_id "
+                     "join datacenters as d on d.uuid=dt.datacenter_id",
+                WHERE=where_)
+            if len(datacenters) > 1:
+                raise NfvoException("More than one datacenters found, try to identify with uuid", HTTP_Conflict)
+            elif datacenters:
+                thread_id = datacenters[0]["datacenter_tenant_id"]
+                thread = vim_threads["running"].get(thread_id)
+        if not thread:
+            raise NfvoException("datacenter '{}' not found".format(str(datacenter_id_name)), HTTP_Not_Found)
+        return thread_id, thread
+    except db_base_Exception as e:
+        raise NfvoException("{} {}".format(type(e).__name__ , str(e)), e.http_code)
 
 def get_datacenter_by_name_uuid(mydb, tenant_id, datacenter_id_name=None, **extra_filter):
     datacenter_id = None
@@ -1932,13 +1940,14 @@ def create_instance(mydb, tenant_id, instance_dict):
 
     #find main datacenter
     myvims = {}
-    myvim_threads = {}
-    datacenter2tenant = {}
+    myvim_threads_id = {}
+    instance_tasks={}
+    tasks_to_launch={}
     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_threads_id[default_datacenter_id], _ = get_vim_thread(mydb, tenant_id, default_datacenter_id)
+    tasks_to_launch[myvim_threads_id[default_datacenter_id]] = []
     #myvim_tenant = myvim['tenant_id']
 #    default_datacenter_name = vim['name']
     rollbackList=[]
@@ -1958,7 +1967,6 @@ def create_instance(mydb, tenant_id, instance_dict):
     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")
-    instance_tasks={}
     try:
         # 0 check correct parameters
         for net_name, net_instance_desc in instance_dict.get("networks",{}).iteritems():
@@ -1978,8 +1986,8 @@ def create_instance(mydb, tenant_id, instance_dict):
                         #Add this datacenter to myvims
                         d, v = get_datacenter_by_name_uuid(mydb, tenant_id, site["datacenter"])
                         myvims[d] = v
-                        myvim_threads[d] = get_vim_thread(tenant_id, site["datacenter"])
-                        datacenter2tenant[d] = v['config']['datacenter_tenant_id']
+                        myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, site["datacenter"])
+                        tasks_to_launch[myvim_threads_id[d]] = []
                         site["datacenter"] = d #change name to id
                 else:
                     if site_without_datacenter_field:
@@ -2000,8 +2008,8 @@ def create_instance(mydb, tenant_id, instance_dict):
                 if vnf_instance_desc["datacenter"] not in myvims:
                     d, v = get_datacenter_by_name_uuid(mydb, tenant_id, vnf_instance_desc["datacenter"])
                     myvims[d] = v
-                    myvim_threads[d] = get_vim_thread(tenant_id, vnf_instance_desc["datacenter"])
-                    datacenter2tenant[d] = v['config']['datacenter_tenant_id']
+                    myvim_threads_id[d],_ = get_vim_thread(mydb, tenant_id, vnf_instance_desc["datacenter"])
+                    tasks_to_launch[myvim_threads_id[d]] = []
                 scenario_vnf["datacenter"] = vnf_instance_desc["datacenter"]
 
     #0.1 parse cloud-config parameters
@@ -2052,11 +2060,11 @@ def create_instance(mydb, tenant_id, instance_dict):
                 if site.get("datacenter"):
                     vim = myvims[ site["datacenter"] ]
                     datacenter_id = site["datacenter"]
-                    myvim_thread = myvim_threads[ site["datacenter"] ]
+                    myvim_thread_id = myvim_threads_id[ site["datacenter"] ]
                 else:
                     vim = myvims[ default_datacenter_id ]
                     datacenter_id = default_datacenter_id
-                    myvim_thread = myvim_threads[default_datacenter_id]
+                    myvim_thread_id = myvim_threads_id[default_datacenter_id]
                 net_type = sce_net['type']
                 lookfor_filter = {'admin_state_up': True, 'status': 'ACTIVE'} #'shared': True
                 if sce_net["external"]:
@@ -2115,8 +2123,9 @@ def create_instance(mydb, tenant_id, instance_dict):
                 if create_network:
                     #if network is not external
                     task = new_task("new-net", (net_vim_name, net_type, sce_net.get('ip_profile',None)))
-                    task_id = myvim_thread.insert_task(task)
+                    task_id = task["id"]
                     instance_tasks[task_id] = task
+                    tasks_to_launch[myvim_thread_id].append(task)
                     #network_id = vim.new_network(net_vim_name, net_type, sce_net.get('ip_profile',None))
                     sce_net["vim_id_sites"][datacenter_id] = task_id
                     auxNetDict['scenario'][sce_net['uuid']][datacenter_id] = task_id
@@ -2130,11 +2139,11 @@ def create_instance(mydb, tenant_id, instance_dict):
                 if sce_vnf.get("datacenter"):
                     vim = myvims[ sce_vnf["datacenter"] ]
                     datacenter_id = sce_vnf["datacenter"]
-                    myvim_thread = myvim_threads[ sce_vnf["datacenter"]]
+                    myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"]]
                 else:
                     vim = myvims[ default_datacenter_id ]
                     datacenter_id = default_datacenter_id
-                    myvim_thread = myvim_threads[default_datacenter_id]
+                    myvim_thread_id = myvim_threads_id[default_datacenter_id]
                 descriptor_net =  instance_dict.get("vnfs",{}).get(sce_vnf["name"],{})
                 net_name = descriptor_net.get("name")
                 if not net_name:
@@ -2142,8 +2151,9 @@ def create_instance(mydb, tenant_id, instance_dict):
                     net_name = net_name[:255]     #limit length
                 net_type = net['type']
                 task = new_task("new-net", (net_name, net_type, net.get('ip_profile',None)))
-                task_id = myvim_thread.insert_task(task)
+                task_id = task["id"]
                 instance_tasks[task_id] = task
+                tasks_to_launch[myvim_thread_id].append(task)
                 # network_id = vim.new_network(net_name, net_type, net.get('ip_profile',None))
                 net['vim_id'] = task_id
                 if sce_vnf['uuid'] not in auxNetDict:
@@ -2161,11 +2171,11 @@ def create_instance(mydb, tenant_id, instance_dict):
         for sce_vnf in scenarioDict['vnfs']:
             if sce_vnf.get("datacenter"):
                 vim = myvims[ sce_vnf["datacenter"] ]
-                myvim_thread = myvim_threads[ sce_vnf["datacenter"] ]
+                myvim_thread_id = myvim_threads_id[ sce_vnf["datacenter"] ]
                 datacenter_id = sce_vnf["datacenter"]
             else:
                 vim = myvims[ default_datacenter_id ]
-                myvim_thread = myvim_threads[ default_datacenter_id ]
+                myvim_thread_id = myvim_threads_id[ default_datacenter_id ]
                 datacenter_id = default_datacenter_id
             sce_vnf["datacenter_id"] =  datacenter_id
             i = 0
@@ -2257,7 +2267,7 @@ def create_instance(mydb, tenant_id, instance_dict):
                                 break
                     else:
                         netDict['net_id'] = auxNetDict[ sce_vnf['uuid'] ][ iface['net_id'] ]
-                    if is_task_id(netDict['net_id']):
+                    if netDict.get('net_id') and is_task_id(netDict['net_id']):
                         task_depends[netDict['net_id']] = instance_tasks[netDict['net_id']]
                     #skip bridge ifaces not connected to any net
                     #if 'net_id' not in netDict or netDict['net_id']==None:
@@ -2275,9 +2285,9 @@ def create_instance(mydb, tenant_id, instance_dict):
                 task = new_task("new-vm", (myVMDict['name'], myVMDict['description'], myVMDict.get('start', None),
                                            myVMDict['imageRef'], myVMDict['flavorRef'], myVMDict['networks'],
                                            cloud_config_vm, myVMDict['disks']), depends=task_depends)
-                vm_id = myvim_thread.insert_task(task)
-                instance_tasks[vm_id] = task
-
+                instance_tasks[task["id"]] = task
+                tasks_to_launch[myvim_thread_id].append(task)
+                vm_id = task["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]
@@ -2287,19 +2297,24 @@ def create_instance(mydb, tenant_id, instance_dict):
                             if net["name"]==iface["internal_name"]:
                                 iface["vim_id"]=net["vim_id"]
                                 break
-        scenarioDict["datacenter2tenant"] = datacenter2tenant
+        scenarioDict["datacenter2tenant"] = myvim_threads_id
         logger.debug("create_instance Deployment done scenarioDict: %s",
                     yaml.safe_dump(scenarioDict, indent=4, default_flow_style=False) )
         instance_id = mydb.new_instance_scenario_as_a_whole(tenant_id,instance_name, instance_description, scenarioDict)
-        # Update database with those ended tasks
-        for task in instance_tasks.values():
-            if task["status"] == "ok":
-                if task["name"] == "new-vm":
-                    mydb.update_rows("instance_vms", UPDATE={"vim_vm_id": task["result"]},
-                                     WHERE={"vim_vm_id": task["id"]})
-                elif task["name"] == "new-net":
-                    mydb.update_rows("instance_nets", UPDATE={"vim_net_id": task["result"]},
-                                     WHERE={"vim_net_id": task["id"]})
+        for myvim_thread_id,task_list in tasks_to_launch.items():
+            for task in task_list:
+                vim_threads["running"][myvim_thread_id].insert_task(task)
+
+        global_instance_tasks[instance_id] = instance_tasks
+        # Update database with those ended instance_tasks
+        # for task in instance_tasks.values():
+        #     if task["status"] == "ok":
+        #         if task["name"] == "new-vm":
+        #             mydb.update_rows("instance_vms", UPDATE={"vim_vm_id": task["result"]},
+        #                             WHERE={"vim_vm_id": task["id"]})
+        #         elif task["name"] == "new-net":
+        #             mydb.update_rows("instance_nets", UPDATE={"vim_net_id": task["result"]},
+        #                              WHERE={"vim_net_id": task["id"]})
         return mydb.get_instance_scenario(instance_id)
     except (NfvoException, vimconn.vimconnException,db_base_Exception)  as e:
         message = rollback(mydb, myvims, rollbackList)
@@ -2335,7 +2350,7 @@ def delete_instance(mydb, tenant_id, instance_id):
         datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
         if datacenter_key not in myvims:
             try:
-                myvim_thread = get_vim_thread(tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
+                _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
             except NfvoException as e:
                 logger.error(str(e))
                 myvim_thread = None
@@ -2358,7 +2373,7 @@ def delete_instance(mydb, tenant_id, instance_id):
                 task=None
                 if is_task_id(vm['vim_vm_id']):
                     task_id = vm['vim_vm_id']
-                    old_task = task_dict.get(task_id)
+                    old_task = global_instance_tasks[instance_id].get(task_id)
                     if not old_task:
                         error_msg += "\n    VM was scheduled for create, but task {} is not found".format(task_id)
                         continue
@@ -2368,11 +2383,11 @@ def delete_instance(mydb, tenant_id, instance_id):
                         elif old_task["status"] == "error":
                             continue
                         elif old_task["status"] == "processing":
-                            task = new_task("del-vm", task_id, depends={task_id: old_task})
+                            task = new_task("del-vm", (task_id, vm["interfaces"]), depends={task_id: old_task})
                         else: #ok
-                            task = new_task("del-vm", old_task["result"])
+                            task = new_task("del-vm", (old_task["result"], vm["interfaces"]))
                 else:
-                    task = new_task("del-vm", vm['vim_vm_id'], store=False)
+                    task = new_task("del-vm", (vm['vim_vm_id'], vm["interfaces"]) )
                 if task:
                     myvim_thread.insert_task(task)
             except vimconn.vimconnNotFoundException as e:
@@ -2392,7 +2407,7 @@ def delete_instance(mydb, tenant_id, instance_id):
         datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
         if datacenter_key not in myvims:
             try:
-                myvim_thread = get_vim_thread(tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
+                _,myvim_thread = get_vim_thread(mydb, tenant_id, sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
             except NfvoException as e:
                 logger.error(str(e))
                 myvim_thread = None
@@ -2414,7 +2429,7 @@ def delete_instance(mydb, tenant_id, instance_id):
             task = None
             if is_task_id(net['vim_net_id']):
                 task_id = net['vim_net_id']
-                old_task = task_dict.get(task_id)
+                old_task = global_instance_tasks[instance_id].get(task_id)
                 if not old_task:
                     error_msg += "\n    NET was scheduled for create, but task {} is not found".format(task_id)
                     continue
@@ -2428,7 +2443,7 @@ def delete_instance(mydb, tenant_id, instance_id):
                     else:  # ok
                         task = new_task("del-net", old_task["result"])
             else:
-                task = new_task("del-net", (net['vim_net_id'], net['sdn_net_id']), store=False)
+                task = new_task("del-net", (net['vim_net_id'], net['sdn_net_id']))
             if task:
                 myvim_thread.insert_task(task)
         except vimconn.vimconnNotFoundException as e:
@@ -2453,161 +2468,161 @@ def refresh_instance(mydb, nfvo_tenant, instanceDict, datacenter=None, vim_tenan
          - result: <0 if there is any unexpected error, n>=0 if no errors where n is the number of vms and nets that couldn't be updated in the database
          - error_msg
     '''
-    # Assumption: nfvo_tenant and instance_id were checked before entering into this function
-    #print "nfvo.refresh_instance begins"
-    #print json.dumps(instanceDict, indent=4)
-
-    #print "Getting the VIM URL and the VIM tenant_id"
-    myvims={}
-
-    # 1. Getting VIM vm and net list
-    vms_updated = [] #List of VM instance uuids in openmano that were updated
-    vms_notupdated=[]
-    vm_list = {}
-    for sce_vnf in instanceDict['vnfs']:
-        datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
-        if datacenter_key not in vm_list:
-            vm_list[datacenter_key] = []
-        if datacenter_key not in myvims:
-            vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
-                           datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
-            if len(vims) == 0:
-                logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
-                myvims[datacenter_key] = None
-            else:
-                myvims[datacenter_key] = vims.values()[0]
-        for vm in sce_vnf['vms']:
-            vm_list[datacenter_key].append(vm['vim_vm_id'])
-            vms_notupdated.append(vm["uuid"])
-
-    nets_updated = [] #List of VM instance uuids in openmano that were updated
-    nets_notupdated=[]
-    net_list = {}
-    for net in instanceDict['nets']:
-        datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
-        if datacenter_key not in net_list:
-            net_list[datacenter_key] = []
-        if datacenter_key not in myvims:
-            vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
-                           datacenter_tenant_id=net["datacenter_tenant_id"])
-            if len(vims) == 0:
-                logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
-                myvims[datacenter_key] = None
-            else:
-                myvims[datacenter_key] = vims.values()[0]
-
-        net_list[datacenter_key].append(net['vim_net_id'])
-        nets_notupdated.append(net["uuid"])
-
-    # 1. Getting the status of all VMs
-    vm_dict={}
-    for datacenter_key in myvims:
-        if not vm_list.get(datacenter_key):
-            continue
-        failed = True
-        failed_message=""
-        if not myvims[datacenter_key]:
-            failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
-        else:
-            try:
-                vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
-                failed = False
-            except vimconn.vimconnException as e:
-                logger.error("VIM exception %s %s", type(e).__name__, str(e))
-                failed_message = str(e)
-        if failed:
-            for vm in vm_list[datacenter_key]:
-                vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
-
-    # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
-    for sce_vnf in instanceDict['vnfs']:
-        for vm in sce_vnf['vms']:
-            vm_id = vm['vim_vm_id']
-            interfaces = vm_dict[vm_id].pop('interfaces', [])
-            #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
-            has_mgmt_iface = False
-            for iface in vm["interfaces"]:
-                if iface["type"]=="mgmt":
-                    has_mgmt_iface = True
-            if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
-                vm_dict[vm_id]['status'] = "ACTIVE"
-            if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
-                vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
-            if vm['status'] != vm_dict[vm_id]['status'] or vm.get('error_msg')!=vm_dict[vm_id].get('error_msg') or vm.get('vim_info')!=vm_dict[vm_id].get('vim_info'):
-                vm['status']    = vm_dict[vm_id]['status']
-                vm['error_msg'] = vm_dict[vm_id].get('error_msg')
-                vm['vim_info']  = vm_dict[vm_id].get('vim_info')
-                # 2.1. Update in openmano DB the VMs whose status changed
-                try:
-                    updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
-                    vms_notupdated.remove(vm["uuid"])
-                    if updates>0:
-                        vms_updated.append(vm["uuid"])
-                except db_base_Exception as e:
-                    logger.error("nfvo.refresh_instance error database update: %s", str(e))
-            # 2.2. Update in openmano DB the interface VMs
-            for interface in interfaces:
-                #translate from vim_net_id to instance_net_id
-                network_id_list=[]
-                for net in instanceDict['nets']:
-                    if net["vim_net_id"] == interface["vim_net_id"]:
-                        network_id_list.append(net["uuid"])
-                if not network_id_list:
-                    continue
-                del interface["vim_net_id"]
-                try:
-                    for network_id in network_id_list:
-                        mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
-                except db_base_Exception as e:
-                    logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
-
-    # 3. Getting the status of all nets
-    net_dict = {}
-    for datacenter_key in myvims:
-        if not net_list.get(datacenter_key):
-            continue
-        failed = True
-        failed_message = ""
-        if not myvims[datacenter_key]:
-            failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
-        else:
-            try:
-                net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
-                failed = False
-            except vimconn.vimconnException as e:
-                logger.error("VIM exception %s %s", type(e).__name__, str(e))
-                failed_message = str(e)
-        if failed:
-            for net in net_list[datacenter_key]:
-                net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
-
-    # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
-    # TODO: update nets inside a vnf
-    for net in instanceDict['nets']:
-        net_id = net['vim_net_id']
-        if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
-            net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
-        if net['status'] != net_dict[net_id]['status'] or net.get('error_msg')!=net_dict[net_id].get('error_msg') or net.get('vim_info')!=net_dict[net_id].get('vim_info'):
-            net['status']    = net_dict[net_id]['status']
-            net['error_msg'] = net_dict[net_id].get('error_msg')
-            net['vim_info']  = net_dict[net_id].get('vim_info')
-            # 5.1. Update in openmano DB the nets whose status changed
-            try:
-                updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
-                nets_notupdated.remove(net["uuid"])
-                if updated>0:
-                    nets_updated.append(net["uuid"])
-            except db_base_Exception as e:
-                logger.error("nfvo.refresh_instance error database update: %s", str(e))
-
-    # Returns appropriate output
-    #print "nfvo.refresh_instance finishes"
-    logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
-                str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
+    # Assumption: nfvo_tenant and instance_id were checked before entering into this function
+    # #print "nfvo.refresh_instance begins"
+    # #print json.dumps(instanceDict, indent=4)
+    #
+    # #print "Getting the VIM URL and the VIM tenant_id"
+    myvims={}
+    #
+    # 1. Getting VIM vm and net list
+    vms_updated = [] #List of VM instance uuids in openmano that were updated
+    vms_notupdated=[]
+    vm_list = {}
+    for sce_vnf in instanceDict['vnfs']:
+        datacenter_key = (sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"])
+        if datacenter_key not in vm_list:
+            vm_list[datacenter_key] = []
+        if datacenter_key not in myvims:
+            vims = get_vim(mydb, nfvo_tenant, datacenter_id=sce_vnf["datacenter_id"],
+                           datacenter_tenant_id=sce_vnf["datacenter_tenant_id"])
+            if len(vims) == 0:
+                logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(sce_vnf["datacenter_id"], sce_vnf["datacenter_tenant_id"]))
+                myvims[datacenter_key] = None
+            else:
+                myvims[datacenter_key] = vims.values()[0]
+        for vm in sce_vnf['vms']:
+            vm_list[datacenter_key].append(vm['vim_vm_id'])
+            vms_notupdated.append(vm["uuid"])
+    #
+    nets_updated = [] #List of VM instance uuids in openmano that were updated
+    nets_notupdated=[]
+    net_list = {}
+    for net in instanceDict['nets']:
+        datacenter_key = (net["datacenter_id"], net["datacenter_tenant_id"])
+        if datacenter_key not in net_list:
+            net_list[datacenter_key] = []
+        if datacenter_key not in myvims:
+            vims = get_vim(mydb, nfvo_tenant, datacenter_id=net["datacenter_id"],
+                           datacenter_tenant_id=net["datacenter_tenant_id"])
+            if len(vims) == 0:
+                logger.error("datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"]))
+                myvims[datacenter_key] = None
+            else:
+                myvims[datacenter_key] = vims.values()[0]
+    #
+        net_list[datacenter_key].append(net['vim_net_id'])
+        nets_notupdated.append(net["uuid"])
+    #
+    # 1. Getting the status of all VMs
+    vm_dict={}
+    for datacenter_key in myvims:
+        if not vm_list.get(datacenter_key):
+            continue
+        failed = True
+        failed_message=""
+        if not myvims[datacenter_key]:
+            failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
+        else:
+            try:
+                vm_dict.update(myvims[datacenter_key].refresh_vms_status(vm_list[datacenter_key]) )
+                failed = False
+            except vimconn.vimconnException as e:
+                logger.error("VIM exception %s %s", type(e).__name__, str(e))
+                failed_message = str(e)
+        if failed:
+            for vm in vm_list[datacenter_key]:
+                vm_dict[vm] = {'status': "VIM_ERROR", 'error_msg': failed_message}
+    #
+    # 2. Update the status of VMs in the instanceDict, while collects the VMs whose status changed
+    for sce_vnf in instanceDict['vnfs']:
+        for vm in sce_vnf['vms']:
+            vm_id = vm['vim_vm_id']
+            interfaces = vm_dict[vm_id].pop('interfaces', [])
+            #2.0 look if contain manamgement interface, and if not change status from ACTIVE:NoMgmtIP to ACTIVE
+            has_mgmt_iface = False
+            for iface in vm["interfaces"]:
+                if iface["type"]=="mgmt":
+                    has_mgmt_iface = True
+            if vm_dict[vm_id]['status'] == "ACTIVE:NoMgmtIP" and not has_mgmt_iface:
+                vm_dict[vm_id]['status'] = "ACTIVE"
+            if vm_dict[vm_id].get('error_msg') and len(vm_dict[vm_id]['error_msg']) >= 1024:
+                vm_dict[vm_id]['error_msg'] = vm_dict[vm_id]['error_msg'][:516] + " ... " + vm_dict[vm_id]['error_msg'][-500:]
+            if vm['status'] != vm_dict[vm_id]['status'] or vm.get('error_msg')!=vm_dict[vm_id].get('error_msg') or vm.get('vim_info')!=vm_dict[vm_id].get('vim_info'):
+                vm['status']    = vm_dict[vm_id]['status']
+                vm['error_msg'] = vm_dict[vm_id].get('error_msg')
+                vm['vim_info']  = vm_dict[vm_id].get('vim_info')
+                # 2.1. Update in openmano DB the VMs whose status changed
+                try:
+                    updates = mydb.update_rows('instance_vms', UPDATE=vm_dict[vm_id], WHERE={'uuid':vm["uuid"]})
+                    vms_notupdated.remove(vm["uuid"])
+                    if updates>0:
+                        vms_updated.append(vm["uuid"])
+                except db_base_Exception as e:
+                    logger.error("nfvo.refresh_instance error database update: %s", str(e))
+            # 2.2. Update in openmano DB the interface VMs
+            for interface in interfaces:
+                #translate from vim_net_id to instance_net_id
+                network_id_list=[]
+                for net in instanceDict['nets']:
+                    if net["vim_net_id"] == interface["vim_net_id"]:
+                        network_id_list.append(net["uuid"])
+                if not network_id_list:
+                    continue
+                del interface["vim_net_id"]
+                try:
+                    for network_id in network_id_list:
+                        mydb.update_rows('instance_interfaces', UPDATE=interface, WHERE={'instance_vm_id':vm["uuid"], "instance_net_id":network_id})
+                except db_base_Exception as e:
+                    logger.error( "nfvo.refresh_instance error with vm=%s, interface_net_id=%s", vm["uuid"], network_id)
+    #
+    # 3. Getting the status of all nets
+    net_dict = {}
+    for datacenter_key in myvims:
+        if not net_list.get(datacenter_key):
+            continue
+        failed = True
+        failed_message = ""
+        if not myvims[datacenter_key]:
+            failed_message = "datacenter '{}' with datacenter_tenant_id '{}' not found".format(net["datacenter_id"], net["datacenter_tenant_id"])
+        else:
+            try:
+                net_dict.update(myvims[datacenter_key].refresh_nets_status(net_list[datacenter_key]) )
+                failed = False
+            except vimconn.vimconnException as e:
+                logger.error("VIM exception %s %s", type(e).__name__, str(e))
+                failed_message = str(e)
+        if failed:
+            for net in net_list[datacenter_key]:
+                net_dict[net] = {'status': "VIM_ERROR", 'error_msg': failed_message}
+    #
+    # 4. Update the status of nets in the instanceDict, while collects the nets whose status changed
+    # TODO: update nets inside a vnf
+    for net in instanceDict['nets']:
+        net_id = net['vim_net_id']
+        if net_dict[net_id].get('error_msg') and len(net_dict[net_id]['error_msg']) >= 1024:
+            net_dict[net_id]['error_msg'] = net_dict[net_id]['error_msg'][:516] + " ... " + net_dict[vm_id]['error_msg'][-500:]
+        if net['status'] != net_dict[net_id]['status'] or net.get('error_msg')!=net_dict[net_id].get('error_msg') or net.get('vim_info')!=net_dict[net_id].get('vim_info'):
+            net['status']    = net_dict[net_id]['status']
+            net['error_msg'] = net_dict[net_id].get('error_msg')
+            net['vim_info']  = net_dict[net_id].get('vim_info')
+            # 5.1. Update in openmano DB the nets whose status changed
+            try:
+                updated = mydb.update_rows('instance_nets', UPDATE=net_dict[net_id], WHERE={'uuid':net["uuid"]})
+                nets_notupdated.remove(net["uuid"])
+                if updated>0:
+                    nets_updated.append(net["uuid"])
+            except db_base_Exception as e:
+                logger.error("nfvo.refresh_instance error database update: %s", str(e))
+    #
+    # Returns appropriate output
+    # #print "nfvo.refresh_instance finishes"
+    logger.debug("VMs updated in the database: %s; nets updated in the database %s; VMs not updated: %s; nets not updated: %s",
+                str(vms_updated), str(nets_updated), str(vms_notupdated), str(nets_notupdated))
     instance_id = instanceDict['uuid']
-    if len(vms_notupdated)+len(nets_notupdated)>0:
-        error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
-        return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
+    if len(vms_notupdated)+len(nets_notupdated)>0:
+        error_msg = "VMs not updated: " + str(vms_notupdated) + "; nets not updated: " + str(nets_notupdated)
+        return len(vms_notupdated)+len(nets_notupdated), 'Scenario instance ' + instance_id + ' refreshed but some elements could not be updated in the database: ' + error_msg
 
     return 0, 'Scenario instance ' + instance_id + ' refreshed.'
 
@@ -2850,7 +2865,7 @@ def associate_datacenter_to_tenant(mydb, nfvo_tenant, datacenter, vim_tenant_id=
     thread_name = get_non_used_vim_name(datacenter_name, datacenter_id, tenant_dict['name'], tenant_dict['uuid'])
     new_thread = vim_thread.vim_thread(myvim, task_lock, thread_name, datacenter_name, db=db, db_lock=db_lock, ovim=ovim)
     new_thread.start()
-    thread_id = datacenter_id + "." + tenant_dict['uuid']
+    thread_id = datacenter_tenants_dict["uuid"]
     vim_threads["running"][thread_id] = new_thread
     return datacenter_id
 
@@ -2930,9 +2945,9 @@ def deassociate_datacenter_to_tenant(mydb, tenant_id, datacenter, vim_tenant_id=
         except db_base_Exception as e:
             logger.error("Cannot delete datacenter_tenants " + str(e))
             pass  # the error will be caused because dependencies, vim_tenant can not be deleted
-        thread_id = datacenter_id + "." + tenant_datacenter_item["nfvo_tenant_id"]
+        thread_id = tenant_datacenter_item["datacenter_tenant_id"]
         thread = vim_threads["running"][thread_id]
-        thread.insert_task(new_task("exit", None, store=False))
+        thread.insert_task(new_task("exit", None))
         vim_threads["deleting"][thread_id] = thread
     return "datacenter {} detached. {}".format(datacenter_id, warning)
 
index d8305a7..fc6b286 100644 (file)
@@ -892,7 +892,7 @@ class nfvo_db(db_base.db_base):
                             vm_manage_iface_list=[]
                             #instance_interfaces
                             cmd = "SELECT vim_interface_id, instance_net_id, internal_name,external_name, mac_address,"\
-                                  " ii.ip_address as ip_address, vim_info, i.type as type"\
+                                  " ii.ip_address as ip_address, vim_info, i.type as type, sdn_port_id"\
                                   " FROM instance_interfaces as ii join interfaces as i on ii.interface_id=i.uuid"\
                                   " WHERE instance_vm_id='{}' ORDER BY created_at".format(vm['uuid'])
                             self.logger.debug(cmd)
index eb740b4..7a73cf0 100644 (file)
@@ -77,6 +77,171 @@ class vim_thread(threading.Thread):
 
         self.task_lock = task_lock
         self.task_queue = Queue.Queue(2000)
+        self.refresh_list = []
+        """Contains time ordered task list for refreshing the status of VIM VMs and nets"""
+
+    def _refres_elements(self):
+        """Call VIM to get VMs and networks status until 10 elements"""
+        now = time.time()
+        vm_to_refresh_list = []
+        net_to_refresh_list = []
+        vm_to_refresh_dict = {}
+        net_to_refresh_dict = {}
+        items_to_refresh = 0
+        while self.refresh_list:
+            task = self.refresh_list[0]
+            with self.task_lock:
+                if task['status'] == 'deleted':
+                    self.refresh_list.pop(0)
+                    continue
+                if task['time'] > now:
+                    break
+                task["status"] = "processing"
+            self.refresh_list.pop(0)
+            if task["name"] == 'get-vm':
+                vm_to_refresh_list.append(task["vim_id"])
+                vm_to_refresh_dict[task["vim_id"]] = task
+            elif task["name"] == 'get-net':
+                net_to_refresh_list.append(task["vim_id"])
+                net_to_refresh_dict[task["vim_id"]] = task
+            else:
+                error_text = "unknown task {}".format(task["name"])
+                self.logger.error(error_text)
+            items_to_refresh += 1
+            if items_to_refresh == 10:
+                break
+
+        if vm_to_refresh_list:
+            try:
+                vim_dict = self.vim.refresh_vms_status(vm_to_refresh_list)
+                for vim_id, vim_info in vim_dict.items():
+                    #look for task
+                    task = vm_to_refresh_dict[vim_id]
+                    self.logger.debug("get-vm vm_id=%s result=%s", task["vim_id"], str(vim_info))
+
+                    # update database
+                    if vim_info.get("error_msg"):
+                        vim_info["error_msg"] = self._format_vim_error_msg(vim_info["error_msg"])
+                    if task["vim_info"].get("status") != vim_info["status"] or \
+                        task["vim_info"].get("error_msg") != vim_info.get("error_msg") or \
+                        task["vim_info"].get("vim_info") != vim_info["vim_info"]:
+                        with self.db_lock:
+                            temp_dict={ "status": vim_info["status"],
+                                        "error_msg": vim_info.get("error_msg"),
+                                        "vim_info": vim_info["vim_info"]
+                                       }
+                            self.db.update_rows('instance_vms', UPDATE=temp_dict, WHERE={"vim_vm_id": vim_id})
+                    for interface in vim_info["interfaces"]:
+                        for task_interface in task["vim_info"]["interfaces"]:
+                            if task_interface["vim_net_id"] == interface["vim_net_id"]:
+                                break
+                        else:
+                            task_interface = {"vim_net_id": interface["vim_net_id"]}
+                            task["vim_info"]["interfaces"].append(task_interface)
+                        if task_interface != interface:
+                            #delete old port
+                            if task_interface.get("sdn_port_id"):
+                                try:
+                                    self.ovim.delete_port(task_interface["sdn_port_id"])
+                                    task_interface["sdn_port_id"] = None
+                                except ovimException as e:
+                                    self.logger.error("ovimException deleting external_port={} ".format(
+                                        task_interface["sdn_port_id"]) + str(e), exc_info=True)
+                                    # TODO Set error_msg at instance_nets
+                            vim_net_id = interface.pop("vim_net_id")
+                            sdn_net_id = None
+                            sdn_port_name = None
+                            with self.db_lock:
+                                where_= {'iv.vim_vm_id': vim_id, "ine.vim_net_id": vim_net_id,
+                                            'ine.datacenter_tenant_id': self.datacenter_tenant_id}
+                                # TODO check why vim_interface_id is not present at database
+                                # if interface.get("vim_interface_id"):
+                                #     where_["vim_interface_id"] = interface["vim_interface_id"]
+                                db_ifaces = self.db.get_rows(
+                                    FROM="instance_interfaces as ii left join instance_nets as ine on "
+                                         "ii.instance_net_id=ine.uuid left join instance_vms as iv on "
+                                         "ii.instance_vm_id=iv.uuid",
+                                    SELECT=("ii.uuid as iface_id", "ine.uuid as net_id", "iv.uuid as vm_id", "sdn_net_id"),
+                                    WHERE=where_)
+                            if len(db_ifaces)>1:
+                                self.logger.error("Refresing interfaces. "
+                                                  "Found more than one interface at database for '{}'".format(where_))
+                            elif len(db_ifaces)==0:
+                                self.logger.error("Refresing interfaces. "
+                                                  "Not found any interface at database for '{}'".format(where_))
+                            else:
+                                db_iface = db_ifaces[0]
+                                if db_iface.get("sdn_net_id") and interface.get("compute_node") and interface.get("pci"):
+                                    sdn_net_id = db_iface["sdn_net_id"]
+                                    sdn_port_name = sdn_net_id + "." + db_iface["vm_id"]
+                                    sdn_port_name = sdn_port_name[:63]
+                                    try:
+                                        sdn_port_id = self.ovim.new_external_port(
+                                            {"compute_node": interface["compute_node"],
+                                             "pci": interface["pci"],
+                                             "vlan": interface.get("vlan"),
+                                             "net_id": sdn_net_id,
+                                             "region": self.vim["config"]["datacenter_id"],
+                                             "name": sdn_port_name})
+                                        interface["sdn_port_id"] = sdn_port_id
+                                    except (ovimException, Exception) as e:
+                                        self.logger.error(
+                                            "ovimException creating new_external_port compute_node={} " \
+                                            "pci={} vlan={} ".format(
+                                                interface["compute_node"],
+                                                interface["pci"],
+                                                interface.get("vlan")) + str(e),
+                                            exc_info=True)
+                                        # TODO Set error_msg at instance_nets
+                                with self.db_lock:
+                                    self.db.update_rows('instance_interfaces', UPDATE=interface,
+                                                    WHERE={'uuid': db_iface["iface_id"]})
+                                # TODO insert instance_id
+                            interface["vim_net_id"] = vim_net_id
+
+                    task["vim_info"] = vim_info
+                    if "ACTIVE" in task["vim_info"]["status"]:
+                        self._insert_refresh(task, now+300) # 5minutes
+                    else:
+                        self._insert_refresh(task, now+5)  # 5seconds
+            except vimconn.vimconnException as e:
+                self.logger.error("vimconnException Exception when trying to refresh vms " + str(e))
+                self._insert_refresh(task, now + 300)  # 5minutes
+
+        if not items_to_refresh:
+            time.sleep(1)
+
+    def _insert_refresh(self, task, threshold_time):
+        """Insert a task at list of refreshing elements. The refreshing list is ordered by threshold_time (task['time']
+        It is assumed that this is called inside this thread
+        """
+        task["time"] = threshold_time
+        for index in range(0, len(self.refresh_list)):
+            if self.refresh_list[index]["time"] > threshold_time:
+                self.refresh_list.insert(index, task)
+                break
+        else:
+            index = len(self.refresh_list)
+            self.refresh_list.append(task)
+        self.logger.debug("new refresh task={} name={}, time={} index={}".format(
+            task["id"], task["name"], task["time"], index))
+
+    def _remove_refresh(self, task_name, vim_id):
+        """Remove a task with this name and vim_id from the list of refreshing elements.
+        It is assumed that this is called inside this thread outside _refres_elements method
+        Return True if self.refresh_list is modified, task is found
+        Return False if not found
+        """
+        index_to_delete = None
+        for index in range(0, len(self.refresh_list)):
+            if self.refresh_list[index]["name"] == task_name and self.refresh_list[index]["vim_id"] == vim_id:
+                index_to_delete = index
+                break
+        else:
+            return False
+        if index_to_delete != None:
+            del self.refresh_list[index_to_delete]
+        return True
 
     def insert_task(self, task):
         try:
@@ -105,11 +270,10 @@ class vim_thread(threading.Thread):
                     if task["status"] == "deleted":
                         self.task_lock.release()
                         continue
-                    task["status"] == "processing"
+                    task["status"] = "processing"
                     self.task_lock.release()
                 else:
-                    now=time.time()
-                    time.sleep(1)
+                    self._refres_elements()
                     continue
                 self.logger.debug("processing task id={} name={} params={}".format(task["id"], task["name"],
                                                                                    str(task["params"])))
@@ -128,6 +292,9 @@ class vim_thread(threading.Thread):
                     self.logger.error(error_text)
                     result = False
                     content = error_text
+                self.logger.debug("task id={} name={} result={}:{} params={}".format(task["id"], task["name"],
+                                                                                    result, content,
+                                                                                    str(task["params"])))
 
                 with self.task_lock:
                     task["status"] = "done" if result else "error"
@@ -145,7 +312,7 @@ class vim_thread(threading.Thread):
         return True, None
 
     def _format_vim_error_msg(self, error_text):
-        if len(error_text) >= 1024:
+        if error_text and len(error_text) >= 1024:
             return error_text[:516] + " ... " + error_text[-500:]
         return error_text
 
@@ -199,27 +366,48 @@ class vim_thread(threading.Thread):
             task_id = task["id"]
             depends = task.get("depends")
             net_list = params[5]
+            error_text = ""
             for net in net_list:
-                if is_task_id(net["net_id"]):  # change task_id into network_id
+                if "net_id" in net and is_task_id(net["net_id"]):  # change task_id into network_id
                     try:
                         task_net = depends[net["net_id"]]
                         with self.task_lock:
                             if task_net["status"] == "error":
-                                return False, "Cannot create VM because depends on a network that cannot be created: " + \
+                                error_text = "Cannot create VM because depends on a network that cannot be created: " +\
                                        str(task_net["result"])
+                                break
                             elif task_net["status"] == "enqueued" or task_net["status"] == "processing":
-                                return False, "Cannot create VM because depends on a network still not created"
+                                error_text = "Cannot create VM because depends on a network still not created"
+                                break
                             network_id = task_net["result"]
                         net["net_id"] = network_id
                     except Exception as e:
-                        return False, "Error trying to map from task_id={} to task result: {}".format(net["net_id"],
-                                                                                                      str(e))
-            vm_id = self.vim.new_vminstance(*params)
+                        error_text = "Error trying to map from task_id={} to task result: {}".format(
+                            net["net_id"],str(e))
+                        break
+            if not error_text:
+                vm_id = self.vim.new_vminstance(*params)
             try:
                 with self.db_lock:
-                    self.db.update_rows("instance_vms", UPDATE={"vim_vm_id": vm_id}, WHERE={"vim_vm_id": task_id})
+                    if error_text:
+                        update = self.db.update_rows("instance_vms",
+                                                     UPDATE={"status": "VIM_ERROR", "error_msg": error_text},
+                                                     WHERE={"vim_vm_id": task_id})
+                    else:
+                        update = self.db.update_rows("instance_vms", UPDATE={"vim_vm_id": vm_id}, WHERE={"vim_vm_id": task_id})
+                    if not update:
+                        self.logger.error("task id={} name={} database not updated vim_vm_id={}".format(
+                            task["id"], task["name"], vm_id))
             except db_base_Exception as e:
                 self.logger.error("Error updating database %s", str(e))
+            if error_text:
+                return False, error_text
+            new_refresh_task = {"status": "enqueued",
+                                "id": task_id,
+                                "name": "get-vm",
+                                "vim_id": vm_id,
+                                "vim_info": {"interfaces":[]} }
+            self._insert_refresh(new_refresh_task, time.time())
             return True, vm_id
         except vimconn.vimconnException as e:
             self.logger.error("Error creating VM, task=%s: %s", str(task_id), str(e))
@@ -228,12 +416,13 @@ class vim_thread(threading.Thread):
                     self.db.update_rows("instance_vms",
                                         UPDATE={"error_msg": self._format_vim_error_msg(str(e)), "status": "VIM_ERROR"},
                                         WHERE={"vim_vm_id": task_id})
-            except db_base_Exception as e:
-                self.logger.error("Error updating database %s", str(e))
+            except db_base_Exception as edb:
+                self.logger.error("Error updating database %s", str(edb))
             return False, str(e)
 
     def del_vm(self, task):
-        vm_id = task["params"]
+        vm_id = task["params"][0]
+        interfaces = task["params"][1]
         if is_task_id(vm_id):
             try:
                 task_create = task["depends"][vm_id]
@@ -241,11 +430,21 @@ class vim_thread(threading.Thread):
                     if task_create["status"] == "error":
                         return True, "VM was not created. It has error: " + str(task_create["result"])
                     elif task_create["status"] == "enqueued" or task_create["status"] == "processing":
-                        return False, "Cannot delete VM because still creating"
+                        return False, "Cannot delete VM vim_id={} because still creating".format(vm_id)
                     vm_id = task_create["result"]
             except Exception as e:
                 return False, "Error trying to get task_id='{}':".format(vm_id, str(e))
         try:
+            self._remove_refresh("get-vm", vm_id)
+            for iface in interfaces:
+                if iface.get("sdn_port_id"):
+                    try:
+                        self.ovim.delete_port(iface["sdn_port_id"])
+                    except ovimException as e:
+                        self.logger.error("ovimException deleting external_port={} at VM vim_id={} deletion ".format(
+                            iface["sdn_port_id"], vm_id) + str(e), exc_info=True)
+                        # TODO Set error_msg at instance_nets
+
             return True, self.vim.delete_vminstance(vm_id)
         except vimconn.vimconnException as e:
             return False, str(e)
index ed259e4..18f4334 100644 (file)
@@ -432,9 +432,9 @@ class vimconnector():
                         vim_net_id:       #network id where this interface is connected, if provided at creation
                         vim_interface_id: #interface/port VIM id
                         ip_address:       #null, or text with IPv4, IPv6 address
-                        physical_compute: #identification of compute node where PF,VF interface is allocated
-                        physical_pci:     #PCI address of the NIC that hosts the PF,VF
-                        physical_vlan:    #physical VLAN used for VF
+                        compute_node:     #identification of compute node where PF,VF interface is allocated
+                        pci:              #PCI address of the NIC that hosts the PF,VF
+                        vlan:             #physical VLAN used for VF
         """
         raise vimconnNotImplemented( "Should have implemented this" )
     
index ceaa762..9b9a2c0 100644 (file)
@@ -1094,9 +1094,9 @@ class vimconnector(vimconn.vimconnector):
                         vim_net_id:       #network id where this interface is connected
                         vim_interface_id: #interface/port VIM id
                         ip_address:       #null, or text with IPv4, IPv6 address
-                        physical_compute: #identification of compute node where PF,VF interface is allocated
-                        physical_pci:     #PCI address of the NIC that hosts the PF,VF
-                        physical_vlan:    #physical VLAN used for VF
+                        compute_node:     #identification of compute node where PF,VF interface is allocated
+                        pci:              #PCI address of the NIC that hosts the PF,VF
+                        vlan:             #physical VLAN used for VF
         '''
         vm_dict={}
         self.logger.debug("refresh_vms status: Getting tenant VM instance information from VIM")
@@ -1129,19 +1129,19 @@ class vimconnector(vimconn.vimconnector):
                         interface["mac_address"] = port.get("mac_address")
                         interface["vim_net_id"] = port["network_id"]
                         interface["vim_interface_id"] = port["id"]
-                        interface["physical_compute"] = vm_vim['OS-EXT-SRV-ATTR:host']
-                        interface["physical_pci"] = None
-                        # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
-                        # TODO: This is just a workaround valid for niantinc. Find a better way to do so
-                        if 'pci_slot' in port['binding:profile']:
-                            pci = list(port['binding:profile']['pci_slot'])
-                            pci[-4] = '0'
-                            pci[-3] = '0'
-                            interface["physical_pci"] = ''.join(pci)
-                        interface["physical_vlan"] = None
+                        interface["compute_node"] = vm_vim['OS-EXT-SRV-ATTR:host']
+                        interface["pci"] = None
+                        if port['binding:profile'].get('pci_slot'):
+                            # TODO: At the moment sr-iov pci addresses are converted to PF pci addresses by setting the slot to 0x00
+                            # TODO: This is just a workaround valid for niantinc. Find a better way to do so
+                            #   CHANGE DDDD:BB:SS.F to DDDD:BB:00.(F%2)   assuming there are 2 ports per nic
+                            pci = port['binding:profile']['pci_slot']
+                            # interface["pci"] = pci[:-4] + "00." + str(int(pci[-1]) % 2)
+                            interface["pci"] = pci
+                        interface["vlan"] = None
                         network = self.neutron.show_network(port["network_id"])
                         if network['network'].get('provider:network_type') == 'vlan':
-                            interface["physical_vlan"] = network['network'].get('provider:segmentation_id')
+                            interface["vlan"] = network['network'].get('provider:segmentation_id')
                         ips=[]
                         #look for floating ip address
                         floating_ip_dict = self.neutron.list_floatingips(port_id=port["id"])