fix VIM edit problem with SDN controller
[osm/LCM.git] / osm_lcm / lcm.py
index a4395eb..83efbca 100644 (file)
@@ -163,14 +163,23 @@ class Lcm:
         self.logger.debug(logging_text + "Enter")
         db_vim = None
         exc = None
+        RO_sdn_id = None
         try:
-            step = "Getting vim from db"
+            step = "Getting vim-id='{}' from db".format(vim_id)
             db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
             if "_admin" not in db_vim:
                 db_vim["_admin"] = {}
             if "deployed" not in db_vim["_admin"]:
                 db_vim["_admin"]["deployed"] = {}
             db_vim["_admin"]["deployed"]["RO"] = None
+            if vim_content.get("config") and vim_content["config"].get("sdn-controller"):
+                step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"])
+                db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]})
+                if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get("RO"):
+                    RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
+                else:
+                    raise LcmException("sdn-controller={} is not available. Not deployed at RO".format(
+                        vim_content["config"]["sdn-controller"]))
 
             step = "Creating vim at RO"
             RO = ROclient.ROClient(self.loop, **self.ro_config)
@@ -183,18 +192,25 @@ class Lcm:
             vim_RO["type"] = vim_RO.pop("vim_type")
             vim_RO.pop("vim_user", None)
             vim_RO.pop("vim_password", None)
+            if RO_sdn_id:
+                vim_RO["config"]["sdn-controller"] = RO_sdn_id
             desc = await RO.create("vim", descriptor=vim_RO)
             RO_vim_id = desc["uuid"]
             db_vim["_admin"]["deployed"]["RO"] = RO_vim_id
             self.update_db("vim_accounts", vim_id, db_vim)
 
-            step = "Attach vim to RO tenant"
-            vim_RO = {"vim_tenant_name": vim_content["vim_tenant_name"],
-                      "vim_username": vim_content["vim_user"],
-                      "vim_password": vim_content["vim_password"],
-                      "config": vim_content["config"]
-                      }
-            desc = await RO.attach_datacenter(RO_vim_id, descriptor=vim_RO)
+            step = "Creating vim_account at RO"
+            vim_account_RO = {"vim_tenant_name": vim_content["vim_tenant_name"],
+                              "vim_username": vim_content["vim_user"],
+                              "vim_password": vim_content["vim_password"]
+                              }
+            if vim_RO.get("config"):
+                vim_account_RO["config"] = vim_RO["config"]
+                if "sdn-controller" in vim_account_RO["config"]:
+                    del vim_account_RO["config"]["sdn-controller"]
+                if "sdn-port-mapping" in vim_account_RO["config"]:
+                    del vim_account_RO["config"]["sdn-port-mapping"]
+            await RO.attach_datacenter(RO_vim_id, descriptor=vim_account_RO)
             db_vim["_admin"]["operationalState"] = "ENABLED"
             self.update_db("vim_accounts", vim_id, db_vim)
 
@@ -219,10 +235,21 @@ class Lcm:
         self.logger.debug(logging_text + "Enter")
         db_vim = None
         exc = None
-        step = "Getting vim from db"
+        RO_sdn_id = None
+        step = "Getting vim-id='{}' from db".format(vim_id)
         try:
             db_vim = self.db.get_one("vim_accounts", {"_id": vim_id})
             if db_vim.get("_admin") and db_vim["_admin"].get("deployed") and db_vim["_admin"]["deployed"].get("RO"):
+                if vim_content.get("config") and vim_content["config"].get("sdn-controller"):
+                    step = "Getting sdn-controller-id='{}' from db".format(vim_content["config"]["sdn-controller"])
+                    db_sdn = self.db.get_one("sdns", {"_id": vim_content["config"]["sdn-controller"]})
+                    if db_sdn.get("_admin") and db_sdn["_admin"].get("deployed") and db_sdn["_admin"]["deployed"].get(
+                            "RO"):
+                        RO_sdn_id = db_sdn["_admin"]["deployed"]["RO"]
+                    else:
+                        raise LcmException("sdn-controller={} is not available. Not deployed at RO".format(
+                            vim_content["config"]["sdn-controller"]))
+
                 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
                 step = "Editing vim at RO"
                 RO = ROclient.ROClient(self.loop, **self.ro_config)
@@ -232,21 +259,33 @@ class Lcm:
                 vim_RO.pop("schema_version", None)
                 vim_RO.pop("schema_type", None)
                 vim_RO.pop("vim_tenant_name", None)
