1997e0c052872f3ce1b1cae2c3abcd17f9f4a1e3
[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
22 logger = logging.getLogger(__name__)
23
24
25 def grant_resource(data, nf_inst_id, job_id, grant_type, vdus):
26     logger.info("Grant resource begin")
27     if grant_type == "Terminate":
28         lifecycleOperration = "Terminate"
29     elif grant_type == "Instantiate":
30         lifecycleOperration = "Instantiate"
31
32     content_args = {
33         'vnfInstanceId': nf_inst_id,
34         'vnfDescriptorId': '',
35         'lifecycleOperation': lifecycleOperration,
36         'vnfLcmOpOccId': job_id,
37         'addResources': [],
38         'removeResources': [],
39         'placementConstraints': [],
40         'additionalParams': {}
41     }
42
43     if grant_type == "Terminate":
44         res_index = 1
45         for vdu in vdus:
46             res_def = {
47                 'type': 'VDU',
48                 'resDefId': str(res_index),
49                 'resDesId': vdu.resouceid}
50             content_args['removeResources'].append(res_def)
51             res_index += 1
52         content_args['additionalParams']['vimid'] = vdus[0].vimid
53     elif grant_type == "Instantiate":
54         vim_id = ignore_case_get(ignore_case_get(data, "additionalParams"), "vimId")
55         res_index = 1
56         for vdu in vdus:
57             res_def = {
58                 'type': 'VDU',
59                 'resDefId': str(res_index),
60                 'resDesId': ignore_case_get(vdu, "vdu_id")
61             }
62             content_args['addResources'].append(res_def)
63             res_index += 1
64         content_args['additionalParams']['vimid'] = vim_id
65
66     vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
67     content_args['additionalParams']['vnfmid'] = vnfInsts[0].vnfminstid
68     logger.info('Grant request data=%s' % content_args)
69     apply_result = apply_grant_to_nfvo(json.dumps(content_args))
70     return apply_result