1 # Copyright 2018 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.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
23 logger = logging.getLogger(__name__)
26 def grant_resource(data, nf_inst_id, job_id, grant_type, vdus):
27 logger.info("Grant resource begin")
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
38 # 'updateResources': [],
39 # 'removeResources': [],
40 # 'placementConstraints': [],
41 # 'vimConstraints': [],
42 'additionalParams': {}
43 # '_links': None # TODO
46 if grant_type == GRANT_TYPE.TERMINATE:
48 content_args['removeResources'] = []
54 # 'resourceTemplateId': None,
56 # 'vimConnectionId': None,
57 # 'resourceProviderId': None,
58 'resourceId': vdu.resourceid,
59 # 'vimLevelResourceType': None
62 content_args['removeResources'].append(res_def)
64 if vdus and vdus[0].vimid:
65 content_args['additionalParams']['vimid'] = vdus[0].vimid
66 elif grant_type == GRANT_TYPE.INSTANTIATE:
67 vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
69 content_args['addResources'] = []
74 # 'vduId': vdu["vdu_id"],
75 'resourceTemplateId': vdu["vdu_id"] # , None,
78 content_args['addResources'].append(res_def)
81 content_args['additionalParams']['vimid'] = vim_id
82 elif grant_type == GRANT_TYPE.OPERATE:
84 content_args['updateResources'] = []
90 'resourceTemplateId': None,
92 'vimConnectionId': None,
93 'resourceProviderId': None,
94 'resourceId': vdu.resourceid,
95 'vimLevelResourceType': None
98 content_args['updateResources'].append(res_def)
100 if vdus and vdus[0].vimid:
101 content_args['additionalParams']['vimid'] = vdus[0].vimid
103 vnfInst_list = NfInstModel.objects.filter(nfinstid=nf_inst_id)
104 addition_paras = content_args['additionalParams']
105 for vnf in vnfInst_list:
107 addition_paras['vnfmid'] = vnf.vnfminstid
108 if vnf.vimInfo and 'vimid' not in addition_paras:
109 vim_info = json.loads(vnf.vimInfo)
111 for key in vim_info.iterkeys():
113 addition_paras['vimid'] = vimid
114 logger.info('Grant request data=%s' % content_args)
115 apply_result = apply_grant_to_nfvo(json.dumps(content_args))
116 logger.info("apply_result: %s" % apply_result)