- def update_db(self, item, _id, _desc):
- try:
- self.db.replace(item, _id, _desc)
- except DbException as e:
- self.logger.error("Updating {} _id={}: {}".format(item, _id, e))
-
- def update_db_2(self, item, _id, _desc):
- try:
- self.db.set_one(item, {"_id": _id}, _desc)
- except DbException as e:
- self.logger.error("Updating {} _id={}: {}".format(item, _id, e))
-
- async def vim_create(self, vim_content, order_id):
- vim_id = vim_content["_id"]
- logging_text = "Task vim_create={} ".format(vim_id)
- self.logger.debug(logging_text + "Enter")
- db_vim = None
- exc = None
- RO_sdn_id = None
- RO_sdn_port_mapping = None
- try:
- 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)
- vim_RO = deepcopy(vim_content)
- vim_RO.pop("_id", None)
- vim_RO.pop("_admin", None)
- 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")
- 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 = "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)
-
- self.logger.debug(logging_text + "Exit Ok RO_vim_id".format(RO_vim_id))
- return RO_vim_id
-
- except (ROclient.ROClientException, DbException) as e:
- self.logger.error(logging_text + "Exit Exception {}".format(e))
- exc = e
- except Exception as e:
- self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
- exc = e
- finally:
- if exc and db_vim:
- db_vim["_admin"]["operationalState"] = "ERROR"
- db_vim["_admin"]["detailed-status"] = "ERROR {}: {}".format(step, exc)
- self.update_db("vim_accounts", vim_id, db_vim)
-
- async def vim_edit(self, vim_content, order_id):
- vim_id = vim_content["_id"]
- logging_text = "Task vim_edit={} ".format(vim_id)
- self.logger.debug(logging_text + "Enter")
- db_vim = None
- exc = None
- 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)
- vim_RO = deepcopy(vim_content)
- vim_RO.pop("_id", None)
- vim_RO.pop("_admin", None)
- vim_RO.pop("schema_version", None)
- vim_RO.pop("schema_type", None)
- vim_RO.pop("vim_tenant_name", None)
- 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_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_account_RO[k] = vim_content[k]
- if "vim_user" in vim_content:
- vim_content["vim_username"] = vim_content["vim_user"]
- if vim_account_RO:
- 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)
-
- self.logger.debug(logging_text + "Exit Ok RO_vim_id".format(RO_vim_id))
- return RO_vim_id
-
- except (ROclient.ROClientException, DbException) as e:
- self.logger.error(logging_text + "Exit Exception {}".format(e))
- exc = e
- except Exception as e:
- self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
- exc = e
- finally:
- if exc and db_vim:
- db_vim["_admin"]["operationalState"] = "ERROR"
- db_vim["_admin"]["detailed-status"] = "ERROR {}: {}".format(step, exc)
- self.update_db("vim_accounts", vim_id, db_vim)
-
- async def vim_delete(self, vim_id, order_id):
- logging_text = "Task vim_delete={} ".format(vim_id)
- self.logger.debug(logging_text + "Enter")
- db_vim = None
- exc = None
- step = "Getting vim from db"
- 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"):
- RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
- RO = ROclient.ROClient(self.loop, **self.ro_config)
- step = "Detaching vim from RO tenant"
- try:
- await RO.detach_datacenter(RO_vim_id)
- except ROclient.ROClientException as e:
- if e.http_code == 404: # not found
- self.logger.debug(logging_text + "RO_vim_id={} already detached".format(RO_vim_id))
- else:
- raise
-
- step = "Deleting vim from RO"
- try:
- await RO.delete("vim", RO_vim_id)
- except ROclient.ROClientException as e:
- if e.http_code == 404: # not found
- self.logger.debug(logging_text + "RO_vim_id={} already deleted".format(RO_vim_id))
- else:
- raise
- else:
- # nothing to delete
- self.logger.error(logging_text + "Skipping. There is not RO information at database")
- self.db.del_one("vim_accounts", {"_id": vim_id})
- self.logger.debug("vim_delete task vim_id={} Exit Ok".format(vim_id))
- return None
-
- except (ROclient.ROClientException, DbException) as e:
- self.logger.error(logging_text + "Exit Exception {}".format(e))
- exc = e
- except Exception as e:
- self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
- exc = e
- finally:
- if exc and db_vim:
- db_vim["_admin"]["operationalState"] = "ERROR"
- db_vim["_admin"]["detailed-status"] = "ERROR {}: {}".format(step, exc)
- self.update_db("vim_accounts", vim_id, db_vim)
-
- async def sdn_create(self, sdn_content, order_id):
- sdn_id = sdn_content["_id"]
- logging_text = "Task sdn_create={} ".format(sdn_id)
- self.logger.debug(logging_text + "Enter")
- db_sdn = None
- exc = None
- try:
- step = "Getting sdn from db"
- db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
- if "_admin" not in db_sdn:
- db_sdn["_admin"] = {}
- if "deployed" not in db_sdn["_admin"]:
- db_sdn["_admin"]["deployed"] = {}
- db_sdn["_admin"]["deployed"]["RO"] = None
-
- step = "Creating sdn at RO"
- RO = ROclient.ROClient(self.loop, **self.ro_config)
- sdn_RO = deepcopy(sdn_content)
- sdn_RO.pop("_id", None)
- sdn_RO.pop("_admin", None)
- sdn_RO.pop("schema_version", None)
- sdn_RO.pop("schema_type", None)
- sdn_RO.pop("description", None)
- desc = await RO.create("sdn", descriptor=sdn_RO)
- RO_sdn_id = desc["uuid"]
- db_sdn["_admin"]["deployed"]["RO"] = RO_sdn_id
- db_sdn["_admin"]["operationalState"] = "ENABLED"
- self.update_db("sdns", sdn_id, db_sdn)
- self.logger.debug(logging_text + "Exit Ok RO_sdn_id".format(RO_sdn_id))
- return RO_sdn_id
-
- except (ROclient.ROClientException, DbException) as e:
- self.logger.error(logging_text + "Exit Exception {}".format(e))
- exc = e
- except Exception as e:
- self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
- exc = e
- finally:
- if exc and db_sdn:
- db_sdn["_admin"]["operationalState"] = "ERROR"
- db_sdn["_admin"]["detailed-status"] = "ERROR {}: {}".format(step, exc)
- self.update_db("sdns", sdn_id, db_sdn)
-
- async def sdn_edit(self, sdn_content, order_id):
- sdn_id = sdn_content["_id"]
- logging_text = "Task sdn_edit={} ".format(sdn_id)
- self.logger.debug(logging_text + "Enter")
- db_sdn = None
- exc = None
- step = "Getting sdn from db"
- try:
- db_sdn = self.db.get_one("sdns", {"_id": sdn_id})
- 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"]
- RO = ROclient.ROClient(self.loop, **self.ro_config)
- step = "Editing sdn at RO"
- sdn_RO = deepcopy(sdn_content)
- sdn_RO.pop("_id", None)
- sdn_RO.pop("_admin", None)
- sdn_RO.pop("schema_version", None)
- sdn_RO.pop("schema_type", None)
- sdn_RO.pop("description", None)
- if sdn_RO:
- await RO.edit("sdn", RO_sdn_id, descriptor=sdn_RO)
- db_sdn["_admin"]["operationalState"] = "ENABLED"
- self.update_db("sdns", sdn_id, db_sdn)
-
- self.logger.debug(logging_text + "Exit Ok RO_sdn_id".format(RO_sdn_id))
- return RO_sdn_id
-
- except (ROclient.ROClientException, DbException) as e:
- self.logger.error(logging_text + "Exit Exception {}".format(e))
- exc = e
- except Exception as e:
- self.logger.critical(logging_text + "Exit Exception {}".format(e), exc_info=True)
- exc = e
- finally:
- if exc and db_sdn:
- db_sdn["_admin"]["operationalState"] = "ERROR"
- db_sdn["_admin"]["detailed-status"] = "ERROR {}: {}".format(step, exc)
- self.update_db("sdns", sdn_id, db_sdn)