1 # Copyright 2016 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from lcm.pub.database.models import NfInstModel
19 from lcm.pub.exceptions import NSLCMException
20 from lcm.pub.msapi import resmgr
21 from lcm.pub.msapi.sdc_run_catalog import query_vnfpackage_by_id
22 from lcm.pub.utils.values import ignore_case_get
24 logger = logging.getLogger(__name__)
27 class GrantVnfs(object):
28 def __init__(self, data, job_id):
30 self.vnfm_inst_id = ''
35 def send_grant_vnf_to_resMgr(self):
36 logger.debug("grant data from vnfm:%s", self.data)
37 if isinstance(self.data, (unicode, str)):
38 self.data = json.JSONDecoder().decode(self.data)
41 if ignore_case_get(self.data, "addResource"):
42 grant_type = "addResource"
43 elif ignore_case_get(self.data, "removeResource"):
44 grant_type = "removeResource"
48 for res in ignore_case_get(self.data, grant_type):
49 if "resourceTemplate" in res:
54 m_vnf_inst_id = ignore_case_get(self.data, "vnfInstanceId")
55 additional_param = ignore_case_get(self.data, "additionalparam")
56 vnfm_inst_id = ignore_case_get(additional_param, "vnfmid")
57 vim_id = ignore_case_get(additional_param, "vimid")
59 vnfinsts = NfInstModel.objects.filter(
60 mnfinstid=m_vnf_inst_id, vnfm_inst_id=vnfm_inst_id)
62 raise NSLCMException("Vnfinst(%s) is not found in vnfm(%s)" % (
63 m_vnf_inst_id, vnfm_inst_id))
65 vnf_pkg_id = vnfinsts[0].package_id
66 nfpackage_info = query_vnfpackage_by_id(vnf_pkg_id)
67 vnf_pkg = nfpackage_info["packageInfo"]
69 vnfd = json.JSONDecoder().decode(vnf_pkg["vnfdModel"])
72 "vnfInstanceId": m_vnf_inst_id,
74 "additionalParam": additional_param,
77 for res in ignore_case_get(self.data, grant_type):
78 vdu_name = ignore_case_get(res, "vdu")
80 "resourceDefinitionId": ignore_case_get(res, "resourceDefinitionId"),
81 "type": ignore_case_get(res, "type"),
84 for vdu in vnfd["vdus"]:
85 if vdu_name in (vdu["vdu_id"], vdu["properties"].get("name", "")):
86 grant_res["resourceTemplate"] = self.get_res_tpl(vdu, vnfd)
88 req_param[grant_type].append(grant_res)
90 return resmgr.grant_vnf(self.data)
92 def get_res_tpl(self, vdu, vnfd):
94 for storage_id in vdu["local_storages"]:
95 storage_size = storage_size + self.get_storage_size(storage_id, vnfd)
97 "virtualComputeDescriptor": {
99 "numVirtualCpu": int(vdu["virtual_compute"]["virtual_cpu"]["num_virtual_cpu"])
102 "virtualMemSize": parse_unit(vdu["virtual_compute"]["virtual_memory"]["virtual_mem_size"], "MB")
105 "virtualStorageDescriptor": {
107 "sizeOfStorage": storage_size,
108 "swImageDescriptor": ""
111 return resourceTemplate
113 def get_storage_size(self, storage_id, vnfd):
114 for storage in vnfd["local_storages"]:
115 if storage_id == storage["local_storage_id"]:
116 return parse_unit(storage["properties"]["size"], "GB")
120 def parse_unit(self, val, base_unit):
121 recognized_units = ["B", "kB", "KiB", "MB", "MiB", "GB", "GiB", "TB", "TiB"]
122 units_rate = [1, 1000, 1024, 1000000, 1048576, 1000000000, 1073741824, 1000000000000, 1099511627776]
123 unit_rate_map = {unit.upper(): rate for unit, rate in zip(recognized_units, units_rate)}
124 num_unit = val.strip().split(" ")
125 if len(num_unit) != 2:
127 num, unit = num_unit[0], num_unit[1]
128 return int(num) * unit_rate_map[unit.upper()] / unit_rate_map[base_unit.upper()]