Add query vnf package from vfc-nfvo-catalog
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / grant_vnfs.py
1 # Copyright 2016 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import json
16 import logging
17
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
23
24 logger = logging.getLogger(__name__)
25
26
27 class GrantVnfs(object):
28     def __init__(self, data, job_id):
29         self.job_id = job_id
30         self.vnfm_inst_id = ''
31         self.vnf_uuid = ''
32         self.vnfm_job_id = ''
33         self.data = data
34
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)
39         has_res_tpl = False
40         grant_type = None
41         if ignore_case_get(self.data, "addResource"):
42             grant_type = "addResource"
43         elif ignore_case_get(self.data, "removeResource"):
44             grant_type = "removeResource"
45         else:
46             has_res_tpl = True
47
48         for res in ignore_case_get(self.data, grant_type):
49             if "resourceTemplate" in res:
50                 has_res_tpl = True
51                 break
52
53         if not has_res_tpl:
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")
58
59             vnfinsts = NfInstModel.objects.filter(
60                 mnfinstid=m_vnf_inst_id, vnfm_inst_id=vnfm_inst_id)
61             if not vnfinsts:
62                 raise NSLCMException("Vnfinst(%s) is not found in vnfm(%s)" % (
63                     m_vnf_inst_id, vnfm_inst_id))
64                 
65             vnf_pkg_id = vnfinsts[0].package_id
66             # vnf_pkgs = NfPackageModel.objects.filter(nfpackageid=vnf_pkg_id)
67             nfpackage_info = query_vnfpackage_by_id(vnf_pkg_id)
68             vnf_pkgs = nfpackage_info["packageInfo"]
69             if not vnf_pkgs:
70                 raise NSLCMException("vnfpkg(%s) is not found" % vnf_pkg_id)
71
72             vnfd = json.JSONDecoder().decode(vnf_pkgs[0].vnfdmodel)
73
74             req_param = {
75                 "vnfInstanceId": m_vnf_inst_id, 
76                 "vimId": vim_id, 
77                 "additionalParam": additional_param,
78                 grant_type: []
79             }
80             for res in ignore_case_get(self.data, grant_type):
81                 vdu_name = ignore_case_get(res, "vdu")
82                 grant_res = {
83                     "resourceDefinitionId": ignore_case_get(res, "resourceDefinitionId"),
84                     "type": ignore_case_get(res,"type"),
85                     "vdu": vdu_name
86                 }
87                 for vdu in vnfd["vdus"]:
88                     if vdu_name in (vdu["vdu_id"], vdu["properties"].get("name", "")):
89                         grant_res["resourceTemplate"] = self.get_res_tpl(vdu, vnfd)
90                         break
91                 req_param[grant_type].append(grant_res)
92             self.data = req_param
93         return resmgr.grant_vnf(self.data)
94
95     def get_res_tpl(self, vdu, vnfd):
96         storage_size = 0
97         for storage_id in vdu["local_storages"]:
98             storage_size = storage_size + self.get_storage_size(storage_id, vnfd)
99         resourceTemplate = {
100             "virtualComputeDescriptor": {
101                 "virtualCpu": {
102                     "numVirtualCpu": int(vdu["nfv_compute"]["num_cpus"])
103                 },
104                 "virtualMemory": {
105                     "virtualMemSize": int(vdu["nfv_compute"]["mem_size"]) 
106                 }
107             },
108             "virtualStorageDescriptor": {
109                 "typeOfStorage": "",
110                 "sizeOfStorage": storage_size,
111                 "swImageDescriptor": ""
112             }
113         }
114         return resourceTemplate
115
116     def get_storage_size(self, storage_id, vnfd):
117         for storage in vnfd["local_storages"]:
118             if storage_id == storage["local_storage_id"]:
119                 return int(storage["properties"]["size"])
120         return 0