-                vim_RO["type"] = vim_RO.pop("vim_type")
+                if "vim_type" in vim_RO:
+                    vim_RO["type"] = vim_RO.pop("vim_type")
                 vim_RO.pop("vim_user", None)
                 vim_RO.pop("vim_password", None)
+                if RO_sdn_id:
+                    vim_RO["config"]["sdn-controller"] = RO_sdn_id
+                # TODO make a deep update of sdn-port-mapping 
                 if vim_RO:
                     await RO.edit("vim", RO_vim_id, descriptor=vim_RO)
 
                 step = "Editing vim-account at RO tenant"
-                vim_RO = {}
+                vim_account_RO = {}
+                if "config" in vim_content:
+                    if "sdn-controller" in vim_content["config"]:
+                        del vim_content["config"]["sdn-controller"]
+                    if "sdn-port-mapping" in vim_content["config"]:
+                        del vim_content["config"]["sdn-port-mapping"]
+                    if not vim_content["config"]:
+                        del vim_content["config"]
                 for k in ("vim_tenant_name", "vim_password", "config"):
                     if k in vim_content:
-                        vim_RO[k] = vim_content[k]
+                        vim_account_RO[k] = vim_content[k]
                 if "vim_user" in vim_content:
                     vim_content["vim_username"] = vim_content["vim_user"]
-                if vim_RO:
-                    await RO.edit("vim_account", RO_vim_id, descriptor=vim_RO)
+                # vim_account must be edited always even if empty in order to ensure changes are translated to RO
+                # vim_thread. RO will remove and relaunch a new thread for this vim_account
+                await RO.edit("vim_account", RO_vim_id, descriptor=vim_account_RO)
                 db_vim["_admin"]["operationalState"] = "ENABLED"
                 self.update_db("vim_accounts", vim_id, db_vim)
 
@@ -663,9 +702,10 @@ class Lcm:
         db_nslcmop = None
         db_vnfr = {}
         exc = None
-        step = "Getting nsr, nslcmop, RO_vims from db"
         try:
+            step = "Getting nslcmop={} from db".format(nslcmop_id)
             db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
+            step = "Getting nsr={} from db".format(nsr_id)
             db_nsr = self.db.get_one("nsrs", {"_id": nsr_id})
             nsd = db_nsr["nsd"]
             nsr_name = db_nsr["name"]   # TODO short-name??
@@ -695,32 +735,34 @@ class Lcm:
             # get vnfds, instantiate at RO
             for vnfd_id, vnfd in needed_vnfd.items():
                 step = db_nsr["detailed-status"] = "Creating vnfd={} at RO".format(vnfd_id)
-                self.logger.debug(logging_text + step)
+                self.logger.debug(logging_text + step)
                 vnfd_id_RO = nsr_id + "." + vnfd_id[:200]
 
                 # look if present
                 vnfd_list = await RO.get_list("vnfd", filter_by={"osm_id": vnfd_id_RO})
                 if vnfd_list:
                     nsr_lcm["RO"]["vnfd_id"][vnfd_id] = vnfd_list[0]["uuid"]
