##
from http import HTTPStatus
+from itertools import product
import logging
from random import choice as random_choice
from threading import Lock
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")
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],
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 = []
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)
return "BUILD", ro_vim_item_update
except (vimconn.VimConnException, NsWorkerException) as e:
+ self.logger.debug(traceback.format_exc())
self.logger.error(
"task={} {} new-vm: {}".format(task_id, ro_task["target_id"], e)
)
self,
epa_params,
):
+
target_flavor = {}
- indata = {}
+ indata = {
+ "vnf": [
+ {
+ "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
+ },
+ ],
+ }
vim_info = {}
target_record_id = ""
self,
epa_params,
):
+
target_flavor = {
"no-target-flavor": "here",
}
self,
epa_params,
):
+
expected_result = {
"find_params": {
"flavor_data": {
self,
epa_params,
):
+
expected_result = {
"find_params": {
"flavor_data": {
self,
epa_params,
):
+ db = MagicMock(name="database mock")
+ kwargs = {
+ "db": db,
+ }
+
+ db.get_one.return_value = {
+ "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
+ "df": [
+ {
+ "id": "default-df",
+ "vdu-profile": [
+ {"id": "without_volumes-VM", "min-number-of-instances": 1}
+ ],
+ }
+ ],
+ "id": "without_volumes-vnf",
+ "product-name": "without_volumes-vnf",
+ "vdu": [
+ {
+ "id": "without_volumes-VM",
+ "name": "without_volumes-VM",
+ "sw-image-desc": "ubuntu20.04",
+ "alternative-sw-image-desc": [
+ "ubuntu20.04-aws",
+ "ubuntu20.04-azure",
+ ],
+ "virtual-storage-desc": ["root-volume", "ephemeral-volume"],
+ }
+ ],
+ "version": "1.0",
+ "virtual-storage-desc": [
+ {"id": "root-volume", "size-of-storage": "10"},
+ {
+ "id": "ephemeral-volume",
+ "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
+ "size-of-storage": "1",
+ },
+ ],
+ "_admin": {
+ "storage": {
+ "fs": "mongo",
+ "path": "/app/storage/",
+ },
+ "type": "vnfd",
+ },
+ }
expected_result = {
"find_params": {
"flavor_data": {
],
},
],
+ "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
},
],
}
indata=indata,
vim_info=vim_info,
target_record_id=target_record_id,
+ **kwargs,
)
self.assertTrue(epa_params.called)
self,
epa_params,
):
+
expected_result = {
"find_params": {
"flavor_data": {
],
},
],
+ "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
+ },
+ ],
+ }
+ vim_info = {}
+ target_record_id = ""
+
+ epa_params.return_value = {}
+
+ result = Ns._process_flavor_params(
+ target_flavor=target_flavor,
+ indata=indata,
+ vim_info=vim_info,
+ target_record_id=target_record_id,
+ )
+
+ self.assertTrue(epa_params.called)
+ self.assertDictEqual(result, expected_result)
+
+ @patch("osm_ng_ro.ns.Ns._process_epa_params")
+ def test__process_flavor_params_with_persistent_root_disk(
+ self,
+ epa_params,
+ ):
+ db = MagicMock(name="database mock")
+
+ kwargs = {
+ "db": db,
+ }
+
+ db.get_one.return_value = {
+ "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
+ "df": [
+ {
+ "id": "default-df",
+ "vdu-profile": [
+ {"id": "several_volumes-VM", "min-number-of-instances": 1}
+ ],
+ }
+ ],
+ "id": "several_volumes-vnf",
+ "product-name": "several_volumes-vnf",
+ "vdu": [
+ {
+ "id": "several_volumes-VM",
+ "name": "several_volumes-VM",
+ "sw-image-desc": "ubuntu20.04",
+ "alternative-sw-image-desc": [
+ "ubuntu20.04-aws",
+ "ubuntu20.04-azure",
+ ],
+ "virtual-storage-desc": [
+ "persistent-root-volume",
+ ],
+ }
+ ],
+ "version": "1.0",
+ "virtual-storage-desc": [
+ {
+ "id": "persistent-root-volume",
+ "type-of-storage": "persistent-storage:persistent-storage",
+ "size-of-storage": "10",
+ },
+ ],
+ "_admin": {
+ "storage": {
+ "fs": "mongo",
+ "path": "/app/storage/",
+ },
+ "type": "vnfd",
+ },
+ }
+ expected_result = {
+ "find_params": {
+ "flavor_data": {
+ "disk": 0,
+ "ram": 1024,
+ "vcpus": 2,
+ },
+ },
+ "params": {
+ "flavor_data": {
+ "disk": 0,
+ "name": "test",
+ "ram": 1024,
+ "vcpus": 2,
+ },
+ },
+ }
+ target_flavor = {
+ "id": "test_id",
+ "name": "test",
+ "storage-gb": "10",
+ "memory-mb": "1024",
+ "vcpu-count": "2",
+ }
+ indata = {
+ "vnf": [
+ {
+ "vdur": [
+ {
+ "vdu-name": "several_volumes-VM",
+ "ns-flavor-id": "test_id",
+ "virtual-storages": [
+ {
+ "type-of-storage": "persistent-storage:persistent-storage",
+ "size-of-storage": "10",
+ },
+ ],
+ },
+ ],
+ "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
},
],
}
indata=indata,
vim_info=vim_info,
target_record_id=target_record_id,
+ **kwargs,
)
self.assertTrue(epa_params.called)
self,
epa_params,
):
+
expected_result = {
"find_params": {
"flavor_data": {
"memory-mb": "1024",
"vcpu-count": "2",
}
- indata = {}
+ indata = {
+ "vnf": [
+ {
+ "vdur": [
+ {
+ "ns-flavor-id": "test_id",
+ },
+ ],
+ "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
+ },
+ ],
+ }
vim_info = {}
target_record_id = ""
self,
epa_params,
):
+ db = MagicMock(name="database mock")
+
+ kwargs = {
+ "db": db,
+ }
+
+ db.get_one.return_value = {
+ "_id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
+ "df": [
+ {
+ "id": "default-df",
+ "vdu-profile": [
+ {"id": "without_volumes-VM", "min-number-of-instances": 1}
+ ],
+ }
+ ],
+ "id": "without_volumes-vnf",
+ "product-name": "without_volumes-vnf",
+ "vdu": [
+ {
+ "id": "without_volumes-VM",
+ "name": "without_volumes-VM",
+ "sw-image-desc": "ubuntu20.04",
+ "alternative-sw-image-desc": [
+ "ubuntu20.04-aws",
+ "ubuntu20.04-azure",
+ ],
+ "virtual-storage-desc": ["root-volume", "ephemeral-volume"],
+ }
+ ],
+ "version": "1.0",
+ "virtual-storage-desc": [
+ {"id": "root-volume", "size-of-storage": "10"},
+ {
+ "id": "ephemeral-volume",
+ "type-of-storage": "etsi-nfv-descriptors:ephemeral-storage",
+ "size-of-storage": "1",
+ },
+ ],
+ "_admin": {
+ "storage": {
+ "fs": "mongo",
+ "path": "/app/storage/",
+ },
+ "type": "vnfd",
+ },
+ }
+
expected_result = {
"find_params": {
"flavor_data": {
],
},
],
+ "vnfd-id": "ad6356e3-698c-43bf-9901-3aae9e9b9d18",
},
],
}
indata=indata,
vim_info=vim_info,
target_record_id=target_record_id,
+ **kwargs,
)
self.assertTrue(epa_params.called)
"Invalid mempage-size %s. Will be ignored",
extended.get("mempage-size"),
)
-
# create flavor
new_flavor = self.nova.flavors.create(
name=name,
if disk_list:
block_device_mapping = {}
for disk in disk_list:
- if disk.get("vim_id"):
- block_device_mapping["_vd" + chr(base_disk_index)] = disk[
- "vim_id"
- ]
- existing_vim_volumes.append({"id": disk["vim_id"]})
- else:
- if "image_id" in disk:
- base_disk_index = ord("a")
+ if "image_id" in disk:
+ # persistent root volume
+ base_disk_index = ord("a")
+ image_id = ""
+ if disk.get("vim_volume_id"):
+
+ # use existing persistent root volume
+ block_device_mapping["vd" + chr(base_disk_index)] = disk[
+ "vim_volume_id"
+ ]
+ existing_vim_volumes.append({"id": disk["vim_volume_id"]})
+
+ else:
+ # create persistent root volume
volume = self.cinder.volumes.create(
size=disk["size"],
- name=name + "_vd" + chr(base_disk_index),
+ name=name + "vd" + chr(base_disk_index),
imageRef=disk["image_id"],
# Make sure volume is in the same AZ as the VM to be attached to
availability_zone=vm_av_zone,
)
boot_volume_id = volume.id
+ created_items["volume:" + str(volume.id)] = True
+ block_device_mapping[
+ "vd" + chr(base_disk_index)
+ ] = volume.id
+ else:
+ # non-root persistent volume
+ if disk.get("vim_volume_id"):
+
+ # use existing persistent volume
+ block_device_mapping["vd" + chr(base_disk_index)] = disk[
+ "vim_volume_id"
+ ]
+ existing_vim_volumes.append({"id": disk["vim_volume_id"]})
+
else:
+
+ # create persistent volume
volume = self.cinder.volumes.create(
size=disk["size"],
- name=name + "_vd" + chr(base_disk_index),
+ name=name + "vd" + chr(base_disk_index),
# Make sure volume is in the same AZ as the VM to be attached to
availability_zone=vm_av_zone,
)
-
- created_items["volume:" + str(volume.id)] = True
- block_device_mapping["_vd" + chr(base_disk_index)] = volume.id
+ created_items["volume:" + str(volume.id)] = True
+ block_device_mapping[
+ "vd" + chr(base_disk_index)
+ ] = volume.id
base_disk_index += 1
)
)
server = self.nova.servers.create(
- name,
- image_id,
- flavor_id,
+ name=name,
+ image=image_id,
+ flavor=flavor_id,
nics=net_list_vim,
security_groups=self.config.get("security_groups"),
# TODO remove security_groups in future versions. Already at neutron port
--- /dev/null
+#######################################################################################
+# Copyright ETSI Contributors and Others.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#######################################################################################
+---
+fixes:
+ - |
+ Fix Bug 2012 use existing volumes as instantiation parameters targeting the Openstack VIM
+ RO processes the existing vim-volume-id which is provided through the
+ instantiation parameters, then they are consumed while creating the new VDUs.
+
+