0889e6dc1eff409ae18afd7bdbc81718bb64d207
[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 from lcm.nf.const import GRANT_TYPE
22
23 logger = logging.getLogger(__name__)
24
25
26 def grant_resource(data, nf_inst_id, job_id, grant_type, vdus):
27     logger.info("Grant resource begin")
28
29     content_args = {
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
37         'addResources': [],
38         'tmpResources': [],
39         'updateResources': [],
40         'removeResources': [],
41         'placementConstraints': [],
42         'vimConstraints': [],
43         'additionalParams': {},
44         '_links': None  # TODO
45     }
46
47     if grant_type == GRANT_TYPE.TERMINATE:
48         res_index = 1
49         for vdu in vdus:
50             res_def = {
51                 'id': str(res_index),
52                 'type': 'COMPUTE',
53                 'vduId': None,
54                 'resourceTemplateId': None,
55                 'resource': {
56                     'vimConnectionId': None,
57                     'resourceProviderId': None,
58                     'resourceid': vdu.resourceid,
59                     'vimLevelResourceType': None
60                 }
61             }
62             content_args['removeResources'].append(res_def)
63             res_index += 1
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")
67         res_index = 1
68         for vdu in vdus:
69             res_def = {
70                 'id': str(res_index),
71                 'type': 'COMPUTE',
72                 'vduId': None,
73                 'resourceTemplateId': None,  # TODO: shall be present for the planned creation of new resources.
74                 'resource': None
75             }
76             content_args['addResources'].append(res_def)
77             res_index += 1
78         content_args['additionalParams']['vimid'] = vim_id
79     elif grant_type == GRANT_TYPE.OPERATE:
80         res_index = 1
81         for vdu in vdus:
82             res_def = {
83                 'id': str(res_index),
84                 'type': 'COMPUTE',
85                 'vduId': None,
86                 'resourceTemplateId': None,
87                 'resource': {
88                     'vimConnectionId': None,
89                     'resourceProviderId': None,
90                     'resourceid': vdu.resourceid,
91                     'vimLevelResourceType': None
92                 }
93             }
94             content_args['updateResources'].append(res_def)
95             res_index += 1
96         content_args['additionalParams']['vimid'] = vdus[0].vimid
97
98     vnfInsts = NfInstModel.objects.filter(nfinstid=nf_inst_id)
99     content_args['additionalParams']['vnfmid'] = vnfInsts[0].vnfminstid
100     logger.info('Grant request data=%s' % content_args)
101     apply_result = apply_grant_to_nfvo(json.dumps(content_args))
102     logger.info("apply_result: %s" % apply_result)
103     return apply_result