Change in vnflcm
[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, VmInstModel
19 from lcm.pub.msapi.gvnfmdriver import apply_grant_to_nfvo
20
21 logger = logging.getLogger(__name__)
22
23
24 def grant_resource(nf_inst_id, job_id):
25     logger.info("Grant resource begin")
26     content_args = {
27         'vnfInstanceId': nf_inst_id,
28         'vnfDescriptorId': '',
29         'lifecycleOperation': 'Terminate',
30         'vnfLcmOpOccId': job_id,
31         'addResource': [],
32         'removeResource': [],
33         'placementConstraint': [],
34         'additionalParam': {}
35     }
36
37     vdus = VmInstModel.objects.filter(instid=nf_inst_id, is_predefined=1)
38     res_index = 1
39     for vdu in vdus:
40         res_def = {
41             'type': 'VDU',
42             'resDefId': str(res_index),
43             'resDesId': vdu.resouceid}
44         content_args['removeResource'].append(res_def)
45         res_index += 1
46
47     vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
48     content_args['additionalParam']['vnfmid'] = vnfInsts[0].vnfminstid
49     content_args['additionalParam']['vimid'] = vdus[0].vimid
50     logger.info('Grant request data=%s' % content_args)
51     apply_result = apply_grant_to_nfvo(json.dumps(content_args))
52     logger.info("Grant resource end, response: %s" % apply_result)