diff options
author | biancunkang <bian.cunkang@zte.com.cn> | 2018-08-14 11:18:23 +0800 |
---|---|---|
committer | biancunkang <bian.cunkang@zte.com.cn> | 2018-08-14 11:29:06 +0800 |
commit | 537499c4e001c76c19a0e501e2381d22b84f40a1 (patch) | |
tree | eefab2bcff651b8a9fb9b3d565c352939a8f9fdb | |
parent | 7d10b4a1c61acba44e49e4ceea12f5c1e91bf0be (diff) |
Change in grant
Modify exec_grant() and test_grant_vnf_normal()
Change-Id: I99b83de960842f863b176db368652634205b38ad
Issue-ID: VFC-1015
Signed-off-by: biancunkang <bian.cunkang@zte.com.cn>
-rw-r--r-- | lcm/v2/grant_vnf.py | 101 | ||||
-rw-r--r-- | lcm/v2/tests.py | 21 |
2 files changed, 117 insertions, 5 deletions
diff --git a/lcm/v2/grant_vnf.py b/lcm/v2/grant_vnf.py index a3ba37f9..5395d7eb 100644 --- a/lcm/v2/grant_vnf.py +++ b/lcm/v2/grant_vnf.py @@ -15,6 +15,11 @@ import json import logging import uuid +from lcm.pub.database.models import NfInstModel +from lcm.pub.exceptions import NSLCMException +from lcm.pub.msapi.sdc_run_catalog import query_vnfpackage_by_id +from lcm.pub.utils.values import ignore_case_get +from lcm.pub.msapi import resmgr logger = logging.getLogger(__name__) @@ -24,12 +29,104 @@ class GrantVnf(object): self.data = grant_data def exec_grant(self): + logger.debug("grant data from vnfm:%s", self.data) if isinstance(self.data, (unicode, str)): self.data = json.JSONDecoder().decode(self.data) + has_res_tpl = False + grant_type = None + vimConnections = [] + if ignore_case_get(self.data, "addResource"): + grant_type = "addResource" + elif ignore_case_get(self.data, "removeResource"): + grant_type = "removeResource" + else: + has_res_tpl = True + + for res in ignore_case_get(self.data, grant_type): + if "resourceTemplate" in res: + has_res_tpl = True + break + + if not has_res_tpl: + m_vnf_inst_id = ignore_case_get(self.data, "vnfInstanceId") + additional_param = ignore_case_get(self.data, "additionalparam") + vnfm_inst_id = ignore_case_get(additional_param, "vnfmid") + vim_id = ignore_case_get(additional_param, "vimid") + + vnfinsts = NfInstModel.objects.filter( + mnfinstid=m_vnf_inst_id, vnfm_inst_id=vnfm_inst_id) + if not vnfinsts: + raise NSLCMException("Vnfinst(%s) is not found in vnfm(%s)" % ( + m_vnf_inst_id, vnfm_inst_id)) + + vnf_pkg_id = vnfinsts[0].package_id + nfpackage_info = query_vnfpackage_by_id(vnf_pkg_id) + vnf_pkg = nfpackage_info["packageInfo"] + vnfd = json.JSONDecoder().decode(vnf_pkg["vnfdModel"]) + + req_param = { + "vnfInstanceId": m_vnf_inst_id, + "vimId": vim_id, + "additionalParam": additional_param, + grant_type: [] + } + for res in ignore_case_get(self.data, grant_type): + vdu_name = ignore_case_get(res, "vdu") + grant_res = { + "resourceDefinitionId": ignore_case_get(res, "resourceDefinitionId"), + "type": ignore_case_get(res, "type"), + "vdu": vdu_name + } + for vdu in vnfd["vdus"]: + if vdu_name in (vdu["vdu_id"], vdu["properties"].get("name", "")): + grant_res["resourceTemplate"] = self.get_res_tpl(vdu, vnfd) + break + req_param[grant_type].append(grant_res) + self.data = req_param + vimConnections.append(resmgr.grant_vnf(self.data)) grant_resp = { "id": str(uuid.uuid4()), - "vnfInstanceId": self.data.get("vnfInstanceId"), - "vnfLcmOpOccId": self.data.get("vnfLcmOpOccId") + "vnfInstanceId": ignore_case_get(self.data, 'vnfInstanceId'), + "vnfLcmOpOccId": ignore_case_get(self.data, "vnfLcmOpOccId"), + "vimConnections": vimConnections } logger.debug("grant_resp=%s", grant_resp) return grant_resp + + def get_res_tpl(self, vdu, vnfd): + storage_size = 0 + for storage_id in vdu["local_storages"]: + storage_size = storage_size + self.get_storage_size(storage_id, vnfd) + resourceTemplate = { + "virtualComputeDescriptor": { + "virtualCpu": { + "numVirtualCpu": int(vdu["virtual_compute"]["virtual_cpu"]["num_virtual_cpu"]) + }, + "virtualMemory": { + "virtualMemSize": parse_unit(vdu["virtual_compute"]["virtual_memory"]["virtual_mem_size"], "MB") + } + }, + "virtualStorageDescriptor": { + "typeOfStorage": "", + "sizeOfStorage": storage_size, + "swImageDescriptor": "" + } + } + return resourceTemplate + + def get_storage_size(self, storage_id, vnfd): + for storage in vnfd["local_storages"]: + if storage_id == storage["local_storage_id"]: + return parse_unit(storage["properties"]["size"], "GB") + return 0 + + +def parse_unit(val, base_unit): + recognized_units = ["B", "kB", "KiB", "MB", "MiB", "GB", "GiB", "TB", "TiB"] + units_rate = [1, 1000, 1024, 1000000, 1048576, 1000000000, 1073741824, 1000000000000, 1099511627776] + unit_rate_map = {unit.upper(): rate for unit, rate in zip(recognized_units, units_rate)} + num_unit = val.strip().split(" ") + if len(num_unit) != 2: + return val.strip() + num, unit = num_unit[0], num_unit[1] + return int(num) * unit_rate_map[unit.upper()] / unit_rate_map[base_unit.upper()] diff --git a/lcm/v2/tests.py b/lcm/v2/tests.py index 55fa2f1d..76f3448b 100644 --- a/lcm/v2/tests.py +++ b/lcm/v2/tests.py @@ -14,8 +14,10 @@ import unittest import json +import mock from django.test import Client from rest_framework import status +from lcm.pub.utils import restcall class VnfGrantViewTest(unittest.TestCase): @@ -25,7 +27,8 @@ class VnfGrantViewTest(unittest.TestCase): def tearDown(self): pass - def test_grant_vnf_normal(self): + @mock.patch.object(restcall, 'call_req') + def test_grant_vnf_normal(self, mock_call_req): data = { "vnfInstanceId": "1", "vnfLcmOpOccId": "2", @@ -40,7 +43,7 @@ class VnfGrantViewTest(unittest.TestCase): "type": "COMPUTE", "vduId": "2", "resourceTemplateId": "3", - "resource": { + "resourceTemplate": { "vimConnectionId": "4", "resourceProviderId": "5", "resourceId": "6", @@ -85,14 +88,26 @@ class VnfGrantViewTest(unittest.TestCase): } } } + vimConnections = { + "id": "1", + "vimId": "1", + } + mock_call_req.return_value = [0, json.JSONEncoder().encode(vimConnections), '200'] response = self.client.post("/api/nslcm/v2/grants", data=data, format='json') self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content) resp_data = json.loads(response.content) expect_resp_data = { "id": resp_data.get("id"), "vnfInstanceId": "1", - "vnfLcmOpOccId": "2" + "vnfLcmOpOccId": "2", + "vimConnections": [ + { + "id": "1", + "vimId": "1" + } + ] } + self.assertEqual(expect_resp_data, resp_data) def test_get_notify_vnf_normal(self): |