Enhance SDN assist error reporting
[osm/RO.git] / osm_ro / vim_thread.py
index fc78d5f..e82e37e 100644 (file)
@@ -37,6 +37,7 @@ The task content is (M: stored at memory, D: stored at database):
             params:     list with the params to be sent to the VIM for CREATE or FIND. For DELETE the vim_id is taken from other related tasks
             find:       (only for CREATE tasks) if present it should FIND before creating and use if existing. Contains the FIND params
             depends_on: list with the 'task_index'es of tasks that must be completed before. e.g. a vm creation depends on a net creation
+                        can contain an int (single index on the same instance-action) or str (compete action ID)
             sdn_net_id: used for net.
             tries:
             interfaces: used for VMs. Each key is the uuid of the instance_interfaces entry at database
@@ -64,6 +65,7 @@ import vimconn
 import yaml
 from db_base import db_base_Exception
 from lib_osm_openvim.ovim import ovimException
+from copy import deepcopy
 
 __author__ = "Alfonso Tierno, Pablo Montes"
 __date__ = "$28-Sep-2017 12:07:15$"
@@ -275,7 +277,7 @@ class vim_thread(threading.Thread):
                             if task_interface.get("sdn_port_id"):
                                 try:
                                     with self.db_lock:
-                                        self.ovim.delete_port(task_interface["sdn_port_id"])
+                                        self.ovim.delete_port(task_interface["sdn_port_id"], idempotent=True)
                                         task_interface["sdn_port_id"] = None
                                         task_need_update = True
                                 except ovimException as e:
@@ -392,18 +394,18 @@ class vim_thread(threading.Thread):
                         except (ovimException, Exception) as e:
                             text_error = "ovimException getting network snd_net_id={}: {}".format(task_sdn_net_id, e)
                             self.logger.error("task={} get-net: {}".format(task_id, text_error), exc_info=True)
-                            sdn_net = {"status": "ERROR", "error_msg": text_error}
+                            sdn_net = {"status": "ERROR", "last_error": text_error}
                         if sdn_net["status"] == "ERROR":
                             if not vim_info_error_msg:
-                                vim_info_error_msg = sdn_net["error_msg"]
+                                vim_info_error_msg = str(sdn_net.get("last_error"))
                             else:
                                 vim_info_error_msg = "VIM_ERROR: {} && SDN_ERROR: {}".format(
                                     self._format_vim_error_msg(vim_info_error_msg, 1024//2-14),
-                                    self._format_vim_error_msg(sdn_net["error_msg"], 1024//2-14))
-                            if vim_info_status == "VIM_ERROR":
-                                vim_info_status = "VIM_SDN_ERROR"
-                            else:
-                                vim_info_status = "SDN_ERROR"
+                                    self._format_vim_error_msg(sdn_net["last_error"], 1024//2-14))
+                            vim_info_status = "ERROR"
+                        elif sdn_net["status"] == "BUILD":
+                            if vim_info_status == "ACTIVE":
+                                vim_info_status = "BUILD"
 
                     # update database
                     if vim_info_error_msg:
@@ -482,7 +484,7 @@ class vim_thread(threading.Thread):
                 for task_index in task["extra"].get("depends_on", ()):
                     task_dependency = task["depends"].get("TASK-" + str(task_index))
                     if not task_dependency:
-                        task_dependency = self._look_for_task(task["instance_action_id"], "TASK-" + str(task_index))
+                        task_dependency = self._look_for_task(task["instance_action_id"], task_index)
                         if not task_dependency:
                             raise VimThreadException(
                                 "Cannot get depending net task trying to get depending task {}.{}".format(
@@ -640,10 +642,19 @@ class vim_thread(threading.Thread):
                 task["params"] = extra.get("params")
                 depends_on_list = extra.get("depends_on")
                 if depends_on_list:
-                    for index in depends_on_list:
+                    for dependency_task in depends_on_list:
+                        if isinstance(dependency_task, int):
+                            index = dependency_task
+                        else:
+                            instance_action_id, _, task_id = dependency_task.rpartition(".")
+                            if instance_action_id != task["instance_action_id"]:
+                                continue
+                            index = int(task_id)
+
                         if index < len(vim_actions_list) and vim_actions_list[index]["task_index"] == index and\
                                     vim_actions_list[index]["instance_action_id"] == task["instance_action_id"]:
-                            task["depends"]["TASK-" + str(index)] = vim_actions_list[index]
+                                task["depends"]["TASK-" + str(index)] = vim_actions_list[index]
+                                task["depends"]["TASK-{}.{}".format(task["instance_action_id"], index)] = vim_actions_list[index]
                 if extra.get("interfaces"):
                     task["vim_interfaces"] = {}
             else:
@@ -738,7 +749,24 @@ class vim_thread(threading.Thread):
         self.logger.debug("Finishing")
 
     def _look_for_task(self, instance_action_id, task_id):
-        task_index = task_id.split("-")[-1]
+        """
+        Look for a concrete task at vim_actions database table
+        :param instance_action_id: The instance_action_id
+        :param task_id: Can have several formats:
+            <task index>: integer
+            TASK-<task index> :backward compatibility,
+            [TASK-]<instance_action_id>.<task index>: this instance_action_id overrides the one in the parameter
+        :return: Task dictionary or None if not found
+        """
+        if isinstance(task_id, int):
+            task_index = task_id
+        else:
+            if task_id.startswith("TASK-"):
+                task_id = task_id[5:]
+            ins_action_id, _, task_index = task_id.rpartition(".")
+            if ins_action_id:
+                instance_action_id = ins_action_id
+
         with self.db_lock:
             tasks = self.db.get_rows(FROM="vim_actions", WHERE={"instance_action_id": instance_action_id,
                                                                 "task_index": task_index})
@@ -784,11 +812,12 @@ class vim_thread(threading.Thread):
                             "Cannot create VM because depends on a network not created or found: " +
                             str(depends[net["net_id"]]["error_msg"]))
                     net["net_id"] = network_id
-            vim_vm_id, created_items = self.vim.new_vminstance(*params)
+            params_copy = deepcopy(params)
+            vim_vm_id, created_items = self.vim.new_vminstance(*params_copy)
 
             # fill task_interfaces. Look for snd_net_id at database for each interface
             task_interfaces = {}
-            for iface in net_list:
+            for iface in params_copy[5]:
                 task_interfaces[iface["vim_id"]] = {"iface_id": iface["uuid"]}
                 with self.db_lock:
                     result = self.db.get_rows(
@@ -830,7 +859,7 @@ class vim_thread(threading.Thread):
                 if iface.get("sdn_port_id"):
                     try:
                         with self.db_lock:
-                            self.ovim.delete_port(iface["sdn_port_id"])
+                            self.ovim.delete_port(iface["sdn_port_id"], idempotent=True)
                     except ovimException as e:
                         self.logger.error("task={} del-VM: ovimException when deleting external_port={}: {} ".format(
                             task_id, iface["sdn_port_id"], e), exc_info=True)
@@ -967,8 +996,8 @@ class vim_thread(threading.Thread):
                     port_list = self.ovim.get_ports(columns={'uuid'},
                                                     filter={'name': 'external_port', 'net_id': sdn_net_id})
                     for port in port_list:
-                        self.ovim.delete_port(port['uuid'])
-                    self.ovim.delete_network(sdn_net_id)
+                        self.ovim.delete_port(port['uuid'], idempotent=True)
+                    self.ovim.delete_network(sdn_net_id, idempotent=True)
             if net_vim_id:
                 self.vim.delete_network(net_vim_id)
             task["status"] = "DONE"