fix bug for vnfm grant request in terminate opratio
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / grant_vnf.py
1 # Copyright 2018 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.msapi.gvnfmdriver import apply_grant_to_nfvo
20 from lcm.pub.utils.values import ignore_case_get
21 from lcm.nf.const import GRANT_TYPE
22
23 logger = logging.getLogger(__name__)
24
25
26 def grant_resource(data, nf_inst_id, job_id, grant_type, vdus):
27     logger.info("Grant resource begin")
28     content_args = {
29         'vnfInstanceId': nf_inst_id,
30         'vnfLcmOpOccId': job_id,
31         # 'vnfdId': None,  # TODO
32         # 'flavourId': "default",  # TODO
33         'operation': grant_type,
34         'isAutomaticInvocation': True,  # TODO
35         # 'instantiationLevelId': None,  # TODO
36         # 'addResources': [],
37         # 'tmpResources': [],
38         # 'updateResources': [],
39         # 'removeResources': [],
40         # 'placementConstraints': [],
41         # 'vimConstraints': [],
42         'additionalParams': {}
43         # '_links': None  # TODO
44     }
45
46     if grant_type == GRANT_TYPE.TERMINATE:
47         res_index = 1
48         content_args['removeResources'] = []
49         for vdu in vdus:
50             res_def = {
51                 'id': str(res_index),
52                 'type': 'COMPUTE',
53                 # 'vduId': None,
54                 # 'resourceTemplateId': None,
55                 'resource': {
56                     #  'vimConnectionId': None,
57                     #  'resourceProviderId': None,
58                     'resourceId': vdu.resourceid,
59                     #  'vimLevelResourceType': None
60                 }
61             }
62             content_args['removeResources'].append(res_def)
63             res_index += 1
64         if vdus and vdus[0].vimid:
65             split_vim = vdus[0].vimid.split('_')
66             cloud_owner = split_vim[0]
67             cloud_region = "".join(split_vim[1:])
68             content_args['additionalParams']['vimid'] = json.dumps({
69                 "cloud_owner": cloud_owner,
70                 'cloud_regionid': cloud_region})
71     elif grant_type == GRANT_TYPE.INSTANTIATE:
72         vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
73         res_index = 1
74         content_args['addResources'] = []
75         for vdu in vdus:
76             res_def = {
77                 'id': str(res_index),
78                 'type': 'COMPUTE',
79                 # 'vduId': vdu["vdu_id"],
80                 'resourceTemplateId': vdu["vdu_id"]  # , None,
81                 # 'resource': None
82             }
83             content_args['addResources'].append(res_def)
84             res_index += 1
85         if vim_id:
86             content_args['additionalParams']['vimid'] = vim_id
87     elif grant_type == GRANT_TYPE.OPERATE:
88         res_index = 1
89         content_args['updateResources'] = []
90         for vdu in vdus:
91             res_def = {
92                 'id': str(res_index),
93                 'type': 'COMPUTE',
94                 'vduId': None,
95                 'resourceTemplateId': None,
96                 'resource': {
97                     'vimConnectionId': None,
98                     'resourceProviderId': None,
99                     'resourceId': vdu.resourceid,
100                     'vimLevelResourceType': None
101                 }
102             }
103             content_args['updateResources'].append(res_def)
104             res_index += 1
105         if vdus and vdus[0].vimid:
106             content_args['additionalParams']['vimid'] = vdus[0].vimid
107
108     vnfInst_list = NfInstModel.objects.filter(nfinstid=nf_inst_id)
109     addition_paras = content_args['additionalParams']
110     for vnf in vnfInst_list:
111         if vnf.vnfminstid:
112             addition_paras['vnfmid'] = vnf.vnfminstid
113         if vnf.vimInfo and 'vimid' not in addition_paras:
114             vim_info = json.loads(vnf.vimInfo)
115             vimid = ""
116             for key in list(vim_info.keys()):
117                 vimid = key
118             addition_paras['vimid'] = vimid
119     logger.info('Grant request data=%s' % content_args)
120     apply_result = apply_grant_to_nfvo(json.dumps(content_args))
121     logger.info("apply_result: %s" % apply_result)
122     return apply_result