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': None, # TODO
34 'operate': grant_type,
35 'isAutomaticInvocation': True, # TODO
36 'instantiationLevelId': None, # TODO
39 'updateResources': [],
40 'removeResources': [],
41 'placementConstraints': [],
43 'additionalParams': {},
47 if grant_type == GRANT_TYPE.TERMINATE:
54 'resourceTemplateId': None,
56 'vimConnectionId': None,
57 'resourceProviderId': None,
58 'resourceid': vdu.resourceid,
59 'vimLevelResourceType': None
62 content_args['removeResources'].append(res_def)
64 content_args['additionalParams']['vimid'] = vdus[0].vimid
65 elif grant_type == GRANT_TYPE.INSTANTIATE:
66 vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
73 'resourceTemplateId': None, # TODO: shall be present for the planned creation of new resources.
76 content_args['addResources'].append(res_def)
78 content_args['additionalParams']['vimid'] = vim_id
79 elif grant_type == GRANT_TYPE.HEAL_RESTART:
83 'resDefId': str(res_index),
84 'resDesId': vdus[0].resourceid}
85 content_args['updateResources'].append(res_def)
86 content_args['additionalParams']['vimid'] = vdus[0].vimid
87 elif grant_type == GRANT_TYPE.HEAL_CREATE:
88 vim_id = vdus[0]["properties"]["location_info"]["vimid"]
92 'resDefId': str(res_index),
93 'resDesId': ignore_case_get(vdus[0], "vdu_id")
95 content_args['addResources'].append(res_def)
96 content_args['additionalParams']['vimid'] = vim_id
97 elif grant_type == GRANT_TYPE.OPERATE:
101 'id': str(res_index),
104 'resourceTemplateId': None,
106 'vimConnectionId': None,
107 'resourceProviderId': None,
108 'resourceid': vdu.resourceid,
109 'vimLevelResourceType': None
112 content_args['updateResources'].append(res_def)
114 content_args['additionalParams']['vimid'] = vdus[0].vimid
116 vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
117 content_args['additionalParams']['vnfmid'] = vnfInsts[0].vnfminstid
118 logger.info('Grant request data=%s' % content_args)
119 apply_result = apply_grant_to_nfvo(json.dumps(content_args))
120 logger.info("apply_result: %s" % apply_result)