-                    self.logger.debug(logging_text + "RO vnfd={} exist. Using RO_id={}".format(
+                    self.logger.debug(logging_text + "vnfd={} exists at RO. Using RO_id={}".format(
                         vnfd_id, vnfd_list[0]["uuid"]))
                 else:
                     vnfd_RO = self.vnfd2RO(vnfd, vnfd_id_RO)
                     desc = await RO.create("vnfd", descriptor=vnfd_RO)
                     nsr_lcm["RO"]["vnfd_id"][vnfd_id] = desc["uuid"]
                     db_nsr["_admin"]["nsState"] = "INSTANTIATED"
+                    self.logger.debug(logging_text + "vnfd={} created at RO. RO_id={}".format(
+                        vnfd_id, desc["uuid"]))
                 self.update_db("nsrs", nsr_id, db_nsr)
 
             # create nsd at RO
             nsd_id = nsd["id"]
             step = db_nsr["detailed-status"] = "Creating nsd={} at RO".format(nsd_id)
-            self.logger.debug(logging_text + step)
+            self.logger.debug(logging_text + step)
 
-            nsd_id_RO = nsd_id + "." + nsd_id[:200]
+            nsd_id_RO = nsr_id + "." + nsd_id[:200]
             nsd_list = await RO.get_list("nsd", filter_by={"osm_id": nsd_id_RO})
             if nsd_list:
                 nsr_lcm["RO"]["nsd_id"] = nsd_list[0]["uuid"]
-                self.logger.debug(logging_text + "RO nsd={} exist. Using RO_id={}".format(
+                self.logger.debug(logging_text + "nsd={} exists at RO. Using RO_id={}".format(
                     nsd_id, nsd_list[0]["uuid"]))
             else:
                 nsd_RO = deepcopy(nsd)
@@ -733,6 +775,7 @@ class Lcm:
                 desc = await RO.create("nsd", descriptor=nsd_RO)
                 db_nsr["_admin"]["nsState"] = "INSTANTIATED"
                 nsr_lcm["RO"]["nsd_id"] = desc["uuid"]
+                self.logger.debug(logging_text + "nsd={} created at RO. RO_id={}".format(nsd_id, desc["uuid"]))
             self.update_db("nsrs", nsr_id, db_nsr)
 
             # Crate ns at RO
@@ -741,7 +784,7 @@ class Lcm:
             if RO_nsr_id:
                 try:
                     step = db_nsr["detailed-status"] = "Looking for existing ns at RO"
-                    self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id))
+                    self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id))
                     desc = await RO.show("ns", RO_nsr_id)
                 except ROclient.ROClientException as e:
                     if e.http_code != HTTPStatus.NOT_FOUND:
@@ -751,13 +794,13 @@ class Lcm:
                     ns_status, ns_status_info = RO.check_ns_status(desc)
                     nsr_lcm["RO"]["nsr_status"] = ns_status
                     if ns_status == "ERROR":
-                        step = db_nsr["detailed-status"] = "Deleting ns at RO"
-                        self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id))
+                        step = db_nsr["detailed-status"] = "Deleting ns at RO. RO_ns_id={}".format(RO_nsr_id)
+                        self.logger.debug(logging_text + step)
                         await RO.delete("ns", RO_nsr_id)
                         RO_nsr_id = nsr_lcm["RO"]["nsr_id"] = None
             if not RO_nsr_id:
                 step = db_nsr["detailed-status"] = "Creating ns at RO"
-                self.logger.debug(logging_text + step)
+                self.logger.debug(logging_text + step)
                 RO_ns_params = self.ns_params_2_RO(db_nsr.get("instantiate_params"))
                 desc = await RO.create("ns", descriptor=RO_ns_params,
                                        name=db_nsr["name"],
@@ -765,24 +808,27 @@ class Lcm:
                 RO_nsr_id = nsr_lcm["RO"]["nsr_id"] = desc["uuid"]
                 db_nsr["_admin"]["nsState"] = "INSTANTIATED"
                 nsr_lcm["RO"]["nsr_status"] = "BUILD"
-
+                self.logger.debug(logging_text + "ns created at RO. RO_id={}".format(desc["uuid"]))
             self.update_db("nsrs", nsr_id, db_nsr)
+
             # update VNFR vimAccount
             step = "Updating VNFR vimAcccount"
             for vnf_index, vnfr in db_vnfr.items():
                 if vnfr.get("vim-account-id"):
                     continue
-                if db_nsr["instantiate_params"].get("vnf") and db_nsr["instantiate_params"]["vnf"].get(vnf_index) \
-                        and db_nsr["instantiate_params"]["vnf"][vnf_index].get("vimAccountId"):
-                    vnfr["vim-account-id"] = db_nsr["instantiate_params"]["vnf"][vnf_index]["vimAccountId"]
-                else:
-                    vnfr["vim-account-id"] = db_nsr["instantiate_params"]["vimAccountId"]
+                vnfr["vim-account-id"] = db_nsr["instantiate_params"]["vimAccountId"]
+                if db_nsr["instantiate_params"].get("vnf"):
+                    for vnf_params in db_nsr["instantiate_params"]["vnf"]:
+                        if vnf_params.get("member-vnf-index") == vnf_index:
+                            if vnf_params.get("vimAccountId"):
+                                vnfr["vim-account-id"] = vnf_params.get("vimAccountId")
+                            break
                 self.update_db("vnfrs", vnfr["_id"], vnfr)
 
             # wait until NS is ready
-            step = ns_status_detailed = "Waiting ns ready at RO"
+            step = ns_status_detailed = "Waiting ns ready at RO. RO_id={}".format(RO_nsr_id)
             db_nsr["detailed-status"] = ns_status_detailed
-            self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id))
+            self.logger.debug(logging_text + step)
             deployment_timeout = 2 * 3600   # Two hours
             while deployment_timeout > 0:
                 desc = await RO.show("ns", RO_nsr_id)
