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")
30 'vnfInstanceId': nf_inst_id,
31 'vnfLcmOpOccId': job_id,
32 # 'vnfdId': None, # TODO
33 # 'flavourId': "default", # TODO
34 'operation': grant_type,
35 'isAutomaticInvocation': True, # TODO
36 # 'instantiationLevelId': None, # TODO
39 # 'updateResources': [],
40 # 'removeResources': [],
41 # 'placementConstraints': [],
42 # 'vimConstraints': [],
43 # 'additionalParams': {}
44 # '_links': None # TODO
47 if grant_type == GRANT_TYPE.TERMINATE:
49 content_args['removeResources'] = []
55 'resourceTemplateId': None,
57 'vimConnectionId': None,
58 'resourceProviderId': None,
59 'resourceid': vdu.resourceid,
60 'vimLevelResourceType': None
63 content_args['removeResources'].append(res_def)
65 if vdus and vdus[0].vimid:
66 content_args['additionalParams'] = {}
67 content_args['additionalParams']['vimid'] = vdus[0].vimid
68 elif grant_type == GRANT_TYPE.INSTANTIATE:
69 vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
71 content_args['addResources'] = []
76 # 'vduId': vdu["vdu_id"],
77 'resourceTemplateId': vdu["vdu_id"] # , None,
80 content_args['addResources'].append(res_def)
83 content_args['additionalParams'] = {}
84 content_args['additionalParams']['vimid'] = vim_id
85 elif grant_type == GRANT_TYPE.OPERATE:
87 content_args['updateResources'] = []
93 'resourceTemplateId': None,
95 'vimConnectionId': None,
96 'resourceProviderId': None,
97 'resourceid': vdu.resourceid,
98 'vimLevelResourceType': None
101 content_args['updateResources'].append(res_def)
104 content_args['additionalParams'] = {}
105 content_args['additionalParams']['vimid'] = vdus[0].vimid
107 vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
108 if vnfInsts[0].vnfminstid:
109 content_args['additionalParams'] = {}
110 content_args['additionalParams']['vnfmid'] = vnfInsts[0].vnfminstid
111 logger.info('Grant request data=%s' % content_args)
112 apply_result = apply_grant_to_nfvo(json.dumps(content_args))
113 logger.info("apply_result: %s" % apply_result)