##
from http import HTTPStatus
+from itertools import product
import logging
from random import choice as random_choice
from threading import Lock
from cryptography.hazmat.primitives.asymmetric import rsa
from jinja2 import (
Environment,
+ select_autoescape,
StrictUndefined,
TemplateError,
TemplateNotFound,
if text_id:
return text_id
- except Exception:
- pass
+ except Exception as error:
+ logging.exception(f"{error} occured while getting process id")
# Return a random id
return "".join(random_choice("0123456789abcdef") for _ in range(12))
str: [description]
"""
try:
- env = Environment(undefined=StrictUndefined)
+ env = Environment(
+ undefined=StrictUndefined,
+ autoescape=select_autoescape(default_for_string=True, default=True),
+ )
template = env.from_string(cloud_init_content)
return template.render(params or {})
Tuple[Dict[str, Any], bool]: [description]
"""
numa = {}
+ numa_list = []
epa_vcpu_set = False
if guest_epa_quota.get("numa-node-policy"):
numa_node_policy = guest_epa_quota.get("numa-node-policy")
if numa_node_policy.get("node"):
- numa_node = numa_node_policy["node"][0]
-
- if numa_node.get("num-cores"):
- numa["cores"] = numa_node["num-cores"]
- epa_vcpu_set = True
-
- paired_threads = numa_node.get("paired-threads", {})
- if paired_threads.get("num-paired-threads"):
- numa["paired-threads"] = int(
- numa_node["paired-threads"]["num-paired-threads"]
- )
- epa_vcpu_set = True
+ for numa_node in numa_node_policy["node"]:
+ vcpu_list = []
+ if numa_node.get("id"):
+ numa["id"] = int(numa_node["id"])
+
+ if numa_node.get("vcpu"):
+ for vcpu in numa_node.get("vcpu"):
+ vcpu_id = int(vcpu.get("id"))
+ vcpu_list.append(vcpu_id)
+ numa["vcpu"] = vcpu_list
+
+ if numa_node.get("num-cores"):
+ numa["cores"] = numa_node["num-cores"]
+ epa_vcpu_set = True
+
+ paired_threads = numa_node.get("paired-threads", {})
+ if paired_threads.get("num-paired-threads"):
+ numa["paired_threads"] = int(
+ numa_node["paired-threads"]["num-paired-threads"]
+ )
+ epa_vcpu_set = True
- if paired_threads.get("paired-thread-ids"):
- numa["paired-threads-id"] = []
+ if paired_threads.get("paired-thread-ids"):
+ numa["paired-threads-id"] = []
- for pair in paired_threads["paired-thread-ids"]:
- numa["paired-threads-id"].append(
- (
- str(pair["thread-a"]),
- str(pair["thread-b"]),
+ for pair in paired_threads["paired-thread-ids"]:
+ numa["paired-threads-id"].append(
+ (
+ str(pair["thread-a"]),
+ str(pair["thread-b"]),
+ )
)
- )
- if numa_node.get("num-threads"):
- numa["threads"] = int(numa_node["num-threads"])
- epa_vcpu_set = True
+ if numa_node.get("num-threads"):
+ numa["threads"] = int(numa_node["num-threads"])
+ epa_vcpu_set = True
- if numa_node.get("memory-mb"):
- numa["memory"] = max(int(int(numa_node["memory-mb"]) / 1024), 1)
+ if numa_node.get("memory-mb"):
+ numa["memory"] = max(int(int(numa_node["memory-mb"]) / 1024), 1)
- return numa, epa_vcpu_set
+ numa_list.append(numa)
+ numa = {}
+
+ return numa_list, epa_vcpu_set
@staticmethod
def _process_guest_epa_cpu_pinning_params(
"""
extended = {}
numa = {}
+ numa_list = []
if target_flavor.get("guest-epa"):
guest_epa = target_flavor["guest-epa"]
- numa, epa_vcpu_set = Ns._process_guest_epa_numa_params(
+ numa_list, epa_vcpu_set = Ns._process_guest_epa_numa_params(
guest_epa_quota=guest_epa
)
if guest_epa.get("mempage-size"):
extended["mempage-size"] = guest_epa.get("mempage-size")
+ if guest_epa.get("cpu-pinning-policy"):
+ extended["cpu-pinning-policy"] = guest_epa.get("cpu-pinning-policy")
+
+ if guest_epa.get("cpu-thread-pinning-policy"):
+ extended["cpu-thread-pinning-policy"] = guest_epa.get(
+ "cpu-thread-pinning-policy"
+ )
+
+ if guest_epa.get("numa-node-policy"):
+ if guest_epa.get("numa-node-policy").get("mem-policy"):
+ extended["mem-policy"] = guest_epa.get("numa-node-policy").get(
+ "mem-policy"
+ )
+
tmp_numa, epa_vcpu_set = Ns._process_guest_epa_cpu_pinning_params(
guest_epa_quota=guest_epa,
vcpu_count=int(target_flavor.get("vcpu-count", 1)),
epa_vcpu_set=epa_vcpu_set,
)
- numa.update(tmp_numa)
+ for numa in numa_list:
+ numa.update(tmp_numa)
extended.update(
Ns._process_guest_epa_quota_params(
)
if numa:
- extended["numas"] = [numa]
+ extended["numas"] = numa_list
return extended
Returns:
Dict[str, Any]: [description]
"""
+ db = kwargs.get("db")
+ target_vdur = {}
+
flavor_data = {
"disk": int(target_flavor["storage-gb"]),
"ram": int(target_flavor["memory-mb"]),
"vcpus": int(target_flavor["vcpu-count"]),
}
- target_vdur = {}
for vnf in indata.get("vnf", []):
for vdur in vnf.get("vdur", []):
- if vdur.get("ns-flavor-id") == target_flavor["id"]:
+ if vdur.get("ns-flavor-id") == target_flavor.get("id"):
target_vdur = vdur
+ if db and isinstance(indata.get("vnf"), list):
+ vnfd_id = indata.get("vnf")[0].get("vnfd-id")
+ vnfd = db.get_one("vnfds", {"_id": vnfd_id})
+ # check if there is persistent root disk
+ for vdu in vnfd.get("vdu", ()):
+ if vdu["name"] == target_vdur.get("vdu-name"):
+ for vsd in vnfd.get("virtual-storage-desc", ()):
+ if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]:
+ root_disk = vsd
+ if (
+ root_disk.get("type-of-storage")
+ == "persistent-storage:persistent-storage"
+ ):
+ flavor_data["disk"] = 0
+
for storage in target_vdur.get("virtual-storages", []):
if (
storage.get("type-of-storage")
"id": vim_info.get("vim_network_id"),
},
}
- elif target_vld.get("mgmt-network"):
+ elif target_vld.get("mgmt-network") and not vim_info.get("provider_network"):
extra_dict["find_params"] = {
"mgmt": True,
"name": target_vld["id"],
return extra_dict
+ @staticmethod
+ def find_persistent_root_volumes(
+ vnfd: dict,
+ target_vdu: str,
+ vdu_instantiation_volumes_list: list,
+ disk_list: list,
+ ) -> (list, dict):
+ """Find the persistent root volumes and add them to the disk_list
+ by parsing the instantiation parameters
+
+ Args:
+ vnfd: VNFD
+ target_vdu: processed VDU
+ vdu_instantiation_volumes_list: instantiation parameters for the each VDU as a list
+ disk_list: to be filled up
+
+ Returns:
+ disk_list: filled VDU list which is used for VDU creation
+
+ """
+ persistent_root_disk = {}
+
+ for vdu, vsd in product(
+ vnfd.get("vdu", ()), vnfd.get("virtual-storage-desc", ())
+ ):
+ if (
+ vdu["name"] == target_vdu["vdu-name"]
+ and vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]
+ ):
+ root_disk = vsd
+ if (
+ root_disk.get("type-of-storage")
+ == "persistent-storage:persistent-storage"
+ ):
+ for vdu_volume in vdu_instantiation_volumes_list:
+ if (
+ vdu_volume["vim-volume-id"]
+ and root_disk["id"] == vdu_volume["name"]
+ ):
+ persistent_root_disk[vsd["id"]] = {
+ "vim_volume_id": vdu_volume["vim-volume-id"],
+ "image_id": vdu.get("sw-image-desc"),
+ }
+
+ disk_list.append(persistent_root_disk[vsd["id"]])
+
+ # There can be only one root disk, when we find it, it will return the result
+ return disk_list, persistent_root_disk
+
+ else:
+ if root_disk.get("size-of-storage"):
+ persistent_root_disk[vsd["id"]] = {
+ "image_id": vdu.get("sw-image-desc"),
+ "size": root_disk.get("size-of-storage"),
+ }
+
+ disk_list.append(persistent_root_disk[vsd["id"]])
+ return disk_list, persistent_root_disk
+
+ return disk_list, persistent_root_disk
+
+ @staticmethod
+ def find_persistent_volumes(
+ persistent_root_disk: dict,
+ target_vdu: str,
+ vdu_instantiation_volumes_list: list,
+ disk_list: list,
+ ) -> list:
+ """Find the ordinary persistent volumes and add them to the disk_list
+ by parsing the instantiation parameters
+
+ Args:
+ persistent_root_disk: persistent root disk dictionary
+ target_vdu: processed VDU
+ vdu_instantiation_volumes_list: instantiation parameters for the each VDU as a list
+ disk_list: to be filled up
+
+ Returns:
+ disk_list: filled VDU list which is used for VDU creation
+
+ """
+ # Find the ordinary volumes which are not added to the persistent_root_disk
+ persistent_disk = {}
+ for disk in target_vdu.get("virtual-storages", {}):
+ if (
+ disk.get("type-of-storage") == "persistent-storage:persistent-storage"
+ and disk["id"] not in persistent_root_disk.keys()
+ ):
+ for vdu_volume in vdu_instantiation_volumes_list:
+ if vdu_volume["vim-volume-id"] and disk["id"] == vdu_volume["name"]:
+ persistent_disk[disk["id"]] = {
+ "vim_volume_id": vdu_volume["vim-volume-id"],
+ }
+ disk_list.append(persistent_disk[disk["id"]])
+
+ else:
+ if disk["id"] not in persistent_disk.keys():
+ persistent_disk[disk["id"]] = {
+ "size": disk.get("size-of-storage"),
+ }
+ disk_list.append(persistent_disk[disk["id"]])
+
+ return disk_list
+
@staticmethod
def _process_vdu_params(
target_vdu: Dict[str, Any],
# If the position info is provided for all the interfaces, it will be sorted
# according to position number ascendingly.
- if all(i.get("position") for i in target_vdu["interfaces"]):
+ if all(
+ i.get("position") + 1
+ for i in target_vdu["interfaces"]
+ if i.get("position") is not None
+ ):
sorted_interfaces = sorted(
target_vdu["interfaces"],
key=lambda x: (x.get("position") is None, x.get("position")),
# If the position info is provided for some interfaces but not all of them, the interfaces
# which has specific position numbers will be placed and others' positions will not be taken care.
else:
- if any(i.get("position") for i in target_vdu["interfaces"]):
+ if any(
+ i.get("position") + 1
+ for i in target_vdu["interfaces"]
+ if i.get("position") is not None
+ ):
n = len(target_vdu["interfaces"])
sorted_interfaces = [-1] * n
k, m = 0, 0
cloud_config["key-pairs"] = ssh_keys
persistent_root_disk = {}
+ vdu_instantiation_volumes_list = []
disk_list = []
vnfd_id = vnfr["vnfd-id"]
vnfd = db.get_one("vnfds", {"_id": vnfd_id})
- for vdu in vnfd.get("vdu", ()):
- if vdu["name"] == target_vdu["vdu-name"]:
- for vsd in vnfd.get("virtual-storage-desc", ()):
- if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]:
- root_disk = vsd
- if root_disk.get(
- "type-of-storage"
- ) == "persistent-storage:persistent-storage" and root_disk.get(
- "size-of-storage"
- ):
- persistent_root_disk[vsd["id"]] = {
- "image_id": vdu.get("sw-image-desc"),
- "size": root_disk["size-of-storage"],
- }
- disk_list.append(persistent_root_disk[vsd["id"]])
- if target_vdu.get("virtual-storages"):
- for disk in target_vdu["virtual-storages"]:
- if (
- disk.get("type-of-storage")
- == "persistent-storage:persistent-storage"
- and disk["id"] not in persistent_root_disk.keys()
- ):
- disk_list.append({"size": disk["size-of-storage"]})
+ if target_vdu.get("additionalParams"):
+ vdu_instantiation_volumes_list = (
+ target_vdu.get("additionalParams").get("OSM").get("vdu_volumes")
+ )
+
+ if vdu_instantiation_volumes_list:
+ # Find the root volumes and add to the disk_list
+ (
+ disk_list,
+ persistent_root_disk,
+ ) = Ns.find_persistent_root_volumes(
+ vnfd, target_vdu, vdu_instantiation_volumes_list, disk_list
+ )
+
+ # Find the ordinary volumes which are not added to the persistent_root_disk
+ # and put them to the disk list
+ disk_list = Ns.find_persistent_volumes(
+ persistent_root_disk,
+ target_vdu,
+ vdu_instantiation_volumes_list,
+ disk_list,
+ )
+
+ else:
+ # vdu_instantiation_volumes_list is empty
+ for vdu in vnfd.get("vdu", ()):
+ if vdu["name"] == target_vdu["vdu-name"]:
+ for vsd in vnfd.get("virtual-storage-desc", ()):
+ if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]:
+ root_disk = vsd
+ if root_disk.get(
+ "type-of-storage"
+ ) == "persistent-storage:persistent-storage" and root_disk.get(
+ "size-of-storage"
+ ):
+ persistent_root_disk[vsd["id"]] = {
+ "image_id": vdu.get("sw-image-desc"),
+ "size": root_disk["size-of-storage"],
+ }
+ disk_list.append(persistent_root_disk[vsd["id"]])
+
+ if target_vdu.get("virtual-storages"):
+ for disk in target_vdu["virtual-storages"]:
+ if (
+ disk.get("type-of-storage")
+ == "persistent-storage:persistent-storage"
+ and disk["id"] not in persistent_root_disk.keys()
+ ):
+ disk_list.append({"size": disk["size-of-storage"]})
affinity_group_list = []
vim_details = yaml.safe_load(f"{vim_details_text}")
for iface_index, interface in enumerate(existing_vdu["interfaces"]):
-
if "port-security-enabled" in interface:
interface["port_security"] = interface.pop("port-security-enabled")
target_record_id = "{}.{}".format(db_record, existing_item["id"])
item_ = item
- if target_vim.startswith("sdn"):
+ if target_vim.startswith("sdn") or target_vim.startswith("wim"):
# item must be sdn-net instead of net if target_vim is a sdn
item_ = "sdn_net"
target_record_id += ".sdn"
target_record_id = "{}.{}".format(db_record, target_item["id"])
item_ = item
- if target_vim.startswith("sdn"):
+ if target_vim.startswith("sdn") or target_vim.startswith("wim"):
# item must be sdn-net instead of net if target_vim is a sdn
item_ = "sdn_net"
target_record_id += ".sdn"
self.logger.warning(
"ns.calculate_diff_items target_item={}".format(target_item)
)
+ if process_params == Ns._process_flavor_params:
+ kwargs.update(
+ {
+ "db": self.db,
+ }
+ )
+ self.logger.warning(
+ "calculate_diff_items for flavor kwargs={}".format(kwargs)
+ )
+
if process_params == Ns._process_vdu_params:
self.logger.warning(
"calculate_diff_items self.fs={}".format(self.fs)
# Check each VNF of the target
for target_vnf in target_list:
- # Find this VNF in the list from DB
- vnfr_id = target_vnf.get("vnfInstanceId", None)
- if vnfr_id:
- existing_vnf = db_vnfrs.get(vnfr_id)
- db_record = "vnfrs:{}:{}".format(vnfr_id, db_path)
- # vim_account_id = existing_vnf.get("vim-account-id", "")
+ # Find this VNF in the list from DB, raise exception if vnfInstanceId is not found
+ vnfr_id = target_vnf["vnfInstanceId"]
+ existing_vnf = db_vnfrs.get(vnfr_id)
+ db_record = "vnfrs:{}:{}".format(vnfr_id, db_path)
+ # vim_account_id = existing_vnf.get("vim-account-id", "")
+ target_vdus = target_vnf.get("additionalParams", {}).get("vdu", [])
# Check each VDU of this VNF
- for target_vdu in target_vnf["additionalParams"].get("vdu", None):
+ if not target_vdus:
+ # Create target_vdu_list from DB, if VDUs are not specified
+ target_vdus = []
+ for existing_vdu in existing_vnf.get("vdur"):
+ vdu_name = existing_vdu.get("vdu-name", None)
+ vdu_index = existing_vdu.get("count-index", 0)
+ vdu_to_be_healed = {"vdu-id": vdu_name, "count-index": vdu_index}
+ target_vdus.append(vdu_to_be_healed)
+ for target_vdu in target_vdus:
vdu_name = target_vdu.get("vdu-id", None)
# For multi instance VDU count-index is mandatory
# For single session VDU count-indes is 0
task_index += 1
break
else:
-
for vdu_index, vdu in enumerate(db_vnfr["vdur"]):
extra_dict["params"] = {
"vim_vm_id": vdu["vim-id"],
exc_info=True,
)
raise NsException(e)
+
+ def verticalscale_task(
+ self, vdu, vnf, vdu_index, action_id, nsr_id, task_index, extra_dict
+ ):
+ target_vim, vim_info = next(k_v for k_v in vdu["vim_info"].items())
+ self._assign_vim(target_vim)
+ target_record = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu_index)
+ target_record_id = "vnfrs:{}:vdur.{}".format(vnf["_id"], vdu["id"])
+ deployment_info = {
+ "action_id": action_id,
+ "nsr_id": nsr_id,
+ "task_index": task_index,
+ }
+
+ task = Ns._create_task(
+ deployment_info=deployment_info,
+ target_id=target_vim,
+ item="verticalscale",
+ action="EXEC",
+ target_record=target_record,
+ target_record_id=target_record_id,
+ extra_dict=extra_dict,
+ )
+ return task
+
+ def verticalscale(self, session, indata, version, nsr_id, *args, **kwargs):
+ task_index = 0
+ extra_dict = {}
+ now = time()
+ action_id = indata.get("action_id", str(uuid4()))
+ step = ""
+ logging_text = "Task deploy nsr_id={} action_id={} ".format(nsr_id, action_id)
+ self.logger.debug(logging_text + "Enter")
+ try:
+ VnfFlavorData = indata.get("changeVnfFlavorData")
+ vnf_instance_id = VnfFlavorData["vnfInstanceId"]
+ step = "Getting vnfrs from db"
+ db_vnfr = self.db.get_one("vnfrs", {"_id": vnf_instance_id})
+ vduid = VnfFlavorData["additionalParams"]["vduid"]
+ vduCountIndex = VnfFlavorData["additionalParams"]["vduCountIndex"]
+ virtualMemory = VnfFlavorData["additionalParams"]["virtualMemory"]
+ numVirtualCpu = VnfFlavorData["additionalParams"]["numVirtualCpu"]
+ sizeOfStorage = VnfFlavorData["additionalParams"]["sizeOfStorage"]
+ flavor_dict = {
+ "name": vduid + "-flv",
+ "ram": virtualMemory,
+ "vcpus": numVirtualCpu,
+ "disk": sizeOfStorage,
+ }
+ db_new_tasks = []
+ step = "Creating Tasks for vertical scaling"
+ with self.write_lock:
+ for vdu_index, vdu in enumerate(db_vnfr["vdur"]):
+ if (
+ vdu["vdu-id-ref"] == vduid
+ and vdu["count-index"] == vduCountIndex
+ ):
+ extra_dict["params"] = {
+ "vim_vm_id": vdu["vim-id"],
+ "flavor_dict": flavor_dict,
+ }
+ task = self.verticalscale_task(
+ vdu,
+ db_vnfr,
+ vdu_index,
+ action_id,
+ nsr_id,
+ task_index,
+ extra_dict,
+ )
+ db_new_tasks.append(task)
+ task_index += 1
+ break
+ self.upload_all_tasks(
+ db_new_tasks=db_new_tasks,
+ now=now,
+ )
+ self.logger.debug(
+ logging_text + "Exit. Created {} tasks".format(len(db_new_tasks))
+ )
+ return (
+ {"status": "ok", "nsr_id": nsr_id, "action_id": action_id},
+ action_id,
+ True,
+ )
+ except Exception as e:
+ if isinstance(e, (DbException, NsException)):
+ self.logger.error(
+ logging_text + "Exit Exception while '{}': {}".format(step, e)
+ )
+ else:
+ e = traceback_format_exc()
+ self.logger.critical(
+ logging_text + "Exit Exception while '{}': {}".format(step, e),
+ exc_info=True,
+ )
+ raise NsException(e)