@@ -809,6 +855,7 @@ class Lcm:
                 deployment_timeout -= 5
             if deployment_timeout <= 0:
                 raise ROclient.ROClientException("Timeout waiting ns to be ready")
+
             step = "Updating VNFRs"
             for vnf_index, vnfr_deployed in ns_RO_info.items():
                 vnfr = db_vnfr[vnf_index]
@@ -940,6 +987,7 @@ class Lcm:
                 db_nslcmop["detailed-status"] = "configuring: init: {}".format(number_to_configure)
             else:
                 db_nslcmop["operationState"] = "COMPLETED"
+                db_nslcmop["statusEnteredTime"] = time()
                 db_nslcmop["detailed-status"] = "done"
                 db_nsr["config-status"] = "configured"
                 db_nsr["detailed-status"] = "done"
@@ -950,10 +998,11 @@ class Lcm:
             return nsr_lcm
 
         except (ROclient.ROClientException, DbException, LcmException) as e:
-            self.logger.error(logging_text + "Exit Exception {}".format(e))
+            self.logger.error(logging_text + "Exit Exception while '{}': {}".format(step, e))
             exc = e
         except Exception as e:
-            self.logger.critical(logging_text + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True)
+            self.logger.critical(logging_text + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
+                                 exc_info=True)
             exc = e
         finally:
             if exc:
@@ -1101,7 +1150,7 @@ class Lcm:
                 db_nsr_update = {
                     "operational-status": "failed",
                     "detailed-status": "Deletion errors " + "; ".join(failed_detail),
-                    "_admin": {"deployed": nsr_lcm, }
+                    "_admin.deployed": nsr_lcm
                 }
                 db_nslcmop_update = {
                     "detailed-status": "; ".join(failed_detail),
@@ -1112,11 +1161,13 @@ class Lcm:
                 self.db.del_one("nsrs", {"_id": nsr_id})
                 self.db.del_list("nslcmops", {"nsInstanceId": nsr_id})
                 self.db.del_list("vnfrs", {"nsr-id-ref": nsr_id})
+                self.logger.debug(logging_text + "Delete from database")
             else:
                 db_nsr_update = {
                     "operational-status": "terminated",
                     "detailed-status": "Done",
-                    "_admin": {"deployed": nsr_lcm, "nsState": "NOT_INSTANTIATED"}
+                    "_admin.deployed": nsr_lcm,
+                    "_admin.nsState": "NOT_INSTANTIATED"
                 }
                 db_nslcmop_update = {
                     "detailed-status": "Done",
@@ -1299,7 +1350,8 @@ class Lcm:
             try:
                 topics = ("admin", "ns", "vim_account", "sdn")
                 topic, command, params = await self.msg.aioread(topics, self.loop)
-                self.logger.debug("Task kafka_read receives {} {}: {}".format(topic, command, params))
+                if topic != "admin" and command != "ping":
+                    self.logger.debug("Task kafka_read receives {} {}: {}".format(topic, command, params))
                 consecutive_errors = 0
                 first_start = False
                 order_id += 1
@@ -1387,7 +1439,7 @@ class Lcm:
                         sys.stdout.flush()
                         continue
                     elif command == "edit":
-                        task = asyncio.ensure_future(self.vim_edit(vim_id, order_id))
+                        task = asyncio.ensure_future(self.vim_edit(params, order_id))
                         if vim_id not in self.lcm_vim_tasks:
                             self.lcm_vim_tasks[vim_id] = {}
                         self.lcm_vim_tasks[vim_id][order_id] = {"vim_edit": task}
@@ -1408,7 +1460,7 @@ class Lcm:
                         self.lcm_sdn_tasks[_sdn_id][order_id] = {"sdn_delete": task}
                         continue
                     elif command == "edit":
-                        task = asyncio.ensure_future(self.sdn_edit(_sdn_id, order_id))
+                        task = asyncio.ensure_future(self.sdn_edit(params, order_id))
                         if _sdn_id not in self.lcm_sdn_tasks:
                             self.lcm_sdn_tasks[_sdn_id] = {}
                         self.lcm_sdn_tasks[_sdn_id][order_id] = {"sdn_edit": task}